refactor: console a warning when no consent url

This commit is contained in:
Yu Long
2023-11-16 13:27:33 +01:00
parent 47bb84386a
commit c6dac0b111
2 changed files with 23 additions and 1 deletions

View File

@@ -21,4 +21,21 @@ describe('getConsentLinkUrl', () => {
expect(getConsentLinkUrl('be', 'fr')).toBe(rivertyConsentUrlMap.be.fr);
});
});
describe('no supported country code & locale', () => {
beforeEach(() => {
console.warn = jest.fn();
});
test('should give a warning if no country code is provided', () => {
getConsentLinkUrl(undefined, 'en');
expect(console.warn).toBeCalled();
});
test('should give a warning if wrong country code is provided', () => {
getConsentLinkUrl('WRONG', 'en');
expect(console.warn).toBeCalled();
});
test('should give a warning if wrong locale is provided', () => {
getConsentLinkUrl('nl', 'fr');
expect(console.warn).toBeCalled();
});
});
});

View File

@@ -2,7 +2,12 @@ import { rivertyConsentUrlMap } from './config';
function getConsentLinkUrl(countryCode: string, locale: string): string {
const languageCode = locale?.toLowerCase().slice(0, 2);
return rivertyConsentUrlMap[countryCode?.toLowerCase()]?.[languageCode];
const consentLink = rivertyConsentUrlMap[countryCode?.toLowerCase()]?.[languageCode];
if (!consentLink) {
console.warn(`Cannot find a consent url for the provided countryCode: ${countryCode} and locale: ${locale}`);
return;
}
return consentLink;
}
export { getConsentLinkUrl };