Skip to content

Commit 7b29f72

Browse files
add test, lint fix
1 parent c26d3b3 commit 7b29f72

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

src/v3/src/util/locUtil.test.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* See the License for the specific language governing permissions and limitations under the License.
1111
*/
1212

13-
import type { loc as origLoc } from './locUtil';
1413
import * as OrigLanguageUtils from './languageUtils';
14+
import type { loc as origLoc } from './locUtil';
1515

1616
jest.unmock('./locUtil');
1717

@@ -86,30 +86,30 @@ describe('locUtil Tests', () => {
8686
expect(localizedText).toBe('This is some test string with multiple tokens: <a href="#"> <span class="strong"> here is a test string </span> </a>');
8787
});
8888
});
89-
89+
9090
// https://www.i18next.com/translation-function/plurals
9191
describe('Pluralization', () => {
9292
const MockedLogin: Record<string, Record<string, string>> = {
9393
en: {
94-
'item_one': 'one item',
95-
'item_other': '{0} items',
96-
'apple_one': 'one apple',
97-
'apple_other': '{0} apples',
98-
'pear_one': 'one pear',
99-
'pear_other': '{0} pears',
94+
item_one: 'one item',
95+
item_other: '{0} items',
96+
apple_one: 'one apple',
97+
apple_other: '{0} apples',
98+
pear_one: 'one pear',
99+
pear_other: '{0} pears',
100100
},
101101
ro: {
102-
'item_one': 'un articol',
103-
'item_few': '{0} articole',
104-
'item_other': '{0} de articole',
102+
item_one: 'un articol',
103+
item_few: '{0} articole',
104+
item_other: '{0} de articole',
105105
// 'apple_one': 'un măr', // missing translation
106-
'apple_few': '{0} mere',
107-
'apple_other': '{0} de mere',
108-
'apple': '{0} de mere', // will be used as fallback
109-
'pear_few': '{0} pere', // no fallback
106+
apple_few: '{0} mere',
107+
apple_other: '{0} de mere',
108+
apple: '{0} de mere', // will be used as fallback
109+
pear_few: '{0} pere', // no fallback
110110
},
111111
};
112-
112+
113113
beforeEach(() => {
114114
jest.resetModules();
115115
jest.unmock('./i18next');
@@ -124,10 +124,7 @@ describe('locUtil Tests', () => {
124124
login: MockedLogin.en,
125125
country: {},
126126
loadLanguage: jest.fn().mockImplementation(
127-
// eslint-disable-next-line no-unused-vars
128-
async (language: string, overrides: any, assets: Record<string, string>,
129-
supportedLanguages: string[], omitDefaultKeys?: (key: string) => boolean
130-
) => {
127+
async (language: string) => {
131128
Bundles.currentLanguage = language;
132129
Bundles.login = MockedLogin[language] ?? {};
133130
Bundles.country = {};
@@ -144,44 +141,56 @@ describe('locUtil Tests', () => {
144141
it('can localize singular/plural in English', () => {
145142
languageUtils.initDefaultLanguage();
146143
const localizedTextOne = loc('item', 'login', [1]);
147-
expect(localizedTextOne).toBe('one item');
144+
expect(localizedTextOne).toBe('one item'); // item_one
148145
const localizedTextTwo = loc('item', 'login', [2]);
149-
expect(localizedTextTwo).toBe('2 items');
146+
expect(localizedTextTwo).toBe('2 items'); // item_other
150147
});
151148

152149
it('can localize multiple plurals in other languages', async () => {
153150
languageUtils.initDefaultLanguage();
154151
await languageUtils.loadLanguage({ language: 'ro' });
155152
const localizedText1 = loc('item', 'login', [1]);
156-
expect(localizedText1).toBe('un articol');
153+
expect(localizedText1).toBe('un articol'); // item_one
157154
const localizedText2 = loc('item', 'login', [2]);
158-
expect(localizedText2).toBe('2 articole');
155+
expect(localizedText2).toBe('2 articole'); // item_few
159156
const localizedText20 = loc('item', 'login', [20]);
160-
expect(localizedText20).toBe('20 de articole');
157+
expect(localizedText20).toBe('20 de articole'); // item_other
161158
});
162159

163160
it('can fallback to default form if some plural form translation is missing', async () => {
164161
languageUtils.initDefaultLanguage();
165162
await languageUtils.loadLanguage({ language: 'ro' });
166163
const localizedText2 = loc('apple', 'login', [2]);
167-
expect(localizedText2).toBe('2 mere');
168-
// fallback
164+
expect(localizedText2).toBe('2 mere'); // apple_few
165+
// no 'apple_one' => fallback to 'apple'
169166
const localizedText1 = loc('apple', 'login', [1]);
170-
expect(localizedText1).toBe('1 de mere');
167+
expect(localizedText1).toBe('1 de mere'); // apple
171168
});
172169

173170
it('will fallback to English if some plural form translation is missing and there is no fallback', async () => {
174171
languageUtils.initDefaultLanguage();
175172
await languageUtils.loadLanguage({ language: 'ro' });
176173
const localizedText2 = loc('pear', 'login', [2]);
177-
expect(localizedText2).toBe('2 pere');
178-
// fallback to English
174+
expect(localizedText2).toBe('2 pere'); // pear_few
175+
// no 'pear_one', no fallback to 'pear' => fallback to English 'pear_one'
179176
const localizedText1 = loc('pear', 'login', [1]);
180177
expect(localizedText1).toBe('one pear');
181178
});
182179

183-
// todo: unload lang
184-
185-
// todo: L10N_ERROR
180+
it('emits error event if requested key is missing in bundle', () => {
181+
// spy on emitting of CustomEvent with type 'okta-i18n-error' in `loc()` util
182+
const dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
183+
languageUtils.initDefaultLanguage();
184+
const localizedText2 = loc('missing_key', 'login');
185+
expect(localizedText2).toBe('L10N_ERROR[missing_key]');
186+
expect(dispatchEventSpy).toHaveBeenCalledWith(new CustomEvent('okta-i18n-error', {
187+
detail: {
188+
type: 'l10n-error',
189+
key: 'missing_key',
190+
bundleName: 'login',
191+
reason: 'key',
192+
},
193+
}));
194+
});
186195
});
187196
});

0 commit comments

Comments
 (0)