From bd308a222c8a0bbbef5795c96a216114c3d2b328 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Fri, 24 Jan 2025 10:20:44 -0500 Subject: [PATCH] add tests https://coveord.atlassian.net/browse/KIT-3895 --- .../src/directives/localized-string.spec.ts | 110 ++++++++++++++++++ .../atomic/src/directives/localized-string.ts | 1 - 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 packages/atomic/src/directives/localized-string.spec.ts diff --git a/packages/atomic/src/directives/localized-string.spec.ts b/packages/atomic/src/directives/localized-string.spec.ts new file mode 100644 index 0000000000..2a1362d978 --- /dev/null +++ b/packages/atomic/src/directives/localized-string.spec.ts @@ -0,0 +1,110 @@ +import i18next, {i18n as I18n} from 'i18next'; +import {html, render} from 'lit'; +import {localizedString, LocalizedStringProps} from './localized-string'; + +describe('localizedString', () => { + let container: HTMLElement; + let i18n: I18n; + + beforeEach(() => { + i18n = i18next.createInstance(); + container = document.createElement('div'); + document.body.appendChild(container); + }); + + afterEach(() => { + document.body.removeChild(container); + }); + + it('should render a localized string with placeholders', async () => { + await i18n.init({ + lng: 'en', + resources: { + en: { + translation: { + 'test.key': 'Hello, {{name}}!', + }, + }, + }, + }); + + const props: LocalizedStringProps = { + i18n, + key: 'test.key', + params: {name: 'World'}, + }; + + render(html`
${localizedString(props)}
`, container); + + expect(container.textContent).toContain('Hello, World!'); + }); + + it('should render a localized string with multiple placeholders', async () => { + await i18n.init({ + lng: 'en', + resources: { + en: { + translation: { + 'test.key': 'Hello, {{name}}! You have {{count}} messages.', + }, + }, + }, + }); + + const props: LocalizedStringProps = { + i18n, + key: 'test.key', + params: {name: 'World', count: '5'}, + }; + + render(html`
${localizedString(props)}
`, container); + + expect(container.textContent).toContain( + 'Hello, World! You have 5 messages.' + ); + }); + + it('should render a localized string with HTML template parameters', async () => { + await i18n.init({ + lng: 'en', + resources: { + en: { + translation: { + 'test.key': 'Hello, {{name}}! Here is a link: {{link}}.', + }, + }, + }, + }); + + const props: LocalizedStringProps = { + i18n, + key: 'test.key', + params: { + name: 'World', + link: html`example`, + }, + }; + + render(html`
${localizedString(props)}
`, container); + + expect(container.textContent).toContain( + 'Hello, World! Here is a link: example' + ); + const link = container.querySelector('a'); + expect(link).toBeTruthy(); + expect(link?.getAttribute('href')).toBe('https://example.com'); + expect(link?.textContent).toBe('example'); + }); + + it('should throw an error if used in an unsupported part type', () => { + const props: LocalizedStringProps = { + i18n, + key: 'test.key', + params: {name: 'World'}, + }; + + expect(() => { + render(html`
`, container); + }).toThrow('localizedString can only be used in child bindings'); + }); +}); diff --git a/packages/atomic/src/directives/localized-string.ts b/packages/atomic/src/directives/localized-string.ts index f7eb3bf693..e3739f0170 100644 --- a/packages/atomic/src/directives/localized-string.ts +++ b/packages/atomic/src/directives/localized-string.ts @@ -9,7 +9,6 @@ export interface LocalizedStringProps { count?: number; } -// TODO: KIT-3822: add unit tests to this directive class LocalizedStringDirective extends Directive { private readonly delimitingCharacter = '\u001d'; // Unicode group separator private readonly placeholderPrefixCharacter = '\u001a'; // Unicode substitute character