Skip to content

Commit 0ad8cf8

Browse files
committed
Add test
1 parent ba1d6fe commit 0ad8cf8

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/codegen/responsive/__tests__/ResponsiveCodegen.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,64 @@ describe('ResponsiveCodegen', () => {
499499
expect(result).toContain('scroll')
500500
expect(result).toContain('default')
501501
})
502+
503+
it('renders OR conditional for child existing in multiple but not all variants', () => {
504+
const generator = new ResponsiveCodegen(null)
505+
506+
const partialChild: NodeTree = {
507+
component: 'Box',
508+
props: { id: 'PartialChild' },
509+
children: [],
510+
nodeType: 'FRAME',
511+
nodeName: 'PartialChild',
512+
}
513+
514+
const treesByVariant = new Map<string, NodeTree>([
515+
[
516+
'scroll',
517+
{
518+
component: 'Flex',
519+
props: {},
520+
children: [partialChild],
521+
nodeType: 'FRAME',
522+
nodeName: 'Root',
523+
},
524+
],
525+
[
526+
'hover',
527+
{
528+
component: 'Flex',
529+
props: {},
530+
children: [{ ...partialChild, props: { id: 'PartialChildHover' } }],
531+
nodeType: 'FRAME',
532+
nodeName: 'Root',
533+
},
534+
],
535+
[
536+
'default',
537+
{
538+
component: 'Flex',
539+
props: {},
540+
children: [], // No child in default variant
541+
nodeType: 'FRAME',
542+
nodeName: 'Root',
543+
},
544+
],
545+
])
546+
547+
const result = generator.generateVariantOnlyMergedCode(
548+
'status',
549+
treesByVariant,
550+
0,
551+
)
552+
553+
// Should contain OR conditional for multiple variants
554+
expect(result).toContain('status === "scroll"')
555+
expect(result).toContain('status === "hover"')
556+
expect(result).toContain('||')
557+
expect(result).toContain('&&')
558+
expect(result).toMatchSnapshot()
559+
})
502560
})
503561

504562
describe('generateVariantResponsiveComponents', () => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Bun Snapshot v1, https://bun.sh/docs/test/snapshots
2+
3+
exports[`ResponsiveCodegen generateVariantOnlyMergedCode renders OR conditional for child existing in multiple but not all variants 1`] = `"render:Flex:depth=0:{}|{(status === "scroll" || status === "hover") && render:Box:depth=0:{"id":{"__variantProp":true,"variantKey":"status","values":{"scroll":"PartialChild","hover":"PartialChildHover"}}}|}"`;

src/codegen/utils/__tests__/props-to-str.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,44 @@ describe('propsToString', () => {
116116
// Should use space separator, not newline
117117
expect(res).not.toContain('\n')
118118
})
119+
120+
test('handles VariantPropValue with object values', () => {
121+
const variantProp = createVariantPropValue('status', {
122+
scroll: { x: 1, y: 2 },
123+
default: { x: 3, y: 4 },
124+
})
125+
const res = propsToString({ transform: variantProp })
126+
expect(res).toContain('scroll: {"x":1,"y":2}')
127+
expect(res).toContain('default: {"x":3,"y":4}')
128+
})
129+
130+
test('handles VariantPropValue with boolean values', () => {
131+
const variantProp = createVariantPropValue('status', {
132+
scroll: true,
133+
default: false,
134+
})
135+
const res = propsToString({ visible: variantProp })
136+
expect(res).toBe('visible={{ scroll: true, default: false }[status]}')
137+
})
138+
139+
test('handles VariantPropValue with undefined values in array', () => {
140+
const variantProp = createVariantPropValue('status', {
141+
scroll: [undefined, '10px'],
142+
default: ['20px', undefined],
143+
})
144+
const res = propsToString({ w: variantProp })
145+
expect(res).toContain('scroll: [undefined, "10px"]')
146+
expect(res).toContain('default: ["20px", undefined]')
147+
})
148+
149+
test('handles VariantPropValue with symbol values (fallback case)', () => {
150+
const sym = Symbol('test')
151+
const variantProp = createVariantPropValue('status', {
152+
scroll: sym as unknown as string,
153+
default: '20px',
154+
})
155+
const res = propsToString({ w: variantProp })
156+
expect(res).toContain('scroll: Symbol(test)')
157+
expect(res).toContain('default: "20px"')
158+
})
119159
})

0 commit comments

Comments
 (0)