svelte-i18n/test/index.test.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-05-19 03:59:26 +02:00
import { dictionary, locale, format } from '../src/index'
let _
let currentLocale
const dict = {
pt: {
hi: 'olá você',
'switch.lang': 'Trocar idioma',
greeting: {
ask: 'Por favor, digite seu nome',
message: 'Olá {name}, como vai?',
2018-07-31 00:57:05 +02:00
},
2019-05-19 03:59:26 +02:00
photos:
'Você {n, plural, =0 {não tem fotos.} =1 {tem uma foto.} other {tem # fotos.}}',
cats: 'Tenho {n, number} {n,plural,=0{gatos}one{gato}other{gatos}}',
},
en: {
hi: 'hi yo',
'switch.lang': 'Switch language',
greeting: {
ask: 'Please type your name',
message: 'Hello {name}, how are you?',
},
2019-05-19 03:59:26 +02:00
photos:
'You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}',
cats: 'I have {n, number} {n,plural,one{cat}other{cats}}',
},
}
2019-05-19 03:59:26 +02:00
format.subscribe(formatFn => {
_ = formatFn
})
dictionary.set(dict)
locale.subscribe(l => (currentLocale = l))
locale.set('pt')
it('should change locale', () => {
locale.set('pt')
expect(currentLocale).toBe('pt')
locale.set('en')
expect(currentLocale).toBe('en')
})
2018-07-26 03:40:38 +02:00
2019-05-19 03:59:26 +02:00
it('should fallback to message id if id is not found', () => {
expect(_('batatinha')).toBe('batatinha')
})
2018-07-31 00:57:05 +02:00
2019-05-19 03:59:26 +02:00
it('should translate to current locale', () => {
locale.set('pt')
expect(_('switch.lang')).toBe('Trocar idioma')
locale.set('en')
expect(_('switch.lang')).toBe('Switch language')
})
2018-07-31 00:57:05 +02:00
2019-05-19 03:59:26 +02:00
it('should translate to passed locale', () => {
expect(_('switch.lang', 'pt')).toBe('Trocar idioma')
expect(_('switch.lang', 'en')).toBe('Switch language')
})
2018-07-31 00:57:05 +02:00
2019-05-19 03:59:26 +02:00
it('should interpolate message with variables', () => {
expect(_('greeting.message', { name: 'Chris' })).toBe(
'Hello Chris, how are you?',
)
})
2018-07-31 00:57:05 +02:00
2019-05-19 03:59:26 +02:00
it('should interpolate message with variables according to passed locale', () => {
expect(_('greeting.message', { name: 'Chris' }, 'pt')).toBe(
'Olá Chris, como vai?',
)
})
2018-08-07 22:44:05 +02:00
2019-05-19 03:59:26 +02:00
describe('utilities', () => {
beforeAll(() => {
locale.set('en')
2018-08-07 22:44:05 +02:00
})
2018-07-31 00:57:05 +02:00
2018-08-07 22:44:05 +02:00
it('should capital a translated message', () => {
2019-05-19 03:59:26 +02:00
expect(_.capital('hi')).toBe('Hi yo')
2018-07-31 00:57:05 +02:00
})
2018-08-07 22:44:05 +02:00
it('should title a translated message', () => {
2019-05-19 03:59:26 +02:00
expect(_.title('hi')).toBe('Hi Yo')
2018-07-31 00:57:05 +02:00
})
it('should lowercase a translated message', () => {
2019-05-19 03:59:26 +02:00
expect(_.lower('hi')).toBe('hi yo')
2018-07-31 00:57:05 +02:00
})
it('should uppercase a translated message', () => {
2019-05-19 03:59:26 +02:00
expect(_.upper('hi')).toBe('HI YO')
})
const date = new Date(2019, 3, 24, 23, 45)
it('should format a time value', () => {
locale.set('en')
expect(_.time(date)).toBe('11:45 PM')
expect(_.time(date, 'medium')).toBe('11:45:00 PM')
})
2018-07-31 00:57:05 +02:00
2019-05-19 03:59:26 +02:00
it('should format a date value', () => {
expect(_.date(date)).toBe('4/24/19')
expect(_.date(date, 'medium')).toBe('Apr 24, 2019')
2018-07-26 03:40:38 +02:00
})
2019-05-19 03:59:26 +02:00
// number
it('should format a date value', () => {
expect(_.number(123123123)).toBe('123,123,123')
})
2018-07-26 03:40:38 +02:00
})