-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.test.ts
More file actions
344 lines (314 loc) · 8.99 KB
/
render.test.ts
File metadata and controls
344 lines (314 loc) · 8.99 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
import { describe, expect, test } from 'bun:test'
import { renderComponent, renderNode } from '../render'
describe('renderNode', () => {
test.each([
{
title: 'removes component specific defaults for Flex',
component: 'Flex',
props: { display: 'flex', gap: '8px' },
deps: 0,
children: [] as string[],
expected: '<Flex gap="8px" />',
},
{
title: 'drops default props and component filtered props',
component: 'Center',
props: {
alignItems: 'flex-start',
justifyContent: 'flex-start',
display: 'flex',
},
deps: 0,
children: [] as string[],
expected: '<Center />',
},
{
title: 'indents nested children',
component: 'Box',
props: { p: '8px' },
deps: 1,
children: ['<Text />'] as string[],
expected: ' <Box p="8px">\n <Text />\n </Box>',
},
])('$title', ({ component, props, deps, children, expected }) => {
const result = renderNode(component, props, deps, children)
expect(result).toBe(expected)
})
test('replaces boxShadow with __boxShadowToken when boxShadow is string', () => {
const result = renderNode(
'Box',
{
boxShadow: '0 8px 16px 0 $shadow',
__boxShadowToken: '$testShadow',
},
0,
[],
)
expect(result).toBe('<Box boxShadow="$testShadow" />')
})
test('does not replace boxShadow with __boxShadowToken when boxShadow is array', () => {
const result = renderNode(
'Box',
{
boxShadow: ['0 8px 16px 0 $shadow', null, '$testShadow'],
__boxShadowToken: '$testShadow',
},
0,
[],
)
expect(result).toBe(`<Box
boxShadow={[
"0 8px 16px 0 $shadow",
null,
"$testShadow"
]}
/>`)
})
test('replaces textShadow with __textShadowToken when textShadow is string', () => {
const result = renderNode(
'Text',
{
textShadow: '0 4px 8px $shadow',
__textShadowToken: '$titleShadow',
},
0,
[],
)
expect(result).toBe('<Text textShadow="$titleShadow" />')
})
test('does not replace textShadow with __textShadowToken when textShadow is array', () => {
const result = renderNode(
'Text',
{
textShadow: ['0 2px 4px $shadow', null, '$titleShadow'],
__textShadowToken: '$titleShadow',
},
0,
[],
)
expect(result).toBe(`<Text
textShadow={[
"0 2px 4px $shadow",
null,
"$titleShadow"
]}
/>`)
})
})
/**
* Indentation format tests to prevent regression.
*
* Expected indentation pattern:
* - Function body: 2 spaces
* - Inside return (): 4 spaces (baseDepth + 1 = 2, so 2 * 2 = 4)
* - Nested children: +2 spaces per level (6, 8, 10, ...)
* - Props on multiple lines: +2 spaces from component tag
*/
describe('renderComponent indentation', () => {
test('single line JSX stays on same line as return', () => {
const result = renderComponent('Button', '<Button />', {})
expect(result).toBe(`export function Button() {
return <Button />
}`)
})
test('multiline JSX has 4 spaces inside return ()', () => {
const code = `<Box>
<Text />
</Box>`
const result = renderComponent('Card', code, {})
// Verify indentation pattern
const lines = result.split('\n')
expect(lines[2]).toBe(' <Box>') // 4 spaces
expect(lines[3]).toBe(' <Text />') // 6 spaces
expect(lines[4]).toBe(' </Box>') // 4 spaces
})
test('deeply nested children increment by 2 spaces each level', () => {
const code = `<VStack>
<Flex>
<Box>
<Text />
</Box>
</Flex>
</VStack>`
const result = renderComponent('DeepNested', code, {})
const lines = result.split('\n')
expect(lines[2]).toBe(' <VStack>') // 4 spaces (level 1)
expect(lines[3]).toBe(' <Flex>') // 6 spaces (level 2)
expect(lines[4]).toBe(' <Box>') // 8 spaces (level 3)
expect(lines[5]).toBe(' <Text />') // 10 spaces (level 4)
expect(lines[6]).toBe(' </Box>') // 8 spaces
expect(lines[7]).toBe(' </Flex>') // 6 spaces
expect(lines[8]).toBe(' </VStack>') // 4 spaces
})
test('multiline props are indented correctly', () => {
// When renderNode produces multiline props (5+ props or complex values)
const code = `<Center
bg="red"
color="white"
p="10px"
m="5px"
w="100%"
>
<Text />
</Center>`
const result = renderComponent('MultiProps', code, {})
const lines = result.split('\n')
expect(lines[2]).toBe(' <Center') // 4 spaces
expect(lines[3]).toBe(' bg="red"') // 6 spaces (props)
expect(lines[9]).toBe(' <Text />') // 6 spaces (child)
expect(lines[10]).toBe(' </Center>') // 4 spaces
})
test('component with variants maintains correct indentation', () => {
const code = `<Button>
<Text />
</Button>`
const result = renderComponent('MyButton', code, {
variant: '"primary" | "secondary"',
})
expect(result).toContain('export interface MyButtonProps')
const lines = result.split('\n')
// Line 0-2: interface, Line 3: empty, Line 4-5: function + return, Line 6+: JSX
expect(lines[6]).toBe(' <Button>') // 4 spaces
expect(lines[7]).toBe(' <Text />') // 6 spaces
expect(lines[8]).toBe(' </Button>') // 4 spaces
})
})
describe('renderComponent', () => {
test.each([
{
title: 'renders simple component without variants',
component: 'Button',
code: '<Button />',
variants: {} as Record<string, string>,
expected: `export function Button() {
return <Button />
}`,
},
{
title: 'renders component with variants and multiline code',
component: 'Banner',
code: `<Box>
<Text />
</Box>`,
variants: { size: '"sm" | "lg"' },
expected: `export interface BannerProps {
size: "sm" | "lg"
}
export function Banner({ size }: BannerProps) {
return (
<Box>
<Text />
</Box>
)
}`,
},
{
title: 'renders boolean variants as optional in interface',
component: 'Button',
code: '<Center />',
variants: {
leftIcon: 'boolean',
size: '"sm" | "lg"',
rightIcon: 'boolean',
} as Record<string, string>,
expected: `export interface ButtonProps {
leftIcon?: boolean
size: "sm" | "lg"
rightIcon?: boolean
}
export function Button({ leftIcon, size, rightIcon }: ButtonProps) {
return <Center />
}`,
},
])('$title', ({ component, code, variants, expected }) => {
const result = renderComponent(component, code, variants)
expect(result).toBe(expected)
})
})
describe('renderComponent with comments', () => {
test('prepends JSDoc comment for variant with comment', () => {
const result = renderComponent(
'Button',
'<Center />',
{ children: 'React.ReactNode', size: '"sm" | "lg"' },
{ children: 'label' },
)
expect(result).toBe(`export interface ButtonProps {
/** label */
children: React.ReactNode
size: "sm" | "lg"
}
export function Button({ children, size }: ButtonProps) {
return <Center />
}`)
})
test('does not add comment when comments map is empty', () => {
const result = renderComponent(
'Button',
'<Center />',
{ children: 'React.ReactNode' },
{},
)
expect(result).toBe(`export interface ButtonProps {
children: React.ReactNode
}
export function Button({ children }: ButtonProps) {
return <Center />
}`)
})
test('does not add comment when comments is undefined', () => {
const result = renderComponent('Button', '<Center />', {
children: 'React.ReactNode',
})
expect(result).toBe(`export interface ButtonProps {
children: React.ReactNode
}
export function Button({ children }: ButtonProps) {
return <Center />
}`)
})
})
describe('renderComponent interface snapshot', () => {
test('VARIANT + BOOLEAN + INSTANCE_SWAP mixed interface', () => {
const code = `<Center gap="10px" px="24px">
{leftIcon}
<Text fontSize="16px">buttonLg</Text>
{rightIcon}
</Center>`
const variants: Record<string, string> = {
leftIcon: 'React.ReactNode',
showLeftIcon: 'boolean',
rightIcon: 'React.ReactNode',
showRightIcon: 'boolean',
size: "'lg' | 'md' | 'sm'",
variant: "'primary' | 'ghost' | 'disabled'",
}
const result = renderComponent('Button', code, variants)
expect(result).toMatchSnapshot()
})
test('BOOLEAN-only interface (all optional)', () => {
const code = `<Flex>
{showBadge && <Box />}
{showIcon && <Box />}
</Flex>`
const variants: Record<string, string> = {
showBadge: 'boolean',
showIcon: 'boolean',
}
const result = renderComponent('Card', code, variants)
expect(result).toMatchSnapshot()
})
test('INSTANCE_SWAP-only interface (all required)', () => {
const code = `<Flex>
{leftIcon}
<Text>label</Text>
{rightIcon}
</Flex>`
const variants: Record<string, string> = {
leftIcon: 'React.ReactNode',
rightIcon: 'React.ReactNode',
}
const result = renderComponent('IconButton', code, variants)
expect(result).toMatchSnapshot()
})
})