Add bun:test unit tests for SceneGraph (15ms)

This commit is contained in:
Danila Poyarkov 2026-02-28 07:45:50 +03:00
parent c1fbf57df2
commit e03a01dbe3
2 changed files with 55 additions and 1 deletions

View file

@ -15,7 +15,8 @@
"test": "playwright test --project=openpencil",
"test:update": "playwright test --project=openpencil --update-snapshots",
"test:figma": "playwright test --project=figma",
"figma:debug": "open -a Figma --args --remote-debugging-port=9222"
"figma:debug": "open -a Figma --args --remote-debugging-port=9222",
"test:unit": "bun test ./tests/engine"
},
"dependencies": {
"@tailwindcss/vite": "^4.2.1",

View file

@ -0,0 +1,53 @@
import { describe, test, expect } from 'bun:test'
import { SceneGraph } from '../../src/engine/scene-graph'
function rect(graph: SceneGraph, name: string, x = 0, y = 0, w = 50, h = 50) {
return graph.createNode('RECTANGLE', graph.rootId, { name, x, y, width: w, height: h }).id
}
describe('SceneGraph', () => {
test('create rectangle', () => {
const graph = new SceneGraph()
const id = rect(graph, 'Rect', 100, 100, 200, 150)
const node = graph.getNode(id)!
expect(node.type).toBe('RECTANGLE')
expect(node.x).toBe(100)
expect(node.width).toBe(200)
})
test('create and delete', () => {
const graph = new SceneGraph()
const id = rect(graph, 'R')
expect(graph.getNode(id)).toBeTruthy()
graph.deleteNode(id)
expect(graph.getNode(id)).toBeFalsy()
})
test('reparent into frame', () => {
const graph = new SceneGraph()
const frame = graph.createNode('FRAME', graph.rootId, { name: 'F', x: 50, y: 50, width: 400, height: 400 }).id
const r = rect(graph, 'R', 100, 100)
graph.reparentNode(r, frame)
const children = graph.getChildren(frame)
expect(children.map(c => c.id)).toContain(r)
})
test('children order', () => {
const graph = new SceneGraph()
rect(graph, 'A')
rect(graph, 'B')
rect(graph, 'C')
const names = graph.getChildren(graph.rootId).map(n => n.name)
expect(names).toEqual(['A', 'B', 'C'])
})
test('update node', () => {
const graph = new SceneGraph()
const id = rect(graph, 'R')
graph.updateNode(id, { x: 200, name: 'Updated' })
const node = graph.getNode(id)!
expect(node.x).toBe(200)
expect(node.name).toBe('Updated')
})
})