/
index.test.js
231 lines (199 loc) · 6.36 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* eslint-env jest */
/* global jasmine */
import {
nextServer,
runNextCommand,
startApp,
stopApp,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = join(__dirname, '../')
let appPort
let server
let app
describe('Prefetching Links in viewport', () => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => stopApp(server))
it('should prefetch with link in viewport onload', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
}
}
expect(found).toBe(true)
} finally {
if (browser) await browser.close()
}
})
it('should prefetch with link in viewport when href changes', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('button').click()
await waitFor(2 * 1000)
const links = await browser.elementsByCss('link[rel=prefetch]')
let foundFirst = false
let foundAnother = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('another')) foundAnother = true
if (href.includes('first')) foundFirst = true
}
expect(foundFirst).toBe(true)
expect(foundAnother).toBe(true)
} finally {
if (browser) await browser.close()
}
})
it('should prefetch with link in viewport on scroll', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('#scroll-to-another').click()
await waitFor(2 * 1000)
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('another')) {
found = true
break
}
}
expect(found).toBe(true)
} finally {
if (browser) await browser.close()
}
})
it('should prefetch with link in viewport and inject script on hover', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.elementByCss('#scroll-to-another').click()
await waitFor(2 * 1000)
const links = await browser.elementsByCss('link[rel=prefetch]')
let foundLink = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('another')) {
foundLink = true
break
}
}
expect(foundLink).toBe(true)
await browser.elementByCss('#link-another').moveTo()
await waitFor(2 * 1000)
const scripts = await browser.elementsByCss(
// Mouse hover is a high-priority fetch
'script:not([async])'
)
let scriptFound = false
for (const aScript of scripts) {
const href = await aScript.getAttribute('src')
if (href.includes('another')) {
scriptFound = true
break
}
}
expect(scriptFound).toBe(true)
} finally {
if (browser) await browser.close()
}
})
it('should inject a <script> tag when onMouseEnter (even with invalid ref)', async () => {
let browser
try {
browser = await webdriver(appPort, '/invalid-ref')
await browser.elementByCss('#btn-link').moveTo()
await waitFor(2 * 1000)
const scripts = await browser.elementsByCss(
// Mouse hover is a high-priority fetch
'script:not([async])'
)
let scriptFound = false
for (const aScript of scripts) {
const href = await aScript.getAttribute('src')
if (href.includes('another')) {
scriptFound = true
break
}
}
expect(scriptFound).toBe(true)
} finally {
if (browser) await browser.close()
}
})
it('should not have unhandledRejection when failing to prefetch on link', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval(`(function() {
window.addEventListener('unhandledrejection', function (err) {
window.hadUnhandledReject = true;
})
window.next.router.push('/invalid-prefetch');
})()`)
expect(await browser.eval('window.hadUnhandledReject')).toBeFalsy()
await browser.elementByCss('#invalid-link').moveTo()
expect(await browser.eval('window.hadUnhandledReject')).toBeFalsy()
})
it('should not prefetch when prefetch is explicitly set to false', async () => {
const browser = await webdriver(appPort, '/opt-out')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('another')) {
found = true
break
}
}
expect(found).toBe(false)
})
it('should not duplicate prefetches', async () => {
const browser = await webdriver(appPort, '/multi-prefetch')
const links = await browser.elementsByCss('link[rel=prefetch]')
const hrefs = []
for (const link of links) {
const href = await link.getAttribute('href')
hrefs.push(href)
}
hrefs.sort()
// Ensure no duplicates
expect(hrefs).toEqual([...new Set(hrefs)])
// Verify encoding
expect(hrefs.some(e => e.includes(`%5Bhello%5D.js`))).toBe(true)
})
it('should not add an another observer for a prefetched page', async () => {
// info: both `/` and `/de-duped` ref the `/first` page, which we don't
// want to be re-fetched/re-observed.
const browser = await webdriver(appPort, '/')
await browser.eval(`(function() {
window.calledPrefetch = false
window.next.router.prefetch = function() {
window.calledPrefetch = true
}
window.next.router.push('/de-duped')
})()`)
await waitFor(2 * 1000)
const calledPrefetch = await browser.eval(`window.calledPrefetch`)
expect(calledPrefetch).toBe(false)
})
})