diagrams-fable-tauri/tests/history.test.ts
2026-07-02 23:22:21 +02:00

58 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { History } from '../src/state/history';
describe('History', () => {
it('starts with nothing to undo or redo', () => {
const h = new History('initial');
expect(h.canUndo).toBe(false);
expect(h.canRedo).toBe(false);
expect(h.undo()).toBeUndefined();
expect(h.redo()).toBeUndefined();
});
it('undoes and redoes pushed states in order', () => {
const h = new History('s0');
h.push('s1');
h.push('s2');
expect(h.undo()).toBe('s1');
expect(h.undo()).toBe('s0');
expect(h.canUndo).toBe(false);
expect(h.redo()).toBe('s1');
expect(h.redo()).toBe('s2');
expect(h.canRedo).toBe(false);
});
it('clears the redo stack on a new push after undo', () => {
const h = new History('s0');
h.push('s1');
h.push('s2');
h.undo(); // back to s1
h.push('s3');
expect(h.canRedo).toBe(false);
expect(h.undo()).toBe('s1');
expect(h.redo()).toBe('s3');
});
it('honors the depth limit by dropping oldest entries', () => {
const h = new History(0, 3);
for (let i = 1; i <= 10; i += 1) h.push(i);
let undos = 0;
while (h.canUndo) {
h.undo();
undos += 1;
}
expect(undos).toBe(3);
expect(h.peek()).toBe(7);
});
it('reset replaces state and clears both stacks', () => {
const h = new History('a');
h.push('b');
h.undo();
h.reset('fresh');
expect(h.canUndo).toBe(false);
expect(h.canRedo).toBe(false);
expect(h.peek()).toBe('fresh');
});
});