Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions libs/ngrx-toolkit/src/lib/with-undo-redo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
});
});
1 change: 1 addition & 0 deletions libs/ngrx-toolkit/src/lib/with-undo-redo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export function withUndoRedo<
clearStack(): void {
undoStack.splice(0);
redoStack.splice(0);
previous = null;
updateInternal();
},
})),
Expand Down