2018-02-14 15:20:41 +00:00
|
|
|
/* global describe, it, expect */
|
|
|
|
|
2018-06-14 17:30:14 +00:00
|
|
|
import {normalize, join} from 'path'
|
2018-09-30 23:02:10 +00:00
|
|
|
import {getPageEntries, createEntry} from 'next/dist/build/webpack/utils'
|
2018-02-14 15:20:41 +00:00
|
|
|
|
2018-07-25 11:45:42 +00:00
|
|
|
const buildId = 'development'
|
|
|
|
|
2018-02-14 15:20:41 +00:00
|
|
|
describe('createEntry', () => {
|
|
|
|
it('Should turn a path into a page entry', () => {
|
|
|
|
const entry = createEntry('pages/index.js')
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize(`static/pages/index.js`))
|
2018-02-14 15:20:41 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.js')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should have a custom name', () => {
|
|
|
|
const entry = createEntry('pages/index.js', {name: 'something-else.js'})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize(`static/something-else.js`))
|
2018-02-14 15:20:41 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.js')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should allow custom extension like .ts to be turned into .js', () => {
|
|
|
|
const entry = createEntry('pages/index.ts', {pageExtensions: ['js', 'ts'].join('|')})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize('static/pages/index.js'))
|
2018-02-14 15:20:41 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.ts')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should allow custom extension like .jsx to be turned into .js', () => {
|
|
|
|
const entry = createEntry('pages/index.jsx', {pageExtensions: ['jsx', 'js'].join('|')})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize('static/pages/index.js'))
|
2018-02-14 15:20:41 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.jsx')
|
|
|
|
})
|
|
|
|
|
2018-02-17 11:36:47 +00:00
|
|
|
it('Should allow custom extension like .tsx to be turned into .js', () => {
|
|
|
|
const entry = createEntry('pages/index.tsx', {pageExtensions: ['tsx', 'ts'].join('|')})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize('static/pages/index.js'))
|
2018-02-17 11:36:47 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.tsx')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should allow custom extension like .tsx to be turned into .js with another order', () => {
|
|
|
|
const entry = createEntry('pages/index.tsx', {pageExtensions: ['ts', 'tsx'].join('|')})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize('static/pages/index.js'))
|
2018-02-17 11:36:47 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/index.tsx')
|
|
|
|
})
|
|
|
|
|
2018-02-14 15:20:41 +00:00
|
|
|
it('Should turn pages/blog/index.js into pages/blog.js', () => {
|
|
|
|
const entry = createEntry('pages/blog/index.js')
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(entry.name).toBe(normalize('static/pages/blog.js'))
|
|
|
|
expect(entry.files[0]).toBe('./pages/blog/index.js')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should add buildId when provided', () => {
|
|
|
|
const entry = createEntry('pages/blog/index.js', {buildId})
|
|
|
|
expect(entry.name).toBe(normalize(`static/${buildId}/pages/blog.js`))
|
2018-02-14 15:20:41 +00:00
|
|
|
expect(entry.files[0]).toBe('./pages/blog/index.js')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPageEntries', () => {
|
2018-06-14 17:30:14 +00:00
|
|
|
const nextPagesDir = join(__dirname, '..', '..', 'dist', 'pages')
|
|
|
|
|
2018-02-14 15:20:41 +00:00
|
|
|
it('Should return paths', () => {
|
|
|
|
const pagePaths = ['pages/index.js']
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageEntries = getPageEntries(pagePaths, {nextPagesDir})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(pageEntries[normalize('static/pages/index.js')][0]).toBe('./pages/index.js')
|
2018-02-14 15:20:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should include default _error', () => {
|
|
|
|
const pagePaths = ['pages/index.js']
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageEntries = getPageEntries(pagePaths, {nextPagesDir})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(pageEntries[normalize('static/pages/_error.js')][0]).toMatch(/dist[/\\]pages[/\\]_error\.js/)
|
2018-02-14 15:20:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not include default _error when _error.js is inside the pages directory', () => {
|
|
|
|
const pagePaths = ['pages/index.js', 'pages/_error.js']
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageEntries = getPageEntries(pagePaths, {nextPagesDir})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(pageEntries[normalize('static/pages/_error.js')][0]).toBe('./pages/_error.js')
|
2018-02-14 15:20:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should include default _document when isServer is true', () => {
|
|
|
|
const pagePaths = ['pages/index.js']
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageEntries = getPageEntries(pagePaths, {nextPagesDir, isServer: true})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(pageEntries[normalize('static/pages/_document.js')][0]).toMatch(/dist[/\\]pages[/\\]_document\.js/)
|
2018-02-14 15:20:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not include default _document when _document.js is inside the pages directory', () => {
|
|
|
|
const pagePaths = ['pages/index.js', 'pages/_document.js']
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageEntries = getPageEntries(pagePaths, {nextPagesDir, isServer: true})
|
2018-07-25 11:45:42 +00:00
|
|
|
expect(pageEntries[normalize('static/pages/_document.js')][0]).toBe('./pages/_document.js')
|
2018-02-14 15:20:41 +00:00
|
|
|
})
|
|
|
|
})
|