Commit f11346c8 authored by Janka Uryga's avatar Janka Uryga

test: convert draftmode.test.ts to nextTestSetup

parent 1f299870
import { createNextDescribe } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils' import { waitFor } from 'next-test-utils'
createNextDescribe( describe('app dir - draft mode', () => {
'app dir - draft mode', const { next, isNextDev } = nextTestSetup({
{
files: __dirname, files: __dirname,
}, })
({ next, isNextDev }) => { async function runTests({ basePath = '/' }: { basePath: string }) {
async function runTests({ basePath = '/' }: { basePath: string }) { let origRandHome = 'unintialized'
let origRandHome = 'unintialized' let origRandWithCookies = 'unintialized'
let origRandWithCookies = 'unintialized' let Cookie = ''
let Cookie = ''
it(`should use initial rand when draft mode is disabled on ${basePath}index`, async () => {
it(`should use initial rand when draft mode is disabled on ${basePath}index`, async () => { const $ = await next.render$(basePath)
const $ = await next.render$(basePath) expect($('#mode').text()).toBe('DISABLED')
expect($('#mode').text()).toBe('DISABLED') expect($('#rand').text()).toBeDefined()
expect($('#rand').text()).toBeDefined() origRandHome = $('#rand').text()
origRandHome = $('#rand').text() })
})
it(`should use initial rand when draft mode is disabled on ${basePath}with-cookies`, async () => {
const $ = await next.render$(`${basePath}with-cookies`)
expect($('#mode').text()).toBe('DISABLED')
expect($('#rand').text()).toBeDefined()
expect($('#data').text()).toBe('')
origRandWithCookies = $('#rand').text()
})
if (!isNextDev) { it(`should use initial rand when draft mode is disabled on ${basePath}with-cookies`, async () => {
if (basePath === '/') { const $ = await next.render$(`${basePath}with-cookies`)
it('should not generate rand when draft mode disabled during next start', async () => { expect($('#mode').text()).toBe('DISABLED')
const $ = await next.render$(basePath) expect($('#rand').text()).toBeDefined()
expect($('#mode').text()).toBe('DISABLED') expect($('#data').text()).toBe('')
expect($('#rand').text()).toBe(origRandHome) origRandWithCookies = $('#rand').text()
}) })
}
it('should not read other cookies when draft mode disabled during next start', async () => { if (!isNextDev) {
const opts = { headers: { Cookie: `data=cool` } } if (basePath === '/') {
const $ = await next.render$(`${basePath}with-cookies`, {}, opts) it('should not generate rand when draft mode disabled during next start', async () => {
const $ = await next.render$(basePath)
expect($('#mode').text()).toBe('DISABLED') expect($('#mode').text()).toBe('DISABLED')
expect($('#data').text()).toBe('') expect($('#rand').text()).toBe(origRandHome)
}) })
} }
it('should be disabled from api route handler', async () => { it('should not read other cookies when draft mode disabled during next start', async () => {
const res = await next.fetch(`${basePath}state`) const opts = { headers: { Cookie: `data=cool` } }
expect(await res.text()).toBe('DISABLED') const $ = await next.render$(`${basePath}with-cookies`, {}, opts)
}) expect($('#mode').text()).toBe('DISABLED')
expect($('#data').text()).toBe('')
it('should have set-cookie header on enable', async () => {
const res = await next.fetch(`${basePath}enable`)
const h = res.headers.get('set-cookie') || ''
Cookie = h.split(';').find((c) => c.startsWith('__prerender_bypass'))
expect(Cookie).toBeDefined()
})
it('should have set-cookie header with redirect location', async () => {
const res = await next.fetch(`${basePath}enable-and-redirect`, {
redirect: 'manual',
})
expect(res.status).toBe(307)
expect(res.headers.get('location')).toContain('/some-other-page')
const h = res.headers.get('set-cookie') || ''
const c = h.split(';').find((c) => c.startsWith('__prerender_bypass'))
expect(c).toBeDefined()
}) })
}
it('should genenerate rand when draft mode enabled', async () => { it('should be disabled from api route handler', async () => {
const opts = { headers: { Cookie } } const res = await next.fetch(`${basePath}state`)
const $ = await next.render$(basePath, {}, opts) expect(await res.text()).toBe('DISABLED')
expect($('#mode').text()).toBe('ENABLED') })
expect($('#rand').text()).not.toBe(origRandHome)
})
it('should read other cookies when draft mode enabled', async () => { it('should have set-cookie header on enable', async () => {
const opts = { headers: { Cookie: `${Cookie};data=cool` } } const res = await next.fetch(`${basePath}enable`)
const $ = await next.render$(`${basePath}with-cookies`, {}, opts) const h = res.headers.get('set-cookie') || ''
expect($('#mode').text()).toBe('ENABLED') Cookie = h.split(';').find((c) => c.startsWith('__prerender_bypass'))
expect($('#rand').text()).not.toBe(origRandWithCookies) expect(Cookie).toBeDefined()
expect($('#data').text()).toBe('cool') })
})
it('should be enabled from api route handler when draft mode enabled', async () => { it('should have set-cookie header with redirect location', async () => {
const opts = { headers: { Cookie } } const res = await next.fetch(`${basePath}enable-and-redirect`, {
const res = await next.fetch(`${basePath}state`, opts) redirect: 'manual',
expect(await res.text()).toBe('ENABLED')
}) })
expect(res.status).toBe(307)
expect(res.headers.get('location')).toContain('/some-other-page')
const h = res.headers.get('set-cookie') || ''
const c = h.split(';').find((c) => c.startsWith('__prerender_bypass'))
expect(c).toBeDefined()
})
it('should not perform full page navigation on router.refresh()', async () => { it('should genenerate rand when draft mode enabled', async () => {
const to = encodeURIComponent('/generate/foo') const opts = { headers: { Cookie } }
const browser = await next.browser( const $ = await next.render$(basePath, {}, opts)
`${basePath}enable-and-redirect?to=${to}` expect($('#mode').text()).toBe('ENABLED')
) expect($('#rand').text()).not.toBe(origRandHome)
await browser.eval('window._test = 42') })
await browser.elementById('refresh').click()
const start = Date.now()
while (Date.now() - start < 5000) {
const value = await browser.eval('window._test')
if (value !== 42) {
throw new Error('Detected a full page navigation')
}
await waitFor(200)
}
expect(await browser.eval('window._test')).toBe(42) it('should read other cookies when draft mode enabled', async () => {
}) const opts = { headers: { Cookie: `${Cookie};data=cool` } }
} const $ = await next.render$(`${basePath}with-cookies`, {}, opts)
expect($('#mode').text()).toBe('ENABLED')
expect($('#rand').text()).not.toBe(origRandWithCookies)
expect($('#data').text()).toBe('cool')
})
describe('in nodejs runtime', () => { it('should be enabled from api route handler when draft mode enabled', async () => {
runTests({ basePath: '/' }) const opts = { headers: { Cookie } }
const res = await next.fetch(`${basePath}state`, opts)
expect(await res.text()).toBe('ENABLED')
}) })
describe('in edge runtime', () => { it('should not perform full page navigation on router.refresh()', async () => {
runTests({ basePath: '/with-edge/' }) const to = encodeURIComponent('/generate/foo')
const browser = await next.browser(
`${basePath}enable-and-redirect?to=${to}`
)
await browser.eval('window._test = 42')
await browser.elementById('refresh').click()
const start = Date.now()
while (Date.now() - start < 5000) {
const value = await browser.eval('window._test')
if (value !== 42) {
throw new Error('Detected a full page navigation')
}
await waitFor(200)
}
expect(await browser.eval('window._test')).toBe(42)
}) })
} }
)
describe('in nodejs runtime', () => {
runTests({ basePath: '/' })
})
describe('in edge runtime', () => {
runTests({ basePath: '/with-edge/' })
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment