/
index.test.js
102 lines (93 loc) · 2.44 KB
/
index.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
98
99
100
101
102
/* eslint-env jest */
import { join } from 'path'
import assert from 'assert'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
check,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const locales = ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en']
let appPort
let app
const runTests = () => {
it('should rewrite index route correctly', async () => {
for (const locale of locales) {
const html = await renderViaHTTP(
appPort,
`/${locale === 'en' ? '' : locale}`
)
const $ = cheerio.load(html)
expect(JSON.parse($('#props').text())).toEqual({
params: {
slug: ['company', 'about-us'],
},
locale,
hello: 'world',
})
}
})
it('should handle index rewrite on client correctly', async () => {
for (const locale of locales) {
const browser = await webdriver(
appPort,
`${locale === 'en' ? '' : `/${locale}`}/hello`
)
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
params: {
slug: ['hello'],
},
locale,
hello: 'world',
})
await browser.eval(`(function() {
window.beforeNav = 1
window.next.router.push('/')
})()`)
await check(async () => {
const html = await browser.eval('document.documentElement.innerHTML')
const props = JSON.parse(cheerio.load(html)('#props').text())
assert.deepEqual(props, {
params: {
slug: ['company', 'about-us'],
},
locale,
hello: 'world',
})
return 'success'
}, 'success')
expect(await browser.eval('window.beforeNav')).toBe(1)
}
})
}
describe('Custom routes i18n support index rewrite', () => {
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode',
() => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests(true)
}
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
}
)
})