|
1 |
| -import { effect } from '@angular/core'; |
| 1 | +import { |
| 2 | + createEnvironmentInjector, |
| 3 | + effect, |
| 4 | + EnvironmentInjector, |
| 5 | + Injectable, |
| 6 | +} from '@angular/core'; |
2 | 7 | import { TestBed } from '@angular/core/testing';
|
3 | 8 | import {
|
4 | 9 | getState,
|
5 | 10 | patchState,
|
6 | 11 | signalState,
|
7 | 12 | signalStore,
|
| 13 | + watchState, |
| 14 | + withHooks, |
8 | 15 | withMethods,
|
9 | 16 | withState,
|
10 | 17 | } from '../src';
|
11 | 18 | import { STATE_SOURCE } from '../src/state-source';
|
| 19 | +import { createLocalService } from './helpers'; |
12 | 20 |
|
13 | 21 | describe('StateSource', () => {
|
14 | 22 | const initialState = {
|
@@ -153,4 +161,155 @@ describe('StateSource', () => {
|
153 | 161 | });
|
154 | 162 | });
|
155 | 163 | });
|
| 164 | + |
| 165 | + describe('watchState', () => { |
| 166 | + describe('with signalState', () => { |
| 167 | + it('watches state changes', () => { |
| 168 | + const state = signalState({ count: 0 }); |
| 169 | + const stateHistory: number[] = []; |
| 170 | + |
| 171 | + TestBed.runInInjectionContext(() => { |
| 172 | + watchState(state, (state) => stateHistory.push(state.count)); |
| 173 | + }); |
| 174 | + |
| 175 | + patchState(state, { count: 1 }); |
| 176 | + patchState(state, { count: 2 }); |
| 177 | + patchState(state, { count: 3 }); |
| 178 | + |
| 179 | + expect(stateHistory).toEqual([0, 1, 2, 3]); |
| 180 | + }); |
| 181 | + |
| 182 | + it('stops watching on injector destroy', () => { |
| 183 | + const stateHistory: number[] = []; |
| 184 | + const state = signalState({ count: 0 }); |
| 185 | + |
| 186 | + @Injectable() |
| 187 | + class TestService { |
| 188 | + constructor() { |
| 189 | + watchState(state, (state) => stateHistory.push(state.count)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + const { destroy } = createLocalService(TestService); |
| 194 | + |
| 195 | + patchState(state, { count: 1 }); |
| 196 | + |
| 197 | + destroy(); |
| 198 | + |
| 199 | + patchState(state, { count: 2 }); |
| 200 | + patchState(state, { count: 3 }); |
| 201 | + |
| 202 | + expect(stateHistory).toEqual([0, 1]); |
| 203 | + }); |
| 204 | + |
| 205 | + it('stops watching on manual destroy', () => { |
| 206 | + const state = signalState({ count: 0 }); |
| 207 | + const stateHistory: number[] = []; |
| 208 | + |
| 209 | + const { destroy } = TestBed.runInInjectionContext(() => |
| 210 | + watchState(state, (state) => stateHistory.push(state.count)) |
| 211 | + ); |
| 212 | + |
| 213 | + patchState(state, { count: 1 }); |
| 214 | + patchState(state, { count: 2 }); |
| 215 | + |
| 216 | + destroy(); |
| 217 | + |
| 218 | + patchState(state, { count: 3 }); |
| 219 | + |
| 220 | + expect(stateHistory).toEqual([0, 1, 2]); |
| 221 | + }); |
| 222 | + |
| 223 | + it('stops watching on provided injector destroy', () => { |
| 224 | + const injector1 = createEnvironmentInjector( |
| 225 | + [], |
| 226 | + TestBed.inject(EnvironmentInjector) |
| 227 | + ); |
| 228 | + const injector2 = createEnvironmentInjector( |
| 229 | + [], |
| 230 | + TestBed.inject(EnvironmentInjector) |
| 231 | + ); |
| 232 | + const state = signalState({ count: 0 }); |
| 233 | + const stateHistory1: number[] = []; |
| 234 | + const stateHistory2: number[] = []; |
| 235 | + |
| 236 | + watchState(state, (state) => stateHistory1.push(state.count), { |
| 237 | + injector: injector1, |
| 238 | + }); |
| 239 | + watchState(state, (state) => stateHistory2.push(state.count), { |
| 240 | + injector: injector2, |
| 241 | + }); |
| 242 | + |
| 243 | + patchState(state, { count: 1 }); |
| 244 | + patchState(state, { count: 2 }); |
| 245 | + |
| 246 | + injector1.destroy(); |
| 247 | + |
| 248 | + patchState(state, { count: 3 }); |
| 249 | + |
| 250 | + injector2.destroy(); |
| 251 | + |
| 252 | + patchState(state, { count: 4 }); |
| 253 | + |
| 254 | + expect(stateHistory1).toEqual([0, 1, 2]); |
| 255 | + expect(stateHistory2).toEqual([0, 1, 2, 3]); |
| 256 | + }); |
| 257 | + |
| 258 | + it('throws an error when called out of injection context', () => { |
| 259 | + expect(() => watchState(signalState({}), () => {})).toThrow( |
| 260 | + /NG0203: watchState\(\) can only be used within an injection context/ |
| 261 | + ); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + describe('with signalStore', () => { |
| 266 | + it('watches state changes when used within the store', () => { |
| 267 | + const stateHistory: number[] = []; |
| 268 | + const CounterStore = signalStore( |
| 269 | + withState({ count: 0 }), |
| 270 | + withHooks({ |
| 271 | + onInit(store) { |
| 272 | + patchState(store, { count: 1 }); |
| 273 | + |
| 274 | + watchState(store, (state) => stateHistory.push(state.count)); |
| 275 | + |
| 276 | + patchState(store, { count: 2 }); |
| 277 | + patchState(store, { count: 3 }); |
| 278 | + }, |
| 279 | + }) |
| 280 | + ); |
| 281 | + |
| 282 | + TestBed.configureTestingModule({ providers: [CounterStore] }); |
| 283 | + TestBed.inject(CounterStore); |
| 284 | + |
| 285 | + expect(stateHistory).toEqual([1, 2, 3]); |
| 286 | + }); |
| 287 | + |
| 288 | + it('watches state changes when used outside of store', () => { |
| 289 | + const stateHistory: number[] = []; |
| 290 | + const CounterStore = signalStore( |
| 291 | + withState({ count: 0 }), |
| 292 | + withMethods((store) => ({ |
| 293 | + increment(): void { |
| 294 | + patchState(store, (state) => ({ count: state.count + 1 })); |
| 295 | + }, |
| 296 | + })) |
| 297 | + ); |
| 298 | + |
| 299 | + TestBed.configureTestingModule({ providers: [CounterStore] }); |
| 300 | + const store = TestBed.inject(CounterStore); |
| 301 | + const injector = TestBed.inject(EnvironmentInjector); |
| 302 | + |
| 303 | + watchState(store, (state) => stateHistory.push(state.count), { |
| 304 | + injector, |
| 305 | + }); |
| 306 | + |
| 307 | + store.increment(); |
| 308 | + store.increment(); |
| 309 | + store.increment(); |
| 310 | + |
| 311 | + expect(stateHistory).toEqual([0, 1, 2, 3]); |
| 312 | + }); |
| 313 | + }); |
| 314 | + }); |
156 | 315 | });
|
0 commit comments