File size: 5,461 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
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'

describe('Document and App - Rendering via HTTP', () => {
  const { next, isNextDev } = nextTestSetup({
    files: __dirname,
  })

  describe('_document', () => {
    it('should include required elements in rendered html', async () => {
      const $ = await next.render$('/')
      // It has a custom html class
      expect($('html').hasClass('test-html-props')).toBe(true)
      // It has a custom body class
      expect($('body').hasClass('custom_class')).toBe(true)
      // It injects custom head tags
      expect($('head').text()).toMatch('body { margin: 0 }')
      // It has __NEXT_DATA__ script tag
      expect($('script#__NEXT_DATA__')).toBeTruthy()
      // It passes props from Document.getInitialProps to Document
      expect($('#custom-property').text()).toBe('Hello Document')
    })

    it('Document.getInitialProps returns html prop representing app shell', async () => {
      // Extract css-in-js-class from the rendered HTML, which is returned by Document.getInitialProps
      const $index = await next.render$('/')
      const $about = await next.render$('/about')
      expect($index('#css-in-cjs-count').text()).toBe('2')
      expect($about('#css-in-cjs-count').text()).toBe('0')
    })

    it('adds nonces to all scripts and preload links', async () => {
      const $ = await next.render$('/')
      const nonce = 'test-nonce'
      let noncesAdded = true
      $('script, link[rel=preload]').each((index, element) => {
        if ($(element).attr('nonce') !== nonce) noncesAdded = false
      })
      expect(noncesAdded).toBe(true)
    })

    it('adds crossOrigin to all scripts and preload links', async () => {
      const $ = await next.render$('/')
      const crossOrigin = 'anonymous'
      $('script, link[rel=preload]').each((index, element) => {
        expect($(element).attr('crossorigin') === crossOrigin).toBeTruthy()
      })
    })

    it('renders ctx.renderPage with enhancer correctly', async () => {
      const $ = await next.render$('/?withEnhancer=true')
      const nonce = 'RENDERED'
      expect($('#render-page-enhance-component').text().includes(nonce)).toBe(
        true
      )
    })

    it('renders ctx.renderPage with enhanceComponent correctly', async () => {
      const $ = await next.render$('/?withEnhanceComponent=true')
      const nonce = 'RENDERED'
      expect($('#render-page-enhance-component').text().includes(nonce)).toBe(
        true
      )
    })

    it('renders ctx.renderPage with enhanceApp correctly', async () => {
      const $ = await next.render$('/?withEnhanceApp=true')
      const nonce = 'RENDERED'
      expect($('#render-page-enhance-app').text().includes(nonce)).toBe(true)
    })

    it('renders ctx.renderPage with enhanceApp and enhanceComponent correctly', async () => {
      const $ = await next.render$(
        '/?withEnhanceComponent=true&withEnhanceApp=true'
      )
      const nonce = 'RENDERED'
      expect($('#render-page-enhance-app').text().includes(nonce)).toBe(true)
      expect($('#render-page-enhance-component').text().includes(nonce)).toBe(
        true
      )
    })

    if (isNextDev) {
      // This is a workaround to fix https://github.com/vercel/next.js/issues/5860
      // TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
      it('adds a timestamp to link tags with preload attribute to invalidate the cache in dev', async () => {
        const $ = await next.render$('/', undefined, {
          headers: { 'user-agent': 'Safari' },
        })
        $('link[rel=preload]').each((index, element) => {
          const href = $(element).attr('href')
          expect(href.match(/\?/g)).toHaveLength(1)
          expect(href).toMatch(/\?ts=/)
        })
        $('script[src]').each((index, element) => {
          const src = $(element).attr('src')
          expect(src.match(/\?/g)).toHaveLength(1)
          expect(src).toMatch(/\?ts=/)
        })
      })
    }
  })

  describe('_app', () => {
    it('shows a custom tag', async () => {
      const $ = await next.render$('/')
      expect($('#hello-app').text()).toBe('Hello App')
    })

    // For example react context uses shared module state
    // Also known as singleton modules
    it('should share module state with pages', async () => {
      const $ = await next.render$('/shared')
      expect($('#currentstate').text()).toBe('UPDATED')
    })

    if (isNextDev) {
      it('should show valid error when thrown in _app getInitialProps', async () => {
        const errMsg = 'have an error from _app getInitialProps'
        const origContent = await next.readFile('pages/_app.js')

        expect(await next.render('/')).toContain('page-index')

        await next.patchFile(
          'pages/_app.js',
          origContent.replace(
            '// throw _app GIP err here',
            `throw new Error("${errMsg}")`
          )
        )

        let foundErr = false
        try {
          await retry(async () =>
            expect(await next.render('/')).toContain(errMsg)
          )
          foundErr = true
        } finally {
          await next.patchFile('pages/_app.js', origContent)

          // Make sure _app is restored
          await retry(async () =>
            expect(await next.render('/')).toContain('page-index')
          )
          expect(foundErr).toBeTruthy()
        }
      })
    }
  })
})