openpencil/tests/engine/figma/api/create/component-from-node.test.ts
xemc 1fd7990cd9 fix(figma-api): preserve auto-layout sizing mode when converting a node to a component
createComponentFromNode copied only a hand-picked list of fields onto the
new COMPONENT node, omitting primaryAxisSizing/counterAxisSizing. A HUG-sized
auto-layout frame silently reverted to FIXED sizing on conversion, so later
padding changes no longer resized the frame (itemSpacing still worked since
it doesn't affect the frame's own size).
2026-08-26 10:17:07 +10:00

40 lines
1.2 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { createAPI } from '../helpers'
describe('createComponentFromNode', () => {
test('converts frame to component', () => {
const api = createAPI()
const frame = api.createFrame()
frame.name = 'MyButton'
frame.resize(200, 50)
const child = api.createRectangle()
child.name = 'Background'
frame.appendChild(child)
const frameId = frame.id
const comp = api.createComponentFromNode(frame)
expect(comp.type).toBe('COMPONENT')
expect(comp.name).toBe('MyButton')
expect(comp.width).toBe(200)
expect(comp.height).toBe(50)
expect(comp.children.length).toBe(1)
expect(comp.children[0].name).toBe('Background')
expect(api.getNodeById(frameId)).toBeNull()
})
test('preserves auto-layout HUG sizing mode', () => {
const api = createAPI()
const frame = api.createFrame()
frame.layoutMode = 'HORIZONTAL'
frame.primaryAxisSizingMode = 'AUTO'
frame.counterAxisSizingMode = 'AUTO'
const comp = api.createComponentFromNode(frame)
const raw = api.graph.getNode(comp.id)
expect(raw?.primaryAxisSizing).toBe('HUG')
expect(raw?.counterAxisSizing).toBe('HUG')
})
})