|
| 1 | +import type { MutateResult } from '@figwright/shared'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { createBindVariableToPaintHandler } from '../../src/handlers/bind-variable-to-paint.js'; |
| 5 | + |
| 6 | +const solid = () => ({ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }); |
| 7 | + |
| 8 | +// setBoundVariableForPaint returns a NEW paint (the binding marker) — the handler must write it back. |
| 9 | +const fakeFigma = (node: unknown, variable: unknown, bound: unknown) => |
| 10 | + ({ |
| 11 | + getNodeByIdAsync: async () => node, |
| 12 | + variables: { |
| 13 | + getVariableByIdAsync: async () => variable, |
| 14 | + setBoundVariableForPaint: vi.fn<() => unknown>(() => bound), |
| 15 | + }, |
| 16 | + }) as unknown as typeof figma & { |
| 17 | + variables: { setBoundVariableForPaint: ReturnType<typeof vi.fn> }; |
| 18 | + }; |
| 19 | + |
| 20 | +describe('bind_variable_to_paint handler', () => { |
| 21 | + it('binds a colour variable to fills[0] and writes back a new paint array', async () => { |
| 22 | + const node = { id: '1:1', fills: [solid()] }; |
| 23 | + const variable = { id: 'V:white' }; |
| 24 | + const boundPaint = { |
| 25 | + type: 'SOLID', |
| 26 | + color: { r: 1, g: 1, b: 1 }, |
| 27 | + boundVariables: { color: variable }, |
| 28 | + }; |
| 29 | + const ctx = fakeFigma(node, variable, boundPaint); |
| 30 | + |
| 31 | + const result = (await createBindVariableToPaintHandler(ctx)({ |
| 32 | + nodeId: '1:1', |
| 33 | + variableId: 'V:white', |
| 34 | + })) as MutateResult; |
| 35 | + |
| 36 | + expect(ctx.variables.setBoundVariableForPaint).toHaveBeenCalledWith( |
| 37 | + expect.objectContaining({ type: 'SOLID' }), |
| 38 | + 'color', |
| 39 | + variable, |
| 40 | + ); |
| 41 | + // a NEW array with the returned paint at the index (not a mutation of the read-only original) |
| 42 | + expect(node.fills).toEqual([boundPaint]); |
| 43 | + expect(result).toEqual({ ok: true, nodeId: '1:1' }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('targets strokes and the given index', async () => { |
| 47 | + const node = { id: '2:2', strokes: [solid(), solid()] }; |
| 48 | + const variable = { id: 'V:grey' }; |
| 49 | + const bound = { type: 'SOLID', color: { r: 0, g: 0, b: 0 } }; |
| 50 | + const ctx = fakeFigma(node, variable, bound); |
| 51 | + |
| 52 | + await createBindVariableToPaintHandler(ctx)({ |
| 53 | + nodeId: '2:2', |
| 54 | + target: 'strokes', |
| 55 | + index: 1, |
| 56 | + variableId: 'V:grey', |
| 57 | + }); |
| 58 | + |
| 59 | + expect(node.strokes[1]).toBe(bound); |
| 60 | + expect(node.strokes[0]).toEqual(solid()); // untouched |
| 61 | + }); |
| 62 | + |
| 63 | + it('unbinds with variableId null, never looking up a variable', async () => { |
| 64 | + const getVariableByIdAsync = vi.fn<() => Promise<unknown>>(); |
| 65 | + const node = { id: '3:3', fills: [solid()] }; |
| 66 | + const bound = solid(); |
| 67 | + const ctx = { |
| 68 | + getNodeByIdAsync: async () => node, |
| 69 | + variables: { |
| 70 | + getVariableByIdAsync, |
| 71 | + setBoundVariableForPaint: vi.fn<() => unknown>(() => bound), |
| 72 | + }, |
| 73 | + } as unknown as typeof figma; |
| 74 | + |
| 75 | + const result = (await createBindVariableToPaintHandler(ctx)({ |
| 76 | + nodeId: '3:3', |
| 77 | + variableId: null, |
| 78 | + })) as MutateResult; |
| 79 | + |
| 80 | + expect(getVariableByIdAsync).not.toHaveBeenCalled(); |
| 81 | + expect(result).toEqual({ ok: true, nodeId: '3:3' }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('rejects a non-SOLID paint at the target index', async () => { |
| 85 | + const node = { id: '4:4', fills: [{ type: 'GRADIENT_LINEAR' }] }; |
| 86 | + await expect( |
| 87 | + createBindVariableToPaintHandler(fakeFigma(node, { id: 'V:0' }, {}))({ |
| 88 | + nodeId: '4:4', |
| 89 | + variableId: 'V:0', |
| 90 | + }), |
| 91 | + ).rejects.toThrow(/only SOLID paints/); |
| 92 | + }); |
| 93 | + |
| 94 | + it('throws on missing node, missing paint index, or missing variable', async () => { |
| 95 | + await expect( |
| 96 | + createBindVariableToPaintHandler(fakeFigma(null, { id: 'V:0' }, {}))({ |
| 97 | + nodeId: '9:9', |
| 98 | + variableId: 'V:0', |
| 99 | + }), |
| 100 | + ).rejects.toThrow(/not found or has no fills/); |
| 101 | + |
| 102 | + await expect( |
| 103 | + createBindVariableToPaintHandler(fakeFigma({ id: '1:1', fills: [] }, { id: 'V:0' }, {}))({ |
| 104 | + nodeId: '1:1', |
| 105 | + index: 0, |
| 106 | + variableId: 'V:0', |
| 107 | + }), |
| 108 | + ).rejects.toThrow(/no paint at fills\[0\]/); |
| 109 | + |
| 110 | + await expect( |
| 111 | + createBindVariableToPaintHandler(fakeFigma({ id: '1:1', fills: [solid()] }, null, {}))({ |
| 112 | + nodeId: '1:1', |
| 113 | + variableId: 'V:missing', |
| 114 | + }), |
| 115 | + ).rejects.toThrow(/variable .* not found/); |
| 116 | + }); |
| 117 | +}); |
0 commit comments