UNPKG

@prezly/theme-kit-core

Version:

Data layer and utility library for developing Prezly themes with JavaScript

40 lines (39 loc) 2.05 kB
"use strict"; var _languages = require("../../__mocks__/languages.cjs"); var _getShortestLocaleSlug = require("./getShortestLocaleSlug.cjs"); var locales = Object.values(_languages.LANGUAGES).map(lang => lang.code); var defaultLocale = Object.values(_languages.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((0, _getShortestLocaleSlug.getShortestLocaleSlug)('en', context)).toBe(false); }); it('returns neutral language code if it is the only culture with that language', () => { expect((0, _getShortestLocaleSlug.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((0, _getShortestLocaleSlug.getShortestLocaleSlug)('en_US', context)).toBe('us'); expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('en_GB', context)).toBe('gb'); expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('nl_NL', context)).toBe('nl-nl'); // cannot use `nl`, as it collides with language code expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('fr', context)).toBe('fr'); }); it('returns full code if it can not be shortened', () => { expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('fr_BE', context)).toBe('fr-be'); expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('nl_BE', context)).toBe('nl-be'); }); it('returns full code when trying to shorten to region code for es-419', () => { expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('es_419', context)).toBe('es-419'); }); it('returns full code when trying to shorten to region code for zg-Hant', () => { expect((0, _getShortestLocaleSlug.getShortestLocaleSlug)('zh_Hant', context)).toBe('zh-hant'); }); });