/
index.test.js
116 lines (90 loc) · 3.31 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
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
check,
waitFor,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let app
let appPort
const runTests = () => {
it('should not shallowly navigate back in history when current page was not shallow', async () => {
const browser = await webdriver(appPort, '/first')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click()
await waitFor(1000)
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props)
await browser.elementByCss('#remove-query-shallow').click()
await waitFor(1000)
const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3).toEqual(props)
await browser.elementByCss('#to-another').click()
await waitFor(1000)
await check(() => browser.elementByCss('#props').text(), /another/)
const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'another' })
expect(props4.random).not.toBe(props.random)
await browser.back()
await waitFor(1000)
const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'first' })
expect(props5.random).not.toBe(props4.random)
})
it('should not shallowly navigate forwards in history when current page was not shallow', async () => {
const browser = await webdriver(appPort, '/first')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click()
await waitFor(1000)
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props)
await browser.elementByCss('#to-another').click()
await waitFor(1000)
const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3.params).toEqual({ slug: 'another' })
expect(props3.random).not.toBe(props2.random)
await browser.back()
await waitFor(1000)
const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'first' })
expect(props4.random).not.toBe(props3.random)
await browser.forward()
await waitFor(1000)
const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'another' })
expect(props5.random).not.toBe(props4.random)
})
}
describe('Client Shallow Routing', () => {
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode',
() => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
}
)
;(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))
runTests()
}
)
})