Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

related to issue #504: also report default Namespace with translate() #506

Merged
merged 1 commit into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function translate(namespaceArg, options = {}) {
this.options = { ...getDefaults(), ...i18nOptions, ...options };

if (context.reportNS) {
const namespaces = Array.isArray(namespaceArg) ? namespaceArg : [namespaceArg];
const namespaces = this.namespaces || [undefined];
namespaces.forEach(context.reportNS);
}

Expand Down
45 changes: 44 additions & 1 deletion test/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ describe('translate', () => {
expect(instance.namespaces.length).toBe(1);
expect(instance.namespaces[0]).toBe('i18nDefaultNS');
});
it('should report namespaces', () => {
it('should report a single used namespace to an array', () => {
const namespaces = [];
const C = translate('ns1')(<div>text</div>);
shallow(<C />, {
context: {
reportNS: ns => namespaces.push(ns)
}
});
expect(namespaces).toEqual(['ns1']);
});
it('should report multiple used namespaces to an array', () => {
const namespaces = [];
const C = translate(['ns1', 'ns2'])(<div>text</div>);
shallow(<C />, {
Expand All @@ -132,4 +142,37 @@ describe('translate', () => {
});
expect(namespaces).toEqual(['ns1', 'ns2']);
});
it('should report undefined if no namespace used and no default namespace defined', () => {
const i18n = {
options: {},
};
translate.setI18n(i18n);

const namespaces = [];
const C = translate()(<div>text</div>);
shallow(<C />, {
context: {
reportNS: ns => namespaces.push(ns)
}
});
expect(namespaces).toEqual([undefined]);
});
it('should report default namespace if no namespace used', () => {
const i18n = {
options: {
defaultNS: 'defaultNS'
},
};
translate.setI18n(i18n);

const namespaces = [];
const C = translate()(<div>text</div>);
shallow(<C />, {
context: {
reportNS: ns => namespaces.push(ns)
}
});

expect(namespaces).toEqual(['defaultNS']);
});
});