-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcore.test.ts
89 lines (79 loc) · 2 KB
/
core.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { get } from 'svelte/store'
import { locale, _, translations, clear, locales } from './core'
const translate = (message: string, interpolation?: any) => get(_)(message, interpolation)
beforeEach(() => {
clear()
translations.update({
en: {
name: 'Name',
},
pt: {
name: 'Nome',
},
})
locale.set('en')
})
describe('#translate', () => {
it('basic example should work', () => {
expect(translate('name')).toBe('Name')
})
it('should set locale', () => {
expect(get(locale)).toBe('en')
expect(translate('name')).toBe('Name')
locale.set('pt')
expect(get(locale)).toBe('pt')
expect(translate('name')).toBe('Nome')
})
it('should extend translations', () => {
translations.update({
en: {
hello: 'hello',
},
})
expect(translate('name')).toBe('Name')
expect(translate('hello')).toBe('hello')
})
it('should translate nested keys', () => {
translations.update({
en: {
foo: {
bar: {
baz: 'hello, {name}',
},
},
},
})
expect(translate('foo.bar.baz', { name: 'john' }))
.toBe('hello, john')
})
it('should return path if key is missing', () => {
expect(translate('missing')).toBe('missing')
})
it('should warn if locale messages are missing', () => {
console.error = jest.fn()
const log = console.error as jest.Mock
locale.set('ru')
expect(log).toHaveBeenCalledWith('[svelte-intl] Couldn\'t find the "ru" locale.')
log.mockRestore()
})
it('subscribe should work', (done) => {
const unsubscribe = _.subscribe((format) => {
if (format('name') === 'Name') {
return
}
expect(translate('name')).toBe('Nome')
unsubscribe()
done()
})
locale.set('pt')
})
test('#locales', () => {
expect(get(locales)).toEqual(['en', 'pt'])
translations.update({
ru: {
name: 'название',
},
})
expect(get(locales)).toEqual(['en', 'pt', 'ru'])
})
})