1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/test/isolated/require-page.test.js
Tim Neutkens 098f3fd7e9
Rename dist to server to be more consistent (#4506)
Previously we called this directory holding the pages/chunks for server rendering `.next/dist` instead of `.next/server` which is confusing both when looking at it and in the codebase, since there's also `distDir` as a configuration option.

Also made this a constant in `next/constants` so functionality using this can be easily found.
2018-05-31 20:56:04 +02:00

98 lines
3 KiB
JavaScript

/* global describe, it, expect */
import { join } from 'path'
import {SERVER_DIRECTORY} from 'next/constants'
import requirePage, {getPagePath, normalizePagePath, pageNotFoundError} from '../../dist/server/require'
const sep = '/'
const pathToBundles = join(__dirname, '_resolvedata', SERVER_DIRECTORY, 'bundles', 'pages')
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', () => {
const pagePath = getPagePath('/', {dir: __dirname, dist: '_resolvedata'})
expect(pagePath).toBe(join(pathToBundles, `${sep}index.js`))
})
it('Should prepend / when a page does not have it', () => {
const pagePath = getPagePath('_error', {dir: __dirname, dist: '_resolvedata'})
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
})
it('Should throw with paths containing ../', () => {
expect(() => getPagePath('/../../package.json', {dir: __dirname, dist: '_resolvedata'})).toThrow()
})
})
describe('requirePage', () => {
it('Should require /index.js when using /', async () => {
const page = await requirePage('/', {dir: __dirname, dist: '_resolvedata'})
expect(page.test).toBe('hello')
})
it('Should require /index.js when using /index', async () => {
const page = await requirePage('/index', {dir: __dirname, dist: '_resolvedata'})
expect(page.test).toBe('hello')
})
it('Should require /world.js when using /world', async () => {
const page = await requirePage('/world', {dir: __dirname, dist: '_resolvedata'})
expect(page.test).toBe('world')
})
it('Should throw when using /../../test.js', async () => {
try {
await requirePage('/../../test', {dir: __dirname, dist: '_resolvedata'})
} catch (err) {
expect(err.code).toBe('ENOENT')
}
})
it('Should throw when using non existent pages like /non-existent.js', async () => {
try {
await requirePage('/non-existent', {dir: __dirname, dist: '_resolvedata'})
} catch (err) {
expect(err.code).toBe('ENOENT')
}
})
it('Should bubble up errors in the child component', async () => {
try {
await requirePage('/non-existent-child', {dir: __dirname, dist: '_resolvedata'})
} catch (err) {
expect(err.code).toBe('MODULE_NOT_FOUND')
}
})
})