|
| 1 | +import { |
| 2 | + getState, |
| 3 | + patchState, |
| 4 | + signalStore, |
| 5 | + signalStoreFeature, |
| 6 | + withHooks, |
| 7 | + withMethods, |
| 8 | + withState, |
| 9 | +} from '@ngrx/signals'; |
| 10 | +import { emptyFeature, withConditional } from './with-conditional'; |
| 11 | +import { inject, InjectionToken } from '@angular/core'; |
| 12 | +import { TestBed } from '@angular/core/testing'; |
| 13 | +import { withDevtools } from './devtools/with-devtools'; |
| 14 | + |
| 15 | +describe('withConditional', () => { |
| 16 | + const withUser = signalStoreFeature( |
| 17 | + withState({ id: 0, name: '' }), |
| 18 | + withHooks((store) => ({ |
| 19 | + onInit() { |
| 20 | + patchState(store, { id: 1, name: 'Konrad' }); |
| 21 | + }, |
| 22 | + })) |
| 23 | + ); |
| 24 | + |
| 25 | + const withFakeUser = signalStoreFeature( |
| 26 | + withState({ id: 0, name: 'Tommy Fake' }) |
| 27 | + ); |
| 28 | + |
| 29 | + for (const isReal of [true, false]) { |
| 30 | + it(`should ${isReal ? '' : 'not '} enable withUser`, () => { |
| 31 | + const REAL_USER_TOKEN = new InjectionToken('REAL_USER', { |
| 32 | + providedIn: 'root', |
| 33 | + factory: () => isReal, |
| 34 | + }); |
| 35 | + const UserStore = signalStore( |
| 36 | + { providedIn: 'root' }, |
| 37 | + withConditional(() => inject(REAL_USER_TOKEN), withUser, withFakeUser) |
| 38 | + ); |
| 39 | + const userStore = TestBed.inject(UserStore); |
| 40 | + |
| 41 | + if (isReal) { |
| 42 | + expect(getState(userStore)).toEqual({ id: 1, name: 'Konrad' }); |
| 43 | + } else { |
| 44 | + expect(getState(userStore)).toEqual({ id: 0, name: 'Tommy Fake' }); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + it(`should access the store`, () => { |
| 50 | + const UserStore = signalStore( |
| 51 | + { providedIn: 'root' }, |
| 52 | + withMethods(() => ({ |
| 53 | + useRealUser: () => true, |
| 54 | + })), |
| 55 | + withConditional((store) => store.useRealUser(), withUser, withFakeUser) |
| 56 | + ); |
| 57 | + const userStore = TestBed.inject(UserStore); |
| 58 | + |
| 59 | + expect(getState(userStore)).toEqual({ id: 1, name: 'Konrad' }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should be used inside a signalStoreFeature', () => { |
| 63 | + const withConditionalUser = (activate: boolean) => |
| 64 | + signalStoreFeature( |
| 65 | + withConditional(() => activate, withUser, withFakeUser) |
| 66 | + ); |
| 67 | + |
| 68 | + const UserStore = signalStore( |
| 69 | + { providedIn: 'root' }, |
| 70 | + withConditionalUser(true) |
| 71 | + ); |
| 72 | + const userStore = TestBed.inject(UserStore); |
| 73 | + |
| 74 | + expect(getState(userStore)).toEqual({ id: 1, name: 'Konrad' }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should ensure that both features return the same type', () => { |
| 78 | + const withUser = signalStoreFeature( |
| 79 | + withState({ id: 0, name: '' }), |
| 80 | + withHooks((store) => ({ |
| 81 | + onInit() { |
| 82 | + patchState(store, { id: 1, name: 'Konrad' }); |
| 83 | + }, |
| 84 | + })) |
| 85 | + ); |
| 86 | + |
| 87 | + const withFakeUser = signalStoreFeature( |
| 88 | + withState({ id: 0, firstname: 'Tommy Fake' }) |
| 89 | + ); |
| 90 | + |
| 91 | + // @ts-expect-error withFakeUser has a different state shape |
| 92 | + signalStore(withConditional(() => true, withUser, withFakeUser)); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should also work with empty features', () => { |
| 96 | + signalStore( |
| 97 | + withConditional( |
| 98 | + () => true, |
| 99 | + withDevtools('dummy'), |
| 100 | + signalStoreFeature(withState({})) |
| 101 | + ) |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should work with `emptyFeature` if falsy is skipped', () => { |
| 106 | + signalStore( |
| 107 | + withConditional( |
| 108 | + () => true, |
| 109 | + signalStoreFeature(withState({})), |
| 110 | + emptyFeature |
| 111 | + ) |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not work with `emptyFeature` if feature is not empty', () => { |
| 116 | + signalStore( |
| 117 | + withConditional( |
| 118 | + () => true, |
| 119 | + // @ts-expect-error feature is not empty |
| 120 | + () => signalStoreFeature(withState({ x: 1 })), |
| 121 | + emptyFeature |
| 122 | + ) |
| 123 | + ); |
| 124 | + }); |
| 125 | +}); |
0 commit comments