Skip to content

AI-native design editor. Open-source Figma alternative.

2.9 KB
Raw Blame
import { describe, expect, test } from 'bun:test'

import { computeLayout } from '@open-pencil/core/layout'
import { SceneGraph } from '@open-pencil/scene-graph'

import { autoFrame, pageId, rect } from '#tests/helpers/layout'

describe('alignment - primary axis', () => {
  test('center alignment (horizontal)', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      width: 400,
      height: 100,
      primaryAxisAlign: 'CENTER'
    })
    rect(graph, frame.id, 100, 50)

    computeLayout(graph, frame.id)

    const child = graph.getChildren(frame.id)[0]
    expect(child.x).toBe(150)
  })

  test('max alignment (horizontal)', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      width: 400,
      height: 100,
      primaryAxisAlign: 'MAX'
    })
    rect(graph, frame.id, 100, 50)

    computeLayout(graph, frame.id)

    const child = graph.getChildren(frame.id)[0]
    expect(child.x).toBe(300)
  })

  test('space-between alignment (horizontal)', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      width: 400,
      height: 100,
      itemSpacing: 32,
      primaryAxisAlign: 'SPACE_BETWEEN'
    })
    rect(graph, frame.id, 50, 50)
    rect(graph, frame.id, 50, 50)
    rect(graph, frame.id, 50, 50)

    computeLayout(graph, frame.id)

    const children = graph.getChildren(frame.id)
    expect(children[0].x).toBe(0)
    expect(children[2].x).toBe(350)
    expect(children[1].x).toBeCloseTo(175, 0)
  })

  test('space-between alignment ignores stored primary spacing vertically', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      layoutMode: 'VERTICAL',
      width: 100,
      height: 400,
      itemSpacing: 32,
      primaryAxisAlign: 'SPACE_BETWEEN'
    })
    rect(graph, frame.id, 50, 50)
    rect(graph, frame.id, 50, 50)
    rect(graph, frame.id, 50, 50)

    computeLayout(graph, frame.id)

    const children = graph.getChildren(frame.id)
    expect(children[0].y).toBe(0)
    expect(children[1].y).toBeCloseTo(175, 0)
    expect(children[2].y).toBe(350)
  })

  test('center alignment (vertical)', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      layoutMode: 'VERTICAL',
      width: 200,
      height: 400,
      primaryAxisAlign: 'CENTER'
    })
    rect(graph, frame.id, 50, 100)

    computeLayout(graph, frame.id)

    const child = graph.getChildren(frame.id)[0]
    expect(child.y).toBe(150)
  })

  test('max alignment (vertical)', () => {
    const graph = new SceneGraph()
    const frame = autoFrame(graph, pageId(graph), {
      layoutMode: 'VERTICAL',
      width: 200,
      height: 400,
      primaryAxisAlign: 'MAX'
    })
    rect(graph, frame.id, 50, 100)

    computeLayout(graph, frame.id)

    const child = graph.getChildren(frame.id)[0]
    expect(child.y).toBe(300)
  })
})