|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { patchState, signalMethod, signalStore, withMethods, withState } from '@ngrx/signals'; |
| 3 | +import { HypermediaResourceData } from './with-hypermedia-resource'; |
| 4 | +import { Injector, provideZonelessChangeDetection, runInInjectionContext } from '@angular/core'; |
| 5 | +import { firstValueFrom, timer } from 'rxjs'; |
| 6 | +import { withExperimentalDeepWritableStateCopy } from './with-deep-writable-state-copy'; |
| 7 | + |
| 8 | +type TestModel = { |
| 9 | + numProp: number, |
| 10 | + objProp: { |
| 11 | + stringProp: string, |
| 12 | + numProp: number, |
| 13 | + }, |
| 14 | + _links?: { |
| 15 | + self: { href: string } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const initialTestModel: TestModel = { |
| 20 | + numProp: 1, |
| 21 | + objProp: { |
| 22 | + stringProp: 'initial string', |
| 23 | + numProp: 42, |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +const testModelWithMetadata: TestModel = { |
| 28 | + numProp: 1, |
| 29 | + objProp: { |
| 30 | + stringProp: 'test model string', |
| 31 | + numProp: 50, |
| 32 | + _metaProp: 'Metadata' |
| 33 | + } as { stringProp: string, numProp: number, _metaProp: string } |
| 34 | +}; |
| 35 | + |
| 36 | +const TestStore = signalStore( |
| 37 | + { providedIn: 'root' }, |
| 38 | + withState<HypermediaResourceData<'model', TestModel>>({ model: initialTestModel }), |
| 39 | + withExperimentalDeepWritableStateCopy(store => ({ |
| 40 | + writableObjProp: store.model.objProp, |
| 41 | + subObjectWithDeepStateCopies: { |
| 42 | + writableNumProp: store.model.objProp.numProp |
| 43 | + } |
| 44 | + })), |
| 45 | + withMethods(store => ({ |
| 46 | + setModel(model: TestModel) { |
| 47 | + patchState(store, { model }); |
| 48 | + } |
| 49 | + })) |
| 50 | +); |
| 51 | + |
| 52 | +describe('withExperimentalDeepWritableStateCopy', () => { |
| 53 | + |
| 54 | + let store: InstanceType<typeof TestStore>; |
| 55 | + let injector: Injector; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + TestBed.configureTestingModule({ providers: [provideZonelessChangeDetection()] }); |
| 59 | + store = TestBed.inject(TestStore); |
| 60 | + injector = TestBed.inject(Injector); |
| 61 | + }); |
| 62 | + |
| 63 | + it('sets correct initial copy state', () => { |
| 64 | + expect(store.writableObjProp()).toBe(initialTestModel.objProp); |
| 65 | + expect(store.subObjectWithDeepStateCopies.writableNumProp()).toBe(initialTestModel.objProp.numProp); |
| 66 | + }); |
| 67 | + |
| 68 | + it('patches a signal inside the writable state', () => { |
| 69 | + const writableSignal = store.writableObjProp; |
| 70 | + |
| 71 | + expect(store.model.objProp.stringProp()).toBe('initial string'); |
| 72 | + expect(store.writableObjProp()).toBe(initialTestModel.objProp); |
| 73 | + |
| 74 | + writableSignal.set({ stringProp: 'mutated string', numProp: 42 }); |
| 75 | + |
| 76 | + expect(store.model.objProp.stringProp()).toBe(initialTestModel.objProp.stringProp); |
| 77 | + expect(store.writableObjProp()).toEqual({ stringProp: 'mutated string', numProp: 42 }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('provides data which is not in type definition', () => { |
| 81 | + expect(store.model.objProp.stringProp()).toBe('initial string'); |
| 82 | + expect(store.writableObjProp()).toBe(initialTestModel.objProp); |
| 83 | + |
| 84 | + store.setModel(testModelWithMetadata); |
| 85 | + |
| 86 | + expect(store.model.objProp.stringProp()).toBe('test model string'); |
| 87 | + expect((store.model.objProp() as Record<string, unknown>)['_metaProp']).toBe('Metadata'); |
| 88 | + expect(store.writableObjProp().stringProp).toBe('test model string'); |
| 89 | + expect((store.writableObjProp() as Record<string, unknown>)['_metaProp']).toBe('Metadata'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('overrides state of writable copy', () => { |
| 93 | + expect(store.model.objProp.stringProp()).toBe('initial string'); |
| 94 | + expect(store.writableObjProp()).toBe(initialTestModel.objProp); |
| 95 | + |
| 96 | + store.writableObjProp.set({ stringProp: 'overridden string', numProp: 42 }); |
| 97 | + |
| 98 | + expect(store.model.objProp.stringProp()).toBe('initial string'); |
| 99 | + expect(store.writableObjProp().stringProp).toBe('overridden string'); |
| 100 | + |
| 101 | + store.setModel(testModelWithMetadata); |
| 102 | + |
| 103 | + expect(store.model.objProp.stringProp()).toBe('test model string'); |
| 104 | + expect(store.writableObjProp().stringProp).toBe('test model string'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('triggers deep computed signals on writable state copy', async () => { |
| 108 | + |
| 109 | + let stringPropTriggered = false; |
| 110 | + let numPropTriggered = false; |
| 111 | + |
| 112 | + runInInjectionContext(injector, () => { |
| 113 | + signalMethod(() => stringPropTriggered = true)(store.writableObjProp.stringProp); |
| 114 | + signalMethod(() => numPropTriggered = true)(store.writableObjProp.numProp); |
| 115 | + }); |
| 116 | + |
| 117 | + await firstValueFrom(timer(0)); |
| 118 | + stringPropTriggered = false; |
| 119 | + numPropTriggered = false; |
| 120 | + |
| 121 | + store.writableObjProp.set({ stringProp: 'overridden string', numProp: 42 }); |
| 122 | + |
| 123 | + await firstValueFrom(timer(10)); |
| 124 | + |
| 125 | + expect(stringPropTriggered).toBe(true); |
| 126 | + expect(numPropTriggered).toBe(false); |
| 127 | + |
| 128 | + stringPropTriggered = false; |
| 129 | + numPropTriggered = false; |
| 130 | + |
| 131 | + store.writableObjProp.set({ stringProp: 'next overridden string', numProp: 100 }); |
| 132 | + |
| 133 | + await firstValueFrom(timer(0)); |
| 134 | + |
| 135 | + expect(stringPropTriggered).toBe(true); |
| 136 | + expect(numPropTriggered).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | +}); |
0 commit comments