2019-02-24 22:08:35 +01:00
|
|
|
/* eslint-env jest */
|
2022-08-26 20:21:53 +02:00
|
|
|
import {
|
|
|
|
findPageFile,
|
2023-03-09 20:10:18 +01:00
|
|
|
createValidFileMatcher,
|
2022-08-26 20:21:53 +02:00
|
|
|
} 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
|
|
|
|
2023-03-09 20:10:18 +01:00
|
|
|
describe('createPageFileMatcher', () => {
|
|
|
|
describe('isAppRouterPage', () => {
|
|
|
|
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
|
|
|
|
const fileMatcher = createValidFileMatcher(pageExtensions, '')
|
|
|
|
|
|
|
|
it('should determine either server or client component page file as leaf node page', () => {
|
|
|
|
expect(fileMatcher.isAppRouterPage('page.js')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('./page.js')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('./page.jsx')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('/page.ts')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('/path/page.tsx')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('\\path\\page.tsx')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('.\\page.jsx')).toBe(true)
|
|
|
|
expect(fileMatcher.isAppRouterPage('\\page.js')).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should determine other files under layout routes as non leaf node', () => {
|
|
|
|
expect(fileMatcher.isAppRouterPage('./not-a-page.js')).toBe(false)
|
|
|
|
expect(fileMatcher.isAppRouterPage('not-a-page.js')).toBe(false)
|
|
|
|
expect(fileMatcher.isAppRouterPage('./page.component.jsx')).toBe(false)
|
|
|
|
expect(fileMatcher.isAppRouterPage('layout.js')).toBe(false)
|
|
|
|
expect(fileMatcher.isAppRouterPage('page')).toBe(false)
|
|
|
|
})
|
2022-08-26 20:21:53 +02:00
|
|
|
})
|
|
|
|
|
2023-03-09 20:10:18 +01:00
|
|
|
describe('isMetadataRouteFile', () => {
|
|
|
|
it('should determine top level metadata routes', () => {
|
2023-03-21 16:55:32 +01:00
|
|
|
const pageExtensions = ['tsx', 'ts', 'jsx', 'js']
|
|
|
|
const fileMatcher = createValidFileMatcher(pageExtensions, 'app')
|
2023-03-17 13:41:56 +01:00
|
|
|
expect(fileMatcher.isMetadataFile('app/route.js')).toBe(false)
|
|
|
|
expect(fileMatcher.isMetadataFile('app/page.js')).toBe(false)
|
|
|
|
expect(fileMatcher.isMetadataFile('pages/index.js')).toBe(false)
|
2023-03-09 20:10:18 +01:00
|
|
|
|
2023-03-17 13:41:56 +01:00
|
|
|
expect(fileMatcher.isMetadataFile('app/robots.txt')).toBe(true)
|
|
|
|
expect(fileMatcher.isMetadataFile('app/path/robots.txt')).toBe(false)
|
2023-03-09 20:10:18 +01:00
|
|
|
|
2023-03-17 13:41:56 +01:00
|
|
|
expect(fileMatcher.isMetadataFile('app/sitemap.xml')).toBe(true)
|
2023-04-26 22:41:37 +02:00
|
|
|
expect(fileMatcher.isMetadataFile('app/path/sitemap.xml')).toBe(true)
|
2023-03-09 20:10:18 +01:00
|
|
|
})
|
2022-08-26 20:21:53 +02:00
|
|
|
})
|
|
|
|
})
|