forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.test.ts
143 lines (134 loc) · 3.54 KB
/
gen.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
142
143
import { it, expect } from 'vitest'
import { parse } from '@babel/parser'
import { generateLoaderOptions } from '../src/gen'
import { DEFAULT_OPTIONS } from '../src/constants'
import type { NuxtI18nOptions, NuxtI18nInternalOptions } from '../src/types'
import type { AdditionalMessages } from '../src/messages'
const LOCALE_CODES = ['en', 'ja', 'fr']
const LOCALE_INFO = [
{
code: 'en',
file: 'en.json',
path: '/path/to/en.json'
},
{
code: 'ja',
file: 'ja.json',
path: '/path/to/ja.json'
}
]
const ADDITIONAL_MESSAGES = {
en: [
{ foo: 'foo', hello: 'hello1' },
{ bar: 'bar', hello: 'hello2' }
],
ja: [
{ buz: 'buz', hello: 'hello3' },
{ baz: 'baz', hello: 'hello4' }
]
} as AdditionalMessages
const NUXT_I18N_OPTIONS = {
defaultLocale: 'en',
vueI18n: {
locale: 'en',
fallbackLocale: 'en',
messages: {
en: { hello: 'Hello!' }
}
}
} as NuxtI18nOptions
const NUXT_I18N_INTERNAL_OPTIONS = {
__normalizedLocales: [
{
code: 'en'
}
]
} as NuxtI18nInternalOptions
function validateSyntax(code: string): boolean {
let ret = false
try {
const node = parse(code, {
allowImportExportEverywhere: true,
sourceType: 'module',
plugins: ['importAssertions']
})
ret = !node.errors.length
} catch (e) {
console.error(e)
}
return ret
}
it('basic', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: {},
nuxtI18nOptions: NUXT_I18N_OPTIONS,
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})
it('lazy', () => {
const code = generateLoaderOptions(true, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: {},
nuxtI18nOptions: NUXT_I18N_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})
it('vueI18n: path', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
localeInfo: LOCALE_INFO,
additionalMessages: ADDITIONAL_MESSAGES,
nuxtI18nOptions: {
vueI18n: '~/plugins/vue-i18n.js'
},
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})
it('toCode: function (arrow)', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
additionalMessages: {},
nuxtI18nOptions: {
...NUXT_I18N_OPTIONS,
locales: LOCALE_INFO.map(locale => ({
...locale,
testFunc: (prop: unknown) => {
return `Hello ${prop}`
}
}))
},
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})
it('toCode: function (named)', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
additionalMessages: {},
nuxtI18nOptions: {
...NUXT_I18N_OPTIONS,
locales: LOCALE_INFO.map(locale => ({
...locale,
testFunc(prop: unknown) {
return `Hello ${prop}`
}
}))
},
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})