|
|
import path from 'path' |
|
|
import fs from 'fs-extra' |
|
|
import webdriver from 'next-webdriver' |
|
|
import { createNext, FileRef, type NextInstance } from 'e2e-utils' |
|
|
import { check, retry } from 'next-test-utils' |
|
|
|
|
|
const pathnames = { |
|
|
'/404': ['/not/a/real/page?with=query', '/not/a/real/page'], |
|
|
|
|
|
|
|
|
|
|
|
'/_error': ['/error?with=query', '/error'], |
|
|
} |
|
|
|
|
|
const basePath = '/docs' |
|
|
|
|
|
const table = [ |
|
|
{ basePath: false, i18n: true, middleware: false }, |
|
|
{ basePath: true, i18n: false, middleware: false }, |
|
|
{ basePath: true, i18n: true, middleware: false }, |
|
|
{ basePath: false, i18n: false, middleware: false }, |
|
|
|
|
|
...((global as any).isNextDev |
|
|
? [] |
|
|
: [ |
|
|
|
|
|
{ basePath: false, i18n: false, middleware: true }, |
|
|
]), |
|
|
] |
|
|
|
|
|
const baseNextConfig = ` |
|
|
module.exports = { |
|
|
BASE_PATH |
|
|
I18N |
|
|
} |
|
|
` |
|
|
|
|
|
describe('404-page-router', () => { |
|
|
let next: NextInstance |
|
|
|
|
|
beforeAll(async () => { |
|
|
const files = { |
|
|
pages: new FileRef(path.join(__dirname, 'app/pages')), |
|
|
components: new FileRef(path.join(__dirname, 'app/components')), |
|
|
} |
|
|
next = await createNext({ files, skipStart: true, patchFileDelay: 500 }) |
|
|
}) |
|
|
afterAll(() => next.destroy()) |
|
|
|
|
|
describe.each(table)( |
|
|
'404-page-router with basePath of $basePath and i18n of $i18n and middleware $middleware', |
|
|
(options) => { |
|
|
const isDev = (global as any).isNextDev |
|
|
|
|
|
if ((global as any).isNextDeploy) { |
|
|
|
|
|
|
|
|
it('should skip for deploy', () => {}) |
|
|
return |
|
|
} |
|
|
|
|
|
beforeAll(async () => { |
|
|
|
|
|
if (options.middleware) { |
|
|
await next.patchFile( |
|
|
'middleware.js', |
|
|
await fs.readFile( |
|
|
path.join(__dirname, 'app', 'middleware.js'), |
|
|
'utf8' |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
let curNextConfig = baseNextConfig |
|
|
.replace('BASE_PATH', options.basePath ? "basePath: '/docs'," : '') |
|
|
.replace( |
|
|
'I18N', |
|
|
options.i18n |
|
|
? "i18n: { defaultLocale: 'en-ca', locales: ['en-ca', 'en-fr'] }," |
|
|
: '' |
|
|
) |
|
|
|
|
|
await next.patchFile('next.config.js', curNextConfig) |
|
|
await next.start() |
|
|
}) |
|
|
afterAll(async () => { |
|
|
await next.stop() |
|
|
await next.deleteFile('middleware.js') |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function translate( |
|
|
pathname: keyof typeof pathnames, |
|
|
shouldPrefixPathname: boolean = false |
|
|
): { url: string; pathname: keyof typeof pathnames; asPath: string }[] { |
|
|
return pathnames[pathname].map((asPath) => ({ |
|
|
|
|
|
url: shouldPrefixPathname ? basePath + asPath : asPath, |
|
|
|
|
|
pathname, |
|
|
|
|
|
asPath, |
|
|
})) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const urls = translate('/404') |
|
|
|
|
|
|
|
|
|
|
|
if (!isDev) { |
|
|
urls.push(...translate('/_error', options.basePath)) |
|
|
} |
|
|
|
|
|
describe.each(urls)('for $url', ({ url, pathname, asPath }) => { |
|
|
it('should have the correct router parameters after it is ready', async () => { |
|
|
const query = url.split('?', 2)[1] ?? '' |
|
|
const browser = await webdriver(next.url, url) |
|
|
|
|
|
try { |
|
|
await check( |
|
|
() => browser.eval('next.router.isReady ? "yes" : "no"'), |
|
|
'yes' |
|
|
) |
|
|
expect(await browser.elementById('pathname').text()).toEqual( |
|
|
pathname |
|
|
) |
|
|
expect(await browser.elementById('asPath').text()).toEqual(asPath) |
|
|
expect(await browser.elementById('query').text()).toEqual(query) |
|
|
} finally { |
|
|
await browser.close() |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should not throw any errors when re-fetching the route info', async () => { |
|
|
const browser = await webdriver(next.url, '/?test=1') |
|
|
await check( |
|
|
() => browser.eval('next.router.isReady ? "yes" : "no"'), |
|
|
'yes' |
|
|
) |
|
|
|
|
|
await retry(async () => { |
|
|
expect(await browser.elementById('query').text()).toEqual('test=1') |
|
|
}) |
|
|
}) |
|
|
} |
|
|
) |
|
|
}) |
|
|
|