From a8afe7cc27835563e56bee06e14635b63300ab45 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Thu, 27 Jul 2023 13:16:44 +0200 Subject: [PATCH] test fallback language --- .../localization.context.test.ts | 59 ++++++++++++------- 1 file changed, 37 insertions(+), 22 deletions(-) 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 index 38162c3ff5..f3de0e463d 100644 --- 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 @@ -3,8 +3,8 @@ 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'; -import { sleep } from '@umbraco-cms/internal/test-utils'; +//#region Translations const english = { type: 'translations', alias: 'test.en', @@ -20,6 +20,20 @@ const english = { }, }; +const englishOverride = { + type: 'translations', + alias: 'test.en.override', + name: 'Test English', + meta: { + culture: 'en', + translations: { + general: { + close: 'Close 2', + }, + }, + }, +}; + const danish = { type: 'translations', alias: 'test.da', @@ -33,6 +47,7 @@ const danish = { }, }, }; +//#endregion describe('Localization', () => { let registry: UmbTranslationRegistry; @@ -83,35 +98,35 @@ describe('Localization', () => { 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', - }, - }, - }, - }); + extensionRegistry.register(englishOverride); const value2 = await firstValueFrom(context.localize('general_close')); expect(value2).to.equal('Close 2'); }); - }); - it('should return a new value if a language is changed', async () => { - const value = await firstValueFrom(context.localize('general_close')); - expect(value).to.equal('Close'); + it('should return a new value if the language is changed', async () => { + const value = await firstValueFrom(context.localize('general_close')); + expect(value).to.equal('Close'); - context.setLanguage(danish.meta.culture); + context.setLanguage(danish.meta.culture); - await sleep(0); + await aTimeout(0); - const value2 = await firstValueFrom(context.localize('general_close')); - expect(value2).to.equal('Luk'); + const value2 = await firstValueFrom(context.localize('general_close')); + expect(value2).to.equal('Luk'); + }); + + it('should use fallback language if key is not found', async () => { + const value = await firstValueFrom(context.localize('general_logout')); + expect(value).to.equal('Log out'); + + context.setLanguage(danish.meta.culture); + + await aTimeout(0); + + const value2 = await firstValueFrom(context.localize('general_logout')); + expect(value2).to.equal('Log out'); + }); }); describe('localizeMany', () => {