Skip to content

Commit

Permalink
(HDS-1854) Remove console.errors for languages that are not supported…
Browse files Browse the repository at this point in the history
…. So that wanted fallback language features does not generate errors and add test for it.
  • Loading branch information
Arkkimaagi committed Oct 8, 2024
1 parent 23eb448 commit ed031d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit ed031d6

Please sign in to comment.