Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.7 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import {
  DASHBOARD_GRID_COLUMNS,
  DASHBOARD_GRID_GAP,
  DASHBOARD_ROW_HEIGHT,
  defaultWidgetSize,
  flowLayouts,
  resizeByKey,
  resizeByPointer,
  resolveLayouts,
  sortByPosition,
} from './layout';

describe('defaultWidgetSize', () => {
  it('gives a stat card room for its value, description and trend line', () => {
    const { h } = defaultWidgetSize('stat');
    const cellHeight = h * DASHBOARD_ROW_HEIGHT + (h - 1) * DASHBOARD_GRID_GAP;
    // padding 24 + header 22 + label/value/description 83 + trend 28 + footer 18 + gaps 28
    expect(cellHeight).toBeGreaterThanOrEqual(203);
  });
});

describe('resolveLayouts', () => {
  it('keeps persisted layouts that fit and do not collide', () => {
    const result = resolveLayouts([
      { id: 'a', layout: { h: 2, w: 3, x: 0, y: 0 } },
      { id: 'b', layout: { h: 4, w: 6, x: 6, y: 0 } },
    ]);
    expect(result).toEqual({
      a: { h: 2, w: 3, x: 0, y: 0 },
      b: { h: 4, w: 6, x: 6, y: 0 },
    });
  });

  it('places items without a layout into the first free slot by output type', () => {
    const result = resolveLayouts([
      { id: 'a', layout: { h: 2, w: 3, x: 0, y: 0 } },
      { id: 'stat', outputType: 'stat' },
      { id: 'table', outputType: 'table' },
    ]);
    expect(result.stat).toEqual({ h: 3, w: 3, x: 3, y: 0 });
    // A full-width table cannot share rows 0-2 with anything.
    expect(result.table).toEqual({ h: 4, w: DASHBOARD_GRID_COLUMNS, x: 0, y: 3 });
  });

  it('re-places a colliding or out-of-grid layout instead of overlapping', () => {
    const result = resolveLayouts([
      { id: 'a', layout: { h: 2, w: 6, x: 0, y: 0 } },
      { id: 'overlap', layout: { h: 2, w: 6, x: 3, y: 0 } },
      { id: 'overflow', layout: { h: 2, w: 6, x: 10, y: 0 } },
    ]);
    expect(result.overlap).toEqual({ h: 2, w: 6, x: 6, y: 0 });
    expect(result.overflow).toEqual({ h: 2, w: 6, x: 0, y: 2 });
  });
});

describe('flowLayouts', () => {
  it('wraps items into rows in the given order', () => {
    expect(
      flowLayouts([
        { h: 2, id: 'a', w: 6 },
        { h: 4, id: 'b', w: 6 },
        { h: 2, id: 'c', w: 4 },
      ]),
    ).toEqual({
      a: { h: 2, w: 6, x: 0, y: 0 },
      b: { h: 4, w: 6, x: 6, y: 0 },
      c: { h: 2, w: 4, x: 0, y: 4 },
    });
  });

  it('clamps sizes into the grid', () => {
    expect(flowLayouts([{ h: 99, id: 'a', w: 99 }]).a).toEqual({ h: 12, w: 12, x: 0, y: 0 });
  });
});

describe('sortByPosition', () => {
  it('orders top to bottom, then left to right', () => {
    const layouts = {
      a: { h: 2, w: 3, x: 6, y: 0 },
      b: { h: 2, w: 3, x: 0, y: 2 },
      c: { h: 2, w: 3, x: 0, y: 0 },
    };
    expect(
      sortByPosition([{ id: 'a' }, { id: 'b' }, { id: 'c' }], layouts).map((i) => i.id),
    ).toEqual(['c', 'a', 'b']);
  });
});

describe('resizeByPointer', () => {
  // 12 columns of 64px with 16px gaps: one column plus its gap is 80px.
  const gridWidth = 12 * 64 + 11 * DASHBOARD_GRID_GAP;
  const rowStep = DASHBOARD_ROW_HEIGHT + DASHBOARD_GRID_GAP;

  it('snaps the dragged size to whole columns and rows', () => {
    expect(resizeByPointer({ h: 3, w: 4 }, { dx: 160, dy: rowStep }, gridWidth)).toEqual({
      h: 4,
      w: 6,
    });
    // Less than half a step does not move the edge.
    expect(resizeByPointer({ h: 3, w: 4 }, { dx: 30, dy: -30 }, gridWidth)).toEqual({
      h: 3,
      w: 4,
    });
  });

  it('stays within the board limits', () => {
    expect(resizeByPointer({ h: 3, w: 4 }, { dx: -2000, dy: -2000 }, gridWidth)).toEqual({
      h: 2,
      w: 3,
    });
    expect(resizeByPointer({ h: 3, w: 4 }, { dx: 5000, dy: 5000 }, gridWidth)).toEqual({
      h: 12,
      w: DASHBOARD_GRID_COLUMNS,
    });
  });

  it('changes only the height on a stacked board, keeping the saved width', () => {
    expect(
      resizeByPointer({ h: 3, w: 4 }, { dx: 400, dy: rowStep }, 360, { stacked: true }),
    ).toEqual({ h: 4, w: 4 });
  });

  it('keeps the size while the grid has no width yet', () => {
    expect(resizeByPointer({ h: 3, w: 4 }, { dx: 100, dy: 100 }, 0)).toEqual({ h: 3, w: 4 });
  });
});

describe('resizeByKey', () => {
  it('steps one column or row per arrow and ignores other keys', () => {
    expect(resizeByKey({ h: 3, w: 4 }, 'ArrowRight')).toEqual({ h: 3, w: 5 });
    expect(resizeByKey({ h: 3, w: 4 }, 'ArrowUp')).toEqual({ h: 2, w: 4 });
    expect(resizeByKey({ h: 2, w: 3 }, 'ArrowUp')).toEqual({ h: 2, w: 3 });
    expect(resizeByKey({ h: 3, w: 4 }, 'Enter')).toBeUndefined();
  });

  it('steps only the height on a stacked board', () => {
    expect(resizeByKey({ h: 3, w: 4 }, 'ArrowDown', { stacked: true })).toEqual({ h: 4, w: 4 });
    expect(resizeByKey({ h: 3, w: 4 }, 'ArrowRight', { stacked: true })).toBeUndefined();
  });
});