-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexportPagesAndComponents.test.ts
More file actions
360 lines (318 loc) · 11.7 KB
/
exportPagesAndComponents.test.ts
File metadata and controls
360 lines (318 loc) · 11.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { describe, expect, mock, test } from 'bun:test'
import {
collectAssetNodes,
DEVUP_COMPONENTS,
extractCustomComponentImports,
extractImports,
generateImportStatements,
} from '../exportPagesAndComponents'
mock.module('../../codegen/utils/check-asset-node', () => ({
checkAssetNode: (node: {
type: string
isAsset?: boolean
}): string | null => {
if (node.type === 'VECTOR') return 'svg'
if (node.type === 'STAR') return 'svg'
if (node.type === 'POLYGON') return 'svg'
if (node.isAsset && node.type === 'RECTANGLE') return 'png'
return null
},
}))
describe('DEVUP_COMPONENTS', () => {
test('should contain expected devup-ui components', () => {
expect(DEVUP_COMPONENTS).toContain('Box')
expect(DEVUP_COMPONENTS).toContain('Flex')
expect(DEVUP_COMPONENTS).toContain('Text')
expect(DEVUP_COMPONENTS).toContain('Image')
expect(DEVUP_COMPONENTS).toContain('Grid')
expect(DEVUP_COMPONENTS).toContain('VStack')
expect(DEVUP_COMPONENTS).toContain('Center')
})
})
describe('extractImports', () => {
test('should extract Box import', () => {
const result = extractImports([['Test', '<Box>Hello</Box>']])
expect(result).toContain('Box')
})
test('should extract multiple devup-ui components', () => {
const result = extractImports([
['Test', '<Box><Flex><Text>Hello</Text></Flex></Box>'],
])
expect(result).toContain('Box')
expect(result).toContain('Flex')
expect(result).toContain('Text')
})
test('should extract keyframes with parenthesis', () => {
const result = extractImports([
['Test', '<Box animationName={keyframes({ "0%": { opacity: 0 } })} />'],
])
expect(result).toContain('keyframes')
expect(result).toContain('Box')
})
test('should extract keyframes with template literal', () => {
const result = extractImports([
['Test', '<Box animationName={keyframes`from { opacity: 0 }`} />'],
])
expect(result).toContain('keyframes')
})
test('should not extract keyframes when not present', () => {
const result = extractImports([['Test', '<Box w="100px" />']])
expect(result).not.toContain('keyframes')
})
test('should return sorted imports', () => {
const result = extractImports([
['Test', '<VStack><Box><Center /></Box></VStack>'],
])
expect(result).toEqual(['Box', 'Center', 'VStack'])
})
test('should not include duplicates', () => {
const result = extractImports([
['Test1', '<Box>A</Box>'],
['Test2', '<Box>B</Box>'],
])
expect(result.filter((x) => x === 'Box').length).toBe(1)
})
test('should handle self-closing tags', () => {
const result = extractImports([['Test', '<Image />']])
expect(result).toContain('Image')
})
test('should handle tags with spaces', () => {
const result = extractImports([['Test', '<Grid rows={2}>']])
expect(result).toContain('Grid')
})
})
describe('extractCustomComponentImports', () => {
test('should extract custom component', () => {
const result = extractCustomComponentImports([
['Test', '<Box><CustomButton /></Box>'],
])
expect(result).toContain('CustomButton')
})
test('should extract multiple custom components', () => {
const result = extractCustomComponentImports([
['Test', '<CustomA><CustomB /><CustomC /></CustomA>'],
])
expect(result).toContain('CustomA')
expect(result).toContain('CustomB')
expect(result).toContain('CustomC')
})
test('should not include devup-ui components', () => {
const result = extractCustomComponentImports([
['Test', '<Box><Flex><CustomCard /></Flex></Box>'],
])
expect(result).toContain('CustomCard')
expect(result).not.toContain('Box')
expect(result).not.toContain('Flex')
})
test('should return sorted imports', () => {
const result = extractCustomComponentImports([
['Test', '<Zebra /><Apple /><Mango />'],
])
expect(result).toEqual(['Apple', 'Mango', 'Zebra'])
})
test('should not include duplicates', () => {
const result = extractCustomComponentImports([
['Test1', '<SharedButton />'],
['Test2', '<SharedButton />'],
])
expect(result.filter((x) => x === 'SharedButton').length).toBe(1)
})
test('should return empty array when no custom components', () => {
const result = extractCustomComponentImports([
['Test', '<Box><Flex>Hello</Flex></Box>'],
])
expect(result).toEqual([])
})
})
describe('collectAssetNodes', () => {
let nodeIdCounter = 0
function createNode(
type: string,
name: string,
{
visible = true,
isAsset = false,
children,
id,
}: {
visible?: boolean
isAsset?: boolean
children?: SceneNode[]
id?: string
} = {},
): SceneNode {
return {
type,
name,
visible,
isAsset,
id: id ?? `node-${nodeIdCounter++}`,
...(children ? { children } : {}),
} as unknown as SceneNode
}
test('should collect SVG asset node', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('VECTOR', 'arrow-icon')
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
expect(assets.get('svg/arrow-icon')?.type).toBe('svg')
})
test('should collect PNG asset node', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('RECTANGLE', 'photo', { isAsset: true })
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
expect(assets.get('png/photo')?.type).toBe('png')
})
test('should skip invisible nodes', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('VECTOR', 'hidden-icon', { visible: false })
collectAssetNodes(node, assets)
expect(assets.size).toBe(0)
})
test('should recursively collect from children', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('FRAME', 'container', {
children: [createNode('VECTOR', 'icon-a'), createNode('STAR', 'icon-b')],
})
collectAssetNodes(node, assets)
expect(assets.size).toBe(2)
expect(assets.has('svg/icon-a')).toBe(true)
expect(assets.has('svg/icon-b')).toBe(true)
})
test('should not descend into asset node children', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
// VECTOR is an asset — even if it somehow had children, we don't walk them
const node = createNode('VECTOR', 'icon-parent')
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
})
test('should deduplicate by type and name', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('FRAME', 'wrapper', {
children: [
createNode('VECTOR', 'same-icon'),
createNode('VECTOR', 'same-icon'),
],
})
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
})
test('should collect from nested frames', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('FRAME', 'outer', {
children: [
createNode('FRAME', 'inner', {
children: [createNode('POLYGON', 'deep-icon')],
}),
],
})
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
expect(assets.has('svg/deep-icon')).toBe(true)
})
test('should collect both SVG and PNG assets', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('FRAME', 'mixed', {
children: [
createNode('VECTOR', 'icon'),
createNode('RECTANGLE', 'image', { isAsset: true }),
],
})
collectAssetNodes(node, assets)
expect(assets.size).toBe(2)
expect(assets.get('svg/icon')?.type).toBe('svg')
expect(assets.get('png/image')?.type).toBe('png')
})
test('should return empty map for non-asset leaf node', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('FRAME', 'empty-frame')
collectAssetNodes(node, assets)
expect(assets.size).toBe(0)
})
test('should skip already-visited nodes when visited set is provided', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const visited = new Set<string>()
const sharedChild = createNode('VECTOR', 'shared-icon', { id: 'shared-1' })
// First call walks the child
const parent1 = createNode('FRAME', 'parent-a', {
children: [sharedChild],
})
collectAssetNodes(parent1, assets, visited)
expect(assets.size).toBe(1)
// Second call with overlapping subtree — shared-1 already visited
const parent2 = createNode('FRAME', 'parent-b', {
children: [sharedChild],
})
collectAssetNodes(parent2, assets, visited)
// Still 1 — child was skipped via visited set
expect(assets.size).toBe(1)
expect(visited.has('shared-1')).toBe(true)
})
test('should skip visited parent node entirely', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const visited = new Set<string>()
const node = createNode('FRAME', 'root', {
id: 'root-1',
children: [createNode('VECTOR', 'icon-inside')],
})
collectAssetNodes(node, assets, visited)
expect(assets.size).toBe(1)
// Clear assets but keep visited — re-collecting same root yields nothing new
assets.clear()
collectAssetNodes(node, assets, visited)
expect(assets.size).toBe(0)
})
test('should work without visited set (backward compatible)', () => {
const assets = new Map<string, { node: SceneNode; type: 'svg' | 'png' }>()
const node = createNode('VECTOR', 'compat-icon')
// No visited parameter — should still work
collectAssetNodes(node, assets)
expect(assets.size).toBe(1)
})
})
describe('generateImportStatements', () => {
test('should generate devup-ui import statement', () => {
const result = generateImportStatements([['Test', '<Box><Flex /></Box>']])
expect(result).toContain("import { Box, Flex } from '@devup-ui/react'")
})
test('should generate custom component import statements', () => {
const result = generateImportStatements([
['Test', '<Box><CustomButton /></Box>'],
])
expect(result).toContain("import { Box } from '@devup-ui/react'")
expect(result).toContain(
"import { CustomButton } from '@/components/CustomButton'",
)
})
test('should generate multiple custom component imports on separate lines', () => {
const result = generateImportStatements([
['Test', '<Box><ButtonA /><ButtonB /></Box>'],
])
expect(result).toContain("import { ButtonA } from '@/components/ButtonA'")
expect(result).toContain("import { ButtonB } from '@/components/ButtonB'")
})
test('should return empty string when no imports', () => {
const result = generateImportStatements([['Test', 'just text']])
expect(result).toBe('')
})
test('should include keyframes in devup-ui import', () => {
const result = generateImportStatements([
['Test', '<Box animation={keyframes({})} />'],
])
expect(result).toContain('keyframes')
expect(result).toContain("from '@devup-ui/react'")
})
test('should end with double newline when has imports', () => {
const result = generateImportStatements([['Test', '<Box />']])
expect(result.endsWith('\n\n')).toBe(true)
})
test('should return the same imports across repeated calls', () => {
const components = [
['Test', '<Box><CustomButton /><Flex /></Box>'],
] as const
const first = generateImportStatements(components)
const second = generateImportStatements(components)
expect(second).toBe(first)
})
})