/
index.test.js
114 lines (98 loc) · 2.92 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
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextStart,
nextBuild,
waitFor,
} from 'next-test-utils'
let app
let appPort
const appDir = join(__dirname, '..')
const noError = async (pathname) => {
const browser = await webdriver(appPort, '/')
await browser.eval(`(function() {
window.caughtErrors = []
const origError = window.console.error
window.console.error = function () {
window.caughtErrors.push(1)
origError(arguments)
}
window.next.router.replace('${pathname}')
})()`)
await waitFor(1000)
const numErrors = await browser.eval(`window.caughtErrors.length`)
expect(numErrors).toBe(0)
await browser.close()
}
const didPrefetch = async (pathname) => {
const browser = await webdriver(appPort, pathname)
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('index')) {
found = true
break
}
}
expect(found).toBe(true)
await browser.close()
}
function runCommonTests() {
// See https://github.com/vercel/next.js/issues/18437
it('should not have a race condition with a click handler', async () => {
const browser = await webdriver(appPort, '/click-away-race-condition')
await browser.elementByCss('#click-me').click()
await browser.waitForElementByCss('#the-menu')
})
}
describe('Invalid hrefs', () => {
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode',
() => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runCommonTests()
it('should not show error for function component with forwardRef', async () => {
await noError('/function')
})
it('should not show error for class component as child of next/link', async () => {
await noError('/class')
})
it('should handle child ref with React.createRef', async () => {
await noError('/child-ref')
})
it('should handle child ref that is a function', async () => {
await noError('/child-ref-func')
})
}
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runCommonTests()
it('should preload with forwardRef', async () => {
await didPrefetch('/function')
})
it('should preload with child ref with React.createRef', async () => {
await didPrefetch('/child-ref')
})
it('should preload with child ref with function', async () => {
await didPrefetch('/child-ref-func')
})
}
)
})