2018-08-09 00:47:16 +02:00
|
|
|
// TODO: A more serious test
|
|
|
|
|
2018-08-08 07:17:09 +02:00
|
|
|
import { i18n } from '../src/index'
|
2018-07-26 06:40:19 +02:00
|
|
|
import { Store } from 'svelte/store.umd'
|
2018-08-14 20:21:06 +02:00
|
|
|
import { capital, title, upper, lower, isObject } from '../src/utils'
|
2018-07-26 06:40:19 +02:00
|
|
|
|
2018-08-12 05:10:40 +02:00
|
|
|
let store = new Store()
|
2018-07-26 06:40:19 +02:00
|
|
|
const locales = {
|
|
|
|
'pt-br': {
|
|
|
|
test: 'teste',
|
2018-07-31 00:57:05 +02:00
|
|
|
phrase: 'adoro banana',
|
2018-07-27 04:24:17 +02:00
|
|
|
phrases: ['Frase 1', 'Frase 2'],
|
2018-08-07 22:44:05 +02:00
|
|
|
pluralization: 'Zero | Um | Muito!',
|
2018-08-14 20:21:06 +02:00
|
|
|
simplePluralization: 'Singular | Plural',
|
2018-07-31 00:57:05 +02:00
|
|
|
interpolation: {
|
|
|
|
key: 'Olá, {0}! Como está {1}?',
|
2018-08-09 00:47:16 +02:00
|
|
|
named: 'Olá, {name}! Como está {time}?',
|
2018-07-31 00:57:05 +02:00
|
|
|
},
|
2018-08-16 03:21:08 +02:00
|
|
|
interpolationPluralization: 'One thingie | {0} thingies',
|
2018-07-31 00:57:05 +02:00
|
|
|
wow: {
|
|
|
|
much: {
|
|
|
|
deep: {
|
2018-08-09 00:47:16 +02:00
|
|
|
list: ['Muito', 'muito profundo'],
|
|
|
|
},
|
|
|
|
},
|
2018-07-31 00:57:05 +02:00
|
|
|
},
|
2018-07-27 04:24:17 +02:00
|
|
|
obj: {
|
2018-08-09 00:47:16 +02:00
|
|
|
a: 'a',
|
|
|
|
b: 'b',
|
|
|
|
},
|
|
|
|
},
|
2018-07-26 06:40:19 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 05:10:40 +02:00
|
|
|
i18n(store, { dictionary: [locales] })
|
2018-07-26 03:40:38 +02:00
|
|
|
|
2018-07-31 00:57:05 +02:00
|
|
|
describe('Utilities', () => {
|
|
|
|
it('should check if a variable is an object', () => {
|
|
|
|
expect(isObject({})).toBe(true)
|
|
|
|
expect(isObject(1)).toBe(false)
|
2018-07-26 06:40:19 +02:00
|
|
|
})
|
|
|
|
})
|
2018-07-26 03:40:38 +02:00
|
|
|
|
2018-07-31 00:57:05 +02:00
|
|
|
describe('Localization', () => {
|
2018-08-09 20:49:01 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
console.error = jest.fn()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
console.error.mockRestore()
|
|
|
|
})
|
|
|
|
|
2018-07-31 00:57:05 +02:00
|
|
|
it('should start with a clean store', () => {
|
|
|
|
const { _, locale } = store.get()
|
|
|
|
expect(locale).toBeFalsy()
|
|
|
|
expect(_).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should change the locale after a "locale" store event', () => {
|
2018-08-09 20:49:01 +02:00
|
|
|
store.fire('locale', 'pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { locale, _ } = store.get()
|
|
|
|
|
2018-08-09 20:49:01 +02:00
|
|
|
expect(locale).toBe('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
expect(_).toBeInstanceOf(Function)
|
|
|
|
})
|
|
|
|
|
2018-08-09 00:47:16 +02:00
|
|
|
it('should have a .i18n.setLocale() method', () => {
|
|
|
|
expect(store.i18n.setLocale).toBeInstanceOf(Function)
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { locale } = store.get()
|
|
|
|
|
|
|
|
expect(locale).toBe('pt-br')
|
|
|
|
})
|
|
|
|
|
2018-08-09 20:49:01 +02:00
|
|
|
it('should handle nonexistent locale', () => {
|
|
|
|
expect(store.i18n.setLocale('foo'))
|
|
|
|
expect(console.error).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2018-07-31 00:57:05 +02:00
|
|
|
it('should return the message id when no message identified by it was found', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-07-31 00:57:05 +02:00
|
|
|
|
|
|
|
expect(_('non.existent')).toBe('non.existent')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get a message by its id', () => {
|
|
|
|
const { _ } = store.get()
|
|
|
|
expect(_('test')).toBe(locales['pt-br'].test)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get a deep nested message by its string path', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-07-31 00:57:05 +02:00
|
|
|
|
|
|
|
expect(_('obj.b')).toBe('b')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get a message within an array by its index', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-07-31 00:57:05 +02:00
|
|
|
|
|
|
|
expect(_('phrases[1]')).toBe(locales['pt-br'].phrases[1])
|
|
|
|
|
|
|
|
/** Not found */
|
|
|
|
expect(_('phrases[2]')).toBe('phrases[2]')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should interpolate with {numeric} placeholders', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-07-31 00:57:05 +02:00
|
|
|
|
2018-08-09 00:47:16 +02:00
|
|
|
expect(_('interpolation.key', ['Chris', 'o dia'])).toBe(
|
|
|
|
'Olá, Chris! Como está o dia?',
|
|
|
|
)
|
2018-07-31 00:57:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should interpolate with {named} placeholders', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-07-31 00:57:05 +02:00
|
|
|
|
|
|
|
expect(
|
|
|
|
_('interpolation.named', {
|
|
|
|
name: 'Chris',
|
2018-08-09 00:47:16 +02:00
|
|
|
time: 'o dia',
|
|
|
|
}),
|
2018-07-31 00:57:05 +02:00
|
|
|
).toBe('Olá, Chris! Como está o dia?')
|
|
|
|
})
|
2018-08-07 22:44:05 +02:00
|
|
|
|
|
|
|
it('should handle pluralization with _.plural()', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
|
|
|
const { _ } = store.get()
|
2018-08-07 22:44:05 +02:00
|
|
|
|
2018-08-14 20:21:06 +02:00
|
|
|
expect(_.plural('simplePluralization')).toBe('Plural')
|
|
|
|
expect(_.plural('simplePluralization', 1)).toBe('Singular')
|
|
|
|
expect(_.plural('simplePluralization', 3)).toBe('Plural')
|
|
|
|
expect(_.plural('simplePluralization', -23)).toBe('Plural')
|
2018-08-09 00:47:16 +02:00
|
|
|
expect(_.plural('pluralization')).toBe('Zero')
|
2018-08-07 22:44:05 +02:00
|
|
|
expect(_.plural('pluralization', 0)).toBe('Zero')
|
|
|
|
expect(_.plural('pluralization', 1)).toBe('Um')
|
|
|
|
expect(_.plural('pluralization', -1)).toBe('Um')
|
|
|
|
expect(_.plural('pluralization', -1000)).toBe('Muito!')
|
|
|
|
expect(_.plural('pluralization', 2)).toBe('Muito!')
|
|
|
|
expect(_.plural('pluralization', 100)).toBe('Muito!')
|
2018-08-16 03:21:08 +02:00
|
|
|
expect(_.plural('interpolationPluralization', 1)).toBe('One thingie')
|
|
|
|
expect(_.plural('interpolationPluralization', 10, [10])).toBe('10 thingies')
|
2018-08-07 22:44:05 +02:00
|
|
|
})
|
2018-07-31 00:57:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Localization utilities', () => {
|
2018-08-07 22:44:05 +02:00
|
|
|
it('should capital a translated message', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { _ } = store.get()
|
|
|
|
|
2018-08-08 07:17:09 +02:00
|
|
|
expect(capital('Adoro banana')).toBe('Adoro banana')
|
|
|
|
expect(_.capital('phrase')).toBe('Adoro banana')
|
2018-07-31 00:57:05 +02:00
|
|
|
})
|
|
|
|
|
2018-08-07 22:44:05 +02:00
|
|
|
it('should title a translated message', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { _ } = store.get()
|
|
|
|
|
2018-08-08 07:17:09 +02:00
|
|
|
expect(title('Adoro Banana')).toBe('Adoro Banana')
|
|
|
|
expect(_.title('phrase')).toBe('Adoro Banana')
|
2018-07-31 00:57:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should lowercase a translated message', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { _ } = store.get()
|
|
|
|
|
2018-08-08 07:17:09 +02:00
|
|
|
expect(lower('adoro banana')).toBe('adoro banana')
|
|
|
|
expect(_.lower('phrase')).toBe('adoro banana')
|
2018-07-31 00:57:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should uppercase a translated message', () => {
|
2018-08-09 00:47:16 +02:00
|
|
|
store.i18n.setLocale('pt-br')
|
2018-07-31 00:57:05 +02:00
|
|
|
const { _ } = store.get()
|
|
|
|
|
2018-08-08 07:17:09 +02:00
|
|
|
expect(upper('ADORO BANANA')).toBe('ADORO BANANA')
|
|
|
|
expect(_.upper('phrase')).toBe('ADORO BANANA')
|
2018-07-26 03:40:38 +02:00
|
|
|
})
|
|
|
|
})
|