diff --git a/libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts b/libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts index 17cfa075..5d6a7b28 100644 --- a/libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts +++ b/libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts @@ -210,5 +210,36 @@ describe('withUndoRedo', () => { expect(store.canUndo()).toBe(false); expect(store.canRedo()).toBe(false); }) + + it('cannot undo after clearing and setting a new value', fakeAsync(() => { + const Store = signalStore( + { providedIn: 'root' }, + withState(testState), + withMethods((store) => ({ + update: (value: string) => patchState(store, { test: value }), + })), + withUndoRedo({ keys: testKeys }) + ); + + const store = TestBed.inject(Store); + + store.update('Alan'); + tick(1); + + store.update('Gordon'); + tick(1); + + store.clearStack(); + tick(1); + + // After clearing the undo/redo stack, there is no previous item anymore. + // The following update becomes the first value. + // Since there is no other value before, it cannot be undone. + store.update('Hugh'); + tick(1); + + expect(store.canUndo()).toBe(false); + expect(store.canRedo()).toBe(false); + })); }); }); diff --git a/libs/ngrx-toolkit/src/lib/with-undo-redo.ts b/libs/ngrx-toolkit/src/lib/with-undo-redo.ts index d94953fd..22a5d8f1 100644 --- a/libs/ngrx-toolkit/src/lib/with-undo-redo.ts +++ b/libs/ngrx-toolkit/src/lib/with-undo-redo.ts @@ -130,6 +130,7 @@ export function withUndoRedo< clearStack(): void { undoStack.splice(0); redoStack.splice(0); + previous = null; updateInternal(); }, })),