forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmr.js
More file actions
172 lines (132 loc) · 5.32 KB
/
hmr.js
File metadata and controls
172 lines (132 loc) · 5.32 KB
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
/* global describe, it, expect */
import webdriver from 'next-webdriver'
import { readFileSync, writeFileSync, renameSync } from 'fs'
import { join } from 'path'
import { waitFor } from 'next-test-utils'
async function check (contentFn, regex) {
while (true) {
try {
const newContent = await contentFn()
if (regex.test(newContent)) break
await waitFor(1000)
} catch (ex) {}
}
}
export default (context, render) => {
describe('Hot Module Reloading', () => {
describe('syntax error', () => {
it('should detect the error and recover', async () => {
const browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser
.elementByCss('p').text()
expect(text).toBe('This is the about page.')
const aboutPagePath = join(__dirname, '../', 'pages', 'hmr', 'about.js')
const originalContent = readFileSync(aboutPagePath, 'utf8')
const erroredContent = originalContent.replace('</div>', 'div')
// change the content
writeFileSync(aboutPagePath, erroredContent, 'utf8')
await check(
() => browser.elementByCss('body').text(),
/Unterminated JSX contents/
)
// add the original content
writeFileSync(aboutPagePath, originalContent, 'utf8')
await check(
() => browser.elementByCss('body').text(),
/This is the about page/
)
browser.close()
})
it('should show the error on all pages', async () => {
const aboutPagePath = join(__dirname, '../', 'pages', 'hmr', 'about.js')
const originalContent = readFileSync(aboutPagePath, 'utf8')
const erroredContent = originalContent.replace('</div>', 'div')
// change the content
writeFileSync(aboutPagePath, erroredContent, 'utf8')
const browser = await webdriver(context.appPort, '/hmr/contact')
await check(
() => browser.elementByCss('body').text(),
/Unterminated JSX contents/
)
// add the original content
writeFileSync(aboutPagePath, originalContent, 'utf8')
await check(
() => browser.elementByCss('body').text(),
/This is the contact page/
)
browser.close()
})
})
describe('delete a page and add it back', () => {
it('should load the page properly', async () => {
const browser = await webdriver(context.appPort, '/hmr/contact')
const text = await browser
.elementByCss('p').text()
expect(text).toBe('This is the contact page.')
const contactPagePath = join(__dirname, '../', 'pages', 'hmr', 'contact.js')
const newContactPagePath = join(__dirname, '../', 'pages', 'hmr', '_contact.js')
// Rename the file to mimic a deleted page
renameSync(contactPagePath, newContactPagePath)
// wait until the 404 page comes
await check(
() => browser.elementByCss('body').text(),
/This page could not be found/
)
// Rename the file back to the original filename
renameSync(newContactPagePath, contactPagePath)
// wait until the page comes back
await check(
() => browser.elementByCss('body').text(),
/This is the contact page/
)
browser.close()
})
})
describe('editing a page', () => {
it('should detect the changes and display it', async () => {
const browser = await webdriver(context.appPort, '/hmr/about')
const text = await browser
.elementByCss('p').text()
expect(text).toBe('This is the about page.')
const aboutPagePath = join(__dirname, '../', 'pages', 'hmr', 'about.js')
const originalContent = readFileSync(aboutPagePath, 'utf8')
const editedContent = originalContent.replace('This is the about page', 'COOL page')
// change the content
writeFileSync(aboutPagePath, editedContent, 'utf8')
await check(
() => browser.elementByCss('body').text(),
/COOL page/
)
// add the original content
writeFileSync(aboutPagePath, originalContent, 'utf8')
await check(
() => browser.elementByCss('body').text(),
/This is the about page/
)
browser.close()
})
it('should not reload unrelated pages', async () => {
const browser = await webdriver(context.appPort, '/hmr/counter')
const text = await browser
.elementByCss('button').click()
.elementByCss('button').click()
.elementByCss('p').text()
expect(text).toBe('COUNT: 2')
const aboutPagePath = join(__dirname, '../', 'pages', 'hmr', 'about.js')
const originalContent = readFileSync(aboutPagePath, 'utf8')
const editedContent = originalContent.replace('This is the about page', 'COOL page')
// Change the about.js page
writeFileSync(aboutPagePath, editedContent, 'utf8')
// wait for 5 seconds
await waitFor(5000)
// Check whether the this page has reloaded or not.
const newText = await browser
.elementByCss('p').text()
expect(newText).toBe('COUNT: 2')
// restore the about page content.
writeFileSync(aboutPagePath, originalContent, 'utf8')
browser.close()
})
})
})
}