Skip to content

Commit 485c9c0

Browse files
committed
Add class tests
1 parent 7b7e439 commit 485c9c0

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

test/class.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { I18n } from '../src'
2+
3+
it('can update options and get the active language', async () => {
4+
const i18n = new I18n({ lang: 'en '})
5+
6+
i18n.setOptions({ lang: 'et' })
7+
8+
expect(i18n.getActiveLanguage()).toBe('et')
9+
})
10+
11+
it('can set the active language and its messages', async () => {
12+
const i18n = new I18n()
13+
14+
i18n.setLanguage({ lang: 'pt', messages: { 'Welcome!': 'Bem-vindo!' }})
15+
16+
expect(i18n.getActiveLanguage()).toBe('pt')
17+
expect(i18n.trans('Welcome!')).toBe('Bem-vindo!')
18+
})
19+
20+
it('does not share state between different instances', async () => {
21+
const enI18n = new I18n()
22+
const ptI18n = new I18n()
23+
24+
enI18n.setLanguage({ lang: 'en', messages: { 'Welcome!': 'Welcome!' }})
25+
ptI18n.setLanguage({ lang: 'pt', messages: { 'Welcome!': 'Bem-vindo!' }})
26+
27+
expect(enI18n.getActiveLanguage()).toBe('en')
28+
expect(enI18n.trans('Welcome!')).toBe('Welcome!')
29+
30+
expect(ptI18n.getActiveLanguage()).toBe('pt')
31+
expect(ptI18n.trans('Welcome!')).toBe('Bem-vindo!')
32+
})
33+
34+
it('allows creating a shared instance', async () => {
35+
const shared1 = I18n.getSharedInstance()
36+
const shared2 = I18n.getSharedInstance()
37+
38+
expect(shared1).toBeInstanceOf(I18n)
39+
expect(shared2).toBe(shared1)
40+
})
41+
42+
it('allows creating a shared instance with options', async () => {
43+
const shared = I18n.getSharedInstance({ lang: 'pt' })
44+
45+
expect(shared.getActiveLanguage()).toBe('pt')
46+
})
47+
48+
it('allows updating options when getting a shared instance', async () => {
49+
const shared = I18n.getSharedInstance({ lang: 'en' })
50+
51+
I18n.getSharedInstance({ lang: 'pt' })
52+
53+
expect(shared.getActiveLanguage()).toBe('pt')
54+
})
55+
56+
it('allows resetting all data', async () => {
57+
const i18n = new I18n({
58+
resolve: lang => import(`./fixtures/lang/${lang}.json`)
59+
})
60+
61+
await i18n.loadLanguageAsync('pt')
62+
63+
expect(i18n.getActiveLanguage()).toBe('pt')
64+
expect(i18n.trans('Welcome!')).toBe('Bem-vindo!')
65+
66+
i18n.reset()
67+
68+
expect(i18n.getActiveLanguage()).toBe('en')
69+
expect(i18n.trans('Welcome!')).toBe('Welcome!')
70+
})

0 commit comments

Comments
 (0)