File size: 9,772 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { join } from 'path'
import { getBrowserBodyText, retry, waitFor } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'

export function runHotModuleReloadHmrTest(nextConfig: {
  basePath: string
  assetPrefix: string
}) {
  const { next } = nextTestSetup({
    files: __dirname,
    nextConfig,
    patchFileDelay: 500,
  })
  const { basePath } = nextConfig

  describe('delete a page and add it back', () => {
    it('should load the page properly', async () => {
      const contactPagePath = join('pages', 'hmr', 'contact.js')
      const newContactPagePath = join('pages', 'hmr', '_contact.js')
      const browser = await next.browser(basePath + '/hmr/contact')
      try {
        const text = await browser.elementByCss('p').text()
        expect(text).toBe('This is the contact page.')

        // Rename the file to mimic a deleted page
        await next.renameFile(contactPagePath, newContactPagePath)

        await retry(async () => {
          expect(await getBrowserBodyText(browser)).toMatch(
            /This page could not be found/
          )
        })

        // Rename the file back to the original filename
        await next.renameFile(newContactPagePath, contactPagePath)

        // wait until the page comes back
        await retry(async () => {
          expect(await getBrowserBodyText(browser)).toMatch(
            /This is the contact page/
          )
        })

        expect(next.cliOutput).toContain('Compiled /_error')
      } finally {
        await next
          .renameFile(newContactPagePath, contactPagePath)
          .catch(() => {})
      }
    })
  })

  describe('editing a page', () => {
    it('should detect the changes and display it', async () => {
      const browser = await next.browser(basePath + '/hmr/about')
      const text = await browser.elementByCss('p').text()
      expect(text).toBe('This is the about page.')

      const aboutPagePath = join('pages', 'hmr', 'about.js')

      const originalContent = await next.readFile(aboutPagePath)
      const editedContent = originalContent.replace(
        'This is the about page',
        'COOL page'
      )

      // change the content
      try {
        await next.patchFile(aboutPagePath, editedContent)
        await retry(async () => {
          expect(await getBrowserBodyText(browser)).toMatch(/COOL page/)
        })
      } finally {
        // add the original content
        await next.patchFile(aboutPagePath, originalContent)
      }

      await retry(async () => {
        expect(await getBrowserBodyText(browser)).toMatch(
          /This is the about page/
        )
      })
    })

    it('should not reload unrelated pages', async () => {
      const browser = await next.browser(basePath + '/hmr/counter')
      const text = await browser
        .elementByCss('button')
        .click()
        .elementByCss('button')
        .click()
        .elementByCss('p')
        .text()
      expect(text).toBe('COUNT: 2')

      const aboutPagePath = join('pages', 'hmr', 'about.js')

      const originalContent = await next.readFile(aboutPagePath)
      const editedContent = originalContent.replace(
        'This is the about page',
        'COOL page'
      )

      try {
        // Change the about.js page
        await next.patchFile(aboutPagePath, editedContent)

        // Check whether the this page has reloaded or not.
        await retry(async () => {
          expect(await browser.elementByCss('p').text()).toMatch(/COUNT: 2/)
        })
      } finally {
        // restore the about page content.
        await next.patchFile(aboutPagePath, originalContent)
      }
    })

    // Added because of a regression in react-hot-loader, see issues: #4246 #4273
    // Also: https://github.com/vercel/styled-jsx/issues/425
    it('should update styles correctly', async () => {
      const browser = await next.browser(basePath + '/hmr/style')
      const pTag = await browser.elementByCss('.hmr-style-page p')
      const initialFontSize = await pTag.getComputedCss('font-size')

      expect(initialFontSize).toBe('100px')

      const pagePath = join('pages', 'hmr', 'style.js')

      const originalContent = await next.readFile(pagePath)
      const editedContent = originalContent.replace('100px', '200px')

      // Change the page
      await next.patchFile(pagePath, editedContent)

      try {
        // Check whether the this page has reloaded or not.
        await retry(async () => {
          const editedPTag = await browser.elementByCss('.hmr-style-page p')
          expect(await editedPTag.getComputedCss('font-size')).toBe('200px')
        })
      } finally {
        // Finally is used so that we revert the content back to the original regardless of the test outcome
        // restore the about page content.
        await next.patchFile(pagePath, originalContent)
      }
    })

    // Added because of a regression in react-hot-loader, see issues: #4246 #4273
    // Also: https://github.com/vercel/styled-jsx/issues/425
    it('should update styles in a stateful component correctly', async () => {
      const browser = await next.browser(
        basePath + '/hmr/style-stateful-component'
      )
      const pagePath = join('pages', 'hmr', 'style-stateful-component.js')
      const originalContent = await next.readFile(pagePath)
      try {
        const pTag = await browser.elementByCss('.hmr-style-page p')
        const initialFontSize = await pTag.getComputedCss('font-size')

        expect(initialFontSize).toBe('100px')
        const editedContent = originalContent.replace('100px', '200px')

        // Change the page
        await next.patchFile(pagePath, editedContent)

        // Check whether the this page has reloaded or not.
        await retry(async () => {
          const editedPTag = await browser.elementByCss('.hmr-style-page p')
          expect(await editedPTag.getComputedCss('font-size')).toBe('200px')
        })
      } finally {
        await next.patchFile(pagePath, originalContent)
      }
    })

    // Added because of a regression in react-hot-loader, see issues: #4246 #4273
    // Also: https://github.com/vercel/styled-jsx/issues/425
    it('should update styles in a dynamic component correctly', async () => {
      const browser = await next.browser(
        basePath + '/hmr/style-dynamic-component'
      )
      const secondBrowser = await next.browser(
        basePath + '/hmr/style-dynamic-component'
      )
      const pagePath = join('components', 'hmr', 'dynamic.js')
      const originalContent = await next.readFile(pagePath)
      try {
        const div = await browser.elementByCss('#dynamic-component')
        const initialClientClassName = await div.getAttribute('class')
        const initialFontSize = await div.getComputedCss('font-size')

        expect(initialFontSize).toBe('100px')

        const initialHtml = await next.render(
          basePath + '/hmr/style-dynamic-component'
        )
        expect(initialHtml.includes('100px')).toBeTruthy()

        const $initialHtml = await next.render$(
          basePath + '/hmr/style-dynamic-component'
        )
        const initialServerClassName =
          $initialHtml('#dynamic-component').attr('class')

        expect(initialClientClassName === initialServerClassName).toBeTruthy()

        const editedContent = originalContent.replace('100px', '200px')

        // Change the page
        await next.patchFile(pagePath, editedContent)

        // wait for 5 seconds
        await waitFor(5000)

        // Check whether the this page has reloaded or not.
        const editedDiv = await secondBrowser.elementByCss('#dynamic-component')
        const editedClientClassName = await editedDiv.getAttribute('class')
        const editedFontSize = await editedDiv.getComputedCss('font-size')
        const browserHtml = await secondBrowser.eval(
          'document.documentElement.innerHTML'
        )

        expect(editedFontSize).toBe('200px')
        expect(browserHtml.includes('font-size:200px')).toBe(true)
        expect(browserHtml.includes('font-size:100px')).toBe(false)

        const editedHtml = await next.render(
          basePath + '/hmr/style-dynamic-component'
        )
        expect(editedHtml.includes('200px')).toBeTruthy()
        const $editedHtml = await next.render$(
          basePath + '/hmr/style-dynamic-component'
        )
        const editedServerClassName =
          $editedHtml('#dynamic-component').attr('class')

        expect(editedClientClassName === editedServerClassName).toBe(true)
      } finally {
        // Finally is used so that we revert the content back to the original regardless of the test outcome
        // restore the about page content.
        await next.patchFile(pagePath, originalContent)
      }
    })

    it('should not full reload when nonlatin characters are used', async () => {
      const browser = await next.browser(basePath + '/hmr/nonlatin')
      const pagePath = join('pages', 'hmr', 'nonlatin.js')
      const originalContent = await next.readFile(pagePath)
      try {
        const timeOrigin = await browser.eval('performance.timeOrigin')
        const editedContent = originalContent.replace(
          '<div>テスト</div>',
          '<div class="updated">テスト</div>'
        )

        // Change the page
        await next.patchFile(pagePath, editedContent)

        await browser.waitForElementByCss('.updated')

        expect(await browser.eval('performance.timeOrigin')).toEqual(timeOrigin)
      } finally {
        // Finally is used so that we revert the content back to the original regardless of the test outcome
        // restore the about page content.
        await next.patchFile(pagePath, originalContent)
      }
    })
  })
}