83 lines
4.7 KiB
TypeScript
83 lines
4.7 KiB
TypeScript
import { defaultDashboardLayout, normalizeDashboardLayout, WIDGETS } from './dashboardLayout';
|
|
|
|
test('legacy preferences migrate to bounded geometry while preserving order, visibility and revision', () => {
|
|
const input = { version: 1, revision: 7, widgets: [
|
|
{ id: 'skills', visible: false, size: 'full' },
|
|
{ id: 'attention', visible: true, size: 'standard' },
|
|
{ id: 'cards', visible: true, size: 'full' },
|
|
] };
|
|
const original = JSON.stringify(input);
|
|
const result = normalizeDashboardLayout(input);
|
|
expect(result.version).toBe(2);
|
|
expect(result.revision).toBe(7);
|
|
expect(result.widgets.map(row => row.id).slice(0, 3)).toEqual(['skills', 'attention', 'cards']);
|
|
expect(result.widgets[0]).toMatchObject({ visible: false, size: 'full', w: 12 });
|
|
expect(result.widgets[1]).toMatchObject({ visible: true, size: 'standard', x: 0, y: 0, w: 6 });
|
|
expect(result.widgets[2]).toMatchObject({ visible: true, size: 'full', x: 0, w: 12 });
|
|
expect(result.widgets.slice(3).every(row => !row.visible)).toBe(true);
|
|
expect(normalizeDashboardLayout(input)).toEqual(result);
|
|
expect(JSON.stringify(input)).toBe(original);
|
|
});
|
|
|
|
test('schema two preserves chosen hidden geometry without resetting the owner revision', () => {
|
|
const result = normalizeDashboardLayout({ version: 2, revision: 12, widgets: [
|
|
{ id: 'attention', visible: false, size: 'standard', x: 4, y: 17, w: 8, h: 10 },
|
|
] });
|
|
expect(result.version).toBe(2);
|
|
expect(result.revision).toBe(12);
|
|
expect(result.widgets[0]).toEqual({ id: 'attention', visible: false, size: 'standard', x: 4, y: 17, w: 8, h: 10 });
|
|
expect(result.widgets.slice(1).every(row => !row.visible)).toBe(true);
|
|
});
|
|
|
|
test('default catalogue preserves every section and supported widths', () => {
|
|
const layout = defaultDashboardLayout();
|
|
expect(new Set(layout.widgets.map(w => w.id))).toEqual(new Set(WIDGETS.map(w => w.id)));
|
|
expect(layout.widgets.every(w => w.visible)).toBe(true);
|
|
expect(WIDGETS[0].sizes).toEqual(['full']);
|
|
layout.widgets[0].visible = false;
|
|
expect(defaultDashboardLayout().widgets[0].visible).toBe(true);
|
|
});
|
|
|
|
test('initial dashboard keeps every widget in a denser bounded layout without resizing saved geometry', () => {
|
|
const fresh = defaultDashboardLayout();
|
|
expect(fresh.widgets).toHaveLength(10);
|
|
expect(fresh.widgets.every(widget => widget.visible)).toBe(true);
|
|
expect(Math.max(...fresh.widgets.map(widget => widget.y + widget.h))).toBeLessThanOrEqual(42);
|
|
const saved = { version: 2, revision: 4, widgets: [
|
|
{ id: 'sources', visible: true, size: 'standard', x: 0, y: 14, w: 6, h: 8 },
|
|
{ id: 'salary', visible: false, size: 'full', x: 0, y: 26, w: 12, h: 12 },
|
|
] };
|
|
const normalized = normalizeDashboardLayout(saved);
|
|
expect(normalized.revision).toBe(4);
|
|
expect(normalized.widgets.slice(0, 2)).toEqual(saved.widgets);
|
|
expect(normalized.widgets.slice(2).every(widget => !widget.visible)).toBe(true);
|
|
});
|
|
|
|
test.each([1, 2])('new schema %s preferences use task-first A1 defaults without omitting widgets', version => {
|
|
const layout = normalizeDashboardLayout({ version, revision: 0, widgets: [] });
|
|
expect(layout.widgets[0]).toMatchObject({ id: 'attention', visible: true, x: 0, y: 0, w: 12 });
|
|
expect(layout.widgets.findIndex(widget => widget.id === 'cards')).toBeGreaterThan(layout.widgets.findIndex(widget => widget.id === 'funnel'));
|
|
expect(layout.widgets).toHaveLength(WIDGETS.length);
|
|
const visible = layout.widgets.filter(widget => widget.visible);
|
|
expect(visible.every(widget => widget.x >= 0 && widget.x + widget.w <= 12 && widget.h <= 24 && widget.y + widget.h <= 256)).toBe(true);
|
|
expect(visible.some((a, index) => visible.slice(index + 1).some(b => a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y))).toBe(false);
|
|
});
|
|
|
|
test('saved layouts retain known order, reject duplicates and append missing sections hidden', () => {
|
|
const result = normalizeDashboardLayout({ version: 1, revision: 7, widgets: [
|
|
{ id: 'skills', visible: false, size: 'full' },
|
|
{ id: 'unknown', visible: true, size: 'full' },
|
|
{ id: 'skills', visible: true, size: 'standard' },
|
|
{ id: 'cards', visible: 'yes', size: 'standard' },
|
|
] });
|
|
expect(result.revision).toBe(7);
|
|
expect(result.widgets[0]).toMatchObject({ id: 'skills', visible: false, size: 'full' });
|
|
expect(result.widgets[1]).toMatchObject({ id: 'cards', visible: false, size: 'full' });
|
|
expect(result.widgets.slice(2).every(w => !w.visible)).toBe(true);
|
|
expect(result.widgets).toHaveLength(WIDGETS.length);
|
|
});
|
|
|
|
test.each([null, {}, { version: 3, revision: 8, widgets: [] }, { version: 1, revision: -1, widgets: [] }])('invalid input has safe defaults: %j', input => {
|
|
expect(normalizeDashboardLayout(input)).toEqual(defaultDashboardLayout());
|
|
});
|