|
| 1 | +import { Injectable, NgZone } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { |
| 4 | + Action, |
| 5 | + provideStore, |
| 6 | + Selector, |
| 7 | + State, |
| 8 | + StateContext, |
| 9 | + Store, |
| 10 | + withNgxsNoopExecutionStrategy |
| 11 | +} from '@ngxs/store'; |
| 12 | + |
| 13 | +describe('Noop execution strategy', () => { |
| 14 | + class ZoneCounter { |
| 15 | + inside = 0; |
| 16 | + outside = 0; |
| 17 | + hit() { |
| 18 | + if (NgZone.isInAngularZone()) { |
| 19 | + this.inside += 1; |
| 20 | + } else { |
| 21 | + this.outside += 1; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + assert(expectation: { inside: number; outside: number }) { |
| 26 | + const self: ZoneCounter = this; |
| 27 | + expect({ ...self }).toEqual(expectation); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + class Increment { |
| 32 | + static readonly type = '[Counter] Increment'; |
| 33 | + } |
| 34 | + |
| 35 | + @State<number>({ |
| 36 | + name: 'counter', |
| 37 | + defaults: 0 |
| 38 | + }) |
| 39 | + @Injectable() |
| 40 | + class CounterState { |
| 41 | + @Selector() |
| 42 | + static getCounter(state: number) { |
| 43 | + return state; |
| 44 | + } |
| 45 | + |
| 46 | + zoneCounter = new ZoneCounter(); |
| 47 | + |
| 48 | + @Action(Increment) |
| 49 | + increment({ setState, getState }: StateContext<number>): void { |
| 50 | + setState(getState() + 1); |
| 51 | + this.zoneCounter.hit(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + function setup() { |
| 56 | + TestBed.configureTestingModule({ |
| 57 | + providers: [provideStore([CounterState], withNgxsNoopExecutionStrategy())] |
| 58 | + }); |
| 59 | + const store = TestBed.inject(Store); |
| 60 | + const zone: NgZone = TestBed.inject(NgZone); |
| 61 | + return { zone, store }; |
| 62 | + } |
| 63 | + |
| 64 | + describe('[store.select]', () => { |
| 65 | + it('should be performed outside Angular zone, when dispatched from outside zones', () => { |
| 66 | + // Arrange |
| 67 | + const { zone, store } = setup(); |
| 68 | + const zoneCounter = new ZoneCounter(); |
| 69 | + // Act |
| 70 | + zone.runOutsideAngular(() => { |
| 71 | + store |
| 72 | + .select<number>(({ counter }) => counter) |
| 73 | + .subscribe(() => { |
| 74 | + zoneCounter.hit(); |
| 75 | + }); |
| 76 | + |
| 77 | + store.dispatch(new Increment()); |
| 78 | + store.dispatch(new Increment()); |
| 79 | + }); |
| 80 | + |
| 81 | + // Assert |
| 82 | + zoneCounter.assert({ |
| 83 | + inside: 0, |
| 84 | + outside: 3 |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should be performed inside Angular zone, when dispatched from inside zones', () => { |
| 89 | + // Arrange |
| 90 | + const { zone, store } = setup(); |
| 91 | + const zoneCounter = new ZoneCounter(); |
| 92 | + // Act |
| 93 | + zone.run(() => { |
| 94 | + store |
| 95 | + .select<number>(({ counter }) => counter) |
| 96 | + .subscribe(() => { |
| 97 | + zoneCounter.hit(); |
| 98 | + }); |
| 99 | + |
| 100 | + store.dispatch(new Increment()); |
| 101 | + store.dispatch(new Increment()); |
| 102 | + }); |
| 103 | + |
| 104 | + // Assert |
| 105 | + zoneCounter.assert({ |
| 106 | + inside: 3, |
| 107 | + outside: 0 |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments