From 273efeb4768cd71eca1ec83cd9dd760d1cb5e6a5 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:07:49 +0200 Subject: [PATCH] add tests to localization context --- .../localization.context.test.ts | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.test.ts diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.test.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.test.ts new file mode 100644 index 0000000000..5f1616aa28 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.test.ts @@ -0,0 +1,97 @@ +import { expect } from '@open-wc/testing'; +import { firstValueFrom } from 'rxjs'; +import { UmbLocalizationContext } from './localization.context.js'; +import { UmbTranslationRegistry } from './registry/translation.registry.js'; +import { UmbExtensionRegistry } from '@umbraco-cms/backoffice/extension-api'; + +const english = { + type: 'translations', + alias: 'test.en', + name: 'Test English', + meta: { + culture: 'en', + translations: { + general: { + close: 'Close', + logout: 'Log out', + }, + }, + }, +}; + +describe('Localization', () => { + let registry: UmbTranslationRegistry; + let extensionRegistry: UmbExtensionRegistry; + + beforeEach(() => { + extensionRegistry = new UmbExtensionRegistry(); + registry = new UmbTranslationRegistry(extensionRegistry); + extensionRegistry.register(english); + registry.register(english.meta.culture); + }); + + describe('UmbTranslationRegistry', () => { + it('should register and get translation', (done) => { + registry.translations.subscribe((translations) => { + expect(translations.get('general_close')).to.equal('Close'); + done(); + }); + }); + }); + + describe('UmbLocalizationContext', () => { + let context: UmbLocalizationContext; + + beforeEach(async () => { + context = new UmbLocalizationContext(extensionRegistry); + context.setLanguage(english.meta.culture); + }); + + describe('localize', () => { + it('should return a value', async () => { + const value = await firstValueFrom(context.localize('general_close')); + expect(value).to.equal('Close'); + }); + + it('should return fallback if key is not found', async () => { + const value = await firstValueFrom(context.localize('general_not_found', 'Not found')); + expect(value).to.equal('Not found'); + }); + + it('should return an empty string if fallback is not provided', async () => { + const value = await firstValueFrom(context.localize('general_not_found')); + expect(value).to.equal(''); + }); + + it('should return a new value if the translation changes', async () => { + const value = await firstValueFrom(context.localize('general_close')); + expect(value).to.equal('Close'); + + extensionRegistry.register({ + type: 'translations', + alias: 'test.en.override', + name: 'Test English', + meta: { + culture: 'en', + translations: { + general: { + close: 'Close 2', + }, + }, + }, + }); + + const value2 = await firstValueFrom(context.localize('general_close')); + expect(value2).to.equal('Close 2'); + }); + }); + + describe('localizeMany', () => { + it('should return values', async () => { + const values = await firstValueFrom(context.localizeMany(['general_close', 'general_logout'])); + expect(values[0]).to.equal('Close'); + expect(values[1]).to.equal('Log out'); + }); + }); + }); +});