|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { of, Subject, tap } from 'rxjs'; |
| 4 | +import { rxEffects, RxEffectsSetupFn } from './rx-effects'; |
| 5 | + |
| 6 | +describe(rxEffects, () => { |
| 7 | + it('should register an observable', () => { |
| 8 | + const spy = jest.fn(); |
| 9 | + setupComponent(({ register }) => register(of('src').pipe(tap(spy)))); |
| 10 | + expect(spy).toHaveBeenCalledWith('src'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should register an observable and sideEffect fn', () => { |
| 14 | + const spy = jest.fn(); |
| 15 | + setupComponent(({ register }) => register(of('src'), spy)); |
| 16 | + expect(spy).toHaveBeenCalledWith('src'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should register multiple observables', () => { |
| 20 | + const spy = jest.fn(); |
| 21 | + setupComponent(({ register }) => { |
| 22 | + register(of('src').pipe(tap(spy))); |
| 23 | + register(of('src2'), spy); |
| 24 | + }); |
| 25 | + expect(spy.mock.calls[0][0]).toEqual('src'); |
| 26 | + expect(spy.mock.calls[1][0]).toEqual('src2'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle errors', () => { |
| 30 | + const spyNext = jest.fn(); |
| 31 | + const spyError = jest.fn(); |
| 32 | + const trigger = new Subject(); |
| 33 | + setupComponent(({ register }) => { |
| 34 | + register(trigger, spyNext); |
| 35 | + register(trigger, { next: () => void 0, error: spyError }); |
| 36 | + }); |
| 37 | + |
| 38 | + expect(spyNext).toHaveBeenCalledTimes(0); |
| 39 | + trigger.next(1); |
| 40 | + expect(spyNext).toHaveBeenCalledTimes(1); |
| 41 | + trigger.error('E'); |
| 42 | + expect(spyNext).toHaveBeenCalledTimes(1); |
| 43 | + expect(spyError).toHaveBeenCalledTimes(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should call onCleanup', () => { |
| 47 | + const spySideEffect = jest.fn(); |
| 48 | + const spyInternalOnCleanup = jest.fn(); |
| 49 | + |
| 50 | + const { fixture, component } = setupComponent(({ register, onCleanup }) => { |
| 51 | + register(of('src').pipe(tap(spySideEffect))); |
| 52 | + onCleanup(spyInternalOnCleanup); |
| 53 | + }); |
| 54 | + const spyOnCleanup = jest.fn(); |
| 55 | + component.effects.onCleanup(spyOnCleanup); |
| 56 | + |
| 57 | + expect(spySideEffect).toHaveBeenCalled(); |
| 58 | + expect(spyInternalOnCleanup).not.toHaveBeenCalled(); |
| 59 | + expect(spyOnCleanup).not.toHaveBeenCalled(); |
| 60 | + fixture.destroy(); |
| 61 | + expect(spyInternalOnCleanup).toHaveBeenCalled(); |
| 62 | + expect(spyOnCleanup).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +function setupComponent<T>(setupFn?: RxEffectsSetupFn<T>) { |
| 67 | + @Component({}) |
| 68 | + class TestComponent { |
| 69 | + readonly effects = rxEffects(setupFn); |
| 70 | + } |
| 71 | + |
| 72 | + TestBed.configureTestingModule({ |
| 73 | + declarations: [TestComponent], |
| 74 | + }); |
| 75 | + |
| 76 | + const fixture = TestBed.createComponent(TestComponent); |
| 77 | + |
| 78 | + return { |
| 79 | + fixture, |
| 80 | + component: fixture.componentInstance, |
| 81 | + }; |
| 82 | +} |
0 commit comments