-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.ts
141 lines (123 loc) · 4.13 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { beforeEach, describe, expect, it } from 'bun:test'
import Elysia from 'elysia'
import { i18next, newLanguageDetector, LanguageDetector } from './index.ts'
import { createInstance, InitOptions, i18n } from 'i18next'
export const req = (path: string, requestInit?: RequestInit) =>
new Request(`http://localhost${path}`, requestInit)
const initOptions: InitOptions = {
lng: 'nl',
resources: {
nl: {
translation: {
greeting: 'Hallo!',
},
},
en: {
translation: {
greeting: 'Hello!',
},
},
fr: {
translation: {
greeting: 'Bonjour!',
},
},
},
fallbackLng: false,
}
describe('i18next', () => {
let instance: i18n
beforeEach(() => {
instance = createInstance(initOptions)
})
it('translates', async () => {
const app = new Elysia()
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Hallo!')
})
it('changes language based on query parameter', async () => {
const app = new Elysia()
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/?lang=en'))
expect(await response.text()).toEqual('Hello!')
})
it('changes language based on accept-language header', async () => {
const app = new Elysia()
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(
req('/', { headers: { 'accept-language': 'en' } })
)
expect(await response.text()).toEqual('Hello!')
})
it('changes language based on property in store', async () => {
const app = new Elysia()
.state('language', 'en')
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Hello!')
})
it('allows to override language detection', async () => {
const app = new Elysia()
.use(i18next({ instance, detectLanguage: () => 'fr' }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Bonjour!')
})
it('changes language based on cookie', async () => {
const app = new Elysia()
.onBeforeHandle(({ cookie: { lang } }) => {
lang.value = 'en'
})
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Hello!')
})
it('changes language based on path param', async () => {
const app = new Elysia()
.use(i18next({ instance }))
.get('/:lang/', ({ t }) => t('greeting'))
const response = await app.handle(req('/fr/'))
expect(await response.text()).toEqual('Bonjour!')
})
it('accepts an i18next instance', async () => {
instance = createInstance({
...initOptions,
lng: 'fr',
})
const app = new Elysia()
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Bonjour!')
})
it('changes language using a custom LanguageDetector created with newLanguageDetector()', async () => {
const detector: LanguageDetector = newLanguageDetector({
searchParamName: 'x-lang',
storeParamName: 'x-lang',
headerName: 'x-lang',
cookieName: 'x-lang',
pathParamName: 'x-lang',
})
const app = new Elysia()
.use(i18next({ instance, detectLanguage: detector }))
.get('/', ({ t }) => t('greeting'))
const response = await app.handle(req('/?x-lang=en'))
expect(await response.text()).toEqual('Hello!')
})
it('resets language to default on every request', async () => {
const app = new Elysia()
.use(i18next({ instance }))
.get('/', ({ t }) => t('greeting'))
await app.modules
const responseEN = await app.handle(req('/?lang=fr'))
const responseNL = await app.handle(req('/'))
expect(await responseEN.text()).toEqual('Bonjour!')
expect(await responseNL.text()).toEqual('Hallo!')
})
})