|
| 1 | +import { expecter } from 'ts-snippet'; |
| 2 | +import { compilerOptions } from './helpers'; |
| 3 | + |
| 4 | +describe('unprotected', () => { |
| 5 | + const expectSnippet = expecter( |
| 6 | + (code) => ` |
| 7 | + import { computed, inject } from '@angular/core'; |
| 8 | + import { signalStore, withState, withComputed } from '@ngrx/signals'; |
| 9 | + import { unprotected } from '@ngrx/signals/testing'; |
| 10 | +
|
| 11 | + ${code} |
| 12 | + `, |
| 13 | + compilerOptions() |
| 14 | + ); |
| 15 | + |
| 16 | + it('replaces StateSource with WritableStateSource', () => { |
| 17 | + const snippet = ` |
| 18 | + const CounterStore = signalStore( |
| 19 | + withState({ count: 0 }), |
| 20 | + withComputed(({ count }) => ({ |
| 21 | + doubleCount: computed(() => count() * 2), |
| 22 | + })), |
| 23 | + ); |
| 24 | +
|
| 25 | + const store = inject(CounterStore); |
| 26 | + const unprotectedStore = unprotected(store); |
| 27 | + `; |
| 28 | + |
| 29 | + expectSnippet(snippet).toSucceed(); |
| 30 | + expectSnippet(snippet).toInfer( |
| 31 | + 'unprotectedStore', |
| 32 | + '{ count: Signal<number>; doubleCount: Signal<number>; [STATE_SOURCE]: WritableSignal<{ count: number; }>; }' |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not affect the store with an unprotected state', () => { |
| 37 | + const snippet = ` |
| 38 | + const CounterStore = signalStore( |
| 39 | + { protectedState: false }, |
| 40 | + withState({ count: 0 }), |
| 41 | + ); |
| 42 | +
|
| 43 | + const store = inject(CounterStore); |
| 44 | + const unprotectedStore = unprotected(store); |
| 45 | + `; |
| 46 | + |
| 47 | + expectSnippet(snippet).toSucceed(); |
| 48 | + expectSnippet(snippet).toInfer( |
| 49 | + 'unprotectedStore', |
| 50 | + '{ count: Signal<number>; [STATE_SOURCE]: WritableSignal<{ count: number; }>; }' |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
0 commit comments