Skip to content

Commit 0f6ff62

Browse files
committed
test(localization): Add basic tests for localization.
1 parent 9bc0c9b commit 0f6ff62

File tree

2 files changed

+209
-2
lines changed

2 files changed

+209
-2
lines changed

src/components/common/i18n/i18n-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type IResourceChangeEventArgs,
55
type IResourceStrings,
66
} from 'igniteui-i18n-core';
7-
import type { ReactiveControllerHost } from 'lit';
7+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
88
import {
99
calendarResourcesMap,
1010
convertToCoreResource,
@@ -21,7 +21,7 @@ type I18nControllerConfig = {
2121
onResourceChange?: (evt: CustomEvent<IResourceChangeEventArgs>) => void;
2222
};
2323

24-
export class I18nController<T> {
24+
export class I18nController<T> implements ReactiveController {
2525
/** Set custom locale that overrides the global one. */
2626
public set locale(value: string) {
2727
this._locale = value;
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import {
2+
defineCE,
3+
elementUpdated,
4+
expect,
5+
fixture,
6+
html,
7+
unsafeStatic,
8+
} from '@open-wc/testing';
9+
import {
10+
ComboResourceStringsEN,
11+
getI18nManager,
12+
type IComboResourceStrings,
13+
type IResourceStrings,
14+
registerI18n,
15+
setCurrentI18n,
16+
} from 'igniteui-i18n-core';
17+
import { ResourceStringsBG } from 'igniteui-i18n-resources';
18+
import { LitElement } from 'lit';
19+
import {
20+
type IgcDateRangePickerResourceStrings,
21+
IgcDateRangePickerResourceStringsEN,
22+
} from './EN/date-range-picker.resources.js';
23+
import { addI18nController, type I18nController } from './i18n-controller.js';
24+
25+
class TestLocalizedClass<T> extends LitElement {
26+
public set locale(value: string) {
27+
this.i18nController.locale = value;
28+
}
29+
public get locale() {
30+
return this.i18nController.locale;
31+
}
32+
33+
public set resourceStrings(value: T) {
34+
this.i18nController.resourceStrings = value;
35+
}
36+
37+
public get resourceStrings(): T {
38+
return this.i18nController.resourceStrings;
39+
}
40+
41+
public readonly i18nController = addI18nController<T>(this, {
42+
defaultEN: this.defaultEN,
43+
});
44+
45+
public get defaultEN(): T {
46+
return {} as T;
47+
}
48+
}
49+
50+
describe('Localization', () => {
51+
let tagOld: string;
52+
let tagNew: string;
53+
let instance: LitElement & {
54+
locale: string;
55+
resourceStrings: object;
56+
i18nController: I18nController<any>;
57+
};
58+
59+
before(() => {
60+
tagOld = defineCE(
61+
class extends TestLocalizedClass<IgcDateRangePickerResourceStrings> {
62+
public override get defaultEN() {
63+
return IgcDateRangePickerResourceStringsEN;
64+
}
65+
66+
protected override render() {
67+
return html`
68+
<div id="select">
69+
<span>${this.resourceStrings.selectDate}</span>
70+
</div>
71+
<div id="previous">
72+
<span>${this.resourceStrings.previousYears}</span>
73+
</div>
74+
`;
75+
}
76+
}
77+
);
78+
79+
tagNew = defineCE(
80+
class extends TestLocalizedClass<IComboResourceStrings> {
81+
public override get defaultEN() {
82+
return ComboResourceStringsEN;
83+
}
84+
85+
protected override render() {
86+
return html`
87+
<div id="start">
88+
<span>${this.resourceStrings.combo_empty_message}</span>
89+
</div>
90+
`;
91+
}
92+
}
93+
);
94+
});
95+
96+
describe('Old resource strings format compatibility', () => {
97+
beforeEach(async () => {
98+
const tagName = unsafeStatic(tagOld);
99+
instance = await fixture(html`<${tagName}></${tagName}`);
100+
(getI18nManager() as any)._resourcesMap = new Map<
101+
string,
102+
IResourceStrings
103+
>([[getI18nManager().defaultLang, {}]]);
104+
});
105+
106+
it('should initialize correct resource strings', () => {
107+
expect(instance.shadowRoot?.getElementById('select')?.innerText).to.equal(
108+
'Select Date'
109+
);
110+
expect(
111+
instance.shadowRoot?.getElementById('previous')?.innerText
112+
).to.equal('Previous {0} Years');
113+
});
114+
115+
it('should update the resource string when they are explicitly set', async () => {
116+
instance.resourceStrings = {
117+
selectDate: 'Избор на дата',
118+
previousYears: 'Предходни {0} години',
119+
};
120+
instance.requestUpdate();
121+
await elementUpdated(instance);
122+
123+
expect(instance.shadowRoot?.getElementById('select')?.innerText).to.equal(
124+
'Избор на дата'
125+
);
126+
expect(
127+
instance.shadowRoot?.getElementById('previous')?.innerText
128+
).to.equal('Предходни {0} години');
129+
});
130+
131+
it('should set custom locale and stay that even when locale is changed globally', async () => {
132+
setCurrentI18n('de');
133+
134+
instance.locale = 'bg';
135+
instance.requestUpdate();
136+
await elementUpdated(instance);
137+
138+
expect(instance.locale).to.equal('bg');
139+
});
140+
141+
it('should convert to old resource names when resource strings are set globally from new API', async () => {
142+
registerI18n(ResourceStringsBG, 'bg');
143+
144+
instance.locale = 'bg';
145+
instance.requestUpdate();
146+
await elementUpdated(instance);
147+
148+
expect(instance.shadowRoot?.getElementById('select')?.innerText).to.equal(
149+
'Избор на дата'
150+
);
151+
expect(
152+
instance.shadowRoot?.getElementById('previous')?.innerText
153+
).to.equal('Предходни {0} години');
154+
});
155+
});
156+
157+
describe('New resource strings format', () => {
158+
beforeEach(async () => {
159+
const tagName = unsafeStatic(tagNew);
160+
instance = await fixture(html`<${tagName}></${tagName}`);
161+
(getI18nManager() as any)._resourcesMap = new Map<
162+
string,
163+
IResourceStrings
164+
>([[getI18nManager().defaultLang, {}]]);
165+
});
166+
167+
it('should initialize correct resource strings', () => {
168+
expect(instance.shadowRoot?.getElementById('start')?.innerText).to.equal(
169+
'The list is empty'
170+
);
171+
});
172+
173+
it('should update the resource string when they are explicitly set', async () => {
174+
instance.resourceStrings = {
175+
combo_empty_message: 'Списъкът e празен',
176+
};
177+
instance.requestUpdate();
178+
await elementUpdated(instance);
179+
180+
expect(instance.shadowRoot?.getElementById('start')?.innerText).to.equal(
181+
'Списъкът e празен'
182+
);
183+
});
184+
185+
it('should set custom locale and stay that even when locale is changed globally', async () => {
186+
setCurrentI18n('de');
187+
188+
instance.locale = 'bg';
189+
instance.requestUpdate();
190+
await elementUpdated(instance);
191+
192+
expect(instance.locale).to.equal('bg');
193+
});
194+
195+
it('should update resource strings when are set globally', async () => {
196+
registerI18n(ResourceStringsBG, 'bg');
197+
198+
instance.locale = 'bg';
199+
instance.requestUpdate();
200+
await elementUpdated(instance);
201+
202+
expect(instance.shadowRoot?.getElementById('start')?.innerText).to.equal(
203+
'Списъкът е празен'
204+
);
205+
});
206+
});
207+
});

0 commit comments

Comments
 (0)