2018-10-20 17:00:01 +02:00
|
|
|
/* eslint-env jest */
|
2018-02-13 14:27:52 +01:00
|
|
|
|
2018-03-30 15:08:09 +02:00
|
|
|
import { join } from 'path'
|
2019-09-04 16:00:54 +02:00
|
|
|
import { SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH } from 'next/constants'
|
2019-05-30 03:19:32 +02:00
|
|
|
import {
|
|
|
|
requirePage,
|
|
|
|
getPagePath,
|
2019-11-11 04:24:53 +01:00
|
|
|
pageNotFoundError,
|
2019-09-04 16:00:54 +02:00
|
|
|
} from 'next/dist/next-server/server/require'
|
|
|
|
import { normalizePagePath } from 'next/dist/next-server/server/normalize-page-path'
|
2018-02-13 14:27:52 +01:00
|
|
|
|
2018-03-30 15:08:09 +02:00
|
|
|
const sep = '/'
|
2018-06-04 15:45:39 +02:00
|
|
|
const distDir = join(__dirname, '_resolvedata')
|
2019-05-30 03:19:32 +02:00
|
|
|
const pathToBundles = join(
|
|
|
|
distDir,
|
|
|
|
SERVER_DIRECTORY,
|
|
|
|
CLIENT_STATIC_FILES_PATH,
|
|
|
|
'development',
|
|
|
|
'pages'
|
|
|
|
)
|
2018-02-13 14:27:52 +01:00
|
|
|
|
|
|
|
describe('pageNotFoundError', () => {
|
|
|
|
it('Should throw error with ENOENT code', () => {
|
2019-11-13 23:18:06 +01:00
|
|
|
expect.assertions(1)
|
2018-02-13 14:27:52 +01:00
|
|
|
try {
|
2019-11-13 23:18:06 +01:00
|
|
|
throw pageNotFoundError('test')
|
2018-02-13 14:27:52 +01:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('ENOENT')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('normalizePagePath', () => {
|
|
|
|
it('Should turn / into /index', () => {
|
|
|
|
expect(normalizePagePath('/')).toBe(`${sep}index`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should turn _error into /_error', () => {
|
|
|
|
expect(normalizePagePath('_error')).toBe(`${sep}_error`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should turn /abc into /abc', () => {
|
|
|
|
expect(normalizePagePath('/abc')).toBe(`${sep}abc`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should turn /abc/def into /abc/def', () => {
|
|
|
|
expect(normalizePagePath('/abc/def')).toBe(`${sep}abc${sep}def`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should throw on /../../test.js', () => {
|
|
|
|
expect(() => normalizePagePath('/../../test.js')).toThrow()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPagePath', () => {
|
|
|
|
it('Should append /index to the / page', () => {
|
2018-12-07 13:35:01 +01:00
|
|
|
const pagePath = getPagePath('/', distDir)
|
2018-03-30 15:08:09 +02:00
|
|
|
expect(pagePath).toBe(join(pathToBundles, `${sep}index.js`))
|
2018-02-13 14:27:52 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should prepend / when a page does not have it', () => {
|
2018-12-07 13:35:01 +01:00
|
|
|
const pagePath = getPagePath('_error', distDir)
|
2018-03-30 15:08:09 +02:00
|
|
|
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
|
2018-02-13 14:27:52 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should throw with paths containing ../', () => {
|
2018-12-07 13:35:01 +01:00
|
|
|
expect(() => getPagePath('/../../package.json', distDir)).toThrow()
|
2018-02-13 14:27:52 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('requirePage', () => {
|
2018-03-09 17:14:30 +01:00
|
|
|
it('Should require /index.js when using /', async () => {
|
2019-05-30 03:19:32 +02:00
|
|
|
const page = await requirePage('/', distDir)
|
2018-02-13 14:27:52 +01:00
|
|
|
expect(page.test).toBe('hello')
|
|
|
|
})
|
|
|
|
|
2018-03-09 17:14:30 +01:00
|
|
|
it('Should require /index.js when using /index', async () => {
|
2019-05-30 03:19:32 +02:00
|
|
|
const page = await requirePage('/index', distDir)
|
2018-02-13 14:27:52 +01:00
|
|
|
expect(page.test).toBe('hello')
|
|
|
|
})
|
|
|
|
|
2018-03-09 17:14:30 +01:00
|
|
|
it('Should require /world.js when using /world', async () => {
|
2019-05-30 03:19:32 +02:00
|
|
|
const page = await requirePage('/world', distDir)
|
2018-02-13 14:27:52 +01:00
|
|
|
expect(page.test).toBe('world')
|
|
|
|
})
|
|
|
|
|
2018-03-09 17:14:30 +01:00
|
|
|
it('Should throw when using /../../test.js', async () => {
|
2019-11-13 23:18:06 +01:00
|
|
|
expect.assertions(1)
|
2018-03-09 17:14:30 +01:00
|
|
|
try {
|
2018-12-07 13:35:01 +01:00
|
|
|
await requirePage('/../../test', distDir)
|
2018-03-09 17:14:30 +01:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('ENOENT')
|
|
|
|
}
|
2018-02-13 14:27:52 +01:00
|
|
|
})
|
|
|
|
|
2018-03-09 17:14:30 +01:00
|
|
|
it('Should throw when using non existent pages like /non-existent.js', async () => {
|
2019-11-13 23:18:06 +01:00
|
|
|
expect.assertions(1)
|
2018-03-09 17:14:30 +01:00
|
|
|
try {
|
2018-12-07 13:35:01 +01:00
|
|
|
await requirePage('/non-existent', distDir)
|
2018-03-09 17:14:30 +01:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('ENOENT')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should bubble up errors in the child component', async () => {
|
2019-11-13 23:18:06 +01:00
|
|
|
expect.assertions(1)
|
2018-03-09 17:14:30 +01:00
|
|
|
try {
|
2018-12-07 13:35:01 +01:00
|
|
|
await requirePage('/non-existent-child', distDir)
|
2018-03-09 17:14:30 +01:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('MODULE_NOT_FOUND')
|
|
|
|
}
|
2018-02-13 14:27:52 +01:00
|
|
|
})
|
|
|
|
})
|