forked from vercel/next.js
/
index.test.js
194 lines (157 loc) · 4.97 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
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
/* eslint-env jest */
import { join } from 'path'
import {
renderViaHTTP,
nextServer,
startApp,
stopApp,
nextBuild,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
jest.setTimeout(1000 * 60 * 5)
let appDir = join(__dirname, '..')
let server
let appPort
describe('Script Loader', () => {
beforeAll(async () => {
await nextBuild(appDir)
const app = nextServer({
dir: appDir,
dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => {
stopApp(server)
})
it('priority afterInteractive', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await waitFor(1000)
async function test(id) {
const script = await browser.elementById(id)
const dataAttr = await script.getAttribute('data-nscript')
const endScripts = await browser.elementsByCss(
`#__NEXT_DATA__ ~ #${id}`
)
// Renders script tag
expect(script).toBeDefined()
expect(dataAttr).toBeDefined()
// Script is inserted at the end
expect(endScripts.length).toBe(1)
}
// afterInteractive script in page
await test('scriptAfterInteractive')
// afterInteractive script in _document
await test('documentAfterInteractive')
} finally {
if (browser) await browser.close()
}
})
it('priority lazyOnload', async () => {
let browser
try {
browser = await webdriver(appPort, '/page3')
await browser.waitForElementByCss('#onload-div')
await waitFor(1000)
const logs = await browser.log('browser')
const filteredLogs = logs.filter(
(log) => !log.message.includes('Failed to load resource')
)
expect(filteredLogs.length).toBe(0)
async function test(id) {
const script = await browser.elementById(id)
const dataAttr = await script.getAttribute('data-nscript')
const endScripts = await browser.elementsByCss(
`#__NEXT_DATA__ ~ #${id}`
)
// Renders script tag
expect(script).toBeDefined()
expect(dataAttr).toBeDefined()
// Script is inserted at the end
expect(endScripts.length).toBe(1)
}
// lazyOnload script in page
await test('scriptLazyOnload')
// lazyOnload script in _document
await test('documentLazyOnload')
} finally {
if (browser) await browser.close()
}
})
it('priority beforeInteractive', async () => {
const html = await renderViaHTTP(appPort, '/page1')
const $ = cheerio.load(html)
function test(id) {
const script = $(`#${id}`)
// Renders script tag
expect(script.length).toBe(1)
expect(script.attr('data-nscript')).toBeDefined()
// Script is inserted before NextScripts
expect(
$(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length
).toBeGreaterThan(0)
}
test('scriptBeforeInteractive')
test('documentBeforeInteractive')
})
it('priority beforeInteractive on navigate', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
// beforeInteractive scripts should load once
let documentBIScripts = await browser.elementsByCss(
'[src$="documentBeforeInteractive"]'
)
expect(documentBIScripts.length).toBe(1)
await browser.waitForElementByCss('[href="/page1"]')
await browser.click('[href="/page1"]')
await browser.waitForElementByCss('.container')
const script = await browser.elementById('scriptBeforeInteractive')
// Ensure beforeInteractive script isn't duplicated on navigation
documentBIScripts = await browser.elementsByCss(
'[src$="documentBeforeInteractive"]'
)
expect(documentBIScripts.length).toBe(1)
// Renders script tag
expect(script).toBeDefined()
} finally {
if (browser) await browser.close()
}
})
it('onload fires correctly', async () => {
let browser
try {
browser = await webdriver(appPort, '/page4')
await waitFor(3000)
const text = await browser.elementById('text').text()
expect(text).toBe('aaabbbccc')
} finally {
if (browser) await browser.close()
}
})
it('Does not duplicate inline scripts', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
// Navigate away and back to page
await browser.waitForElementByCss('[href="/page5"]')
await browser.click('[href="/page5"]')
await browser.waitForElementByCss('[href="/"]')
await browser.click('[href="/"]')
await browser.waitForElementByCss('[href="/page5"]')
await browser.click('[href="/page5"]')
await browser.waitForElementByCss('.container')
await waitFor(1000)
const text = await browser.elementById('text').text()
expect(text).toBe('abc')
} finally {
if (browser) await browser.close()
}
})
})