-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathhistory-option.test.ts
More file actions
80 lines (66 loc) · 2.76 KB
/
Copy pathhistory-option.test.ts
File metadata and controls
80 lines (66 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { describe, expect, it, vi } from 'vitest';
import { createStore, batch } from './store';
describe('createStore history option', () => {
it('undo()/redo() round-trip a plain setState', () => {
const store = createStore(() => ({ count: 0 }), { history: { limit: 50 } });
store.setState({ count: 1 });
store.setState({ count: 2 });
store.undo();
expect(store.getState().count).toBe(1);
store.undo();
expect(store.getState().count).toBe(0);
store.redo();
expect(store.getState().count).toBe(1);
});
it('batch() collapses to exactly one history entry', () => {
const store = createStore((set) => ({
count: 0,
increment: () => set((s) => ({ count: s.count + 1 })),
}), { history: { limit: 50 } });
batch(() => {
store.getState().increment();
store.getState().increment();
store.getState().increment();
});
expect(store.getHistory().past.length).toBe(1);
store.undo();
expect(store.getState().count).toBe(0);
});
it('undo() throws when history option is not set', () => {
const store = createStore(() => ({ count: 0 }));
expect(() => store.undo()).toThrow(/requires the "history" option/);
});
it('undo() throws when called inside batch()', () => {
const store = createStore(() => ({ count: 0 }), { history: {} });
expect(() => {
batch(() => {
store.undo();
});
}).toThrow(/cannot be called inside batch/);
});
it('coalesceMs merges updates that occur within the window (timestamp-based, not timer-based)', () => {
vi.useFakeTimers();
vi.setSystemTime(0);
const store = createStore(() => ({ n: 0 }), { history: { coalesceMs: 300 } });
store.setState({ n: 1 });
vi.setSystemTime(100); // still inside the 300ms window
store.setState({ n: 2 });
expect(store.getHistory().past.length).toBe(1);
vi.setSystemTime(500); // window has elapsed
store.setState({ n: 3 });
expect(store.getHistory().past.length).toBe(2);
vi.useRealTimers();
});
it('limit caps the past stack', () => {
const store = createStore(() => ({ n: 0 }), { history: { limit: 3 } });
for (let i = 1; i <= 5; i++) store.setState({ n: i });
expect(store.getHistory().past.length).toBe(3);
});
it('resetHistory() clears both stacks without touching state', () => {
const store = createStore(() => ({ n: 0 }), { history: {} });
store.setState({ n: 1 });
store.resetHistory();
expect(store.getHistory()).toEqual({ past: [], future: [] });
expect(store.getState().n).toBe(1);
});
});