Skip to content

Commit 5da6247

Browse files
committed
chore(i18n): add unit tests for getDefaultI18nBundles function
1 parent 5e8b287 commit 5da6247

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {describe, expect, it} from 'vitest';
20+
import getDefaultI18nBundles from '../getDefaultI18nBundles';
21+
import TranslationBundleConstants from '../../constants/TranslationBundleConstants';
22+
import type {I18nBundle} from '../../models/i18n';
23+
import {en_US} from '../../translations';
24+
25+
describe('getDefaultI18nBundles', (): void => {
26+
it('should return a collection of i18n bundles', (): void => {
27+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
28+
29+
expect(bundles).toBeDefined();
30+
expect(typeof bundles).toBe('object');
31+
expect(bundles).not.toBeNull();
32+
});
33+
34+
it('should return bundles for all default locales', (): void => {
35+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
36+
const expectedLocales: readonly string[] = TranslationBundleConstants.DEFAULT_LOCALES;
37+
38+
expectedLocales.forEach((localeCode: string) => {
39+
expect(bundles[localeCode]).toBeDefined();
40+
expect(bundles[localeCode]).toHaveProperty('metadata');
41+
expect(bundles[localeCode]).toHaveProperty('translations');
42+
});
43+
});
44+
45+
it('should include en-US bundle by default', (): void => {
46+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
47+
48+
expect(bundles['en-US']).toBeDefined();
49+
expect(bundles['en-US']).toEqual(en_US);
50+
});
51+
52+
it('should return bundles with correct structure', (): void => {
53+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
54+
55+
Object.values(bundles).forEach((bundle: I18nBundle) => {
56+
expect(bundle).toHaveProperty('metadata');
57+
expect(bundle).toHaveProperty('translations');
58+
59+
expect(bundle.metadata).toHaveProperty('localeCode');
60+
expect(bundle.metadata).toHaveProperty('countryCode');
61+
expect(bundle.metadata).toHaveProperty('languageCode');
62+
expect(bundle.metadata).toHaveProperty('displayName');
63+
expect(bundle.metadata).toHaveProperty('direction');
64+
65+
expect(typeof bundle.metadata.localeCode).toBe('string');
66+
expect(typeof bundle.metadata.countryCode).toBe('string');
67+
expect(typeof bundle.metadata.languageCode).toBe('string');
68+
expect(typeof bundle.metadata.displayName).toBe('string');
69+
expect(['ltr', 'rtl']).toContain(bundle.metadata.direction);
70+
71+
expect(typeof bundle.translations).toBe('object');
72+
expect(bundle.translations).not.toBeNull();
73+
});
74+
});
75+
76+
it('should return bundles with valid locale codes as keys', (): void => {
77+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
78+
79+
Object.keys(bundles).forEach((localeCode: string) => {
80+
expect(localeCode).toMatch(/^[a-z]{2}-[A-Z]{2}$/);
81+
expect(bundles[localeCode].metadata.localeCode).toBe(localeCode);
82+
});
83+
});
84+
85+
it('should return bundles with required translation keys', (): void => {
86+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
87+
88+
Object.values(bundles).forEach((bundle: I18nBundle) => {
89+
const translations = bundle.translations;
90+
91+
expect(translations).toHaveProperty('elements.buttons.signIn');
92+
expect(translations).toHaveProperty('elements.buttons.signOut');
93+
expect(translations).toHaveProperty('signin.title');
94+
expect(translations).toHaveProperty('signin.subtitle');
95+
expect(translations).toHaveProperty('errors.title');
96+
97+
Object.values(translations).forEach((value: string) => {
98+
expect(typeof value).toBe('string');
99+
expect(value.length).toBeGreaterThan(0);
100+
});
101+
});
102+
});
103+
104+
it('should handle dynamic module key conversion correctly', (): void => {
105+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
106+
107+
TranslationBundleConstants.DEFAULT_LOCALES.forEach((localeCode: string) => {
108+
if (localeCode.includes('-')) {
109+
expect(bundles[localeCode]).toBeDefined();
110+
expect(bundles[localeCode].metadata.localeCode).toBe(localeCode);
111+
}
112+
});
113+
});
114+
115+
it('should return a new object on each call', (): void => {
116+
const bundles1: Record<string, I18nBundle> = getDefaultI18nBundles();
117+
const bundles2: Record<string, I18nBundle> = getDefaultI18nBundles();
118+
119+
expect(bundles1).not.toBe(bundles2);
120+
expect(bundles1).toEqual(bundles2);
121+
});
122+
123+
it('should filter out invalid bundles', (): void => {
124+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
125+
126+
Object.values(bundles).forEach((bundle: I18nBundle) => {
127+
expect(bundle.metadata).toBeDefined();
128+
expect(bundle.metadata.localeCode).toBeTruthy();
129+
expect(typeof bundle.metadata.localeCode).toBe('string');
130+
});
131+
});
132+
133+
it('should maintain consistent bundle count', (): void => {
134+
const bundles: Record<string, I18nBundle> = getDefaultI18nBundles();
135+
const expectedCount: number = TranslationBundleConstants.DEFAULT_LOCALES.length;
136+
137+
expect(Object.keys(bundles)).toHaveLength(expectedCount);
138+
});
139+
});

0 commit comments

Comments
 (0)