2019-11-22 02:12:56 +01:00
|
|
|
// TODO remake this, it's a mess
|
2019-11-19 17:18:42 +01:00
|
|
|
import { Formatter } from '../../src/client/types'
|
2019-11-12 03:15:09 +01:00
|
|
|
import {
|
|
|
|
dictionary,
|
|
|
|
locale,
|
|
|
|
format,
|
|
|
|
addCustomFormats,
|
|
|
|
customFormats,
|
2019-11-20 20:31:55 +01:00
|
|
|
register,
|
|
|
|
waitLocale,
|
2019-11-19 17:18:42 +01:00
|
|
|
} from '../../src/client'
|
2019-11-24 03:38:15 +01:00
|
|
|
import { getClientLocale } from '../../src/client/includes/utils'
|
2019-05-19 03:59:26 +02:00
|
|
|
|
2019-11-19 17:18:42 +01:00
|
|
|
global.Intl = require('intl')
|
|
|
|
|
|
|
|
let _: Formatter
|
|
|
|
let currentLocale: string
|
2019-05-19 03:59:26 +02:00
|
|
|
|
|
|
|
const dict = {
|
2019-11-19 17:18:42 +01:00
|
|
|
en: require('../fixtures/en.json'),
|
2018-07-26 06:40:19 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
register('en-GB', () => import('../fixtures/en-GB.json'))
|
|
|
|
register('pt', () => import('../fixtures/pt.json'))
|
|
|
|
register('pt-BR', () => import('../fixtures/pt-BR.json'))
|
|
|
|
register('pt-PT', () => import('../fixtures/pt-PT.json'))
|
2019-11-20 05:32:54 +01:00
|
|
|
|
2019-05-19 03:59:26 +02:00
|
|
|
format.subscribe(formatFn => {
|
|
|
|
_ = formatFn
|
|
|
|
})
|
|
|
|
dictionary.set(dict)
|
2019-11-19 17:18:42 +01:00
|
|
|
locale.subscribe((l: string) => {
|
|
|
|
currentLocale = l
|
|
|
|
})
|
2019-05-19 03:59:26 +02:00
|
|
|
|
2019-11-19 17:18:42 +01:00
|
|
|
describe('locale', () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should change locale', async () => {
|
|
|
|
await locale.set('en')
|
2019-11-19 17:18:42 +01:00
|
|
|
expect(currentLocale).toBe('en')
|
2019-06-18 23:02:02 +02:00
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
await locale.set('en-US')
|
2019-11-20 03:38:01 +01:00
|
|
|
expect(currentLocale).toBe('en-US')
|
2019-11-19 17:18:42 +01:00
|
|
|
})
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
|
|
|
|
2019-11-19 17:18:42 +01:00
|
|
|
describe('dictionary', () => {
|
2019-11-20 05:32:54 +01:00
|
|
|
it('load a partial dictionary and merge it with the existing one', async () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
await locale.set('en')
|
|
|
|
register('en', () => import('../fixtures/partials/en.json'))
|
2019-11-20 05:32:54 +01:00
|
|
|
expect(_('page.title_about')).toBe('page.title_about')
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
await waitLocale('en')
|
2019-11-20 05:32:54 +01:00
|
|
|
expect(_('page.title_about')).toBe('About')
|
|
|
|
})
|
2019-05-19 03:59:26 +02:00
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-11-19 17:18:42 +01:00
|
|
|
describe('formatting', () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should translate to current locale', async () => {
|
|
|
|
await locale.set('en')
|
2019-11-20 01:04:50 +01:00
|
|
|
expect(_('switch.lang')).toBe('Switch language')
|
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should fallback to message id if id is not found', async () => {
|
|
|
|
await locale.set('en')
|
2019-11-19 17:18:42 +01:00
|
|
|
expect(_('batatinha.quente')).toBe('batatinha.quente')
|
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should fallback to default value if id is not found', async () => {
|
|
|
|
await locale.set('en')
|
2019-11-19 17:18:42 +01:00
|
|
|
expect(_('batatinha.quente', { default: 'Hot Potato' })).toBe('Hot Potato')
|
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-11-20 03:38:01 +01:00
|
|
|
it('should fallback to generic locale XX if id not found in XX-YY', async () => {
|
|
|
|
await locale.set('en-GB')
|
|
|
|
expect(_('sneakers', { locale: 'en-GB' })).toBe('trainers')
|
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should fallback to generic locale XX if id not found in XX-YY', async () => {
|
|
|
|
await locale.set('en-GB')
|
2019-11-19 17:18:42 +01:00
|
|
|
expect(_('switch.lang')).toBe('Switch language')
|
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should accept single object with id prop as the message path', async () => {
|
|
|
|
await locale.set('en')
|
2019-11-19 17:18:42 +01:00
|
|
|
expect(_({ id: 'switch.lang' })).toBe('Switch language')
|
|
|
|
})
|
|
|
|
|
2019-11-24 03:38:15 +01:00
|
|
|
it('should translate to passed locale', async () => {
|
|
|
|
await waitLocale('pt-BR')
|
2019-11-20 20:31:55 +01:00
|
|
|
expect(_('switch.lang', { locale: 'pt' })).toBe('Trocar idioma')
|
2019-11-19 17:18:42 +01:00
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should interpolate message with variables', async () => {
|
|
|
|
await locale.set('en')
|
|
|
|
expect(_('greeting.message', { values: { name: 'Chris' } })).toBe(
|
|
|
|
'Hello Chris, how are you?'
|
|
|
|
)
|
2019-11-19 17:18:42 +01:00
|
|
|
})
|
2019-05-19 03:59:26 +02:00
|
|
|
})
|
2018-08-07 22:44:05 +02:00
|
|
|
|
2019-05-19 03:59:26 +02:00
|
|
|
describe('utilities', () => {
|
2019-06-18 23:02:02 +02:00
|
|
|
describe('get locale', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
delete window.location
|
|
|
|
window.location = {
|
2019-11-20 21:10:40 +01:00
|
|
|
pathname: '/',
|
|
|
|
hostname: 'example.com',
|
2019-06-18 23:02:02 +02:00
|
|
|
hash: '',
|
|
|
|
search: '',
|
2019-11-19 17:18:42 +01:00
|
|
|
} as any
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
it('should get the locale based on the passed hash parameter', () => {
|
|
|
|
window.location.hash = '#locale=en-US&lang=pt-BR'
|
|
|
|
expect(getClientLocale({ hash: 'lang' })).toBe('pt-BR')
|
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
it('should get the locale based on the passed search parameter', () => {
|
|
|
|
window.location.search = '?locale=en-US&lang=pt-BR'
|
|
|
|
expect(getClientLocale({ search: 'lang' })).toBe('pt-BR')
|
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
it('should get the locale based on the navigator language', () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
expect(getClientLocale({ navigator: true })).toBe(
|
|
|
|
window.navigator.language
|
|
|
|
)
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-11-20 21:10:40 +01:00
|
|
|
it('should get the default locale', () => {
|
|
|
|
expect(getClientLocale({ default: 'pt' })).toBe('pt')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the fallback locale', () => {
|
|
|
|
window.location.pathname = '/en-US/foo/'
|
|
|
|
expect(getClientLocale({ pathname: /^\/(.*?)\// })).toBe('en-US')
|
|
|
|
})
|
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
it('should get the fallback locale', () => {
|
2019-11-20 21:10:40 +01:00
|
|
|
window.location.hostname = 'pt.example.com'
|
2019-11-20 22:29:31 +01:00
|
|
|
expect(getClientLocale({ hostname: /^(.*?)\./ })).toBe('pt')
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
2019-05-19 03:59:26 +02:00
|
|
|
})
|
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
describe('format utils', () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
beforeAll(async () => {
|
|
|
|
await locale.set('en')
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2019-06-18 23:02:02 +02:00
|
|
|
it('should capital a translated message', () => {
|
|
|
|
expect(_.capital('hi')).toBe('Hi yo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should title a translated message', () => {
|
|
|
|
expect(_.title('hi')).toBe('Hi Yo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should lowercase a translated message', () => {
|
|
|
|
expect(_.lower('hi')).toBe('hi yo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should uppercase a translated message', () => {
|
|
|
|
expect(_.upper('hi')).toBe('HI YO')
|
|
|
|
})
|
|
|
|
|
|
|
|
const date = new Date(2019, 3, 24, 23, 45)
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should format a time value', async () => {
|
|
|
|
await locale.set('en')
|
2019-06-18 23:02:02 +02:00
|
|
|
expect(_.time(date)).toBe('11:45 PM')
|
2019-11-12 03:15:09 +01:00
|
|
|
expect(_.time(date, { format: 'medium' })).toBe('11:45:00 PM')
|
2019-11-20 20:31:55 +01:00
|
|
|
expect(_.time(date, { format: 'medium', locale: 'pt-BR' })).toBe(
|
|
|
|
'23:45:00'
|
|
|
|
)
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should format a date value', () => {
|
|
|
|
expect(_.date(date)).toBe('4/24/19')
|
2019-11-12 03:15:09 +01:00
|
|
|
expect(_.date(date, { format: 'medium' })).toBe('Apr 24, 2019')
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
|
|
|
// number
|
2019-05-19 03:59:26 +02:00
|
|
|
it('should format a date value', () => {
|
|
|
|
expect(_.number(123123123)).toBe('123,123,123')
|
|
|
|
})
|
2019-06-18 23:02:02 +02:00
|
|
|
})
|
2018-07-26 03:40:38 +02:00
|
|
|
})
|
2019-11-12 03:15:09 +01:00
|
|
|
|
|
|
|
describe('custom formats', () => {
|
2019-11-20 20:31:55 +01:00
|
|
|
beforeAll(async () => {
|
|
|
|
await locale.set('pt-BR')
|
2019-11-12 03:15:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should have default number custom formats', () => {
|
|
|
|
expect(customFormats.number).toMatchObject({
|
|
|
|
scientific: { notation: 'scientific' },
|
|
|
|
engineering: { notation: 'engineering' },
|
|
|
|
compactLong: { notation: 'compact', compactDisplay: 'long' },
|
|
|
|
compactShort: { notation: 'compact', compactDisplay: 'short' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow to add custom formats', () => {
|
|
|
|
addCustomFormats({
|
|
|
|
number: {
|
|
|
|
usd: { style: 'currency', currency: 'USD' },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(customFormats.number).toMatchObject({
|
|
|
|
usd: { style: 'currency', currency: 'USD' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
it('should format messages with custom formats', async () => {
|
2019-11-12 03:15:09 +01:00
|
|
|
addCustomFormats({
|
|
|
|
number: {
|
|
|
|
usd: { style: 'currency', currency: 'USD' },
|
|
|
|
brl: { style: 'currency', currency: 'BRL' },
|
|
|
|
},
|
2019-11-19 17:18:42 +01:00
|
|
|
date: {
|
|
|
|
customDate: { year: 'numeric', era: 'short' },
|
|
|
|
},
|
|
|
|
time: {
|
2019-11-22 18:52:29 +01:00
|
|
|
customTime: { hour: '2-digit', minute: '2-digit' },
|
2019-11-19 17:18:42 +01:00
|
|
|
},
|
2019-11-12 03:15:09 +01:00
|
|
|
})
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
await locale.set('en-US')
|
2019-11-19 17:18:42 +01:00
|
|
|
|
|
|
|
expect(_.number(123123123, { format: 'usd' })).toContain('$123,123,123.00')
|
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
expect(_.date(new Date(2019, 0, 1), { format: 'customDate' })).toEqual(
|
|
|
|
'2019 AD'
|
|
|
|
)
|
2019-11-12 03:15:09 +01:00
|
|
|
|
2019-11-20 20:31:55 +01:00
|
|
|
expect(
|
|
|
|
_.time(new Date(2019, 0, 1, 2, 0, 0), { format: 'customTime' })
|
2019-11-22 18:52:29 +01:00
|
|
|
).toEqual('02:00')
|
2019-11-12 03:15:09 +01:00
|
|
|
})
|
|
|
|
})
|