|
| 1 | +import { patchState, signalStore, type, withComputed, withMethods, withState } from '@ngrx/signals'; |
| 2 | +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; |
| 3 | +import { withUndoRedo } from './with-undo-redo'; |
| 4 | +import { addEntity, withEntities } from '@ngrx/signals/entities'; |
| 5 | +import { computed, inject } from '@angular/core'; |
| 6 | +import { withCallState } from './with-call-state'; |
| 7 | + |
| 8 | +const testState = { test: '' }; |
| 9 | +const testKeys = ['test' as const]; |
| 10 | +const newValue = 'new value'; |
| 11 | +const newerValue = 'newer value'; |
| 12 | + |
| 13 | +describe('withUndoRedo', () => { |
| 14 | + it('adds methods for undo, redo, canUndo, canRedo', () => { |
| 15 | + TestBed.runInInjectionContext(() => { |
| 16 | + const Store = signalStore(withState(testState), withUndoRedo({ keys: testKeys })); |
| 17 | + const store = new Store(); |
| 18 | + |
| 19 | + expect(Object.keys(store)).toEqual([ |
| 20 | + 'test', |
| 21 | + 'canUndo', |
| 22 | + 'canRedo', |
| 23 | + 'undo', |
| 24 | + 'redo' |
| 25 | + ]); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should check keys and collection types', () => { |
| 30 | + signalStore(withState(testState), |
| 31 | + // @ts-expect-error - should not allow invalid keys |
| 32 | + withUndoRedo({ keys: ['tes'] })); |
| 33 | + signalStore(withState(testState), |
| 34 | + withEntities({ entity: type(), collection: 'flight' }), |
| 35 | + // @ts-expect-error - should not allow invalid keys when entities are present |
| 36 | + withUndoRedo({ keys: ['flightIdsTest'] })); |
| 37 | + signalStore(withState(testState), |
| 38 | + // @ts-expect-error - should not allow collections without named entities |
| 39 | + withUndoRedo({ collections: ['tee'] })); |
| 40 | + signalStore(withState(testState), withComputed(store => ({ testComputed: computed(() => store.test()) })), |
| 41 | + // @ts-expect-error - should not allow collections without named entities with other computed |
| 42 | + withUndoRedo({ collections: ['tested'] })); |
| 43 | + signalStore(withEntities({ entity: type() }), |
| 44 | + // @ts-expect-error - should not allow collections without named entities |
| 45 | + withUndoRedo({ collections: ['test'] })); |
| 46 | + signalStore(withEntities({ entity: type(), collection: 'flight' }), |
| 47 | + // @ts-expect-error - should not allow invalid collections |
| 48 | + withUndoRedo({ collections: ['test'] })); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('undo and redo', () => { |
| 52 | + it('restores previous state for regular store key', fakeAsync(() => { |
| 53 | + TestBed.runInInjectionContext(() => { |
| 54 | + const Store = signalStore( |
| 55 | + withState(testState), |
| 56 | + withMethods(store => ({ updateTest: (newTest: string) => patchState(store, { test: newTest }) })), |
| 57 | + withUndoRedo({ keys: testKeys }) |
| 58 | + ); |
| 59 | + |
| 60 | + const store = new Store(); |
| 61 | + tick(1); |
| 62 | + |
| 63 | + store.updateTest(newValue); |
| 64 | + tick(1); |
| 65 | + expect(store.test()).toEqual(newValue); |
| 66 | + expect(store.canUndo()).toBe(true); |
| 67 | + expect(store.canRedo()).toBe(false); |
| 68 | + |
| 69 | + store.undo(); |
| 70 | + tick(1); |
| 71 | + |
| 72 | + expect(store.test()).toEqual(''); |
| 73 | + expect(store.canUndo()).toBe(false); |
| 74 | + expect(store.canRedo()).toBe(true); |
| 75 | + }); |
| 76 | + })); |
| 77 | + |
| 78 | + it('restores previous state for regular store key and respects skip', fakeAsync(() => { |
| 79 | + TestBed.runInInjectionContext(() => { |
| 80 | + const Store = signalStore( |
| 81 | + withState(testState), |
| 82 | + withMethods(store => ({ updateTest: (newTest: string) => patchState(store, { test: newTest }) })), |
| 83 | + withUndoRedo({ keys: testKeys, skip: 1 }) |
| 84 | + ); |
| 85 | + |
| 86 | + const store = new Store(); |
| 87 | + tick(1); |
| 88 | + |
| 89 | + store.updateTest(newValue); |
| 90 | + tick(1); |
| 91 | + expect(store.test()).toEqual(newValue); |
| 92 | + |
| 93 | + store.updateTest(newerValue); |
| 94 | + tick(1); |
| 95 | + |
| 96 | + store.undo(); |
| 97 | + tick(1); |
| 98 | + |
| 99 | + expect(store.test()).toEqual(newValue); |
| 100 | + expect(store.canUndo()).toBe(false); |
| 101 | + |
| 102 | + store.undo(); |
| 103 | + tick(1); |
| 104 | + |
| 105 | + // should not change |
| 106 | + expect(store.test()).toEqual(newValue); |
| 107 | + }); |
| 108 | + })); |
| 109 | + |
| 110 | + it('undoes and redoes previous state for entity', fakeAsync(() => { |
| 111 | + const Store = signalStore( |
| 112 | + withEntities({ entity: type<{ id: string }>() }), |
| 113 | + withMethods(store => ({ |
| 114 | + addEntity: (newTest: string) => patchState(store, addEntity({ id: newTest })) |
| 115 | + })), |
| 116 | + withUndoRedo() |
| 117 | + ); |
| 118 | + TestBed.configureTestingModule({ providers: [Store] }); |
| 119 | + TestBed.runInInjectionContext(() => { |
| 120 | + const store = inject(Store); |
| 121 | + tick(1); |
| 122 | + expect(store.entities()).toEqual([]); |
| 123 | + expect(store.canUndo()).toBe(false); |
| 124 | + expect(store.canRedo()).toBe(false); |
| 125 | + |
| 126 | + store.addEntity(newValue); |
| 127 | + tick(1); |
| 128 | + expect(store.entities()).toEqual([{ id: newValue }]); |
| 129 | + expect(store.canUndo()).toBe(true); |
| 130 | + expect(store.canRedo()).toBe(false); |
| 131 | + |
| 132 | + store.addEntity(newerValue); |
| 133 | + tick(1); |
| 134 | + expect(store.entities()).toEqual([{ id: newValue }, { id: newerValue }]); |
| 135 | + expect(store.canUndo()).toBe(true); |
| 136 | + expect(store.canRedo()).toBe(false); |
| 137 | + |
| 138 | + store.undo(); |
| 139 | + |
| 140 | + expect(store.entities()).toEqual([{ id: newValue }]); |
| 141 | + expect(store.canUndo()).toBe(true); |
| 142 | + expect(store.canRedo()).toBe(true); |
| 143 | + |
| 144 | + store.undo(); |
| 145 | + |
| 146 | + expect(store.entities()).toEqual([]); |
| 147 | + expect(store.canUndo()).toBe(false); |
| 148 | + expect(store.canRedo()).toBe(true); |
| 149 | + |
| 150 | + store.redo(); |
| 151 | + tick(1); |
| 152 | + |
| 153 | + expect(store.entities()).toEqual([{ id: newValue }]); |
| 154 | + expect(store.canUndo()).toBe(true); |
| 155 | + expect(store.canRedo()).toBe(true); |
| 156 | + |
| 157 | + // should return canRedo=false after a change |
| 158 | + store.addEntity('newest'); |
| 159 | + tick(1); |
| 160 | + expect(store.canUndo()).toBe(true); |
| 161 | + expect(store.canRedo()).toBe(false); |
| 162 | + }); |
| 163 | + })); |
| 164 | + |
| 165 | + it('restores previous state for named entity', fakeAsync(() => { |
| 166 | + TestBed.runInInjectionContext(() => { |
| 167 | + const Store = signalStore( |
| 168 | + withEntities({ entity: type<{ id: string }>(), collection: 'flight' }), |
| 169 | + withMethods(store => ({ |
| 170 | + addEntity: (newTest: string) => patchState(store, addEntity({ id: newTest }, { collection: 'flight' })) |
| 171 | + })), |
| 172 | + withCallState({ collection: 'flight' }), |
| 173 | + withUndoRedo({ collections: ['flight'] }) |
| 174 | + ); |
| 175 | + |
| 176 | + const store = new Store(); |
| 177 | + tick(1); |
| 178 | + |
| 179 | + store.addEntity(newValue); |
| 180 | + tick(1); |
| 181 | + expect(store.flightEntities()).toEqual([{ id: newValue }]); |
| 182 | + expect(store.canUndo()).toBe(true); |
| 183 | + expect(store.canRedo()).toBe(false); |
| 184 | + |
| 185 | + store.undo(); |
| 186 | + tick(1); |
| 187 | + |
| 188 | + expect(store.flightEntities()).toEqual([]); |
| 189 | + expect(store.canUndo()).toBe(false); |
| 190 | + expect(store.canRedo()).toBe(true); |
| 191 | + }); |
| 192 | + })); |
| 193 | + }); |
| 194 | +}); |
0 commit comments