2024-01-29 16:44:54 +00:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2023-07-04 05:24:50 +00:00
|
|
|
import type { NuxtPage } from 'nuxt/schema'
|
2024-01-29 16:44:54 +00:00
|
|
|
import { generateRoutesFromFiles, normalizeRoutes, pathToNitroGlob } from '../src/pages/utils'
|
2022-02-07 11:32:04 +00:00
|
|
|
import { generateRouteKey } from '../src/pages/runtime/utils'
|
2021-10-20 18:12:55 +00:00
|
|
|
|
|
|
|
describe('pages:generateRoutesFromFiles', () => {
|
|
|
|
const pagesDir = 'pages'
|
2023-09-12 09:46:35 +00:00
|
|
|
const layerDir = 'layer/pages'
|
2024-01-29 16:44:54 +00:00
|
|
|
const DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const
|
|
|
|
|
|
|
|
vi.mock('knitwork', async (original) => {
|
|
|
|
return {
|
|
|
|
...(await original<typeof import('knitwork')>()),
|
|
|
|
'genArrayFromRaw': (val: any) => val,
|
|
|
|
'genSafeVariableName': (..._args: string[]) => {
|
|
|
|
return 'mock'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-07-04 05:24:50 +00:00
|
|
|
const tests: Array<{
|
|
|
|
description: string
|
2024-01-29 16:44:54 +00:00
|
|
|
files?: Array<{ path: string; template?: string; }>
|
2023-07-04 05:24:50 +00:00
|
|
|
output?: NuxtPage[]
|
2024-01-29 16:44:54 +00:00
|
|
|
normalized?: Record<string, any>[]
|
2023-07-04 05:24:50 +00:00
|
|
|
error?: string
|
|
|
|
}> = [
|
2021-10-20 18:12:55 +00:00
|
|
|
{
|
|
|
|
description: 'should generate correct routes for index pages',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/index.vue` },
|
|
|
|
{ path: `${pagesDir}/parent/index.vue` },
|
|
|
|
{ path: `${pagesDir}/parent/child/index.vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'index',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'parent',
|
|
|
|
path: '/parent',
|
|
|
|
file: `${pagesDir}/parent/index.vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'parent-child',
|
|
|
|
path: '/parent/child',
|
|
|
|
file: `${pagesDir}/parent/child/index.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should generate correct routes for parent/child',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/parent.vue` },
|
|
|
|
{ path: `${pagesDir}/parent/child.vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'parent',
|
|
|
|
path: '/parent',
|
|
|
|
file: `${pagesDir}/parent.vue`,
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: 'parent-child',
|
|
|
|
path: 'child',
|
|
|
|
file: `${pagesDir}/parent/child.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2023-02-13 22:56:39 +00:00
|
|
|
{
|
|
|
|
description: 'should not generate colliding route names when hyphens are in file name',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/parent/[child].vue` },
|
|
|
|
{ path: `${pagesDir}/parent-[child].vue` }
|
2023-02-13 22:56:39 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'parent-child',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/parent/:child()',
|
2023-02-13 22:56:39 +00:00
|
|
|
file: `${pagesDir}/parent/[child].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'parent-child',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/parent-:child()',
|
2023-02-13 22:56:39 +00:00
|
|
|
file: `${pagesDir}/parent-[child].vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2022-08-01 07:51:46 +00:00
|
|
|
{
|
|
|
|
description: 'should generate correct id for catchall (order 1)',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/[...stories].vue` },
|
|
|
|
{ path: `${pagesDir}/stories/[id].vue` }
|
2022-08-01 07:51:46 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'stories',
|
|
|
|
path: '/:stories(.*)*',
|
|
|
|
file: `${pagesDir}/[...stories].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'stories-id',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/stories/:id()',
|
2022-08-01 07:51:46 +00:00
|
|
|
file: `${pagesDir}/stories/[id].vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should generate correct id for catchall (order 2)',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/stories/[id].vue` },
|
|
|
|
{ path: `${pagesDir}/[...stories].vue` }
|
2022-08-01 07:51:46 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'stories-id',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/stories/:id()',
|
2022-08-01 07:51:46 +00:00
|
|
|
file: `${pagesDir}/stories/[id].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'stories',
|
|
|
|
path: '/:stories(.*)*',
|
|
|
|
file: `${pagesDir}/[...stories].vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2021-10-20 18:12:55 +00:00
|
|
|
{
|
|
|
|
description: 'should generate correct route for snake_case file',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/snake_case.vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'snake_case',
|
|
|
|
path: '/snake_case',
|
|
|
|
file: `${pagesDir}/snake_case.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should generate correct route for kebab-case file',
|
2023-07-04 05:24:50 +00:00
|
|
|
files: [{ path: `${pagesDir}/kebab-case.vue` }],
|
2021-10-20 18:12:55 +00:00
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'kebab-case',
|
|
|
|
path: '/kebab-case',
|
|
|
|
file: `${pagesDir}/kebab-case.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should generate correct dynamic routes',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/index.vue` },
|
|
|
|
{ path: `${pagesDir}/[slug].vue` },
|
|
|
|
{ path: `${pagesDir}/[[foo]]` },
|
|
|
|
{ path: `${pagesDir}/[[foo]]/index.vue` },
|
2023-09-11 08:13:24 +00:00
|
|
|
{ path: `${pagesDir}/optional/[[opt]].vue` },
|
|
|
|
{ path: `${pagesDir}/optional/prefix-[[opt]].vue` },
|
|
|
|
{ path: `${pagesDir}/optional/[[opt]]-postfix.vue` },
|
|
|
|
{ path: `${pagesDir}/optional/prefix-[[opt]]-postfix.vue` },
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/[bar]/index.vue` },
|
|
|
|
{ path: `${pagesDir}/nonopt/[slug].vue` },
|
|
|
|
{ path: `${pagesDir}/opt/[[slug]].vue` },
|
|
|
|
{ path: `${pagesDir}/[[sub]]/route-[slug].vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
2022-06-09 13:03:08 +00:00
|
|
|
name: 'index',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
children: []
|
|
|
|
},
|
2022-06-09 12:09:34 +00:00
|
|
|
{
|
|
|
|
children: [],
|
2022-06-10 13:15:53 +00:00
|
|
|
name: 'slug',
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/[slug].vue`,
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/:slug()'
|
2022-06-09 12:09:34 +00:00
|
|
|
},
|
2022-04-26 16:10:05 +00:00
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
|
|
|
|
name: 'foo',
|
|
|
|
path: '',
|
|
|
|
file: `${pagesDir}/[[foo]]/index.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/[[foo]]`,
|
2022-04-26 16:10:05 +00:00
|
|
|
path: '/:foo?'
|
|
|
|
},
|
2023-09-11 08:13:24 +00:00
|
|
|
{
|
|
|
|
children: [],
|
|
|
|
path: '/optional/:opt?',
|
|
|
|
name: 'optional-opt',
|
|
|
|
file: `${pagesDir}/optional/[[opt]].vue`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [],
|
|
|
|
path: '/optional/prefix-:opt?',
|
|
|
|
name: 'optional-prefix-opt',
|
|
|
|
file: `${pagesDir}/optional/prefix-[[opt]].vue`
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
children: [],
|
|
|
|
path: '/optional/:opt?-postfix',
|
|
|
|
name: 'optional-opt-postfix',
|
|
|
|
file: `${pagesDir}/optional/[[opt]]-postfix.vue`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [],
|
|
|
|
path: '/optional/prefix-:opt?-postfix',
|
|
|
|
name: 'optional-prefix-opt-postfix',
|
|
|
|
file: `${pagesDir}/optional/prefix-[[opt]]-postfix.vue`
|
|
|
|
},
|
2022-04-26 16:10:05 +00:00
|
|
|
{
|
2022-06-10 13:15:53 +00:00
|
|
|
children: [],
|
|
|
|
name: 'bar',
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/[bar]/index.vue`,
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/:bar()'
|
2022-06-10 13:15:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'nonopt-slug',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/nonopt/:slug()',
|
2022-06-10 13:15:53 +00:00
|
|
|
file: `${pagesDir}/nonopt/[slug].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'opt-slug',
|
|
|
|
path: '/opt/:slug?',
|
|
|
|
file: `${pagesDir}/opt/[[slug]].vue`,
|
2021-10-20 18:12:55 +00:00
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'sub-route-slug',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/:sub?/route-:slug()',
|
2022-04-26 16:10:05 +00:00
|
|
|
file: `${pagesDir}/[[sub]]/route-[slug].vue`,
|
2021-10-20 18:12:55 +00:00
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should generate correct catch-all route',
|
2023-07-04 05:24:50 +00:00
|
|
|
files: [{ path: `${pagesDir}/[...slug].vue` }, { path: `${pagesDir}/index.vue` }],
|
2021-10-20 18:12:55 +00:00
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'slug',
|
|
|
|
path: '/:slug(.*)*',
|
|
|
|
file: `${pagesDir}/[...slug].vue`,
|
|
|
|
children: []
|
2022-06-10 13:15:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'index',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
children: []
|
2021-10-20 18:12:55 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should throw unfinished param error for dynamic route',
|
2023-07-04 05:24:50 +00:00
|
|
|
files: [{ path: `${pagesDir}/[slug.vue` }],
|
2021-10-20 18:12:55 +00:00
|
|
|
error: 'Unfinished param "slug"'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should throw empty param error for dynamic route',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/[].vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
error: 'Empty param'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should only allow "_" & "." as special character for dynamic route',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/[a1_1a].vue` },
|
|
|
|
{ path: `${pagesDir}/[b2.2b].vue` },
|
|
|
|
{ path: `${pagesDir}/[b2]_[2b].vue` },
|
|
|
|
{ path: `${pagesDir}/[[c3@3c]].vue` },
|
|
|
|
{ path: `${pagesDir}/[[d4-4d]].vue` }
|
2021-10-20 18:12:55 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'a1_1a',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/:a1_1a()',
|
2021-10-20 18:12:55 +00:00
|
|
|
file: `${pagesDir}/[a1_1a].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'b2.2b',
|
2023-04-03 09:56:44 +00:00
|
|
|
path: '/:b2.2b()',
|
2021-10-20 18:12:55 +00:00
|
|
|
file: `${pagesDir}/[b2.2b].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
2023-04-03 09:56:44 +00:00
|
|
|
{
|
|
|
|
name: 'b2_2b',
|
|
|
|
path: '/:b2()_:2b()',
|
|
|
|
file: `${pagesDir}/[b2]_[2b].vue`,
|
|
|
|
children: []
|
|
|
|
},
|
2021-10-20 18:12:55 +00:00
|
|
|
{
|
|
|
|
name: 'c33c',
|
|
|
|
path: '/:c33c?',
|
2022-04-26 16:10:05 +00:00
|
|
|
file: `${pagesDir}/[[c3@3c]].vue`,
|
2021-10-20 18:12:55 +00:00
|
|
|
children: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'd44d',
|
|
|
|
path: '/:d44d?',
|
2022-04-26 16:10:05 +00:00
|
|
|
file: `${pagesDir}/[[d4-4d]].vue`,
|
2021-10-20 18:12:55 +00:00
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
2023-06-25 16:40:30 +00:00
|
|
|
},
|
2023-07-04 05:24:50 +00:00
|
|
|
{
|
|
|
|
description: 'should properly override route name if definePageMeta name override is defined.',
|
|
|
|
files: [
|
|
|
|
{
|
|
|
|
path: `${pagesDir}/index.vue`,
|
|
|
|
template: `
|
|
|
|
<script setup lang="ts">
|
|
|
|
definePageMeta({
|
|
|
|
name: 'home'
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2023-06-25 16:40:30 +00:00
|
|
|
{
|
|
|
|
description: 'should allow pages with `:` in their path',
|
|
|
|
files: [
|
2023-07-04 05:24:50 +00:00
|
|
|
{ path: `${pagesDir}/test:name.vue` }
|
2023-06-25 16:40:30 +00:00
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'test:name',
|
|
|
|
path: '/test\\:name',
|
|
|
|
file: `${pagesDir}/test:name.vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
2023-09-11 10:50:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should not merge required param as a child of optional param',
|
|
|
|
files: [
|
|
|
|
{ path: `${pagesDir}/[[foo]].vue` },
|
|
|
|
{ path: `${pagesDir}/[foo].vue` }
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
path: '/:foo?',
|
|
|
|
file: `${pagesDir}/[[foo]].vue`,
|
|
|
|
children: [
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
path: '/:foo()',
|
|
|
|
file: `${pagesDir}/[foo].vue`,
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should correctly merge nested routes',
|
|
|
|
files: [
|
|
|
|
{ path: `${pagesDir}/param.vue` },
|
2023-09-12 09:46:35 +00:00
|
|
|
{ path: `${layerDir}/param/index.vue` },
|
2023-09-11 10:50:19 +00:00
|
|
|
{ path: `${pagesDir}/param/index/index.vue` },
|
2023-09-12 09:46:35 +00:00
|
|
|
{ path: `${layerDir}/param/index/sibling.vue` },
|
2023-09-11 10:50:19 +00:00
|
|
|
{ path: `${pagesDir}/wrapper-expose/other.vue` },
|
2023-09-12 09:46:35 +00:00
|
|
|
{ path: `${layerDir}/wrapper-expose/other/index.vue` },
|
2023-09-11 10:50:19 +00:00
|
|
|
{ path: `${pagesDir}/wrapper-expose/other/sibling.vue` },
|
|
|
|
{ path: `${pagesDir}/param/sibling.vue` }
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/param/index/index.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
name: 'param-index',
|
|
|
|
path: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${layerDir}/param/index/sibling.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
name: 'param-index-sibling',
|
|
|
|
path: 'sibling'
|
|
|
|
}
|
|
|
|
],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${layerDir}/param/index.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
path: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/param/sibling.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
name: 'param-sibling',
|
|
|
|
path: 'sibling'
|
|
|
|
}
|
|
|
|
],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/param.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
path: '/param'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${layerDir}/wrapper-expose/other/index.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
name: 'wrapper-expose-other',
|
|
|
|
path: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
children: [],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/wrapper-expose/other/sibling.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
name: 'wrapper-expose-other-sibling',
|
|
|
|
path: 'sibling'
|
|
|
|
}
|
|
|
|
],
|
2023-09-12 09:46:35 +00:00
|
|
|
file: `${pagesDir}/wrapper-expose/other.vue`,
|
2023-09-11 10:50:19 +00:00
|
|
|
path: '/wrapper-expose/other'
|
|
|
|
}
|
|
|
|
]
|
2023-09-26 00:09:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should handle trailing slashes with index routes',
|
|
|
|
files: [
|
|
|
|
{ path: `${pagesDir}/index/index.vue` },
|
|
|
|
{ path: `${pagesDir}/index/index/all.vue` }
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [],
|
|
|
|
file: `${pagesDir}/index/index/all.vue`,
|
|
|
|
name: 'index-index-all',
|
|
|
|
path: 'all'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
file: `${pagesDir}/index/index.vue`,
|
|
|
|
name: 'index',
|
|
|
|
path: '/'
|
|
|
|
}
|
2024-01-29 16:44:54 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should use fallbacks when normalized with `overrideMeta: true`',
|
|
|
|
files: [
|
|
|
|
{
|
|
|
|
path: `${pagesDir}/index.vue`,
|
|
|
|
template: `
|
|
|
|
<script setup lang="ts">
|
|
|
|
const routeName = ref('home')
|
|
|
|
const routeAliases = ref(['sweet-home'])
|
|
|
|
definePageMeta({
|
|
|
|
name: routeName.value,
|
|
|
|
alias: routeAliases.value,
|
|
|
|
hello: 'world',
|
|
|
|
redirect: () => '/'
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'index',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
meta: { [DYNAMIC_META_KEY]: new Set(['name', 'alias', 'redirect', 'meta']) },
|
|
|
|
children: []
|
|
|
|
}
|
2023-09-26 00:09:12 +00:00
|
|
|
]
|
2024-01-29 16:44:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should extract serializable values and override fallback when normalized with `overrideMeta: true`',
|
|
|
|
files: [
|
|
|
|
{
|
|
|
|
path: `${pagesDir}/index.vue`,
|
|
|
|
template: `
|
|
|
|
<script setup lang="ts">
|
|
|
|
definePageMeta({
|
|
|
|
name: 'home',
|
|
|
|
alias: ['sweet-home'],
|
|
|
|
redirect: '/',
|
|
|
|
hello: 'world'
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
path: '/',
|
|
|
|
file: `${pagesDir}/index.vue`,
|
|
|
|
alias: ['sweet-home'],
|
|
|
|
redirect: '/',
|
|
|
|
children: [],
|
|
|
|
meta: { [DYNAMIC_META_KEY]: new Set(['meta']) },
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'route without file',
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
path: '/',
|
|
|
|
alias: ['sweet-home'],
|
|
|
|
meta: { hello: 'world' },
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2021-10-20 18:12:55 +00:00
|
|
|
]
|
|
|
|
|
2024-01-29 16:44:54 +00:00
|
|
|
const normalizedResults: Record<string, any> = {}
|
|
|
|
const normalizedOverrideMetaResults: Record<string, any> = {}
|
|
|
|
|
2021-10-20 18:12:55 +00:00
|
|
|
for (const test of tests) {
|
|
|
|
it(test.description, async () => {
|
2023-07-04 05:24:50 +00:00
|
|
|
|
2023-09-11 10:50:19 +00:00
|
|
|
let result
|
2024-01-29 16:44:54 +00:00
|
|
|
if (test.files) {
|
|
|
|
const vfs = Object.fromEntries(
|
|
|
|
test.files.map(file => [file.path, 'template' in file ? file.template : ''])
|
|
|
|
) as Record<string, string>
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = await generateRoutesFromFiles(test.files.map(file => ({
|
|
|
|
absolutePath: file.path,
|
|
|
|
relativePath: file.path.replace(/^(pages|layer\/pages)\//, '')
|
|
|
|
})), { shouldExtractBuildMeta: true, vfs })
|
|
|
|
} catch (error: any) {
|
|
|
|
expect(error.message).toEqual(test.error)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = test.output ?? []
|
2021-10-20 18:12:55 +00:00
|
|
|
}
|
2024-01-29 16:44:54 +00:00
|
|
|
|
2023-09-11 10:50:19 +00:00
|
|
|
if (result) {
|
|
|
|
expect(result).toEqual(test.output)
|
2024-01-29 16:44:54 +00:00
|
|
|
normalizedResults[test.description] = normalizeRoutes(result, new Set()).routes
|
|
|
|
normalizedOverrideMetaResults[test.description] = normalizeRoutes(result, new Set(), true).routes
|
2023-09-11 10:50:19 +00:00
|
|
|
}
|
2021-10-20 18:12:55 +00:00
|
|
|
})
|
|
|
|
}
|
2024-01-29 16:44:54 +00:00
|
|
|
|
|
|
|
it('should consistently normalize routes', async () => {
|
|
|
|
await expect(normalizedResults).toMatchFileSnapshot('./__snapshots__/pages-override-meta-disabled.test.ts.snap')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should consistently normalize routes when overriding meta', async () => {
|
|
|
|
await expect(normalizedOverrideMetaResults).toMatchFileSnapshot('./__snapshots__/pages-override-meta-enabled.test.ts.snap')
|
|
|
|
})
|
2021-10-20 18:12:55 +00:00
|
|
|
})
|
2022-02-07 11:32:04 +00:00
|
|
|
|
|
|
|
describe('pages:generateRouteKey', () => {
|
|
|
|
const defaultComponent = { type: {} }
|
|
|
|
const getRouteProps = (matchedRoute = {}) => ({
|
|
|
|
Component: defaultComponent,
|
|
|
|
route: {
|
|
|
|
meta: { key: 'route-meta-key' },
|
|
|
|
params: {
|
|
|
|
id: 'foo',
|
|
|
|
optional: 'bar',
|
|
|
|
array: ['a', 'b']
|
|
|
|
},
|
|
|
|
matched: [
|
|
|
|
{
|
|
|
|
components: { default: {} },
|
|
|
|
meta: { key: 'other-meta-key' }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
components: { default: defaultComponent.type },
|
|
|
|
meta: { key: 'matched-meta-key' },
|
|
|
|
...matchedRoute
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}) as any
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{ description: 'should handle overrides', override: 'key', route: getRouteProps(), output: 'key' },
|
2022-08-22 10:12:02 +00:00
|
|
|
{ description: 'should handle overrides', override: (route: any) => route.meta.key as string, route: getRouteProps(), output: 'route-meta-key' },
|
2022-02-07 11:32:04 +00:00
|
|
|
{ description: 'should handle overrides', override: false as any, route: getRouteProps(), output: false },
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes without keys',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:id',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/foo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes without keys',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:id(\\d+)',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/foo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with optional params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:optional?',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/bar'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with optional params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:optional(\\d+)?',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/bar'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with optional params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:undefined(\\d+)?',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with array params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/:array+',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/a,b'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with array params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:array*',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/a,b'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should key dynamic routes with array params',
|
|
|
|
route: getRouteProps({
|
|
|
|
path: '/test/:other*',
|
|
|
|
meta: {}
|
|
|
|
}),
|
|
|
|
output: '/test/'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const test of tests) {
|
|
|
|
it(test.description, () => {
|
2022-11-21 13:03:22 +00:00
|
|
|
expect(generateRouteKey(test.route, test.override)).to.deep.equal(test.output)
|
2022-02-07 11:32:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2023-08-23 20:38:17 +00:00
|
|
|
|
|
|
|
const pathToNitroGlobTests = {
|
|
|
|
'/': '/',
|
|
|
|
'/:id': '/**',
|
|
|
|
'/:id()': '/**',
|
|
|
|
'/:id?': '/**',
|
|
|
|
'/some-:id?': '/**',
|
|
|
|
'/other/some-:id?': '/other/**',
|
|
|
|
'/other/some-:id()-more': '/other/**',
|
|
|
|
'/other/nested': '/other/nested'
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('pages:pathToNitroGlob', () => {
|
|
|
|
it.each(Object.entries(pathToNitroGlobTests))('should convert %s to %s', (path, expected) => {
|
|
|
|
expect(pathToNitroGlob(path)).to.equal(expected)
|
|
|
|
})
|
|
|
|
})
|