UNPKG

@prezly/theme-kit-core

Version:

Data layer and utility library for developing Prezly themes with JavaScript

38 lines 1.73 kB
import { LANGUAGES } from "../../__mocks__/languages.mjs"; import { getShortestLocaleSlug } from "./getShortestLocaleSlug.mjs"; var locales = Object.values(LANGUAGES).map(lang => lang.code); var defaultLocale = Object.values(LANGUAGES).filter(lang => lang.is_default).map(lang => lang.code)[0]; var context = { locales, defaultLocale }; function without(codes, exclude) { return codes.filter(code => code !== exclude); } describe('getShortestLocaleSlug', () => { it('returns false for default language', () => { expect(getShortestLocaleSlug('en', context)).toBe(false); }); it('returns neutral language code if it is the only culture with that language', () => { expect(getShortestLocaleSlug('es_ES', { locales: without(locales, 'es_419'), defaultLocale })).toBe('es'); }); it('returns region code if it is the only culture with that region', () => { expect(getShortestLocaleSlug('en_US', context)).toBe('us'); expect(getShortestLocaleSlug('en_GB', context)).toBe('gb'); expect(getShortestLocaleSlug('nl_NL', context)).toBe('nl-nl'); // cannot use `nl`, as it collides with language code expect(getShortestLocaleSlug('fr', context)).toBe('fr'); }); it('returns full code if it can not be shortened', () => { expect(getShortestLocaleSlug('fr_BE', context)).toBe('fr-be'); expect(getShortestLocaleSlug('nl_BE', context)).toBe('nl-be'); }); it('returns full code when trying to shorten to region code for es-419', () => { expect(getShortestLocaleSlug('es_419', context)).toBe('es-419'); }); it('returns full code when trying to shorten to region code for zg-Hant', () => { expect(getShortestLocaleSlug('zh_Hant', context)).toBe('zh-hant'); }); });