-
Notifications
You must be signed in to change notification settings - Fork 462
/
routing-tests.ts
290 lines (240 loc) · 10.7 KB
/
routing-tests.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { STRATEGIES } from '../../src/constants'
import type { Strategies } from '../../src/types'
import { getText, gotoPath, renderPage, waitForURL } from '../helper'
import { url } from '../utils'
export async function localePathTests(strategy: Strategies) {
const prefix = strategy !== STRATEGIES.NO_PREFIX
// helper function to add prefix to path based on `strategy`
const prefixPath = (path: string = '/', locale: string = 'en') => {
if (!prefix) return path.startsWith('/') ? path : '/' + path
const resolvedRoute = path === '/' ? undefined : path
return ['/', locale, resolvedRoute].filter(Boolean).join('')
}
const { page, consoleLogs } = await renderPage(prefixPath('/'))
// path
expect(await getText(page, '#locale-path .index')).toEqual(prefixPath('/'))
expect(await getText(page, '#locale-path .index-ja')).toEqual(prefixPath('/', 'ja'))
// name
expect(await getText(page, '#locale-path .about')).toEqual(prefixPath('/about'))
expect(await getText(page, '#locale-path .about-ja-path')).toEqual(prefixPath('/about', 'ja'))
// pathMatch
// TODO: fix named paths https://github.com/nuxt-modules/i18n/issues/2581
expect(await getText(page, '#locale-path .not-found')).toEqual(prefixPath('/'))
expect(await getText(page, '#locale-path .not-found-ja')).toEqual(prefixPath('', 'ja'))
// // object
expect(await getText(page, '#locale-path .about-ja-name-object')).toEqual(prefixPath('/about', 'ja'))
// // omit name & path
expect(await getText(page, '#locale-path .state-foo')).toEqual(prefixPath('/'))
// // preserve query parameters
expect(await getText(page, '#locale-path .query-foo')).toEqual(prefixPath('?foo=1'))
expect(await getText(page, '#locale-path .query-foo-index')).toEqual(prefixPath('?foo=1'))
expect(await getText(page, '#locale-path .query-foo-name-about')).toEqual(prefixPath('/about?foo=1'))
expect(await getText(page, '#locale-path .query-foo-path-about')).toEqual(prefixPath('/about?foo=1'))
expect(await getText(page, '#locale-path .query-foo-string')).toEqual(prefixPath('?foo=1'))
expect(await getText(page, '#locale-path .query-foo-string-about')).toEqual(prefixPath('/about?foo=1'))
expect(await getText(page, '#locale-path .query-foo-test-string')).toEqual(prefixPath('/about?foo=1&test=2'))
if (strategy === 'no_prefix') {
// TODO: fix localePath escapes paths for `no_prefix` strategy
// unexpectedly resolves to /path/as%20a%20test?foo=bar+sentence
expect(await getText(page, '#locale-path .query-foo-path-param')).not.toEqual(
prefixPath('/path/as a test?foo=bar+sentence')
)
} else {
expect(await getText(page, '#locale-path .query-foo-path-param')).toEqual(
prefixPath('/path/as a test?foo=bar+sentence')
)
}
expect(await getText(page, '#locale-path .query-foo-path-param-escaped')).toEqual(
prefixPath('/path/as%20a%20test?foo=bar+sentence')
)
expect(await getText(page, '#locale-path .hash-path-about')).toEqual(prefixPath('/about#foo=bar'))
// undefined path
expect(await getText(page, '#locale-path .undefined-path')).toEqual(prefixPath('/vue-i18n'))
// undefined name
expect(await getText(page, '#locale-path .undefined-name')).toEqual('')
// external
expect(await getText(page, '#locale-path .external-link')).toEqual('https://github.com')
expect(await getText(page, '#locale-path .external-mail')).toEqual('mailto:example@mail.com')
expect(await getText(page, '#locale-path .external-phone')).toEqual('tel:+31612345678')
// for vue-router deprecation
// https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22
expect(consoleLogs.find(log => log.text.includes('Discarded invalid param(s)'))).toBeFalsy()
}
export async function switchLocalePathTests() {
const { page } = await renderPage('/en')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja')
expect(await getText(page, '#switch-locale-path .undefined')).toEqual('')
await gotoPath(page, '/ja/about')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/about')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about')
await gotoPath(page, '/ja/about?foo=1&test=2')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/about?foo=1&test=2')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about?foo=1&test=2')
await page.goto(url('/ja/about?foo=bär&four=四&foo=bar'))
await waitForURL(page, '/ja/about?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/about?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
await page.goto(url('/ja/about?foo=bär&four=四'))
await waitForURL(page, '/ja/about?foo=b%C3%A4r&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about?foo=b%C3%A4r&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/about?foo=b%C3%A4r&four=%E5%9B%9B')
await gotoPath(page, '/ja/about#foo=bar')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about#foo=bar')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/about#foo=bar')
await page.goto(url('/ja/about?foo=é'))
await waitForURL(page, '/ja/about?foo=%C3%A9')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/about?foo=%C3%A9')
// TODO: figure out what was being tested originally
// await gotoPath(page, '/ja/category/1')
// expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/category/japanese')
// expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/category/english')
await page.goto(url('/ja/count/三'))
await waitForURL(page, '/ja/count/%E4%B8%89')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/count/三')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/count/三')
await page.goto(url('/ja/count/三?foo=bär&four=四&foo=bar'))
await waitForURL(page, '/ja/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/count/三?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/count/三?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
// TODO: figure out what was being tested originally
// await gotoPath(page, '/ja/foo')
// expect(await getText(page, '#switch-locale-path .ja')).toEqual('/ja/not-found-japanese')
// expect(await getText(page, '#switch-locale-path .en')).toEqual('/en/not-found-english')
}
export async function localeLocationTests() {
const { page } = await renderPage('/en')
expect(JSON.parse(await getText(page, '#locale-location .index'))).include({
fullPath: '/en',
path: '/en',
name: 'index___en',
href: '/en'
})
expect(JSON.parse(await getText(page, '#locale-location .index-name-ja'))).include({
fullPath: '/ja',
path: '/ja',
name: 'index___ja',
href: '/ja'
})
expect(JSON.parse(await getText(page, '#locale-location .about-name'))).include({
fullPath: '/en/about',
path: '/en/about',
name: 'about___en',
href: '/en/about'
})
expect(JSON.parse(await getText(page, '#locale-location .about-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
expect(JSON.parse(await getText(page, '#locale-location .about-name-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
expect(JSON.parse(await getText(page, '#locale-location .path-match-ja'))).include({
fullPath: '/ja/:pathMatch(.*)*',
path: '/ja/:pathMatch(.*)*',
name: 'pathMatch___ja',
href: '/ja/:pathMatch(.*)*'
})
// name
expect(JSON.parse(await getText(page, '#locale-location .path-match-name'))).include({
fullPath: '/en',
path: '/en',
name: 'pathMatch___en',
href: '/en'
})
expect(JSON.parse(await getText(page, '#locale-location .path-match-name-ja'))).include({
fullPath: '/ja',
path: '/ja',
name: 'pathMatch___ja',
href: '/ja'
})
// object
expect(JSON.parse(await getText(page, '#locale-location .about-object-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
// undefined path
expect(JSON.parse(await getText(page, '#locale-location .undefined-path-ja'))).include({
fullPath: '/ja/vue-i18n',
path: '/ja/vue-i18n',
name: 'pathMatch___ja',
href: '/ja/vue-i18n'
})
// undefined name
expect(await getText(page, '#locale-location .undefined-name-ja')).toEqual('')
}
export async function localeRouteTests() {
const { page } = await renderPage('/en')
expect(JSON.parse(await getText(page, '#locale-route .index'))).include({
fullPath: '/en',
path: '/en',
name: 'index___en',
href: '/en'
})
expect(JSON.parse(await getText(page, '#locale-route .index-name-ja'))).include({
fullPath: '/ja',
path: '/ja',
name: 'index___ja',
href: '/ja'
})
expect(JSON.parse(await getText(page, '#locale-route .about-name'))).include({
fullPath: '/en/about',
path: '/en/about',
name: 'about___en',
href: '/en/about'
})
expect(JSON.parse(await getText(page, '#locale-route .about-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
expect(JSON.parse(await getText(page, '#locale-route .about-name-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
expect(JSON.parse(await getText(page, '#locale-route .path-match-ja'))).include({
fullPath: '/ja/:pathMatch(.*)*',
path: '/ja/:pathMatch(.*)*',
name: 'pathMatch___ja',
href: '/ja/:pathMatch(.*)*'
})
// name
expect(JSON.parse(await getText(page, '#locale-route .path-match-name'))).include({
fullPath: '/en',
path: '/en',
name: 'pathMatch___en',
href: '/en'
})
expect(JSON.parse(await getText(page, '#locale-route .path-match-name-ja'))).include({
fullPath: '/ja',
path: '/ja',
name: 'pathMatch___ja',
href: '/ja'
})
// object
expect(JSON.parse(await getText(page, '#locale-route .about-object-ja'))).include({
fullPath: '/ja/about',
path: '/ja/about',
name: 'about___ja',
href: '/ja/about'
})
// undefined path
expect(JSON.parse(await getText(page, '#locale-route .undefined-path-ja'))).include({
fullPath: '/ja/vue-i18n',
path: '/ja/vue-i18n',
name: 'pathMatch___ja',
href: '/ja/vue-i18n'
})
// undefined name
expect(await getText(page, '#locale-route .undefined-name-ja')).toEqual('')
}