|
| 1 | +import { of } from 'rxjs'; |
| 2 | +import { expecter } from 'ts-snippet'; |
| 3 | +import { createEffect, getCreateEffectMetadata } from '../src/effect_creator'; |
| 4 | + |
| 5 | +describe('createEffect()', () => { |
| 6 | + describe('types', () => { |
| 7 | + const expectSnippet = expecter( |
| 8 | + code => ` |
| 9 | + import { Action } from '@ngrx/store'; |
| 10 | + import { createEffect } from '@ngrx/effects'; |
| 11 | + import { of } from 'rxjs'; |
| 12 | + ${code}`, |
| 13 | + { |
| 14 | + moduleResolution: 'node', |
| 15 | + target: 'es2015', |
| 16 | + baseUrl: '.', |
| 17 | + experimentalDecorators: true, |
| 18 | + paths: { |
| 19 | + '@ngrx/store': ['./modules/store'], |
| 20 | + '@ngrx/effects': ['./modules/effects'], |
| 21 | + rxjs: ['../npm/node_modules/rxjs', './node_modules/rxjs'], |
| 22 | + }, |
| 23 | + } |
| 24 | + ); |
| 25 | + |
| 26 | + describe('dispatch: true', () => { |
| 27 | + it('should enforce an Action return value', () => { |
| 28 | + expectSnippet(` |
| 29 | + const effect = createEffect(() => of({ type: 'a' })); |
| 30 | + `).toSucceed(); |
| 31 | + |
| 32 | + expectSnippet(` |
| 33 | + const effect = createEffect(() => of({ foo: 'a' })); |
| 34 | + `).toFail( |
| 35 | + /Type 'Observable<{ foo: string; }>' is not assignable to type 'Observable<Action> | ((...args: any[]) => Observable<Action>)'./ |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should enforce an Action return value when dispatch is provided', () => { |
| 40 | + expectSnippet(` |
| 41 | + const effect = createEffect(() => of({ type: 'a' }), { dispatch: true }); |
| 42 | + `).toSucceed(); |
| 43 | + |
| 44 | + expectSnippet(` |
| 45 | + const effect = createEffect(() => of({ foo: 'a' }), { dispatch: true }); |
| 46 | + `).toFail( |
| 47 | + /Type 'Observable<{ foo: string; }>' is not assignable to type 'Observable<Action> | ((...args: any[]) => Observable<Action>)'./ |
| 48 | + ); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('dispatch: false', () => { |
| 53 | + it('should enforce an Observable return value', () => { |
| 54 | + expectSnippet(` |
| 55 | + const effect = createEffect(() => of({ foo: 'a' }), { dispatch: false }); |
| 56 | + `).toSucceed(); |
| 57 | + |
| 58 | + expectSnippet(` |
| 59 | + const effect = createEffect(() => ({ foo: 'a' }), { dispatch: false }); |
| 60 | + `).toFail( |
| 61 | + /Type '{ foo: string; }' is not assignable to type 'Observable<Action> | ((...args: any[]) => Observable<Action>)'./ |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should flag the variable with a meta tag', () => { |
| 68 | + const effect = createEffect(() => of({ type: 'a' })); |
| 69 | + |
| 70 | + expect(effect.hasOwnProperty('__@ngrx/effects_create__')).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should dispatch by default', () => { |
| 74 | + const effect: any = createEffect(() => of({ type: 'a' })); |
| 75 | + |
| 76 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should be possible to explicitly create a dispatching effect', () => { |
| 80 | + const effect: any = createEffect(() => of({ type: 'a' }), { |
| 81 | + dispatch: true, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should be possible to create a non-dispatching effect', () => { |
| 88 | + const effect: any = createEffect(() => of({ type: 'a' }), { |
| 89 | + dispatch: false, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: false }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should be possible to create a non-dispatching effect returning a non-action', () => { |
| 96 | + const effect: any = createEffect(() => of('foo'), { |
| 97 | + dispatch: false, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: false }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('getCreateEffectMetadata', () => { |
| 104 | + it('should get the effects metadata for a class instance', () => { |
| 105 | + class Fixture { |
| 106 | + a = createEffect(() => of({ type: 'a' })); |
| 107 | + b = createEffect(() => of({ type: 'b' }), { dispatch: true }); |
| 108 | + c = createEffect(() => of({ type: 'c' }), { dispatch: false }); |
| 109 | + } |
| 110 | + |
| 111 | + const mock = new Fixture(); |
| 112 | + |
| 113 | + expect(getCreateEffectMetadata(mock)).toEqual([ |
| 114 | + { propertyName: 'a', dispatch: true }, |
| 115 | + { propertyName: 'b', dispatch: true }, |
| 116 | + { propertyName: 'c', dispatch: false }, |
| 117 | + ]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return an empty array if the effect has not been created with createEffect()', () => { |
| 121 | + const fakeCreateEffect: any = () => {}; |
| 122 | + class Fixture { |
| 123 | + a = fakeCreateEffect(() => of({ type: 'A' })); |
| 124 | + } |
| 125 | + |
| 126 | + const mock = new Fixture(); |
| 127 | + |
| 128 | + expect(getCreateEffectMetadata(mock)).toEqual([]); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments