2018-02-13 13:27:52 +00:00
|
|
|
/* global describe, it, expect */
|
|
|
|
|
2018-03-30 13:08:09 +00:00
|
|
|
import { join } from 'path'
|
2018-05-31 18:56:04 +00:00
|
|
|
import {SERVER_DIRECTORY} from 'next/constants'
|
2018-02-13 13:27:52 +00:00
|
|
|
import requirePage, {getPagePath, normalizePagePath, pageNotFoundError} from '../../dist/server/require'
|
|
|
|
|
2018-03-30 13:08:09 +00:00
|
|
|
const sep = '/'
|
2018-06-04 13:45:39 +00:00
|
|
|
const distDir = join(__dirname, '_resolvedata')
|
|
|
|
const pathToBundles = join(distDir, SERVER_DIRECTORY, 'bundles', 'pages')
|
2018-02-13 13:27:52 +00:00
|
|
|
|
|
|
|
describe('pageNotFoundError', () => {
|
|
|
|
it('Should throw error with ENOENT code', () => {
|
|
|
|
try {
|
|
|
|
pageNotFoundError('test')
|
|
|
|
} 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-06-04 13:45:39 +00:00
|
|
|
const pagePath = getPagePath('/', {distDir})
|
2018-03-30 13:08:09 +00:00
|
|
|
expect(pagePath).toBe(join(pathToBundles, `${sep}index.js`))
|
2018-02-13 13:27:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should prepend / when a page does not have it', () => {
|
2018-06-04 13:45:39 +00:00
|
|
|
const pagePath = getPagePath('_error', {distDir})
|
2018-03-30 13:08:09 +00:00
|
|
|
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
|
2018-02-13 13:27:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should throw with paths containing ../', () => {
|
2018-06-04 13:45:39 +00:00
|
|
|
expect(() => getPagePath('/../../package.json', {distDir})).toThrow()
|
2018-02-13 13:27:52 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('requirePage', () => {
|
2018-03-09 16:14:30 +00:00
|
|
|
it('Should require /index.js when using /', async () => {
|
2018-06-04 13:45:39 +00:00
|
|
|
const page = await requirePage('/', {distDir})
|
2018-02-13 13:27:52 +00:00
|
|
|
expect(page.test).toBe('hello')
|
|
|
|
})
|
|
|
|
|
2018-03-09 16:14:30 +00:00
|
|
|
it('Should require /index.js when using /index', async () => {
|
2018-06-04 13:45:39 +00:00
|
|
|
const page = await requirePage('/index', {distDir})
|
2018-02-13 13:27:52 +00:00
|
|
|
expect(page.test).toBe('hello')
|
|
|
|
})
|
|
|
|
|
2018-03-09 16:14:30 +00:00
|
|
|
it('Should require /world.js when using /world', async () => {
|
2018-06-04 13:45:39 +00:00
|
|
|
const page = await requirePage('/world', {distDir})
|
2018-02-13 13:27:52 +00:00
|
|
|
expect(page.test).toBe('world')
|
|
|
|
})
|
|
|
|
|
2018-03-09 16:14:30 +00:00
|
|
|
it('Should throw when using /../../test.js', async () => {
|
|
|
|
try {
|
2018-06-04 13:45:39 +00:00
|
|
|
await requirePage('/../../test', {distDir})
|
2018-03-09 16:14:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('ENOENT')
|
|
|
|
}
|
2018-02-13 13:27:52 +00:00
|
|
|
})
|
|
|
|
|
2018-03-09 16:14:30 +00:00
|
|
|
it('Should throw when using non existent pages like /non-existent.js', async () => {
|
|
|
|
try {
|
2018-06-04 13:45:39 +00:00
|
|
|
await requirePage('/non-existent', {distDir})
|
2018-03-09 16:14:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('ENOENT')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should bubble up errors in the child component', async () => {
|
|
|
|
try {
|
2018-06-04 13:45:39 +00:00
|
|
|
await requirePage('/non-existent-child', {distDir})
|
2018-03-09 16:14:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).toBe('MODULE_NOT_FOUND')
|
|
|
|
}
|
2018-02-13 13:27:52 +00:00
|
|
|
})
|
|
|
|
})
|