|
| 1 | +import { of } from 'rxjs'; |
| 2 | +import { createEffect, getCreateEffectMetadata } from '../src/effect_creator'; |
| 3 | + |
| 4 | +describe('createEffect()', () => { |
| 5 | + it('should flag the variable with a meta tag', () => { |
| 6 | + const effect = createEffect(() => of({ type: 'a' })); |
| 7 | + |
| 8 | + expect(effect.hasOwnProperty('__@ngrx/effects_create__')).toBe(true); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should dispatch by default', () => { |
| 12 | + const effect: any = createEffect(() => of({ type: 'a' })); |
| 13 | + |
| 14 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should be possible to explicitly create a dispatching effect', () => { |
| 18 | + const effect: any = createEffect(() => of({ type: 'a' }), { |
| 19 | + dispatch: true, |
| 20 | + }); |
| 21 | + |
| 22 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should be possible to create a non-dispatching effect', () => { |
| 26 | + const effect: any = createEffect(() => of({ type: 'a' }), { |
| 27 | + dispatch: false, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: false }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('getCreateEffectMetadata', () => { |
| 34 | + it('should get the effects metadata for a class instance', () => { |
| 35 | + class Fixture { |
| 36 | + a = createEffect(() => of({ type: 'a' })); |
| 37 | + b = createEffect(() => of({ type: 'b' }), { dispatch: true }); |
| 38 | + c = createEffect(() => of({ type: 'c' }), { dispatch: false }); |
| 39 | + } |
| 40 | + |
| 41 | + const mock = new Fixture(); |
| 42 | + |
| 43 | + expect(getCreateEffectMetadata(mock)).toEqual([ |
| 44 | + { propertyName: 'a', dispatch: true }, |
| 45 | + { propertyName: 'b', dispatch: true }, |
| 46 | + { propertyName: 'c', dispatch: false }, |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return an empty array if the effect has not been created with createEffect()', () => { |
| 51 | + const fakeCreateEffect: any = () => {}; |
| 52 | + class Fixture { |
| 53 | + a = fakeCreateEffect(() => of({ type: 'A' })); |
| 54 | + } |
| 55 | + |
| 56 | + const mock = new Fixture(); |
| 57 | + |
| 58 | + expect(getCreateEffectMetadata(mock)).toEqual([]); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments