forked from vercel/next.js
/
index.test.js
163 lines (125 loc) · 5.23 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
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import { killApp, findPort, launchApp, File, check } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..')
let appPort
let app
const installCheckVisible = (browser) => {
return browser.eval(`(function() {
window.checkInterval = setInterval(function() {
let watcherDiv = document.querySelector('#__next-build-watcher')
watcherDiv = watcherDiv.shadowRoot || watcherDiv
window.showedBuilder = window.showedBuilder || (
watcherDiv.querySelector('div').className.indexOf('visible') > -1
)
if (window.showedBuilder) clearInterval(window.checkInterval)
}, 50)
})()`)
}
describe('GS(S)P Server-Side Change Reloading', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
it('should not reload page when client-side is changed too GSP', async () => {
const browser = await webdriver(appPort, '/gsp-blog/first')
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
const page = new File(join(appDir, 'pages/gsp-blog/[post].js'))
page.replace('change me', 'changed')
await check(() => browser.elementByCss('#change').text(), 'changed')
expect(await browser.eval(() => window.beforeChange)).toBe('hi')
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual(props2)
page.restore()
await check(() => browser.elementByCss('#change').text(), 'change me')
})
it('should update page when getStaticProps is changed only', async () => {
const browser = await webdriver(appPort, '/gsp-blog/first')
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.count).toBe(1)
const page = new File(join(appDir, 'pages/gsp-blog/[post].js'))
page.replace('count = 1', 'count = 2')
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'2'
)
expect(await browser.eval(() => window.beforeChange)).toBe('hi')
page.restore()
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'1'
)
})
it('should show indicator when re-fetching data', async () => {
const browser = await webdriver(appPort, '/gsp-blog/second')
await installCheckVisible(browser)
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.count).toBe(1)
const page = new File(join(appDir, 'pages/gsp-blog/[post].js'))
page.replace('count = 1', 'count = 2')
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'2'
)
expect(await browser.eval(() => window.beforeChange)).toBe('hi')
expect(await browser.eval(() => window.showedBuilder)).toBe(true)
page.restore()
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'1'
)
})
it('should update page when getStaticPaths is changed only', async () => {
const browser = await webdriver(appPort, '/gsp-blog/first')
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.count).toBe(1)
const page = new File(join(appDir, 'pages/gsp-blog/[post].js'))
page.replace('paths = 1', 'paths = 2')
expect(await browser.eval('window.beforeChange')).toBe('hi')
page.restore()
})
it('should not reload page when client-side is changed too GSSP', async () => {
const browser = await webdriver(appPort, '/gssp-blog/first')
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
const page = new File(join(appDir, 'pages/gssp-blog/[post].js'))
page.replace('change me', 'changed')
await check(() => browser.elementByCss('#change').text(), 'changed')
expect(await browser.eval(() => window.beforeChange)).toBe('hi')
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual(props2)
page.restore()
await check(() => browser.elementByCss('#change').text(), 'change me')
})
it('should update page when getServerSideProps is changed only', async () => {
const browser = await webdriver(appPort, '/gssp-blog/first')
await browser.eval(() => (window.beforeChange = 'hi'))
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.count).toBe(1)
const page = new File(join(appDir, 'pages/gssp-blog/[post].js'))
page.replace('count = 1', 'count = 2')
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'2'
)
expect(await browser.eval(() => window.beforeChange)).toBe('hi')
page.restore()
await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'1'
)
})
})