2022-04-15 15:19:05 +00:00
|
|
|
import { resolve } from 'node:path'
|
2023-05-15 12:34:04 +00:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2023-02-13 22:42:04 +00:00
|
|
|
import type { ComponentsDir } from 'nuxt/schema'
|
2021-11-15 16:22:46 +00:00
|
|
|
|
2023-05-15 12:34:04 +00:00
|
|
|
import { resolveComponentName, scanComponents } from '../src/components/scan'
|
2023-03-11 21:16:01 +00:00
|
|
|
|
2021-11-15 16:22:46 +00:00
|
|
|
const fixtureDir = resolve(__dirname, 'fixture')
|
2022-08-22 10:12:02 +00:00
|
|
|
const rFixture = (...p: string[]) => resolve(fixtureDir, ...p)
|
2021-11-15 16:22:46 +00:00
|
|
|
|
2022-02-28 16:11:46 +00:00
|
|
|
vi.mock('@nuxt/kit', () => ({
|
|
|
|
isIgnored: () => false
|
|
|
|
}))
|
|
|
|
|
2021-11-15 16:22:46 +00:00
|
|
|
const dirs: ComponentsDir[] = [
|
2022-11-24 12:24:14 +00:00
|
|
|
{
|
|
|
|
path: rFixture('components/islands'),
|
|
|
|
enabled: true,
|
|
|
|
extensions: [
|
|
|
|
'vue'
|
|
|
|
],
|
|
|
|
pattern: '**/*.{vue,}',
|
|
|
|
ignore: [
|
|
|
|
'**/*.stories.{js,ts,jsx,tsx}',
|
|
|
|
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}',
|
|
|
|
'**/*.d.ts'
|
|
|
|
],
|
|
|
|
transpile: false,
|
|
|
|
island: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: rFixture('components/global'),
|
|
|
|
enabled: true,
|
|
|
|
extensions: [
|
|
|
|
'vue'
|
|
|
|
],
|
|
|
|
pattern: '**/*.{vue,}',
|
|
|
|
ignore: [
|
|
|
|
'**/*.stories.{js,ts,jsx,tsx}',
|
|
|
|
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}',
|
|
|
|
'**/*.d.ts'
|
|
|
|
],
|
|
|
|
transpile: false,
|
|
|
|
global: true
|
|
|
|
},
|
2021-11-15 16:22:46 +00:00
|
|
|
{
|
|
|
|
path: rFixture('components'),
|
|
|
|
enabled: true,
|
|
|
|
extensions: [
|
|
|
|
'vue'
|
|
|
|
],
|
|
|
|
pattern: '**/*.{vue,}',
|
|
|
|
ignore: [
|
|
|
|
'**/*.stories.{js,ts,jsx,tsx}',
|
|
|
|
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}',
|
|
|
|
'**/*.d.ts'
|
|
|
|
],
|
|
|
|
transpile: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: rFixture('components'),
|
|
|
|
enabled: true,
|
|
|
|
extensions: [
|
|
|
|
'vue'
|
|
|
|
],
|
|
|
|
pattern: '**/*.{vue,}',
|
|
|
|
ignore: [
|
|
|
|
'**/*.stories.{js,ts,jsx,tsx}',
|
|
|
|
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}',
|
|
|
|
'**/*.d.ts'
|
|
|
|
],
|
|
|
|
transpile: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: rFixture('components'),
|
|
|
|
extensions: [
|
|
|
|
'vue'
|
|
|
|
],
|
|
|
|
prefix: 'nuxt',
|
|
|
|
enabled: true,
|
|
|
|
pattern: '**/*.{vue,}',
|
|
|
|
ignore: [
|
|
|
|
'**/*.stories.{js,ts,jsx,tsx}',
|
|
|
|
'**/*{M,.m,-m}ixin.{js,ts,jsx,tsx}',
|
|
|
|
'**/*.d.ts'
|
|
|
|
],
|
|
|
|
transpile: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const expectedComponents = [
|
2022-11-24 12:24:14 +00:00
|
|
|
{
|
|
|
|
chunkName: 'components/isle-server',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
|
|
|
island: true,
|
|
|
|
kebabName: 'isle',
|
|
|
|
mode: 'server',
|
|
|
|
pascalName: 'Isle',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2022-11-24 12:24:14 +00:00
|
|
|
shortPath: 'components/islands/Isle.vue'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chunkName: 'components/glob',
|
|
|
|
export: 'default',
|
|
|
|
global: true,
|
|
|
|
island: undefined,
|
|
|
|
kebabName: 'glob',
|
|
|
|
mode: 'all',
|
|
|
|
pascalName: 'Glob',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2022-11-24 12:24:14 +00:00
|
|
|
shortPath: 'components/global/Glob.vue'
|
|
|
|
},
|
2021-11-15 16:22:46 +00:00
|
|
|
{
|
2022-04-19 19:13:55 +00:00
|
|
|
mode: 'all',
|
2021-11-15 16:22:46 +00:00
|
|
|
pascalName: 'HelloWorld',
|
|
|
|
kebabName: 'hello-world',
|
|
|
|
chunkName: 'components/hello-world',
|
|
|
|
shortPath: 'components/HelloWorld.vue',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
2022-11-24 12:24:14 +00:00
|
|
|
island: undefined,
|
2021-11-15 16:22:46 +00:00
|
|
|
prefetch: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
preload: false,
|
|
|
|
priority: 1
|
2021-11-15 16:22:46 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-19 19:13:55 +00:00
|
|
|
mode: 'client',
|
2021-11-15 16:22:46 +00:00
|
|
|
pascalName: 'Nuxt3',
|
|
|
|
kebabName: 'nuxt3',
|
2022-04-19 19:13:55 +00:00
|
|
|
chunkName: 'components/nuxt3-client',
|
|
|
|
shortPath: 'components/Nuxt3.client.vue',
|
2021-11-15 16:22:46 +00:00
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
2022-11-24 12:24:14 +00:00
|
|
|
island: undefined,
|
2021-11-15 16:22:46 +00:00
|
|
|
prefetch: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
preload: false,
|
|
|
|
priority: 1
|
2021-11-15 16:22:46 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-19 19:13:55 +00:00
|
|
|
mode: 'server',
|
|
|
|
pascalName: 'Nuxt3',
|
|
|
|
kebabName: 'nuxt3',
|
|
|
|
chunkName: 'components/nuxt3-server',
|
|
|
|
shortPath: 'components/Nuxt3.server.vue',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
2022-11-24 12:24:14 +00:00
|
|
|
island: undefined,
|
2022-04-19 19:13:55 +00:00
|
|
|
prefetch: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
preload: false,
|
|
|
|
priority: 1
|
2022-04-19 19:13:55 +00:00
|
|
|
},
|
2023-02-16 12:47:42 +00:00
|
|
|
{
|
|
|
|
chunkName: 'components/client-component-with-props',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
|
|
|
island: undefined,
|
|
|
|
kebabName: 'client-component-with-props',
|
|
|
|
mode: 'all',
|
|
|
|
pascalName: 'ClientComponentWithProps',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2023-02-16 12:47:42 +00:00
|
|
|
shortPath: 'components/client/ComponentWithProps.vue'
|
|
|
|
},
|
2023-02-08 08:59:57 +00:00
|
|
|
{
|
|
|
|
chunkName: 'components/client-with-client-only-setup',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
|
|
|
island: undefined,
|
|
|
|
kebabName: 'client-with-client-only-setup',
|
|
|
|
mode: 'all',
|
|
|
|
pascalName: 'ClientWithClientOnlySetup',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2023-02-08 08:59:57 +00:00
|
|
|
shortPath: 'components/client/WithClientOnlySetup.vue'
|
|
|
|
},
|
2022-04-19 19:13:55 +00:00
|
|
|
{
|
|
|
|
mode: 'server',
|
2021-11-15 16:22:46 +00:00
|
|
|
pascalName: 'ParentFolder',
|
|
|
|
kebabName: 'parent-folder',
|
2022-04-19 19:13:55 +00:00
|
|
|
chunkName: 'components/parent-folder-server',
|
|
|
|
shortPath: 'components/parent-folder/index.server.vue',
|
2021-11-15 16:22:46 +00:00
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
2022-11-24 12:24:14 +00:00
|
|
|
island: undefined,
|
2021-11-15 16:22:46 +00:00
|
|
|
prefetch: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
preload: false,
|
|
|
|
priority: 1
|
2022-11-24 12:24:14 +00:00
|
|
|
},
|
2023-05-15 12:34:04 +00:00
|
|
|
{
|
|
|
|
chunkName: 'components/same-name-same',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
|
|
|
island: undefined,
|
|
|
|
kebabName: 'same-name-same',
|
|
|
|
mode: 'all',
|
|
|
|
pascalName: 'SameNameSame',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
|
|
|
priority: 1,
|
|
|
|
shortPath: 'components/same-name/same/Same.vue'
|
|
|
|
},
|
2022-11-24 12:24:14 +00:00
|
|
|
{
|
|
|
|
chunkName: 'components/some-glob',
|
|
|
|
export: 'default',
|
|
|
|
global: true,
|
|
|
|
island: undefined,
|
|
|
|
kebabName: 'some-glob',
|
|
|
|
mode: 'all',
|
|
|
|
pascalName: 'SomeGlob',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2022-11-24 12:24:14 +00:00
|
|
|
shortPath: 'components/some-glob.global.vue'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chunkName: 'components/some-server',
|
|
|
|
export: 'default',
|
|
|
|
global: undefined,
|
|
|
|
island: true,
|
|
|
|
kebabName: 'some',
|
|
|
|
mode: 'server',
|
|
|
|
pascalName: 'Some',
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 1,
|
2022-11-24 12:24:14 +00:00
|
|
|
shortPath: 'components/some.island.vue'
|
2021-11-15 16:22:46 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const srcDir = rFixture('.')
|
|
|
|
|
|
|
|
it('components:scanComponents', async () => {
|
|
|
|
const scannedComponents = await scanComponents(dirs, srcDir)
|
|
|
|
for (const c of scannedComponents) {
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error filePath is not optional but we don't want it to be in the snapshot
|
2021-11-15 16:22:46 +00:00
|
|
|
delete c.filePath
|
|
|
|
}
|
|
|
|
expect(scannedComponents).deep.eq(expectedComponents)
|
|
|
|
})
|
2023-05-15 12:34:04 +00:00
|
|
|
|
|
|
|
const tests: Array<[string, string[], string]> = [
|
|
|
|
['WithClientOnlySetup', ['Client'], 'ClientWithClientOnlySetup'],
|
|
|
|
['ItemHolderItem', ['Item', 'Holder', 'Item'], 'ItemHolderItem'],
|
|
|
|
['Item', ['Item'], 'Item'],
|
|
|
|
['Item', ['Item', 'Item'], 'Item'],
|
|
|
|
['ItemTest', ['Item', 'Test'], 'ItemTest'],
|
|
|
|
['ThingItemTest', ['Item', 'Thing'], 'ItemThingItemTest'],
|
|
|
|
['Item', ['Thing', 'Item'], 'ThingItem'],
|
|
|
|
['Item', ['Item', 'Holder', 'Item'], 'ItemHolderItem'],
|
|
|
|
['ItemHolder', ['Item', 'Holder', 'Item'], 'ItemHolderItemHolder'],
|
|
|
|
['ThingItemTest', ['Item', 'Thing', 'Foo'], 'ItemThingFooThingItemTest'],
|
|
|
|
['ItemIn', ['Item', 'Holder', 'Item', 'In'], 'ItemHolderItemIn'],
|
|
|
|
['Item', ['Item', 'Holder', 'Test'], 'ItemHolderTestItem'],
|
2023-05-19 07:11:46 +00:00
|
|
|
['ItemHolderItem', ['Item', 'Holder', 'Item', 'Holder'], 'ItemHolderItemHolderItem'],
|
|
|
|
['Icones', ['Icon'], 'IconIcones'],
|
|
|
|
['Icon', ['Icones'], 'IconesIcon'],
|
|
|
|
['IconHolder', ['IconHolder'], 'IconHolder']
|
2023-05-15 12:34:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
describe('components:resolveComponentName', () => {
|
|
|
|
it.each(tests)('resolves %s with prefix parts %s and filename %s', (fileName, prefixParts: string[], result) => {
|
|
|
|
expect(resolveComponentName(fileName, prefixParts)).toBe(result)
|
|
|
|
})
|
|
|
|
})
|