Skip to content

Commit

Permalink
Add test case for loading 404 on invalid bundle (#14082)
Browse files Browse the repository at this point in the history
This adds a test case to ensure hard navigating when a client bundle fails to load is occurring correctly

x-ref: #13516
  • Loading branch information
ijjk authored Jun 11, 2020
1 parent c2c0e6d commit 1c057e7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
7 changes: 7 additions & 0 deletions test/integration/client-404/pages/invalid-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Link from 'next/link'

export default () => (
<Link href="/[id]/non-existent" as="/another/non-existent">
<a id="to-nonexistent">to 404</a>
</Link>
)
9 changes: 9 additions & 0 deletions test/integration/client-404/test/client-navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { check } from 'next-test-utils'

export default (context) => {
describe('Client Navigation 404', () => {
Expand All @@ -22,5 +23,13 @@ export default (context) => {
await browser.close()
})
})

it('should hard navigate to URL on failing to load bundle', async () => {
const browser = await webdriver(context.appPort, '/invalid-link')
await browser.eval(() => (window.beforeNav = 'hi'))
await browser.elementByCss('#to-nonexistent').click()
await check(() => browser.elementByCss('#errorStatusCode').text(), /404/)
expect(await browser.eval(() => window.beforeNav)).not.toBe('hi')
})
})
}
41 changes: 33 additions & 8 deletions test/integration/client-404/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
/* eslint-env jest */

import { join } from 'path'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'
import {
renderViaHTTP,
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
} from 'next-test-utils'

// test suite
import clientNavigation from './client-navigation'

const context = {}
const appDir = join(__dirname, '../')
jest.setTimeout(1000 * 60 * 5)

const runTests = () => {
clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q))
}

describe('Client 404', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.server = await launchApp(join(__dirname, '../'), context.appPort)
describe('dev mode', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.server = await launchApp(appDir, context.appPort)

// pre-build page at the start
await renderViaHTTP(context.appPort, '/')
})
afterAll(() => killApp(context.server))

// pre-build page at the start
await renderViaHTTP(context.appPort, '/')
runTests()
})
afterAll(() => killApp(context.server))

clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q))
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
context.appPort = await findPort()
context.server = await nextStart(appDir, context.appPort)
})
afterAll(() => killApp(context.server))

runTests()
})
})

0 comments on commit 1c057e7

Please sign in to comment.