-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.ts
More file actions
126 lines (115 loc) · 2.99 KB
/
Copy pathindex.test.ts
File metadata and controls
126 lines (115 loc) · 2.99 KB
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
import { createCoreContext } from '@intlify/core'
import { describe, expect, test } from 'vitest'
import type { LocaleDetector } from '@intlify/core'
import type { Context } from 'hono'
import {
defineI18nMiddleware,
detectLocaleFromAcceptLanguageHeader,
getDetectorLocale,
useTranslation
} from './index.ts'
test('detectLocaleFromAcceptLanguageHeader', () => {
const mockContext = {
req: {
raw: {
headers: {
get: _name => (_name === 'accept-language' ? 'en-US,en;q=0.9,ja;q=0.8' : '')
}
}
}
} as Context
expect(detectLocaleFromAcceptLanguageHeader(mockContext)).toBe('en-US')
})
test('defineI18nMiddleware', () => {
const middleware = defineI18nMiddleware({
locale: detectLocaleFromAcceptLanguageHeader,
messages: {
en: {
hello: 'hello, {name}'
},
ja: {
hello: 'こんにちは, {name}'
}
}
})
expect(typeof middleware).toBe('function')
})
describe('useTranslation', () => {
test('basic', async () => {
/**
* setup `defineI18nMiddleware` emulates
*/
const context = createCoreContext({
locale: detectLocaleFromAcceptLanguageHeader,
messages: {
en: {
hello: 'hello, {name}'
},
ja: {
hello: 'こんにちは, {name}'
}
}
})
const mockContext = {
req: {
raw: {
headers: {
get: _name => (_name === 'accept-language' ? 'ja;q=0.9,en;q=0.8' : '')
}
}
},
get: (key: string) => {
if (key === 'i18n') {
return context
} else if (key === 'i18nLocaleDetector') {
const locale = context.locale as unknown
return (locale as LocaleDetector).bind(null, mockContext)
}
}
} as Context
// test `useTranslation`
const t = await useTranslation(mockContext)
expect(t('hello', { name: 'hono' })).toEqual('こんにちは, hono')
})
test('not initialize context', async () => {
const mockContext = {
req: {
raw: {
headers: {
get: _name => 'ja,en'
}
}
},
get: (_key: string) => {}
} as Context
await expect(() => useTranslation(mockContext)).rejects.toThrowError()
})
})
test('getDetectorLocale', async () => {
/**
* setup `defineI18nMiddleware` emulates
*/
const context = createCoreContext({
locale: detectLocaleFromAcceptLanguageHeader
})
const mockContext = {
req: {
raw: {
headers: {
get: _name => (_name === 'accept-language' ? 'ja;q=0.9,en;q=0.8' : '')
}
}
},
get: (key: string) => {
if (key === 'i18n') {
return context
} else if (key === 'i18nLocaleDetector') {
const locale = context.locale as unknown
return (locale as LocaleDetector).bind(null, mockContext)
}
}
} as Context
// test `getDetectorLocale`
const locale = await getDetectorLocale(mockContext)
expect(locale.language).toEqual('ja')
})