import { IntlShape } from 'react-intl'; import { getLangFromLocale, adjustLocale, getCountryFromLocale, getDirectionFromLocale, getLocaleCurrencyName, } from '.'; describe('locale utils', () => { beforeAll(() => { jest.spyOn(console, 'error').mockImplementation(); jest.spyOn(console, 'error').mockImplementation(() => {}); jest.spyOn(console, 'warn').mockImplementation(() => {}); }); afterAll(() => { jest.restoreAllMocks(); }); describe('getLangFromLocale', () => { it.each([ ['en-GB', 'en'], ['en_GB', 'en'], ['EN-GB', 'en'], ['en-gb', 'en'], ['en_US', 'en'], ['en-US', 'en'], ['ES', 'es'], ['ZH-HK', 'zh'], ['ZH_HK', 'zh'], ['zh_HK', 'zh'], ['ja-JP', 'ja'], ['zhHK', null], [null, null], [undefined, null], ['', null], [' ', null], ['ar-dz', null], ['ar-iq', null], ['zh-tw', 'zh'], ['nl-nl', 'nl'], ])( 'locale "%s" -> language "%s"', (locale: string | null | undefined, expectedValue: string | null) => { expect(getLangFromLocale(locale!)).toBe(expectedValue); }, ); }); describe('adjustLocale', () => { it.each([ ['en-GB', 'en-GB'], ['en_GB', 'en-GB'], ['EN-GB', 'en-GB'], ['en-gb', 'en-GB'], ['en_US', 'en-US'], ['en-US', 'en-US'], ['ES', 'es'], ['ZH-HK', 'zh-HK'], ['ZH_HK', 'zh-HK'], ['zh_HK', 'zh-HK'], ['ja-JP', 'ja-JP'], ['zhHK', null], [null, null], [undefined, null], ['', null], [' ', null], ['ar-dz', 'ar-DZ'], ['ar-iq', 'ar-IQ'], ['zh-tw', 'zh-TW'], ['nl-nl', 'nl-NL'], ])( 'locale "%s" -> language "%s"', (locale: string | null | undefined, expectedValue: string | null) => { expect(adjustLocale(locale!)).toBe(expectedValue); }, ); }); describe('getCountryFromLocale', () => { it.each([ ['en', null], ['fr', null], ['it', null], ['en-US', 'US'], ['en-GB', 'GB'], ['es-ES', 'ES'], ['es_ES', 'ES'], ['es_E', null], ['es-E', null], ])( 'given an "%s" as a locale value it should return "%s"', (locale: string, expectedValue: string | null) => { expect(getCountryFromLocale(locale)).toBe(expectedValue); }, ); }); describe('getDirectionFromLocale', () => { it.each([ ['en', 'ltr'], ['ar', 'rtl'], ['ar-AE', 'rtl'], ['ar_AE', 'rtl'], ['he', 'rtl'], ])( 'given an "%s" as a locale value it should return "%s"', (locale: string, expectedValue: string) => { expect(getDirectionFromLocale(locale)).toBe(expectedValue); }, ); }); describe('getLocaleCurrencyName', () => { it('should return the localized currency name if Intl.DisplayNames is supported', () => { const intl = { formatDisplayName: jest.fn().mockReturnValue('US Dollar'), } as unknown as IntlShape; const result = getLocaleCurrencyName(intl, 'USD'); expect(intl.formatDisplayName).toHaveBeenCalledWith('USD', { type: 'currency', fallback: 'none', }); expect(result).toBe('US Dollar'); }); it('should return the formatted currency code if Intl.DisplayNames is not supported', () => { const intl = { formatDisplayName: jest.fn().mockReturnValue('US Dollar'), } as unknown as IntlShape; // Mock Intl.DisplayNames to be undefined // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access (Intl as any).DisplayNames = undefined; const result = getLocaleCurrencyName(intl, 'USC'); expect(result).toBe('USDC'); }); it('should return the formatted code when formatDisplayName does not support the currency code', () => { const intl = { formatDisplayName: jest.fn().mockReturnValue(undefined), } as unknown as IntlShape; const result = getLocaleCurrencyName(intl, 'USC'); expect(result).toBe('USDC'); }); }); });