Skip to content

Commit 64b77e3

Browse files
committed
undo-state.component: add limit support in insert()
1 parent 3dc350f commit 64b77e3

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

projects/angular-redux2/undo/src/components/undo-state.component.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,90 @@ describe('NgUndoStateActions', () => {
3333

3434
expect(nextState).toBe(currentState); // Ensure the current state is returned
3535
});
36+
37+
test('should return the currentState if snapshot is undefined', () => {
38+
const key = 'test';
39+
const settings = {
40+
path: [],
41+
filter: () => true,
42+
limit: 5
43+
};
44+
45+
const currentState = { [HISTORY_STATE_KEY]: { [key]: { past: [] } } };
46+
const snapshot = undefined;
47+
const actions = new NgUndoStateActions(key, settings);
48+
const result = actions.insert(currentState, snapshot);
49+
50+
51+
expect(result).toBe(currentState);
52+
});
53+
54+
test('should return the currentState if the filter function returns false', () => {
55+
const key = 'test';
56+
const settings = {
57+
path: [],
58+
filter: () => false,
59+
limit: 5
60+
};
61+
const currentState = { [HISTORY_STATE_KEY]: { [key]: { past: [] } } };
62+
const snapshot = 'snapshot';
63+
const actions = new NgUndoStateActions(key, settings);
64+
const result = actions.insert(currentState, snapshot);
65+
66+
expect(result).toBe(currentState);
67+
});
68+
69+
test('should insert the snapshot into the past array and update the state', () => {
70+
const key = 'test';
71+
const settings = {
72+
path: [],
73+
filter: () => true,
74+
limit: 5
75+
};
76+
const currentState = { [HISTORY_STATE_KEY]: { [key]: { past: [ 'past1', 'past2' ] } } };
77+
const snapshot = 'snapshot';
78+
const expectedState = {
79+
[HISTORY_STATE_KEY]: {
80+
[key]: {
81+
past: [ 'past1', 'past2', 'snapshot' ],
82+
future: []
83+
}
84+
}
85+
};
86+
87+
const actions = new NgUndoStateActions(key, settings);
88+
const result = actions.insert(currentState, snapshot);
89+
90+
expect(result).toEqual(expectedState);
91+
});
92+
93+
test('should remove the oldest past snapshot if the past array exceeds the limit', () => {
94+
const key = 'test';
95+
const settings = {
96+
path: [],
97+
filter: () => true,
98+
limit: 2
99+
};
100+
const currentState = {
101+
[HISTORY_STATE_KEY]: {
102+
[key]: { past: [ 'past1', 'past2', 'past3' ] }
103+
}
104+
};
105+
const snapshot = 'snapshot';
106+
const expectedState = {
107+
[HISTORY_STATE_KEY]: {
108+
[key]: {
109+
past: [ 'past2', 'past3', 'snapshot' ],
110+
future: []
111+
}
112+
}
113+
};
114+
115+
const actions = new NgUndoStateActions(key, settings);
116+
const result = actions.insert(currentState, snapshot);
117+
118+
expect(result).toEqual(expectedState);
119+
});
36120
});
37121

38122
describe('undo', () => {

projects/angular-redux2/undo/src/components/undo-state.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import { get, set } from '@angular-redux2/store';
88
* angular-redux2/undo
99
*/
1010

11-
import { HISTORY_STATE_KEY, Settings } from '../interfaces/undo.interface';
11+
import { HISTORY_STATE_KEY } from '../interfaces/undo.interface';
12+
13+
/**
14+
* angular-redux2/undo types
15+
*/
16+
17+
import type { Settings } from '../interfaces/undo.interface';
1218

1319
/**
1420
* Represents the actions for managing undo state.
@@ -76,9 +82,12 @@ export class NgUndoStateActions {
7682
}
7783

7884
const { state, undoState } = this.getStates(currentState);
79-
8085
undoState.future = [];
8186

87+
if (undoState.past.length >= this.settings.limit) {
88+
undoState.past = undoState.past.slice(1);
89+
}
90+
8291
return set(state, [ HISTORY_STATE_KEY, this.key, 'past' ], [
8392
...undoState.past,
8493
snapshot
@@ -184,9 +193,8 @@ export class NgUndoStateActions {
184193
const snapshot = get(state, this.path);
185194
const activeSnapshot = undoState.future[index];
186195
const future = undoState.future.slice(index + 1);
187-
const past = undoState.past.concat([ snapshot ], undoState.future.slice(0, index));
188196

189-
undoState.past = past;
197+
undoState.past = undoState.past.concat([ snapshot ], undoState.future.slice(0, index));
190198
undoState.future = future;
191199

192200
return set(state, this.path, activeSnapshot);

0 commit comments

Comments
 (0)