2019-02-24 22:08:35 +01:00
|
|
|
/* eslint-env jest */
|
2022-08-26 20:21:53 +02:00
|
|
|
import {
|
|
|
|
findPageFile,
|
|
|
|
isLayoutsLeafPage,
|
|
|
|
} from 'next/dist/server/lib/find-page-file'
|
2022-04-30 13:19:27 +02:00
|
|
|
import { normalizePagePath } from 'next/dist/shared/lib/page-path/normalize-page-path'
|
2019-02-24 22:08:35 +01:00
|
|
|
|
|
|
|
import { join } from 'path'
|
|
|
|
|
2021-08-24 14:52:45 +02:00
|
|
|
const resolveDataDir = join(__dirname, 'isolated', '_resolvedata')
|
2019-02-24 22:08:35 +01:00
|
|
|
const dirWithPages = join(resolveDataDir, 'readdir', 'pages')
|
|
|
|
|
|
|
|
describe('findPageFile', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
const pagePath = normalizePagePath('/nav/about')
|
2022-09-03 02:13:47 +02:00
|
|
|
const result = await findPageFile(
|
|
|
|
dirWithPages,
|
|
|
|
pagePath,
|
|
|
|
['jsx', 'js'],
|
|
|
|
false
|
|
|
|
)
|
2019-02-24 22:08:35 +01:00
|
|
|
expect(result).toMatch(/^[\\/]nav[\\/]about\.js/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work with nested index.js', async () => {
|
|
|
|
const pagePath = normalizePagePath('/nested')
|
2022-09-03 02:13:47 +02:00
|
|
|
const result = await findPageFile(
|
|
|
|
dirWithPages,
|
|
|
|
pagePath,
|
|
|
|
['jsx', 'js'],
|
|
|
|
false
|
|
|
|
)
|
2019-02-24 22:08:35 +01:00
|
|
|
expect(result).toMatch(/^[\\/]nested[\\/]index\.js/)
|
|
|
|
})
|
|
|
|
|
2020-07-19 06:38:20 +02:00
|
|
|
it('should prefer prefered.js before preferred/index.js', async () => {
|
2019-02-24 22:08:35 +01:00
|
|
|
const pagePath = normalizePagePath('/prefered')
|
2022-09-03 02:13:47 +02:00
|
|
|
const result = await findPageFile(
|
|
|
|
dirWithPages,
|
|
|
|
pagePath,
|
|
|
|
['jsx', 'js'],
|
|
|
|
false
|
|
|
|
)
|
2019-02-24 22:08:35 +01:00
|
|
|
expect(result).toMatch(/^[\\/]prefered\.js/)
|
|
|
|
})
|
|
|
|
})
|
2022-08-26 20:21:53 +02:00
|
|
|
|
|
|
|
describe('isLayoutsLeafPage', () => {
|
2022-11-16 22:46:04 +01:00
|
|
|
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
|
2022-08-26 20:21:53 +02:00
|
|
|
it('should determine either server or client component page file as leaf node page', () => {
|
2022-11-16 22:46:04 +01:00
|
|
|
expect(isLayoutsLeafPage('page.js', pageExtensions)).toBe(true)
|
|
|
|
expect(isLayoutsLeafPage('./page.js', pageExtensions)).toBe(true)
|
|
|
|
expect(isLayoutsLeafPage('./page.jsx', pageExtensions)).toBe(true)
|
|
|
|
expect(isLayoutsLeafPage('/page.ts', pageExtensions)).toBe(true)
|
|
|
|
expect(isLayoutsLeafPage('/path/page.tsx', pageExtensions)).toBe(true)
|
2022-08-26 20:21:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should determine other files under layout routes as non leaf node', () => {
|
2022-11-16 22:46:04 +01:00
|
|
|
expect(isLayoutsLeafPage('./not-a-page.js', pageExtensions)).toBe(false)
|
|
|
|
expect(isLayoutsLeafPage('not-a-page.js', pageExtensions)).toBe(false)
|
|
|
|
expect(isLayoutsLeafPage('./page.component.jsx', pageExtensions)).toBe(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
expect(isLayoutsLeafPage('layout.js', pageExtensions)).toBe(false)
|
|
|
|
expect(isLayoutsLeafPage('page', pageExtensions)).toBe(false)
|
2022-08-26 20:21:53 +02:00
|
|
|
})
|
|
|
|
})
|