|
| 1 | +import { expecter } from 'ts-snippet'; |
| 2 | +import { compilerOptions } from './helpers'; |
| 3 | + |
| 4 | +describe('patchState', () => { |
| 5 | + const expectSnippet = expecter( |
| 6 | + (code) => ` |
| 7 | + import { computed, inject, linkedSignal, Signal, signal } from '@angular/core'; |
| 8 | + import { |
| 9 | + patchState, |
| 10 | + signalStore, |
| 11 | + withState, |
| 12 | + withLinkedState, |
| 13 | + withMethods |
| 14 | + } from '@ngrx/signals'; |
| 15 | +
|
| 16 | + ${code} |
| 17 | + `, |
| 18 | + compilerOptions() |
| 19 | + ); |
| 20 | + |
| 21 | + it('does not have access to methods', () => { |
| 22 | + const snippet = ` |
| 23 | + signalStore( |
| 24 | + withMethods(() => ({ |
| 25 | + foo: () => 'bar', |
| 26 | + })), |
| 27 | + withLinkedState(({ foo }) => ({ value: foo() })) |
| 28 | + ); |
| 29 | + `; |
| 30 | + |
| 31 | + expectSnippet(snippet).toFail(/Property 'foo' does not exist on type '{}'/); |
| 32 | + }); |
| 33 | + |
| 34 | + it('does not have access to STATE_SOURCE', () => { |
| 35 | + const snippet = ` |
| 36 | + signalStore( |
| 37 | + withState({ foo: 'bar' }), |
| 38 | + withLinkedState((store) => { |
| 39 | + patchState(store, { foo: 'baz' }); |
| 40 | + return { bar: 'foo' }; |
| 41 | + }) |
| 42 | + ) |
| 43 | + `; |
| 44 | + |
| 45 | + expectSnippet(snippet).toFail( |
| 46 | + /is not assignable to parameter of type 'WritableStateSource<object>'./ |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('cannot return a primitive value', () => { |
| 51 | + const snippet = ` |
| 52 | + signalStore( |
| 53 | + withLinkedState(() => ({ foo: 'bar' })) |
| 54 | + ) |
| 55 | + `; |
| 56 | + |
| 57 | + expectSnippet(snippet).toFail( |
| 58 | + /Type 'string' is not assignable to type 'WritableSignal<unknown> | (() => unknown)'./ |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('resolves to a normal state signal with automatic linkedSignal', () => { |
| 63 | + const snippet = ` |
| 64 | + const UserStore = signalStore( |
| 65 | + { providedIn: 'root' }, |
| 66 | + withLinkedState(() => ({ firstname: () => 'John', lastname: () => 'Doe' })) |
| 67 | + ); |
| 68 | +
|
| 69 | + const userStore = new UserStore(); |
| 70 | +
|
| 71 | + const firstname = userStore.firstname; |
| 72 | + const lastname = userStore.lastname; |
| 73 | + `; |
| 74 | + |
| 75 | + expectSnippet(snippet).toSucceed(); |
| 76 | + |
| 77 | + expectSnippet(snippet).toInfer('firstname', 'Signal<string>'); |
| 78 | + expectSnippet(snippet).toInfer('lastname', 'Signal<string>'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('resolves to a normal state signal with manual linkedSignal', () => { |
| 82 | + const snippet = ` |
| 83 | + const UserStore = signalStore( |
| 84 | + { providedIn: 'root' }, |
| 85 | + withLinkedState(() => ({ |
| 86 | + firstname: linkedSignal(() => 'John'), |
| 87 | + lastname: linkedSignal(() => 'Doe') |
| 88 | + })) |
| 89 | + ); |
| 90 | +
|
| 91 | + const userStore = new UserStore(); |
| 92 | +
|
| 93 | + const firstname = userStore.firstname; |
| 94 | + const lastname = userStore.lastname; |
| 95 | + `; |
| 96 | + |
| 97 | + expectSnippet(snippet).toSucceed(); |
| 98 | + |
| 99 | + expectSnippet(snippet).toInfer('firstname', 'Signal<string>'); |
| 100 | + expectSnippet(snippet).toInfer('lastname', 'Signal<string>'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should set stateSignals as DeepSignal for automatic linkedSignal', () => { |
| 104 | + const snippet = ` |
| 105 | + const UserStore = signalStore( |
| 106 | + { providedIn: 'root' }, |
| 107 | + withLinkedState(() => ({ |
| 108 | + user: () => ({ id: 1, name: 'John Doe' }), |
| 109 | + location: () => ({ city: 'Berlin', country: 'Germany' }), |
| 110 | + })) |
| 111 | + ); |
| 112 | +
|
| 113 | + const userStore = new UserStore(); |
| 114 | +
|
| 115 | + const location = userStore.location; |
| 116 | + const user = userStore.user; |
| 117 | + `; |
| 118 | + |
| 119 | + expectSnippet(snippet).toSucceed(); |
| 120 | + |
| 121 | + expectSnippet(snippet).toInfer( |
| 122 | + 'location', |
| 123 | + 'DeepSignal<{ city: string; country: string; }>' |
| 124 | + ); |
| 125 | + expectSnippet(snippet).toInfer( |
| 126 | + 'user', |
| 127 | + 'DeepSignal<{ id: number; name: string; }>' |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should set stateSignals as DeepSignal for manual linkedSignal', () => { |
| 132 | + const snippet = ` |
| 133 | + const UserStore = signalStore( |
| 134 | + { providedIn: 'root' }, |
| 135 | + withLinkedState(() => ({ |
| 136 | + user: linkedSignal(() => ({ id: 1, name: 'John Doe' })), |
| 137 | + location: linkedSignal(() => ({ city: 'Berlin', country: 'Germany' })), |
| 138 | + })) |
| 139 | + ); |
| 140 | +
|
| 141 | + const userStore = new UserStore(); |
| 142 | +
|
| 143 | + const location = userStore.location; |
| 144 | + const user = userStore.user; |
| 145 | + `; |
| 146 | + |
| 147 | + expectSnippet(snippet).toSucceed(); |
| 148 | + |
| 149 | + expectSnippet(snippet).toInfer( |
| 150 | + 'location', |
| 151 | + 'DeepSignal<{ city: string; country: string; }>' |
| 152 | + ); |
| 153 | + expectSnippet(snippet).toInfer( |
| 154 | + 'user', |
| 155 | + 'DeepSignal<{ id: number; name: string; }>' |
| 156 | + ); |
| 157 | + }); |
| 158 | +}); |
0 commit comments