|
|
import { FallbackMode } from '../../lib/fallback' |
|
|
import type { Params } from '../../server/request/params' |
|
|
import { |
|
|
assignErrorIfEmpty, |
|
|
generateAllParamCombinations, |
|
|
calculateFallbackMode, |
|
|
filterUniqueParams, |
|
|
generateRouteStaticParams, |
|
|
} from './app' |
|
|
import type { PrerenderedRoute } from './types' |
|
|
import type { WorkStore } from '../../server/app-render/work-async-storage.external' |
|
|
import type { AppSegment } from '../segment-config/app/app-segments' |
|
|
|
|
|
describe('assignErrorIfEmpty', () => { |
|
|
it('should assign throwOnEmptyStaticShell true for a static route with no children', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/', |
|
|
encodedPathname: '/', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, []) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should assign throwOnEmptyStaticShell based on route hierarchy', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/[id]', |
|
|
encodedPathname: '/[id]', |
|
|
fallbackRouteParams: ['id'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1' }, |
|
|
pathname: '/1', |
|
|
encodedPathname: '/1', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['id']) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should handle more complex routes', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/[id]/[name]', |
|
|
encodedPathname: '/[id]/[name]', |
|
|
fallbackRouteParams: ['id', 'name'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1' }, |
|
|
pathname: '/1/[name]', |
|
|
encodedPathname: '/1/[name]', |
|
|
fallbackRouteParams: ['name'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1', name: 'test' }, |
|
|
pathname: '/1/test', |
|
|
encodedPathname: '/1/test', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '2', name: 'test' }, |
|
|
pathname: '/2/test', |
|
|
encodedPathname: '/2/test', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '2' }, |
|
|
pathname: '/2/[name]', |
|
|
encodedPathname: '/2/[name]', |
|
|
fallbackRouteParams: ['name'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['id', 'name']) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[2].throwOnEmptyStaticShell).toBe(true) |
|
|
expect(prerenderedRoutes[3].throwOnEmptyStaticShell).toBe(true) |
|
|
expect(prerenderedRoutes[4].throwOnEmptyStaticShell).toBe(false) |
|
|
}) |
|
|
|
|
|
it('should handle multiple routes at the same trie node', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: { id: '1' }, |
|
|
pathname: '/1/[name]', |
|
|
encodedPathname: '/1/[name]', |
|
|
fallbackRouteParams: ['name'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1' }, |
|
|
pathname: '/1/[name]/[extra]', |
|
|
encodedPathname: '/1/[name]/[extra]', |
|
|
fallbackRouteParams: ['name', 'extra'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1', name: 'test' }, |
|
|
pathname: '/1/test', |
|
|
encodedPathname: '/1/test', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['id', 'name', 'extra']) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[2].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should handle empty input', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [] |
|
|
assignErrorIfEmpty(prerenderedRoutes, []) |
|
|
expect(prerenderedRoutes).toEqual([]) |
|
|
}) |
|
|
|
|
|
it('should handle blog/[slug] not throwing when concrete routes exist (from docs example)', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/blog/[slug]', |
|
|
encodedPathname: '/blog/[slug]', |
|
|
fallbackRouteParams: ['slug'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { slug: 'first-post' }, |
|
|
pathname: '/blog/first-post', |
|
|
encodedPathname: '/blog/first-post', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { slug: 'second-post' }, |
|
|
pathname: '/blog/second-post', |
|
|
encodedPathname: '/blog/second-post', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['slug']) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(true) |
|
|
expect(prerenderedRoutes[2].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should handle catch-all routes with different fallback parameter counts (from docs example)', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/[id]/[...slug]', |
|
|
encodedPathname: '/[id]/[...slug]', |
|
|
fallbackRouteParams: ['id', 'slug'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1234' }, |
|
|
pathname: '/1234/[...slug]', |
|
|
encodedPathname: '/1234/[...slug]', |
|
|
fallbackRouteParams: ['slug'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { id: '1234', slug: ['about', 'us'] }, |
|
|
pathname: '/1234/about/us', |
|
|
encodedPathname: '/1234/about/us', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['id', 'slug']) |
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[2].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should handle nested routes with multiple parameter depths', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: '/[category]/[subcategory]/[item]', |
|
|
encodedPathname: '/[category]/[subcategory]/[item]', |
|
|
fallbackRouteParams: ['category', 'subcategory', 'item'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { category: 'electronics' }, |
|
|
pathname: '/electronics/[subcategory]/[item]', |
|
|
encodedPathname: '/electronics/[subcategory]/[item]', |
|
|
fallbackRouteParams: ['subcategory', 'item'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { category: 'electronics', subcategory: 'phones' }, |
|
|
pathname: '/electronics/phones/[item]', |
|
|
encodedPathname: '/electronics/phones/[item]', |
|
|
fallbackRouteParams: ['item'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { |
|
|
category: 'electronics', |
|
|
subcategory: 'phones', |
|
|
item: 'iphone', |
|
|
}, |
|
|
pathname: '/electronics/phones/iphone', |
|
|
encodedPathname: '/electronics/phones/iphone', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['category', 'subcategory', 'item']) |
|
|
|
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[2].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[3].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
|
|
|
it('should handle routes at same trie node with different fallback parameter lengths', () => { |
|
|
const prerenderedRoutes: PrerenderedRoute[] = [ |
|
|
{ |
|
|
params: { locale: 'en' }, |
|
|
pathname: '/en/[...segments]', |
|
|
encodedPathname: '/en/[...segments]', |
|
|
fallbackRouteParams: ['segments'], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
{ |
|
|
params: { locale: 'en' }, |
|
|
pathname: '/en', |
|
|
encodedPathname: '/en', |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
] |
|
|
|
|
|
assignErrorIfEmpty(prerenderedRoutes, ['locale', 'segments']) |
|
|
|
|
|
|
|
|
expect(prerenderedRoutes[0].throwOnEmptyStaticShell).toBe(false) |
|
|
expect(prerenderedRoutes[1].throwOnEmptyStaticShell).toBe(true) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('filterUniqueParams', () => { |
|
|
it('should filter out duplicate parameters', () => { |
|
|
const params = [ |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '2' }, |
|
|
] |
|
|
|
|
|
const unique = filterUniqueParams(['id', 'name'], params) |
|
|
|
|
|
expect(unique).toEqual([{ id: '1', name: 'test' }, { id: '2' }]) |
|
|
}) |
|
|
|
|
|
it('should handle more complex routes', () => { |
|
|
const params = [ |
|
|
{ id: '1', name: 'test', age: '10' }, |
|
|
{ id: '1', name: 'test', age: '20' }, |
|
|
{ id: '2', name: 'test', age: '10' }, |
|
|
] |
|
|
|
|
|
const unique = filterUniqueParams(['id', 'name', 'age'], params) |
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ id: '1', name: 'test', age: '10' }, |
|
|
{ id: '1', name: 'test', age: '20' }, |
|
|
{ id: '2', name: 'test', age: '10' }, |
|
|
]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('generateParamPrefixCombinations', () => { |
|
|
it('should return only the route parameters', () => { |
|
|
const params = [ |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '2', name: 'test' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations(['id'], params, []) |
|
|
|
|
|
expect(unique).toEqual([{ id: '1' }, { id: '2' }]) |
|
|
}) |
|
|
|
|
|
it('should handle multiple route parameters', () => { |
|
|
const params = [ |
|
|
{ lang: 'en', region: 'US', page: 'home' }, |
|
|
{ lang: 'en', region: 'US', page: 'about' }, |
|
|
{ lang: 'fr', region: 'CA', page: 'home' }, |
|
|
{ lang: 'fr', region: 'CA', page: 'about' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations(['lang', 'region'], params, []) |
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en' }, |
|
|
{ lang: 'en', region: 'US' }, |
|
|
{ lang: 'fr' }, |
|
|
{ lang: 'fr', region: 'CA' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle parameter value collisions', () => { |
|
|
const params = [{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }] |
|
|
|
|
|
const unique = generateAllParamCombinations(['slug'], params, []) |
|
|
|
|
|
expect(unique).toEqual([{ slug: ['foo', 'bar'] }, { slug: 'foo,bar' }]) |
|
|
}) |
|
|
|
|
|
it('should handle empty inputs', () => { |
|
|
|
|
|
expect(generateAllParamCombinations([], [{ id: '1' }], [])).toEqual([]) |
|
|
|
|
|
|
|
|
expect(generateAllParamCombinations(['id'], [], [])).toEqual([]) |
|
|
|
|
|
|
|
|
expect(generateAllParamCombinations([], [], [])).toEqual([]) |
|
|
}) |
|
|
|
|
|
it('should handle undefined parameters', () => { |
|
|
const params = [ |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '2', name: undefined }, |
|
|
{ id: '3' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations(['id', 'name'], params, []) |
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ id: '1' }, |
|
|
{ id: '1', name: 'test' }, |
|
|
{ id: '2' }, |
|
|
{ id: '3' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle missing parameter keys in objects', () => { |
|
|
const params = [ |
|
|
{ lang: 'en', region: 'US', category: 'tech' }, |
|
|
{ lang: 'en', region: 'US' }, |
|
|
{ lang: 'fr' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'region', 'category'], |
|
|
params, |
|
|
[] |
|
|
) |
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en' }, |
|
|
{ lang: 'en', region: 'US' }, |
|
|
{ lang: 'en', region: 'US', category: 'tech' }, |
|
|
{ lang: 'fr' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should prevent collisions with special characters', () => { |
|
|
const params = [ |
|
|
{ slug: ['foo', 'bar'] }, |
|
|
{ slug: 'foo,bar' }, |
|
|
{ slug: 'A:foo,bar' }, |
|
|
{ slug: ['A:foo', 'bar'] }, |
|
|
{ slug: undefined }, |
|
|
{ slug: 'U:undefined' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations(['slug'], params, []) |
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ slug: ['foo', 'bar'] }, |
|
|
{ slug: 'foo,bar' }, |
|
|
{ slug: 'A:foo,bar' }, |
|
|
{ slug: ['A:foo', 'bar'] }, |
|
|
{ slug: undefined }, |
|
|
{ slug: 'U:undefined' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle parameters with pipe characters', () => { |
|
|
const params = [ |
|
|
{ slug: 'foo|bar' }, |
|
|
{ slug: ['foo', 'bar|baz'] }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations(['slug'], params, []) |
|
|
|
|
|
expect(unique).toEqual([{ slug: 'foo|bar' }, { slug: ['foo', 'bar|baz'] }]) |
|
|
}) |
|
|
|
|
|
it('should handle deep parameter hierarchies', () => { |
|
|
const params = [ |
|
|
{ a: '1', b: '2', c: '3', d: '4', e: '5' }, |
|
|
{ a: '1', b: '2', c: '3', d: '4', e: '6' }, |
|
|
{ a: '1', b: '2', c: '3', d: '7' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['a', 'b', 'c', 'd', 'e'], |
|
|
params, |
|
|
[] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ a: '1' }, |
|
|
{ a: '1', b: '2' }, |
|
|
{ a: '1', b: '2', c: '3' }, |
|
|
{ a: '1', b: '2', c: '3', d: '4' }, |
|
|
{ a: '1', b: '2', c: '3', d: '4', e: '5' }, |
|
|
{ a: '1', b: '2', c: '3', d: '4', e: '6' }, |
|
|
{ a: '1', b: '2', c: '3', d: '7' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should only generate combinations with complete root params', () => { |
|
|
const params = [ |
|
|
{ lang: 'en', region: 'US', slug: 'home' }, |
|
|
{ lang: 'en', region: 'US', slug: 'about' }, |
|
|
{ lang: 'fr', region: 'CA', slug: 'about' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'region', 'slug'], |
|
|
params, |
|
|
['lang', 'region'] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en', region: 'US' }, |
|
|
{ lang: 'en', region: 'US', slug: 'home' }, |
|
|
{ lang: 'en', region: 'US', slug: 'about' }, |
|
|
{ lang: 'fr', region: 'CA' }, |
|
|
{ lang: 'fr', region: 'CA', slug: 'about' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle routes without root params normally', () => { |
|
|
const params = [ |
|
|
{ category: 'tech', slug: 'news' }, |
|
|
{ category: 'tech', slug: 'reviews' }, |
|
|
{ category: 'sports', slug: 'news' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['category', 'slug'], |
|
|
params, |
|
|
[] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ category: 'tech' }, |
|
|
{ category: 'tech', slug: 'news' }, |
|
|
{ category: 'tech', slug: 'reviews' }, |
|
|
{ category: 'sports' }, |
|
|
{ category: 'sports', slug: 'news' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle single root param', () => { |
|
|
const params = [ |
|
|
{ lang: 'en', page: 'home' }, |
|
|
{ lang: 'en', page: 'about' }, |
|
|
{ lang: 'fr', page: 'home' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'page'], |
|
|
params, |
|
|
['lang'] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en' }, |
|
|
{ lang: 'en', page: 'home' }, |
|
|
{ lang: 'en', page: 'about' }, |
|
|
{ lang: 'fr' }, |
|
|
{ lang: 'fr', page: 'home' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle missing root params gracefully', () => { |
|
|
const params = [ |
|
|
{ lang: 'en', page: 'home' }, |
|
|
{ lang: 'en', page: 'about' }, |
|
|
{ page: 'contact' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'page'], |
|
|
params, |
|
|
['lang'] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en' }, |
|
|
{ lang: 'en', page: 'home' }, |
|
|
{ lang: 'en', page: 'about' }, |
|
|
|
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle root params not in route params', () => { |
|
|
const params = [ |
|
|
{ category: 'tech', slug: 'news' }, |
|
|
{ category: 'sports', slug: 'news' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['category', 'slug'], |
|
|
params, |
|
|
['lang', 'region'] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ category: 'tech' }, |
|
|
{ category: 'tech', slug: 'news' }, |
|
|
{ category: 'sports' }, |
|
|
{ category: 'sports', slug: 'news' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle test case scenario: route with extra param but missing value', () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const params = [ |
|
|
{ lang: 'en', locale: 'us' }, |
|
|
] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'locale', 'slug'], |
|
|
params, |
|
|
['lang', 'locale'] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
expect(unique).toEqual([ |
|
|
{ lang: 'en', locale: 'us' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle empty routeParams with root params', () => { |
|
|
|
|
|
const params: Params[] = [] |
|
|
|
|
|
const unique = generateAllParamCombinations( |
|
|
['lang', 'locale', 'slug'], |
|
|
params, |
|
|
['lang', 'locale'] |
|
|
) |
|
|
|
|
|
|
|
|
expect(unique).toEqual([]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
type TestAppSegment = Pick<AppSegment, 'config' | 'generateStaticParams'> |
|
|
|
|
|
|
|
|
const createMockWorkStore = (fetchCache?: WorkStore['fetchCache']) => ({ |
|
|
fetchCache, |
|
|
}) |
|
|
|
|
|
|
|
|
const createMockSegment = ( |
|
|
generateStaticParams?: (options: { params?: Params }) => Promise<Params[]>, |
|
|
config?: TestAppSegment['config'] |
|
|
): TestAppSegment => ({ |
|
|
config, |
|
|
generateStaticParams, |
|
|
}) |
|
|
|
|
|
describe('generateRouteStaticParams', () => { |
|
|
describe('Basic functionality', () => { |
|
|
it('should return empty array for empty segments', async () => { |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams([], store) |
|
|
expect(result).toEqual([]) |
|
|
}) |
|
|
|
|
|
it('should return empty array for segments without generateStaticParams', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(), |
|
|
createMockSegment(), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([]) |
|
|
}) |
|
|
|
|
|
it('should process single segment with generateStaticParams', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ id: '1' }, { id: '2' }]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ id: '1' }, { id: '2' }]) |
|
|
}) |
|
|
|
|
|
it('should process multiple segments with generateStaticParams', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [ |
|
|
{ category: 'tech' }, |
|
|
{ category: 'sports' }, |
|
|
]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ slug: `${params?.category}-post-1` }, |
|
|
{ slug: `${params?.category}-post-2` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([ |
|
|
{ category: 'tech', slug: 'tech-post-1' }, |
|
|
{ category: 'tech', slug: 'tech-post-2' }, |
|
|
{ category: 'sports', slug: 'sports-post-1' }, |
|
|
{ category: 'sports', slug: 'sports-post-2' }, |
|
|
]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Parameter inheritance', () => { |
|
|
it('should inherit parent parameters', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ lang: 'en' }, { lang: 'fr' }]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ category: `${params?.lang}-tech` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([ |
|
|
{ lang: 'en', category: 'en-tech' }, |
|
|
{ lang: 'fr', category: 'fr-tech' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle mixed segments (some with generateStaticParams, some without)', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ lang: 'en' }]), |
|
|
createMockSegment(), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ slug: `${params?.lang}-slug` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ lang: 'en', slug: 'en-slug' }]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Empty and undefined handling', () => { |
|
|
it('should handle empty generateStaticParams results', async () => { |
|
|
const segments: TestAppSegment[] = [createMockSegment(async () => [])] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([]) |
|
|
}) |
|
|
|
|
|
it('should handle generateStaticParams returning empty array with parent params', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ lang: 'en' }]), |
|
|
createMockSegment(async () => []), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ lang: 'en' }]) |
|
|
}) |
|
|
|
|
|
it('should handle missing parameters in parent params', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ lang: 'en' }, {}]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ category: `${params?.lang || 'default'}-tech` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([ |
|
|
{ lang: 'en', category: 'en-tech' }, |
|
|
{ category: 'default-tech' }, |
|
|
]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('FetchCache configuration', () => { |
|
|
it('should set fetchCache on store when segment has fetchCache config', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ id: '1' }], { |
|
|
fetchCache: 'force-cache', |
|
|
}), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
await generateRouteStaticParams(segments, store) |
|
|
expect(store.fetchCache).toBe('force-cache') |
|
|
}) |
|
|
|
|
|
it('should not modify fetchCache when segment has no fetchCache config', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ id: '1' }]), |
|
|
] |
|
|
const store = createMockWorkStore('force-cache') |
|
|
await generateRouteStaticParams(segments, store) |
|
|
expect(store.fetchCache).toBe('force-cache') |
|
|
}) |
|
|
|
|
|
it('should update fetchCache for multiple segments', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ category: 'tech' }], { |
|
|
fetchCache: 'force-cache', |
|
|
}), |
|
|
createMockSegment(async () => [{ slug: 'post' }], { |
|
|
fetchCache: 'default-cache', |
|
|
}), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
await generateRouteStaticParams(segments, store) |
|
|
|
|
|
expect(store.fetchCache).toBe('default-cache') |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Array parameter values', () => { |
|
|
it('should handle array parameter values', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [ |
|
|
{ slug: ['a', 'b'] }, |
|
|
{ slug: ['c', 'd', 'e'] }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ slug: ['a', 'b'] }, { slug: ['c', 'd', 'e'] }]) |
|
|
}) |
|
|
|
|
|
it('should handle mixed array and string parameters', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ lang: 'en' }]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ slug: [`${params?.lang}`, 'post'] }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ lang: 'en', slug: ['en', 'post'] }]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Deep nesting scenarios', () => { |
|
|
it('should handle deeply nested segments', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ a: '1' }]), |
|
|
createMockSegment(async ({ params }) => [{ b: `${params?.a}-2` }]), |
|
|
createMockSegment(async ({ params }) => [{ c: `${params?.b}-3` }]), |
|
|
createMockSegment(async ({ params }) => [{ d: `${params?.c}-4` }]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([{ a: '1', b: '1-2', c: '1-2-3', d: '1-2-3-4' }]) |
|
|
}) |
|
|
|
|
|
it('should handle many parameter combinations', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ x: '1' }, { x: '2' }]), |
|
|
createMockSegment(async () => [{ y: 'a' }, { y: 'b' }]), |
|
|
createMockSegment(async () => [{ z: 'i' }, { z: 'ii' }]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([ |
|
|
{ x: '1', y: 'a', z: 'i' }, |
|
|
{ x: '1', y: 'a', z: 'ii' }, |
|
|
{ x: '1', y: 'b', z: 'i' }, |
|
|
{ x: '1', y: 'b', z: 'ii' }, |
|
|
{ x: '2', y: 'a', z: 'i' }, |
|
|
{ x: '2', y: 'a', z: 'ii' }, |
|
|
{ x: '2', y: 'b', z: 'i' }, |
|
|
{ x: '2', y: 'b', z: 'ii' }, |
|
|
]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Error handling', () => { |
|
|
it('should handle generateStaticParams throwing an error', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => { |
|
|
throw new Error('Test error') |
|
|
}), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
await expect(generateRouteStaticParams(segments, store)).rejects.toThrow( |
|
|
'Test error' |
|
|
) |
|
|
}) |
|
|
|
|
|
it('should handle generateStaticParams returning a rejected promise', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => { |
|
|
return Promise.reject(new Error('Async error')) |
|
|
}), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
await expect(generateRouteStaticParams(segments, store)).rejects.toThrow( |
|
|
'Async error' |
|
|
) |
|
|
}) |
|
|
|
|
|
it('should handle partially failing generateStaticParams', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [{ category: 'tech' }]), |
|
|
createMockSegment(async ({ params }) => { |
|
|
if (params?.category === 'tech') { |
|
|
throw new Error('Tech not allowed') |
|
|
} |
|
|
return [{ slug: 'post' }] |
|
|
}), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
await expect(generateRouteStaticParams(segments, store)).rejects.toThrow( |
|
|
'Tech not allowed' |
|
|
) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Complex real-world scenarios', () => { |
|
|
it('should handle i18n routing pattern', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(async () => [ |
|
|
{ lang: 'en' }, |
|
|
{ lang: 'fr' }, |
|
|
{ lang: 'es' }, |
|
|
]), |
|
|
createMockSegment(async ({ params: _params }) => [ |
|
|
{ category: 'tech' }, |
|
|
{ category: 'sports' }, |
|
|
]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ slug: `${params?.lang}-${params?.category}-post-1` }, |
|
|
{ slug: `${params?.lang}-${params?.category}-post-2` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toHaveLength(12) |
|
|
expect(result).toContainEqual({ |
|
|
lang: 'en', |
|
|
category: 'tech', |
|
|
slug: 'en-tech-post-1', |
|
|
}) |
|
|
expect(result).toContainEqual({ |
|
|
lang: 'fr', |
|
|
category: 'sports', |
|
|
slug: 'fr-sports-post-2', |
|
|
}) |
|
|
}) |
|
|
|
|
|
it('should handle e-commerce routing pattern', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(), |
|
|
createMockSegment(async () => [ |
|
|
{ category: 'electronics' }, |
|
|
{ category: 'clothing' }, |
|
|
]), |
|
|
createMockSegment(async ({ params }) => { |
|
|
if (params?.category === 'electronics') { |
|
|
return [{ subcategory: 'phones' }, { subcategory: 'laptops' }] |
|
|
} |
|
|
return [{ subcategory: 'shirts' }, { subcategory: 'pants' }] |
|
|
}), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ product: `${params?.subcategory}-item-1` }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toEqual([ |
|
|
{ |
|
|
category: 'electronics', |
|
|
subcategory: 'phones', |
|
|
product: 'phones-item-1', |
|
|
}, |
|
|
{ |
|
|
category: 'electronics', |
|
|
subcategory: 'laptops', |
|
|
product: 'laptops-item-1', |
|
|
}, |
|
|
{ |
|
|
category: 'clothing', |
|
|
subcategory: 'shirts', |
|
|
product: 'shirts-item-1', |
|
|
}, |
|
|
{ category: 'clothing', subcategory: 'pants', product: 'pants-item-1' }, |
|
|
]) |
|
|
}) |
|
|
|
|
|
it('should handle blog with optional catch-all', async () => { |
|
|
const segments: TestAppSegment[] = [ |
|
|
createMockSegment(), |
|
|
createMockSegment(async () => [{ year: '2023' }, { year: '2024' }]), |
|
|
createMockSegment(async ({ params: _params }) => [ |
|
|
{ month: '01' }, |
|
|
{ month: '02' }, |
|
|
]), |
|
|
createMockSegment(async ({ params }) => [ |
|
|
{ slug: [`${params?.year}-${params?.month}-post`] }, |
|
|
{ slug: [] }, |
|
|
]), |
|
|
] |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toHaveLength(8) |
|
|
expect(result).toContainEqual({ |
|
|
year: '2023', |
|
|
month: '01', |
|
|
slug: ['2023-01-post'], |
|
|
}) |
|
|
expect(result).toContainEqual({ year: '2024', month: '02', slug: [] }) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('Performance considerations', () => { |
|
|
it('should handle recursive calls without stack overflow', async () => { |
|
|
const segments: TestAppSegment[] = [] |
|
|
for (let i = 0; i < 5000; i++) { |
|
|
segments.push( |
|
|
createMockSegment(async () => [{ [`param${i}`]: `value${i}` }]) |
|
|
) |
|
|
} |
|
|
const store = createMockWorkStore() |
|
|
const result = await generateRouteStaticParams(segments, store) |
|
|
expect(result).toHaveLength(1) |
|
|
expect(Object.keys(result[0])).toHaveLength(5000) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
describe('calculateFallbackMode', () => { |
|
|
it('should return NOT_FOUND when dynamic params are disabled', () => { |
|
|
const result = calculateFallbackMode(false, [], FallbackMode.PRERENDER) |
|
|
|
|
|
expect(result).toBe(FallbackMode.NOT_FOUND) |
|
|
}) |
|
|
|
|
|
it('should return NOT_FOUND when dynamic params are disabled regardless of root params', () => { |
|
|
const result = calculateFallbackMode( |
|
|
false, |
|
|
['rootParam'], |
|
|
FallbackMode.BLOCKING_STATIC_RENDER |
|
|
) |
|
|
|
|
|
expect(result).toBe(FallbackMode.NOT_FOUND) |
|
|
}) |
|
|
|
|
|
it('should return BLOCKING_STATIC_RENDER when dynamic params are enabled and root params exist', () => { |
|
|
const result = calculateFallbackMode( |
|
|
true, |
|
|
['rootParam1', 'rootParam2'], |
|
|
FallbackMode.PRERENDER |
|
|
) |
|
|
|
|
|
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER) |
|
|
}) |
|
|
|
|
|
it('should return base fallback mode when dynamic params are enabled and no root params', () => { |
|
|
const result = calculateFallbackMode(true, [], FallbackMode.PRERENDER) |
|
|
|
|
|
expect(result).toBe(FallbackMode.PRERENDER) |
|
|
}) |
|
|
|
|
|
it('should return base fallback mode when dynamic params are enabled and empty root params', () => { |
|
|
const result = calculateFallbackMode( |
|
|
true, |
|
|
[], |
|
|
FallbackMode.BLOCKING_STATIC_RENDER |
|
|
) |
|
|
|
|
|
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER) |
|
|
}) |
|
|
|
|
|
it('should return NOT_FOUND when dynamic params are enabled but no base fallback mode provided', () => { |
|
|
const result = calculateFallbackMode(true, [], undefined) |
|
|
|
|
|
expect(result).toBe(FallbackMode.NOT_FOUND) |
|
|
}) |
|
|
|
|
|
it('should prioritize root params over base fallback mode', () => { |
|
|
const result = calculateFallbackMode( |
|
|
true, |
|
|
['rootParam'], |
|
|
FallbackMode.PRERENDER |
|
|
) |
|
|
|
|
|
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER) |
|
|
}) |
|
|
|
|
|
it('should handle single root param correctly', () => { |
|
|
const result = calculateFallbackMode( |
|
|
true, |
|
|
['singleParam'], |
|
|
FallbackMode.PRERENDER |
|
|
) |
|
|
|
|
|
expect(result).toBe(FallbackMode.BLOCKING_STATIC_RENDER) |
|
|
}) |
|
|
}) |
|
|
|