Skip to content

Commit 69e2084

Browse files
committed
Add case
1 parent 5d50b1e commit 69e2084

File tree

1 file changed

+147
-1
lines changed

1 file changed

+147
-1
lines changed

src/codegen/utils/__tests__/node-proxy.test.ts

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { beforeEach, describe, expect, test } from 'bun:test'
2-
import { nodeProxyTracker } from '../node-proxy'
2+
import {
3+
assembleNodeTree,
4+
nodeProxyTracker,
5+
setupVariableMocks,
6+
} from '../node-proxy'
37

48
// Mock SceneNode
59
function createMockNode(overrides: Partial<SceneNode> = {}): SceneNode {
@@ -622,4 +626,146 @@ describe('nodeProxyTracker', () => {
622626
expect(result.variables[0].id).toBe('VariableID:123')
623627
expect(result.variables[0].name).toBe('primary-color')
624628
})
629+
630+
test('toTestCaseFormatWithVariables should collect variables from nested objects', async () => {
631+
// Setup figma global mock
632+
const mockFigma = {
633+
variables: {
634+
getVariableByIdAsync: async (id: string) => {
635+
if (id === 'VariableID:nested') {
636+
return { id: 'VariableID:nested', name: 'nested-color' }
637+
}
638+
return null
639+
},
640+
},
641+
}
642+
;(globalThis as unknown as { figma: typeof mockFigma }).figma = mockFigma
643+
644+
// Create a node with nested object containing variable reference
645+
const nodeWithNestedVariable = {
646+
...createMockNode({ id: 'node-nested', name: 'NodeWithNestedVariable' }),
647+
customProp: {
648+
nested: {
649+
deep: {
650+
variableRef: '[NodeId: VariableID:nested]',
651+
},
652+
},
653+
},
654+
} as unknown as SceneNode
655+
656+
const wrapped = nodeProxyTracker.wrap(nodeWithNestedVariable)
657+
658+
// Access customProp to trigger tracking
659+
const _customProp = (wrapped as unknown as Record<string, unknown>)
660+
.customProp
661+
662+
// Call toTestCaseFormatWithVariables
663+
const result = await nodeProxyTracker.toTestCaseFormatWithVariables()
664+
665+
expect(result.variables.length).toBe(1)
666+
expect(result.variables[0].id).toBe('VariableID:nested')
667+
expect(result.variables[0].name).toBe('nested-color')
668+
})
669+
670+
test('toTestCaseFormatWithVariables should collect variables from arrays', async () => {
671+
// Setup figma global mock
672+
const mockFigma = {
673+
variables: {
674+
getVariableByIdAsync: async (id: string) => {
675+
if (id === 'VariableID:arr1') {
676+
return { id: 'VariableID:arr1', name: 'arr-color-1' }
677+
}
678+
if (id === 'VariableID:arr2') {
679+
return { id: 'VariableID:arr2', name: 'arr-color-2' }
680+
}
681+
return null
682+
},
683+
},
684+
}
685+
;(globalThis as unknown as { figma: typeof mockFigma }).figma = mockFigma
686+
687+
// Create a node with array containing variable references
688+
const nodeWithArrayVariable = {
689+
...createMockNode({ id: 'node-arr', name: 'NodeWithArrayVariable' }),
690+
fills: [
691+
{
692+
type: 'SOLID',
693+
boundVariables: {
694+
color: '[NodeId: VariableID:arr1]',
695+
},
696+
},
697+
{
698+
type: 'SOLID',
699+
boundVariables: {
700+
color: '[NodeId: VariableID:arr2]',
701+
},
702+
},
703+
],
704+
} as unknown as SceneNode
705+
706+
const wrapped = nodeProxyTracker.wrap(nodeWithArrayVariable)
707+
708+
// Access fills to trigger tracking
709+
const _fills = (wrapped as unknown as FrameNode).fills
710+
711+
// Call toTestCaseFormatWithVariables
712+
const result = await nodeProxyTracker.toTestCaseFormatWithVariables()
713+
714+
expect(result.variables.length).toBe(2)
715+
const varIds = result.variables.map((v) => v.id)
716+
expect(varIds).toContain('VariableID:arr1')
717+
expect(varIds).toContain('VariableID:arr2')
718+
})
719+
720+
test('toTestCaseFormatWithVariables should collect variables from direct array of variable refs', async () => {
721+
// Setup figma global mock
722+
const mockFigma = {
723+
variables: {
724+
getVariableByIdAsync: async (id: string) => {
725+
if (id === 'VariableID:direct') {
726+
return { id: 'VariableID:direct', name: 'direct-color' }
727+
}
728+
return null
729+
},
730+
},
731+
}
732+
;(globalThis as unknown as { figma: typeof mockFigma }).figma = mockFigma
733+
734+
// Create a node with direct array of variable reference strings
735+
const nodeWithDirectArray = {
736+
...createMockNode({ id: 'node-direct', name: 'NodeWithDirectArray' }),
737+
colorRefs: ['[NodeId: VariableID:direct]'],
738+
} as unknown as SceneNode
739+
740+
const wrapped = nodeProxyTracker.wrap(nodeWithDirectArray)
741+
742+
// Access colorRefs to trigger tracking
743+
const _colorRefs = (wrapped as unknown as Record<string, unknown>).colorRefs
744+
745+
// Call toTestCaseFormatWithVariables
746+
const result = await nodeProxyTracker.toTestCaseFormatWithVariables()
747+
748+
expect(result.variables.length).toBe(1)
749+
expect(result.variables[0].id).toBe('VariableID:direct')
750+
})
751+
752+
test('re-exported assembleNodeTree works from node-proxy', () => {
753+
const nodes = [{ id: 'test-1', name: 'Test', type: 'FRAME' }]
754+
const result = assembleNodeTree(nodes)
755+
expect(result.id).toBe('test-1')
756+
})
757+
758+
test('re-exported setupVariableMocks works from node-proxy', async () => {
759+
;(globalThis as unknown as { figma: Record<string, unknown> }).figma = {}
760+
setupVariableMocks([{ id: 'VariableID:test', name: 'test-var' }])
761+
762+
const g = globalThis as {
763+
figma?: {
764+
variables?: { getVariableByIdAsync?: (id: string) => Promise<unknown> }
765+
}
766+
}
767+
const result =
768+
await g.figma?.variables?.getVariableByIdAsync?.('VariableID:test')
769+
expect(result).toEqual({ id: 'VariableID:test', name: 'test-var' })
770+
})
625771
})

0 commit comments

Comments
 (0)