/
index.test.js
90 lines (80 loc) · 2.69 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
/* eslint-env jest */
import { join } from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import assert from 'assert'
import { check, findPort, killApp, nextBuild, nextStart } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const context = {
appDir: join(__dirname, '../'),
buildLogs: { output: '', stdout: '', stderr: '' },
logs: { output: '', stdout: '', stderr: '' },
}
describe('Middleware Production Prefetch', () => {
afterAll(() => killApp(context.app))
beforeAll(async () => {
const build = await nextBuild(context.appDir, undefined, {
stderr: true,
stdout: true,
})
context.buildId = await fs.readFile(
join(context.appDir, '.next/BUILD_ID'),
'utf8'
)
context.buildLogs = {
output: build.stdout + build.stderr,
stderr: build.stderr,
stdout: build.stdout,
}
context.dev = false
context.appPort = await findPort()
context.app = await nextStart(context.appDir, context.appPort, {
env: {
MIDDLEWARE_TEST: 'asdf',
NEXT_RUNTIME: 'edge',
},
onStdout(msg) {
context.logs.output += msg
context.logs.stdout += msg
},
onStderr(msg) {
context.logs.output += msg
context.logs.stderr += msg
},
})
})
it(`prefetch correctly for unexistent routes`, async () => {
const browser = await webdriver(context.appPort, `/`)
await browser.elementByCss('#made-up-link').moveTo()
await check(async () => {
const scripts = await browser.elementsByCss('script')
const attrs = await Promise.all(
scripts.map((script) => script.getAttribute('src'))
)
return attrs.find((src) => src.includes('/ssg-page')) ? 'yes' : 'nope'
}, 'yes')
})
it(`prefetches data when it is ssg`, async () => {
const browser = await webdriver(context.appPort, `/`)
await browser.elementByCss('#made-up-link').moveTo()
await check(async () => {
const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
const mapped = hrefs.map((href) =>
new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '')
)
assert.deepEqual(mapped, ['/index.json'])
return 'yes'
}, 'yes')
})
it(`prefetches provided path even if it will be rewritten`, async () => {
const browser = await webdriver(context.appPort, `/`)
await browser.elementByCss('#ssg-page-2').moveTo()
await check(async () => {
const scripts = await browser.elementsByCss('script')
const attrs = await Promise.all(
scripts.map((script) => script.getAttribute('src'))
)
return attrs.find((src) => src.includes('/ssg-page-2')) ? 'yes' : 'nope'
}, 'yes')
})
})