1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/test/integration/app-document/test/rendering.js
Adam Lane 4ce095df89 Add crossOrigin via props to _document Head and NextScript (#5646)
This alternative implementation of https://github.com/zeit/next.js/pull/5150 follows @timneutkens suggestion of using props.

Fixes #5150 
Fixes #3630
2018-11-13 21:36:09 +01:00

87 lines
3.1 KiB
JavaScript

/* eslint-env jest */
import cheerio from 'cheerio'
export default function ({ app }, suiteName, render, fetch) {
async function get$ (path, query) {
const html = await render(path, query)
return cheerio.load(html)
}
describe(suiteName, () => {
describe('_document', () => {
test('It has a custom body class', async () => {
const $ = await get$('/')
expect($('body').hasClass('custom_class'))
})
test('It injects custom head tags', async () => {
const $ = await get$('/')
expect($('head').text().includes('body { margin: 0 }'))
})
test('It passes props from Document.getInitialProps to Document', async () => {
const $ = await get$('/')
expect($('#custom-property').text() === 'Hello Document')
})
test('It adds nonces to all scripts and preload links', async () => {
const $ = await get$('/')
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)
})
test('It adds crossOrigin to all scripts and preload links', async () => {
const $ = await get$('/')
const crossOrigin = 'anonymous'
$('script, link[rel=preload]').each((index, element) => {
expect($(element).attr('crossorigin') === crossOrigin).toBeTruthy()
})
})
test('It renders ctx.renderPage with enhancer correctly', async () => {
const $ = await get$('/?withEnhancer=true')
const nonce = 'RENDERED'
expect($('#render-page-enhance-component').text().includes(nonce)).toBe(true)
})
test('It renders ctx.renderPage with enhanceComponent correctly', async () => {
const $ = await get$('/?withEnhanceComponent=true')
const nonce = 'RENDERED'
expect($('#render-page-enhance-component').text().includes(nonce)).toBe(true)
})
test('It renders ctx.renderPage with enhanceApp correctly', async () => {
const $ = await get$('/?withEnhanceApp=true')
const nonce = 'RENDERED'
expect($('#render-page-enhance-app').text().includes(nonce)).toBe(true)
})
test('It renders ctx.renderPage with enhanceApp and enhanceComponent correctly', async () => {
const $ = await get$('/?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)
})
})
describe('_app', () => {
test('It shows a custom tag', async () => {
const $ = await get$('/')
expect($('hello-app').text() === 'Hello App')
})
// For example react context uses shared module state
// Also known as singleton modules
test('It should share module state with pages', async () => {
const $ = await get$('/shared')
expect($('#currentstate').text() === 'UPDATED')
})
})
})
}