-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlazy-hydration-plugin.test.ts
More file actions
225 lines (208 loc) · 9.14 KB
/
lazy-hydration-plugin.test.ts
File metadata and controls
225 lines (208 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, it, expect, vi } from 'vitest'
import type { Plugin } from 'vite'
import type { HookFnMap, ObjectHook, UnpluginBuildContext, UnpluginContext } from 'unplugin'
import { LazyLoadHintPlugin } from '../../../src/plugins/lazy-load'
import { useNuxt } from '@nuxt/kit'
vi.mock('@nuxt/kit', () => ({
useNuxt: vi.fn(() => ({
apps: {
default: {
components: [],
},
},
hook: vi.fn(),
})),
}))
const plugin = LazyLoadHintPlugin.vite() as Plugin
const transform = (plugin.transform as ObjectHook<any, any>).handler
const unpluginCtx: UnpluginBuildContext & UnpluginContext = {
addWatchFile: vi.fn(),
emitFile: vi.fn(),
getWatchFiles: vi.fn(),
parse: vi.fn(),
getNativeBuildContext: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
}
describe('LazyLoadHintPlugin', () => {
describe('default imports', () => {
it('should wrap a default import from a .vue file', async () => {
const code = `import MyComp from './MyComp.vue'\nexport default { components: { MyComp } }`
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain('import __original_MyComp from \'./MyComp.vue\'')
expect(result.code).toContain(
`const MyComp = __wrapImportedComponent(__original_MyComp, 'MyComp', './MyComp.vue'`,
)
})
it('should handle multiple default imports from different .vue files', async () => {
const code = [
`import CompA from './CompA.vue'`,
`import CompB from './CompB.vue'`,
`export default { components: { CompA, CompB } }`,
].join('\n')
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain('import __original_CompA from \'./CompA.vue\'')
expect(result.code).toContain('import __original_CompB from \'./CompB.vue\'')
expect(result.code).toContain(`__wrapImportedComponent(__original_CompA, 'CompA'`)
expect(result.code).toContain(`__wrapImportedComponent(__original_CompB, 'CompB'`)
})
})
describe('named imports', () => {
it('should wrap a named import from a .vue file', async () => {
const code = `import { MyComp } from './MyComp.vue'\nexport default { components: { MyComp } }`
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain('import { MyComp as __original_MyComp } from \'./MyComp.vue\'')
expect(result.code).toContain(
`const MyComp = __wrapImportedComponent(__original_MyComp, 'MyComp', './MyComp.vue'`,
)
})
it('should wrap an aliased named import from a .vue file', async () => {
const code = `import { default as AliasComp } from './MyComp.vue'\nexport default { components: { AliasComp } }`
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain('__original_AliasComp')
expect(result.code).toContain(
`__wrapImportedComponent(__original_AliasComp, 'AliasComp', './MyComp.vue'`,
)
})
})
describe('_sfc_main wrapping', () => {
it('should wrap _sfc_main with __wrapMainComponent when present', async () => {
const code = [
`import ChildComp from './ChildComp.vue'`,
`const _sfc_main = {}`,
`export default _sfc_main`,
].join('\n')
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain('const _sfc_main_wrapped = __wrapMainComponent(_sfc_main,')
expect(result.code).toContain('export default _sfc_main_wrapped')
expect(result.code).toContain(`componentName: 'ChildComp'`)
expect(result.code).toContain(`importSource: './ChildComp.vue'`)
})
})
describe('skipped imports', () => {
it('should not transform type-only imports', async () => {
const code = `import type MyComp from './MyComp.vue'\nexport default {}`
const result = await transform(code, '/src/Parent.vue')
expect(result).toBeUndefined()
})
it('should return undefined when there are no .vue imports at all', async () => {
const code = `const x = 1\nexport default { x }`
const result = await transform(code, '/src/Parent.vue')
expect(result).toBeUndefined()
})
})
describe('nuxt auto-imported components (__nuxt prefix)', () => {
it('should use file basename as componentName for __nuxt prefixed imports in _sfc_main', async () => {
const code = [
`import __nuxt_component_0 from './ChildComp.vue'`,
`const _sfc_main = {}`,
`export default _sfc_main`,
].join('\n')
const result = await transform(code, '/src/Parent.vue')
expect(result.code).toContain(`componentName: 'ChildComp'`)
expect(result.code).toContain(`importSource: './ChildComp.vue'`)
})
it('should use file basename as componentName for __nuxt prefixed imports in defineComponent setup', async () => {
const code = [
`import { defineComponent } from 'vue'`,
`import __nuxt_component_0 from './SomeWidget.vue'`,
`export default defineComponent({`,
` setup() {`,
` return {}`,
` }`,
`})`,
].join('\n')
const result = await transform(code, '/src/Parent.ts')
expect(result.code).toContain(`componentName: 'SomeWidget'`)
expect(result.code).toContain('useLazyComponentTracking(')
})
})
describe('nuxtComponents matching (pascalName)', () => {
it('should use pascalName from nuxtComponents when component filePath matches', async () => {
vi.mocked(useNuxt).mockReturnValueOnce({
apps: {
// @ts-expect-error partial mock
default: {
components: [
{
filePath: './MyWidget.vue',
pascalName: 'MyWidgetPascal',
kebabName: '',
export: '',
shortPath: '',
chunkName: '',
prefetch: false,
preload: false,
},
],
},
},
hook: vi.fn(),
})
const { LazyLoadHintPlugin: PluginWithComponents } = await import('../../../src/plugins/lazy-load')
const p = PluginWithComponents.vite() as Plugin
const t = (p.transform as ObjectHook<HookFnMap['transform'], 'code' | 'id'>).handler
const code = `import MyWidget from './MyWidget.vue'\nexport default { components: { MyWidget } }`
const result = await t.call(unpluginCtx, code, '/src/Parent.vue') as unknown as { code: string }
expect(result.code).toContain(
`__wrapImportedComponent(__original_MyWidget, 'MyWidgetPascal', './MyWidget.vue'`,
)
})
})
describe('defineComponent setup injection', () => {
it('should inject useLazyComponentTracking in defineComponent setup', async () => {
const code = [
`import { defineComponent } from 'vue'`,
`import ChildComp from './ChildComp.vue'`,
`export default defineComponent({`,
` setup() {`,
` return {}`,
` }`,
`})`,
].join('\n')
const result = await transform(code, '/src/Parent.ts')
expect(result.code).toContain('useLazyComponentTracking(')
expect(result.code).toContain(`componentName: 'ChildComp'`)
expect(result.code).toMatchInlineSnapshot(`
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
import { defineComponent } from 'vue'
import __original_ChildComp from './ChildComp.vue'
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
export default defineComponent({
setup() {
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);
return {}
}
})"
`)
})
it('should inject useLazyComponentTracking in arrow function setup', async () => {
const code = [
`import { defineComponent } from 'vue'`,
`import ChildComp from './ChildComp.vue'`,
`export default defineComponent({`,
` setup: () => {`,
` return {}`,
` }`,
`})`,
].join('\n')
const result = await transform(code, '/src/Parent.ts')
expect(result.code).toContain('useLazyComponentTracking(')
expect(result.code).toMatchInlineSnapshot(`
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
import { defineComponent } from 'vue'
import __original_ChildComp from './ChildComp.vue'
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
export default defineComponent({
setup: () => {
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);
return {}
}
})"
`)
})
})
})