-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.test.js
81 lines (70 loc) · 2.08 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
import cheerio from 'cheerio'
import { join } from 'path'
import {
findPort,
launchApp,
killApp,
nextStart,
nextBuild,
renderViaHTTP,
File,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 5)
let app
let appPort
const appDir = join(__dirname, '..')
const nextConfig = new File(join(appDir, 'next.config.js'))
const runTests = () => {
it('should have gip in __NEXT_DATA__', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).gip).toBe(true)
})
it('should not have gip in __NEXT_DATA__ for non-GIP page', async () => {
const html = await renderViaHTTP(appPort, '/normal')
const $ = cheerio.load(html)
expect('gip' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false)
})
it('should have correct router.asPath for direct visit dynamic page', async () => {
const html = await renderViaHTTP(appPort, '/blog/1')
const $ = cheerio.load(html)
expect($('#as-path').text()).toBe('/blog/1')
})
it('should have correct router.asPath for direct visit dynamic page rewrite direct', async () => {
const html = await renderViaHTTP(appPort, '/blog/post/1')
const $ = cheerio.load(html)
expect($('#as-path').text()).toBe('/blog/post/1')
})
}
describe('getInitialProps', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
await nextConfig.replace('// replace me', `target: 'serverless', `)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})