forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.test.js
97 lines (74 loc) · 3.48 KB
/
unit.test.js
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
import path from 'path'
import { matchBrowserLocale, parseAcceptLanguage } from '../src/templates/utils-common'
describe('parsePages', () => {
test('parses in-component options', async () => {
const { extractComponentOptions } = await import('../src/helpers/components')
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/index.vue'))
expect(options).toHaveProperty('paths')
expect(options.paths).toHaveProperty('pl')
expect(options.paths.pl).toBe('/polish')
})
test('triggers warning with invalid in-component options', async () => {
const { extractComponentOptions } = await import('../src/helpers/components')
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/invalidOptions.vue'))
expect(spy.mock.calls[0][0]).toContain('Error parsing')
spy.mockRestore()
expect(Object.keys(options).length).toBe(0)
})
})
describe('parseAcceptLanguage', () => {
test('parses browser accept-language', () => {
expect(parseAcceptLanguage('en-US,en;q=0.9,nb;q=0.8,no;q=0.7')).toStrictEqual(['en-US', 'en', 'nb', 'no'])
})
})
describe('matchBrowserLocale', () => {
test('matches highest-ranked full locale', () => {
// Both locales match first browser locale - full locale should win.
const appLocales = ['en', 'en-US']
const browserLocales = ['en-US', 'en']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en-US')
})
test('matches highest-ranked short locale', () => {
// Both locales match first browser locale - short locale should win.
// This is because browser locale order defines scoring so we prefer higher-scored over exact.
const appLocales = ['en', 'en-US']
const browserLocales = ['en', 'en-US']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en')
})
test('matches highest-ranked short locale (only short defined)', () => {
const appLocales = ['en']
const browserLocales = ['en-US', 'en']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en')
})
test('matches highest-ranked short locale', () => {
const appLocales = ['en', 'fr']
const browserLocales = ['en-US', 'en-GB']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en')
})
test('does not match any locale', () => {
const appLocales = ['pl', 'fr']
const browserLocales = ['en-US', 'en']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe(null)
})
test('matches full locale with mixed short and full, full having highest rank', () => {
const appLocales = ['en-US', 'en-GB', 'en']
const browserLocales = ['en-GB', 'en-US', 'en']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en-GB')
})
test('matches short locale with mixed short and full, short having highest rank', () => {
const appLocales = ['en-US', 'en-GB', 'en']
const browserLocales = ['en', 'en-GB', 'en-US']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en')
})
test('matches short locale case-insensitively', () => {
const appLocales = ['EN', 'en-GB']
const browserLocales = ['en', 'en-GB', 'en-US']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('EN')
})
test('matches long locale case-insensitively', () => {
const appLocales = ['en-gb', 'en-US']
const browserLocales = ['en-GB', 'en-US']
expect(matchBrowserLocale(appLocales, browserLocales)).toBe('en-gb')
})
})