|
| 1 | +import { retry } from 'next-test-utils' |
| 2 | +import { nextTestSetup } from 'e2e-utils' |
| 3 | + |
| 4 | +describe('app-document - Rendering via HTTP', () => { |
| 5 | + const { next, isNextDev } = nextTestSetup({ |
| 6 | + files: __dirname, |
| 7 | + }) |
| 8 | + |
| 9 | + describe('_document', () => { |
| 10 | + it('should include required elements in rendered html', async () => { |
| 11 | + const $ = await next.render$('/') |
| 12 | + // It has a custom html class |
| 13 | + expect($('html').hasClass('test-html-props')).toBe(true) |
| 14 | + // It has a custom body class |
| 15 | + expect($('body').hasClass('custom_class')).toBe(true) |
| 16 | + // It injects custom head tags |
| 17 | + expect($('head').text()).toMatch('body { margin: 0 }') |
| 18 | + // It has __NEXT_DATA__ script tag |
| 19 | + expect($('script#__NEXT_DATA__')).toBeTruthy() |
| 20 | + // It passes props from Document.getInitialProps to Document |
| 21 | + expect($('#custom-property').text()).toBe('Hello Document') |
| 22 | + }) |
| 23 | + |
| 24 | + it('Document.getInitialProps returns html prop representing app shell', async () => { |
| 25 | + // Extract css-in-js-class from the rendered HTML, which is returned by Document.getInitialProps |
| 26 | + const $index = await next.render$('/') |
| 27 | + const $about = await next.render$('/about') |
| 28 | + expect($index('#css-in-cjs-count').text()).toBe('2') |
| 29 | + expect($about('#css-in-cjs-count').text()).toBe('0') |
| 30 | + }) |
| 31 | + |
| 32 | + it('adds nonces to all scripts and preload links', async () => { |
| 33 | + const $ = await next.render$('/') |
| 34 | + const nonce = 'test-nonce' |
| 35 | + let noncesAdded = true |
| 36 | + $('script, link[rel=preload]').each((index, element) => { |
| 37 | + if ($(element).attr('nonce') !== nonce) noncesAdded = false |
| 38 | + }) |
| 39 | + expect(noncesAdded).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + it('adds crossOrigin to all scripts and preload links', async () => { |
| 43 | + const $ = await next.render$('/') |
| 44 | + const crossOrigin = 'anonymous' |
| 45 | + $('script, link[rel=preload]').each((index, element) => { |
| 46 | + expect($(element).attr('crossorigin') === crossOrigin).toBeTruthy() |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('renders ctx.renderPage with enhancer correctly', async () => { |
| 51 | + const $ = await next.render$('/?withEnhancer=true') |
| 52 | + const nonce = 'RENDERED' |
| 53 | + expect($('#render-page-enhance-component').text().includes(nonce)).toBe( |
| 54 | + true |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + it('renders ctx.renderPage with enhanceComponent correctly', async () => { |
| 59 | + const $ = await next.render$('/?withEnhanceComponent=true') |
| 60 | + const nonce = 'RENDERED' |
| 61 | + expect($('#render-page-enhance-component').text().includes(nonce)).toBe( |
| 62 | + true |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + it('renders ctx.renderPage with enhanceApp correctly', async () => { |
| 67 | + const $ = await next.render$('/?withEnhanceApp=true') |
| 68 | + const nonce = 'RENDERED' |
| 69 | + expect($('#render-page-enhance-app').text().includes(nonce)).toBe(true) |
| 70 | + }) |
| 71 | + |
| 72 | + it('renders ctx.renderPage with enhanceApp and enhanceComponent correctly', async () => { |
| 73 | + const $ = await next.render$( |
| 74 | + '/?withEnhanceComponent=true&withEnhanceApp=true' |
| 75 | + ) |
| 76 | + const nonce = 'RENDERED' |
| 77 | + expect($('#render-page-enhance-app').text().includes(nonce)).toBe(true) |
| 78 | + expect($('#render-page-enhance-component').text().includes(nonce)).toBe( |
| 79 | + true |
| 80 | + ) |
| 81 | + }) |
| 82 | + |
| 83 | + if (isNextDev) { |
| 84 | + // This is a workaround to fix https://github.com/vercel/next.js/issues/5860 |
| 85 | + // TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed. |
| 86 | + it('adds a timestamp to link tags with preload attribute to invalidate the cache in dev', async () => { |
| 87 | + const $ = await next.render$('/', undefined, { |
| 88 | + headers: { 'user-agent': 'Safari' }, |
| 89 | + }) |
| 90 | + $('link[rel=preload]').each((index, element) => { |
| 91 | + const href = $(element).attr('href') |
| 92 | + expect(href.match(/\?/g)).toHaveLength(1) |
| 93 | + expect(href).toMatch(/\?ts=/) |
| 94 | + }) |
| 95 | + $('script[src]').each((index, element) => { |
| 96 | + const src = $(element).attr('src') |
| 97 | + expect(src.match(/\?/g)).toHaveLength(1) |
| 98 | + expect(src).toMatch(/\?ts=/) |
| 99 | + }) |
| 100 | + }) |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + describe('_app', () => { |
| 105 | + it('shows a custom tag', async () => { |
| 106 | + const $ = await next.render$('/') |
| 107 | + expect($('#hello-app').text()).toBe('Hello App') |
| 108 | + }) |
| 109 | + |
| 110 | + // For example react context uses shared module state |
| 111 | + // Also known as singleton modules |
| 112 | + it('should share module state with pages', async () => { |
| 113 | + const $ = await next.render$('/shared') |
| 114 | + expect($('#currentstate').text()).toBe('UPDATED') |
| 115 | + }) |
| 116 | + |
| 117 | + if (isNextDev) { |
| 118 | + it('should show valid error when thrown in _app getInitialProps', async () => { |
| 119 | + const errMsg = 'have an error from _app getInitialProps' |
| 120 | + const origContent = await next.readFile('pages/_app.js') |
| 121 | + |
| 122 | + expect(await next.render('/')).toContain('page-index') |
| 123 | + |
| 124 | + await next.patchFile( |
| 125 | + 'pages/_app.js', |
| 126 | + origContent.replace( |
| 127 | + '// throw _app GIP err here', |
| 128 | + `throw new Error("${errMsg}")` |
| 129 | + ) |
| 130 | + ) |
| 131 | + |
| 132 | + let foundErr = false |
| 133 | + try { |
| 134 | + await retry(async () => |
| 135 | + expect(await next.render('/')).toContain(errMsg) |
| 136 | + ) |
| 137 | + foundErr = true |
| 138 | + } finally { |
| 139 | + await next.patchFile('pages/_app.js', origContent) |
| 140 | + |
| 141 | + // Make sure _app is restored |
| 142 | + await retry(async () => |
| 143 | + expect(await next.render('/')).toContain('page-index') |
| 144 | + ) |
| 145 | + expect(foundErr).toBeTruthy() |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | + }) |
| 150 | +}) |
0 commit comments