-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve fallback and empty namespace treatment (#3243)
- Loading branch information
Showing
9 changed files
with
171 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Controller } from '../Controller/Controller'; | ||
|
||
describe('namespace internal function in controller', () => { | ||
it('it works with no default ns defined', () => { | ||
const controller = Controller({ | ||
options: { | ||
language: 'en', | ||
staticData: { | ||
en: { test: 'emptyNs' }, | ||
'en:common': { test: 'commonNs' }, | ||
}, | ||
}, | ||
}); | ||
expect(controller.getRequiredNamespaces()).toEqual(['']); | ||
expect(controller.getTranslationNs({ key: 'test' })).toEqual(['']); | ||
expect(controller.t({ key: 'test' })).toEqual('emptyNs'); | ||
}); | ||
|
||
it('default ns can be overriten with empty string', () => { | ||
const controller = Controller({ | ||
options: { | ||
language: 'en', | ||
staticData: { | ||
en: { test: 'emptyNs' }, | ||
'en:common': { test: 'commonNs' }, | ||
}, | ||
defaultNs: 'common', | ||
}, | ||
}); | ||
expect(controller.getRequiredNamespaces()).toEqual(['common']); | ||
expect(controller.getTranslationNs({ key: 'test' })).toEqual(['common']); | ||
expect(controller.t({ key: 'test' })).toEqual('commonNs'); | ||
|
||
expect(controller.getTranslationNs({ key: 'test', ns: '' })).toEqual(['']); | ||
expect(controller.t({ key: 'test', ns: '' })).toEqual('emptyNs'); | ||
}); | ||
|
||
it("can be overriten when key doesn't exist", () => { | ||
const controller = Controller({ | ||
options: { | ||
language: 'en', | ||
staticData: { | ||
en: { test: 'emptyNs' }, | ||
'en:common': { test: 'commonNs' }, | ||
}, | ||
defaultNs: 'common', | ||
}, | ||
}); | ||
expect(controller.getRequiredNamespaces()).toEqual(['common']); | ||
expect(controller.getTranslationNs({ key: 'unknown' })).toEqual(['common']); | ||
expect(controller.t({ key: 'unknown' })).toEqual('unknown'); | ||
|
||
expect(controller.getTranslationNs({ key: 'unknown', ns: '' })).toEqual([ | ||
'', | ||
]); | ||
expect(controller.t({ key: 'unknown', ns: '' })).toEqual('unknown'); | ||
}); | ||
|
||
it('returns correct namespaces if there are fallbacks', () => { | ||
const controller = Controller({ | ||
options: { | ||
language: 'en', | ||
staticData: { | ||
en: { test: 'emptyNs' }, | ||
'en:common': { test: 'commonNs' }, | ||
'en:fallback': { test: 'fallbackNs' }, | ||
}, | ||
defaultNs: 'common', | ||
fallbackNs: ['', 'fallback'], | ||
}, | ||
}); | ||
expect(controller.getRequiredNamespaces()).toEqual([ | ||
'common', | ||
'', | ||
'fallback', | ||
]); | ||
expect(controller.getTranslationNs({ key: 'unknown' })).toEqual([ | ||
'common', | ||
'', | ||
'fallback', | ||
]); | ||
expect(controller.t({ key: 'unknown' })).toEqual('unknown'); | ||
|
||
expect( | ||
controller.getTranslationNs({ key: 'unknown', ns: 'fallback' }) | ||
).toEqual(['fallback', '']); | ||
expect(controller.t({ key: 'unknown', ns: '' })).toEqual('unknown'); | ||
}); | ||
|
||
it('returns correct namespaces if there are empty translations', () => { | ||
const controller = Controller({ | ||
options: { | ||
language: 'en', | ||
staticData: { | ||
en: { test: 'hello' }, | ||
'en:translation': { test: null }, | ||
}, | ||
ns: ['', 'translation'], | ||
defaultNs: 'translation', | ||
fallbackNs: '', | ||
}, | ||
}); | ||
expect(controller.getDefaultAndFallbackNs()).toEqual(['translation', '']); | ||
expect(controller.getTranslationNs({ key: 'test' })).toEqual(['']); | ||
expect(controller.t({ key: 'test' })).toEqual('hello'); | ||
|
||
expect(controller.getTranslationNs({ key: 'unknown' })).toEqual([ | ||
'translation', | ||
'', | ||
]); | ||
expect(controller.t({ key: 'unknown' })).toEqual('unknown'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters