Skip to content

Commit c372275

Browse files
committed
fix(undo-redo): unset previous item when "clearStack" is executed
1 parent 3e5b806 commit c372275

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,36 @@ describe('withUndoRedo', () => {
210210
expect(store.canUndo()).toBe(false);
211211
expect(store.canRedo()).toBe(false);
212212
})
213+
214+
it('cannot undo after clearing and setting a new value', fakeAsync(() => {
215+
const Store = signalStore(
216+
{ providedIn: 'root' },
217+
withState(testState),
218+
withMethods((store) => ({
219+
update: (value: string) => patchState(store, { test: value }),
220+
})),
221+
withUndoRedo({ keys: testKeys })
222+
);
223+
224+
const store = TestBed.inject(Store);
225+
226+
store.update('Alan');
227+
tick(1);
228+
229+
store.update('Gordon');
230+
tick(1);
231+
232+
store.clearStack();
233+
tick(1);
234+
235+
// After clearing the undo/redo stack, there is no previous item anymore.
236+
// The following update becomes the first value.
237+
// Since there is no other value before, it cannot be undone.
238+
store.update('Hugh');
239+
tick(1);
240+
241+
expect(store.canUndo()).toBe(false);
242+
expect(store.canRedo()).toBe(false);
243+
}));
213244
});
214245
});

libs/ngrx-toolkit/src/lib/with-undo-redo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export function withUndoRedo<
130130
clearStack(): void {
131131
undoStack.splice(0);
132132
redoStack.splice(0);
133+
previous = null;
133134
updateInternal();
134135
},
135136
})),

0 commit comments

Comments
 (0)