Files
site-builder/craft/src/editor/spacing-drag.test.ts
T
shadowdao 092c8f6747 feat(site-builder): drag padding handles on the canvas
Edge handles on a selected Section/Container/Columns write padding via the
per-device setter as one undo burst; Alt = opposite side, Shift = all four,
Ctrl/Cmd snaps to 8px, Escape cancels.

Refs #56
2026-10-07 12:02:32 -07:00

64 lines
3.4 KiB
TypeScript

import { describe, test, expect } from 'vitest';
import { computePadding, sidesFor, readPaddingPx, parsePx, isLegacyShorthand } from './spacing-drag';
describe('sidesFor', () => {
test('plain / alt / shift', () => {
expect(sidesFor('top', false, false)).toEqual(['top']);
expect(sidesFor('top', true, false)).toEqual(['top', 'bottom']);
expect(sidesFor('left', true, false)).toEqual(['left', 'right']);
expect(sidesFor('right', false, true).sort()).toEqual(['bottom', 'left', 'right', 'top']);
expect(sidesFor('bottom', true, true)).toHaveLength(4);
});
});
describe('computePadding', () => {
const base = { start: 100, startValue: 40 };
test('top/left grow with positive delta, bottom/right with negative', () => {
expect(computePadding({ ...base, current: 124, side: 'top' }).sides).toEqual({ paddingTop: '64px' });
expect(computePadding({ ...base, current: 124, side: 'left' }).sides).toEqual({ paddingLeft: '64px' });
expect(computePadding({ ...base, current: 76, side: 'bottom' }).sides).toEqual({ paddingBottom: '64px' });
expect(computePadding({ ...base, current: 76, side: 'right' }).sides).toEqual({ paddingRight: '64px' });
expect(computePadding({ ...base, current: 124, side: 'bottom' }).sides).toEqual({ paddingBottom: '16px' });
});
test('clamps to 0..400', () => {
expect(computePadding({ ...base, current: -500, side: 'top' }).value).toBe(0);
expect(computePadding({ ...base, current: 9999, side: 'top' }).value).toBe(400);
});
test('alt writes both opposite sides, shift all four, with the same value', () => {
expect(computePadding({ ...base, current: 110, side: 'top', alt: true }).sides).toEqual({ paddingTop: '50px', paddingBottom: '50px' });
const all = computePadding({ ...base, current: 110, side: 'left', shift: true }).sides;
expect(Object.keys(all).sort()).toEqual(['paddingBottom', 'paddingLeft', 'paddingRight', 'paddingTop']);
expect(new Set(Object.values(all))).toEqual(new Set(['50px']));
});
test('snap rounds to the 8px grid; default is 1px', () => {
expect(computePadding({ ...base, current: 113, side: 'top' }).value).toBe(53);
expect(computePadding({ ...base, current: 113, side: 'top', snap: true }).value).toBe(56);
expect(computePadding({ ...base, current: 110.4, side: 'top' }).value).toBe(50);
});
});
describe('readPaddingPx', () => {
test('longhand wins over everything', () => {
expect(readPaddingPx({ paddingTop: '12px', padding: '40px 0' }, 'top', '99px')).toBe(12);
});
test('shorthand token when longhand empty (CSS order)', () => {
expect(readPaddingPx({ padding: '40px 0' }, 'top')).toBe(40);
expect(readPaddingPx({ padding: '40px 0' }, 'left')).toBe(0);
expect(readPaddingPx({ padding: '1px 2px 3px 4px' }, 'bottom')).toBe(3);
});
test('falls back to the stubbed computed value, then 0', () => {
expect(readPaddingPx({}, 'top', '17px')).toBe(17);
expect(readPaddingPx({ paddingTop: '5%' }, 'top', '23px')).toBe(23);
expect(readPaddingPx({ padding: '2em' }, 'top', '32px')).toBe(32);
expect(readPaddingPx({}, 'top', '')).toBe(0);
expect(readPaddingPx({}, 'top')).toBe(0);
});
test('parsePx / isLegacyShorthand', () => {
expect(parsePx('12px')).toBe(12);
expect(parsePx('auto')).toBeNull();
expect(isLegacyShorthand({ padding: '4px' })).toBe(true);
expect(isLegacyShorthand({ padding: '4px', paddingTop: '1px' })).toBe(false);
expect(isLegacyShorthand({ padding: '', paddingTop: '' })).toBe(false);
});
});