Skip to content

Commit aa08587

Browse files
committed
Merge branch 'add/server-only-tests' into update/ssr-changes-tracking
2 parents b8722d7 + c4e6ffc commit aa08587

File tree

6 files changed

+275
-197
lines changed

6 files changed

+275
-197
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
/* eslint-env jest */
2+
3+
import { join } from 'path'
4+
import webdriver from 'next-webdriver'
5+
import { createNext, FileRef } from 'e2e-utils'
6+
import { check, getRedboxHeader, hasRedbox } from 'next-test-utils'
7+
import { NextInstance } from 'test/lib/next-modes/base'
8+
9+
const installCheckVisible = (browser) => {
10+
return browser.eval(`(function() {
11+
window.checkInterval = setInterval(function() {
12+
let watcherDiv = document.querySelector('#__next-build-watcher')
13+
watcherDiv = watcherDiv.shadowRoot || watcherDiv
14+
window.showedBuilder = window.showedBuilder || (
15+
watcherDiv.querySelector('div').className.indexOf('visible') > -1
16+
)
17+
if (window.showedBuilder) clearInterval(window.checkInterval)
18+
}, 50)
19+
})()`)
20+
}
21+
22+
describe('GS(S)P Server-Side Change Reloading', () => {
23+
let next: NextInstance
24+
25+
beforeAll(async () => {
26+
next = await createNext({
27+
files: {
28+
pages: new FileRef(join(__dirname, '../pages')),
29+
},
30+
})
31+
})
32+
afterAll(() => next.destroy())
33+
34+
it('should not reload page when client-side is changed too GSP', async () => {
35+
const browser = await webdriver(next.url, '/gsp-blog/first')
36+
await check(() => browser.elementByCss('#change').text(), 'change me')
37+
await browser.eval(`window.beforeChange = 'hi'`)
38+
39+
const props = JSON.parse(await browser.elementByCss('#props').text())
40+
41+
const page = 'pages/gsp-blog/[post].js'
42+
const originalContent = await next.readFile(page)
43+
await next.patchFile(page, originalContent.replace('change me', 'changed'))
44+
45+
await check(() => browser.elementByCss('#change').text(), 'changed')
46+
expect(await browser.eval(`window.beforeChange`)).toBe('hi')
47+
48+
const props2 = JSON.parse(await browser.elementByCss('#props').text())
49+
expect(props).toEqual(props2)
50+
51+
await next.patchFile(page, originalContent)
52+
await check(() => browser.elementByCss('#change').text(), 'change me')
53+
})
54+
55+
it('should update page when getStaticProps is changed only', async () => {
56+
const browser = await webdriver(next.url, '/gsp-blog/first')
57+
await browser.eval(`window.beforeChange = 'hi'`)
58+
59+
const props = JSON.parse(await browser.elementByCss('#props').text())
60+
expect(props.count).toBe(1)
61+
62+
const page = 'pages/gsp-blog/[post].js'
63+
const originalContent = await next.readFile(page)
64+
await next.patchFile(
65+
page,
66+
originalContent.replace('count = 1', 'count = 2')
67+
)
68+
69+
await check(
70+
async () =>
71+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
72+
'2'
73+
)
74+
expect(await browser.eval(`window.beforeChange`)).toBe('hi')
75+
await next.patchFile(page, originalContent)
76+
77+
await check(
78+
async () =>
79+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
80+
'1'
81+
)
82+
})
83+
84+
it('should show indicator when re-fetching data', async () => {
85+
const browser = await webdriver(next.url, '/gsp-blog/second')
86+
await installCheckVisible(browser)
87+
await browser.eval(`window.beforeChange = 'hi'`)
88+
89+
const props = JSON.parse(await browser.elementByCss('#props').text())
90+
expect(props.count).toBe(1)
91+
92+
const page = 'pages/gsp-blog/[post].js'
93+
const originalContent = await next.readFile(page)
94+
await next.patchFile(
95+
page,
96+
originalContent.replace('count = 1', 'count = 2')
97+
)
98+
99+
await check(
100+
async () =>
101+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
102+
'2'
103+
)
104+
expect(await browser.eval(`window.beforeChange`)).toBe('hi')
105+
expect(await browser.eval(`window.showedBuilder`)).toBe(true)
106+
107+
await next.patchFile(page, originalContent)
108+
await check(
109+
async () =>
110+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
111+
'1'
112+
)
113+
})
114+
115+
it('should update page when getStaticPaths is changed only', async () => {
116+
const browser = await webdriver(next.url, '/gsp-blog/first')
117+
await browser.eval(`window.beforeChange = 'hi'`)
118+
119+
const props = JSON.parse(await browser.elementByCss('#props').text())
120+
expect(props.count).toBe(1)
121+
122+
const page = 'pages/gsp-blog/[post].js'
123+
const originalContent = await next.readFile(page)
124+
await next.patchFile(
125+
page,
126+
originalContent.replace('paths = 1', 'paths = 2')
127+
)
128+
129+
expect(await browser.eval('window.beforeChange')).toBe('hi')
130+
await next.patchFile(page, originalContent)
131+
})
132+
133+
it('should update page when getStaticProps is changed only for /index', async () => {
134+
const browser = await webdriver(next.url, '/')
135+
await browser.eval(`window.beforeChange = 'hi'`)
136+
137+
const props = JSON.parse(await browser.elementByCss('#props').text())
138+
expect(props.count).toBe(1)
139+
140+
const page = 'pages/index.js'
141+
const originalContent = await next.readFile(page)
142+
await next.patchFile(
143+
page,
144+
originalContent.replace('count = 1', 'count = 2')
145+
)
146+
147+
expect(await browser.eval('window.beforeChange')).toBe('hi')
148+
await next.patchFile(page, originalContent)
149+
})
150+
151+
it('should update page when getStaticProps is changed only for /another/index', async () => {
152+
const browser = await webdriver(next.url, '/another')
153+
await browser.eval(`window.beforeChange = 'hi'`)
154+
155+
const props = JSON.parse(await browser.elementByCss('#props').text())
156+
expect(props.count).toBe(1)
157+
158+
const page = 'pages/another/index.js'
159+
const originalContent = await next.readFile(page)
160+
await next.patchFile(
161+
page,
162+
originalContent.replace('count = 1', 'count = 2')
163+
)
164+
165+
expect(await browser.eval('window.beforeChange')).toBe('hi')
166+
await next.patchFile(page, originalContent)
167+
})
168+
169+
it('should not reload page when client-side is changed too GSSP', async () => {
170+
const browser = await webdriver(next.url, '/gssp-blog/first')
171+
await check(() => browser.elementByCss('#change').text(), 'change me')
172+
await browser.eval(`window.beforeChange = 'hi'`)
173+
174+
const props = JSON.parse(await browser.elementByCss('#props').text())
175+
176+
const page = 'pages/gssp-blog/[post].js'
177+
const originalContent = await next.readFile(page)
178+
await next.patchFile(page, originalContent.replace('change me', 'changed'))
179+
180+
await check(() => browser.elementByCss('#change').text(), 'changed')
181+
expect(await browser.eval(`window.beforeChange`)).toBe('hi')
182+
183+
const props2 = JSON.parse(await browser.elementByCss('#props').text())
184+
expect(props).toEqual(props2)
185+
186+
await next.patchFile(page, originalContent)
187+
await check(() => browser.elementByCss('#change').text(), 'change me')
188+
})
189+
190+
it('should update page when getServerSideProps is changed only', async () => {
191+
const browser = await webdriver(next.url, '/gssp-blog/first')
192+
await check(
193+
async () =>
194+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
195+
'1'
196+
)
197+
await browser.eval(`window.beforeChange = 'hi'`)
198+
199+
const props = JSON.parse(await browser.elementByCss('#props').text())
200+
expect(props.count).toBe(1)
201+
202+
const page = 'pages/gssp-blog/[post].js'
203+
const originalContent = await next.readFile(page)
204+
await next.patchFile(
205+
page,
206+
originalContent.replace('count = 1', 'count = 2')
207+
)
208+
209+
await check(
210+
async () =>
211+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
212+
'2'
213+
)
214+
expect(await browser.eval(`window.beforeChange`)).toBe('hi')
215+
await next.patchFile(page, originalContent)
216+
217+
await check(
218+
async () =>
219+
JSON.parse(await browser.elementByCss('#props').text()).count + '',
220+
'1'
221+
)
222+
})
223+
224+
it('should update on props error in getStaticProps', async () => {
225+
const browser = await webdriver(next.url, '/')
226+
await browser.eval(`window.beforeChange = 'hi'`)
227+
228+
const props = JSON.parse(await browser.elementByCss('#props').text())
229+
expect(props.count).toBe(1)
230+
231+
const page = 'pages/index.js'
232+
const originalContent = await next.readFile(page)
233+
234+
try {
235+
await next.patchFile(page, originalContent.replace('props:', 'propss:'))
236+
expect(await hasRedbox(browser, true)).toBe(true)
237+
expect(await getRedboxHeader(browser)).toContain(
238+
'Additional keys were returned from'
239+
)
240+
241+
await next.patchFile(page, originalContent)
242+
expect(await hasRedbox(browser, false)).toBe(false)
243+
} finally {
244+
await next.patchFile(page, originalContent)
245+
}
246+
})
247+
248+
it('should update on thrown error in getStaticProps', async () => {
249+
const browser = await webdriver(next.url, '/')
250+
await browser.eval(`window.beforeChange = 'hi'`)
251+
252+
const props = JSON.parse(await browser.elementByCss('#props').text())
253+
expect(props.count).toBe(1)
254+
255+
const page = 'pages/index.js'
256+
const originalContent = await next.readFile(page)
257+
258+
try {
259+
await next.patchFile(
260+
page,
261+
originalContent.replace(
262+
'const count',
263+
'throw new Error("custom oops"); const count'
264+
)
265+
)
266+
expect(await hasRedbox(browser, true)).toBe(true)
267+
expect(await getRedboxHeader(browser)).toContain('custom oops')
268+
269+
await next.patchFile(page, originalContent)
270+
expect(await hasRedbox(browser, false)).toBe(false)
271+
} finally {
272+
await next.patchFile(page, originalContent)
273+
}
274+
})
275+
})

0 commit comments

Comments
 (0)