From ed031d680a8f6c12076f8bd60096ebc8419e0c69 Mon Sep 17 00:00:00 2001 From: Mikko Tapionlinna Date: Tue, 8 Oct 2024 15:50:44 +0300 Subject: [PATCH] (HDS-1854) Remove console.errors for languages that are not supported. So that wanted fallback language features does not generate errors and add test for it. --- .../CookieConsentCore.test.ts | 40 ++++++++++++++++++- .../cookieConsentCore/translations.js | 8 +++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/react/src/components/cookieConsentCore/CookieConsentCore.test.ts b/packages/react/src/components/cookieConsentCore/CookieConsentCore.test.ts index 7f6940a41a..384030c8e9 100644 --- a/packages/react/src/components/cookieConsentCore/CookieConsentCore.test.ts +++ b/packages/react/src/components/cookieConsentCore/CookieConsentCore.test.ts @@ -51,6 +51,18 @@ describe('cookieConsentCore', () => { }); } + /** + * Wait for console log to NOT be called with a specific message + * @param level Console level, e.g. 'log', 'warn', 'error' + * @param messageToWait Message to ensure was NOT called + */ + async function waitForConsoleNotCalled(level, messageToWait) { + const consoleLogSpy = jest.spyOn(console, level); + await waitFor(() => { + expect(consoleLogSpy).not.toHaveBeenCalledWith(messageToWait); + }); + } + const urls = { siteSettingsJsonUrl: '/helfi_sitesettings.json', siteSettings404: '/404.json', @@ -1503,11 +1515,35 @@ describe('cookieConsentCore', () => { }); // - Language tests - it('should fall back to fallback language if the wanted language is not found in site settings and complain in console.error', async () => { + it('should fall back to fallback language if the wanted language is NOT found in site settings and NOT complain in console.error', async () => { instance = await CookieConsentCore.create(siteSettingsObj, { ...optionsEvent, language: 'ru' }); await waitForRoot(); addBoundingClientRect(getContainerElement()); - await waitForConsole('error', 'Cookie consent: Missing translation: description:ru, using fallback language: en'); + await waitForConsoleNotCalled( + 'error', + 'Cookie consent: Missing translation: description:ru, using fallback language: en', + ); + const showButton = getShowDetailsButtonElement(); + expect(showButton).not.toBeNull(); + }); + + it('should fall back to fallback language if the wanted language is found in site settings and SHOULD complain in console.error', async () => { + instance = await CookieConsentCore.create( + { + ...siteSettingsObj, + translations: { + ...siteSettingsObj.translations, + description: { + en: 'fallback', + fi: 'fallback', + }, + }, + }, + { ...optionsEvent, language: 'sv' }, + ); + await waitForRoot(); + addBoundingClientRect(getContainerElement()); + await waitForConsole('error', 'Cookie consent: Missing translation: description:sv, using fallback language: en'); const showButton = getShowDetailsButtonElement(); expect(showButton).not.toBeNull(); }); diff --git a/packages/react/src/components/cookieConsentCore/translations.js b/packages/react/src/components/cookieConsentCore/translations.js index 545b18f1c7..2b6ebf2d42 100644 --- a/packages/react/src/components/cookieConsentCore/translations.js +++ b/packages/react/src/components/cookieConsentCore/translations.js @@ -114,8 +114,12 @@ export function getTranslation(translations, key, lang, directions, fallbackLang lang: fallbackLang, dir: getDir(fallbackLang), }; - // eslint-disable-next-line no-console - console.error(`Cookie consent: Missing translation: ${key}:${lang}, using fallback language: ${fallbackLang}`); + + // Show error message only if wanted language is defined in directions (originally from siteSettings.languages) + if (directions[lang]) { + // eslint-disable-next-line no-console + console.error(`Cookie consent: Missing translation: ${key}:${lang}, using fallback language: ${fallbackLang}`); + } } else { // Translation was not found in given language or fallback language, use first available translation const firstLang = Object.keys(translations[key])[0];