mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 09:59:58 +01:00
ff541367f8
✅ Closes: Closes #83
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import {
|
|
getLocaleFromQueryString,
|
|
getLocaleFromHash,
|
|
getLocaleFromNavigator,
|
|
getLocaleFromPathname,
|
|
getLocaleFromHostname,
|
|
} from '../../../src/runtime/includes/localeGetters';
|
|
|
|
describe('getting client locale', () => {
|
|
beforeEach(() => {
|
|
delete window.location;
|
|
window.location = {
|
|
pathname: '/',
|
|
hostname: 'example.com',
|
|
hash: '',
|
|
search: '',
|
|
} as any;
|
|
});
|
|
|
|
it('gets the locale based on the passed hash parameter', () => {
|
|
window.location.hash = '#locale=en-US&lang=pt-BR';
|
|
expect(getLocaleFromHash('lang')).toBe('pt-BR');
|
|
});
|
|
|
|
it('gets the locale based on the passed search parameter', () => {
|
|
window.location.search = '?locale=en-US&lang=pt-BR';
|
|
expect(getLocaleFromQueryString('lang')).toBe('pt-BR');
|
|
});
|
|
|
|
it('gets the locale based on the navigator language', () => {
|
|
expect(getLocaleFromNavigator()).toBe(window.navigator.language);
|
|
});
|
|
|
|
it('gets the locale based on the pathname', () => {
|
|
window.location.pathname = '/en-US/foo/';
|
|
expect(getLocaleFromPathname(/^\/(.*?)\//)).toBe('en-US');
|
|
});
|
|
|
|
it('gets the locale base on the hostname', () => {
|
|
window.location.hostname = 'pt.example.com';
|
|
expect(getLocaleFromHostname(/^(.*?)\./)).toBe('pt');
|
|
});
|
|
|
|
it('returns null if no locale was found', () => {
|
|
expect(getLocaleFromQueryString('lang')).toBeNull();
|
|
});
|
|
});
|