2020-02-03 17:33:03 +01:00
|
|
|
import {
|
|
|
|
getLocaleFromQueryString,
|
|
|
|
getLocaleFromHash,
|
|
|
|
getLocaleFromNavigator,
|
|
|
|
getLocaleFromPathname,
|
|
|
|
getLocaleFromHostname,
|
|
|
|
} from '../../../src/runtime/includes/localeGetters'
|
2020-01-23 14:11:04 +01:00
|
|
|
import { flatObj } from '../../../src/runtime/includes/flatObj'
|
2019-11-26 17:05:17 +01:00
|
|
|
|
|
|
|
describe('getting client locale', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
delete window.location
|
|
|
|
window.location = {
|
|
|
|
pathname: '/',
|
|
|
|
hostname: 'example.com',
|
|
|
|
hash: '',
|
|
|
|
search: '',
|
|
|
|
} as any
|
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the locale based on the passed hash parameter', () => {
|
|
|
|
window.location.hash = '#locale=en-US&lang=pt-BR'
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromHash('lang')).toBe('pt-BR')
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the locale based on the passed search parameter', () => {
|
|
|
|
window.location.search = '?locale=en-US&lang=pt-BR'
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromQueryString('lang')).toBe('pt-BR')
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the locale based on the navigator language', () => {
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromNavigator()).toBe(window.navigator.language)
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the locale based on the pathname', () => {
|
|
|
|
window.location.pathname = '/en-US/foo/'
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromPathname(/^\/(.*?)\//)).toBe('en-US')
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the locale base on the hostname', () => {
|
|
|
|
window.location.hostname = 'pt.example.com'
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromHostname(/^(.*?)\./)).toBe('pt')
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('returns null if no locale was found', () => {
|
2020-02-03 17:33:03 +01:00
|
|
|
expect(getLocaleFromQueryString('lang')).toBe(null)
|
2019-11-26 17:05:17 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-01-15 17:47:02 +01:00
|
|
|
describe('deep object handling', () => {
|
|
|
|
test('flattens a deep object', () => {
|
|
|
|
const obj = {
|
|
|
|
a: { b: { c: { d: 'foo' } } },
|
|
|
|
e: { f: 'bar' },
|
|
|
|
}
|
|
|
|
expect(flatObj(obj)).toMatchObject({
|
|
|
|
'a.b.c.d': 'foo',
|
|
|
|
'e.f': 'bar',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('flattens a deep object with array values', () => {
|
|
|
|
const obj = {
|
|
|
|
a: { b: { c: { d: ['foo', 'bar'] } } },
|
|
|
|
e: { f: ['foo', 'bar'] },
|
|
|
|
}
|
|
|
|
expect(flatObj(obj)).toMatchObject({
|
|
|
|
'a.b.c.d.0': 'foo',
|
|
|
|
'a.b.c.d.1': 'bar',
|
|
|
|
'e.f.0': 'foo',
|
|
|
|
'e.f.1': 'bar',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|