-
Notifications
You must be signed in to change notification settings - Fork 26k
/
index.test.js
149 lines (128 loc) · 4.72 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
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
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import { validateAMP } from 'amp-test-utils'
import {
nextBuild,
renderViaHTTP,
findPort,
launchApp,
killApp,
nextStart,
nextExport,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let builtServerPagesDir
let appPort
let app
const fsExists = (file) =>
fs
.access(file)
.then(() => true)
.catch(() => false)
const runTests = (isDev = false) => {
it('should load an amp first page correctly', async () => {
const html = await renderViaHTTP(appPort, '/amp')
if (!isDev) {
await validateAMP(html)
}
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('yes')
})
it('should load a hybrid amp page without query correctly', async () => {
const html = await renderViaHTTP(appPort, '/hybrid')
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('no')
expect($('#hello').text()).toContain('hello')
})
it('should load dynamic hybrid SSG/AMP page', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('no')
expect($('#hello').text()).toContain('hello')
expect($('#slug').text()).toContain('post-1')
})
it('should load dynamic hybrid SSG/AMP page with trailing slash', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1/')
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('no')
expect($('#hello').text()).toContain('hello')
expect($('#slug').text()).toContain('post-1')
})
it('should load dynamic hybrid SSG/AMP page with query', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1?amp=1')
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('yes')
expect($('#hello').text()).toContain('hello')
expect($('#slug').text()).toContain('post-1')
})
it('should load a hybrid amp page with query correctly', async () => {
const html = await renderViaHTTP(appPort, '/hybrid?amp=1')
if (!isDev) {
await validateAMP(html)
}
const $ = cheerio.load(html)
expect($('#use-amp').text()).toContain('yes')
expect($('#hello').text()).toContain('hello')
})
if (!isDev) {
const builtPage = (file) => join(builtServerPagesDir, file)
it('should output prerendered files correctly during build', async () => {
expect(await fsExists(builtPage('amp.js'))).toBe(true)
expect(await fsExists(builtPage('amp.html'))).toBe(true)
expect(await fsExists(builtPage('amp.json'))).toBe(true)
expect(await fsExists(builtPage('hybrid.js'))).toBe(true)
expect(await fsExists(builtPage('hybrid.html'))).toBe(true)
expect(await fsExists(builtPage('hybrid.json'))).toBe(true)
expect(await fsExists(builtPage('hybrid.amp.js'))).toBe(false)
expect(await fsExists(builtPage('hybrid.amp.html'))).toBe(true)
expect(await fsExists(builtPage('hybrid.amp.json'))).toBe(true)
})
}
}
describe('AMP SSG Support', () => {
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
// TODO: use browser instead to do checks that now need filesystem access
builtServerPagesDir = join(appDir, '.next', 'server', 'pages')
})
afterAll(() => killApp(app))
runTests()
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests(true)
})
describe('export mode', () => {
let buildId
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir: join(appDir, 'out') })
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
it('should have copied SSG files correctly', async () => {
const outFile = (file) => join(appDir, 'out', file)
expect(await fsExists(outFile('amp.html'))).toBe(true)
expect(await fsExists(outFile('index.html'))).toBe(true)
expect(await fsExists(outFile('hybrid.html'))).toBe(true)
expect(await fsExists(outFile('amp.amp.html'))).toBe(false)
expect(await fsExists(outFile('hybrid.amp.html'))).toBe(true)
expect(await fsExists(outFile('blog/post-1.html'))).toBe(true)
expect(await fsExists(outFile('blog/post-1.amp.html'))).toBe(true)
expect(
await fsExists(outFile(join('_next/data', buildId, 'amp.json')))
).toBe(true)
expect(
await fsExists(outFile(join('_next/data', buildId, 'hybrid.json')))
).toBe(true)
})
})
})