|
| 1 | +import { expecter } from 'ts-snippet'; |
| 2 | +import { compilerOptions } from './utils'; |
| 3 | + |
| 4 | +describe('ofType()', () => { |
| 5 | + describe('action creators', () => { |
| 6 | + const expectSnippet = expecter( |
| 7 | + code => ` |
| 8 | + import { Action, createAction, props } from '@ngrx/store'; |
| 9 | + import { Actions, ofType } from '@ngrx/effects'; |
| 10 | + import { of } from 'rxjs'; |
| 11 | +
|
| 12 | + const actions$ = {} as Actions; |
| 13 | +
|
| 14 | + ${code}`, |
| 15 | + compilerOptions() |
| 16 | + ); |
| 17 | + |
| 18 | + it('should infer correctly', () => { |
| 19 | + expectSnippet(` |
| 20 | + const actionA = createAction('Action A'); |
| 21 | + const effect = actions$.pipe(ofType(actionA)) |
| 22 | + `).toInfer('effect', 'Observable<TypedAction<"Action A">>'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should infer correctly with props', () => { |
| 26 | + expectSnippet(` |
| 27 | + const actionA = createAction('Action A', props<{ foo: string }>()}); |
| 28 | + const effect = actions$.pipe(ofType(actionA)) |
| 29 | + `).toInfer( |
| 30 | + 'effect', |
| 31 | + 'Observable<{ foo: string; } & TypedAction<"Action A">>' |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should infer correctly with function', () => { |
| 36 | + expectSnippet(` |
| 37 | + const actionA = createAction('Action A', (foo: string) => ({ foo })); |
| 38 | + const effect = actions$.pipe(ofType(actionA)) |
| 39 | + `).toInfer( |
| 40 | + 'effect', |
| 41 | + 'Observable<{ foo: string; } & TypedAction<"Action A">>' |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should infer correctly with multiple actions (with over 5 actions)', () => { |
| 46 | + expectSnippet(` |
| 47 | + const actionA = createAction('Action A'); |
| 48 | + const actionB = createAction('Action B'); |
| 49 | + const actionC = createAction('Action C'); |
| 50 | + const actionD = createAction('Action D'); |
| 51 | + const actionE = createAction('Action E'); |
| 52 | + const actionF = createAction('Action F'); |
| 53 | + const actionG = createAction('Action G'); |
| 54 | +
|
| 55 | + const effect = actions$.pipe(ofType(actionA, actionB, actionC, actionD, actionE, actionF, actionG)) |
| 56 | + `).toInfer( |
| 57 | + 'effect', |
| 58 | + 'Observable<TypedAction<"Action A"> | TypedAction<"Action B"> | TypedAction<"Action C"> | TypedAction<"Action D"> | TypedAction<"Action E"> | TypedAction<"Action F"> | TypedAction<"Action G">>' |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('strings with typed Actions', () => { |
| 64 | + const expectSnippet = expecter( |
| 65 | + code => ` |
| 66 | + import { Action } from '@ngrx/store'; |
| 67 | + import { Actions, ofType } from '@ngrx/effects'; |
| 68 | + import { of } from 'rxjs'; |
| 69 | +
|
| 70 | + const ACTION_A = 'ACTION A' |
| 71 | + const ACTION_B = 'ACTION B' |
| 72 | + const ACTION_C = 'ACTION C' |
| 73 | + const ACTION_D = 'ACTION D' |
| 74 | + const ACTION_E = 'ACTION E' |
| 75 | + const ACTION_F = 'ACTION F' |
| 76 | +
|
| 77 | + interface ActionA { type: typeof ACTION_A }; |
| 78 | + interface ActionB { type: typeof ACTION_B }; |
| 79 | + interface ActionC { type: typeof ACTION_C }; |
| 80 | + interface ActionD { type: typeof ACTION_D }; |
| 81 | + interface ActionE { type: typeof ACTION_E }; |
| 82 | + interface ActionF { type: typeof ACTION_F }; |
| 83 | +
|
| 84 | + ${code}`, |
| 85 | + compilerOptions() |
| 86 | + ); |
| 87 | + |
| 88 | + it('should infer correctly', () => { |
| 89 | + expectSnippet(` |
| 90 | + const actions$ = {} as Actions<ActionA>; |
| 91 | + const effect = actions$.pipe(ofType(ACTION_A)) |
| 92 | + `).toInfer('effect', 'Observable<ActionA>'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should infer correctly with multiple actions (up to 5 actions)', () => { |
| 96 | + expectSnippet(` |
| 97 | + const actions$ = {} as Actions<ActionA | ActionB | ActionC | ActionD | ActionE>; |
| 98 | + const effect = actions$.pipe(ofType(ACTION_A, ACTION_B, ACTION_C, ACTION_D, ACTION_E)) |
| 99 | + `).toInfer( |
| 100 | + 'effect', |
| 101 | + 'Observable<ActionA | ActionB | ActionC | ActionD | ActionE>' |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should infer to Action when more than 5 actions', () => { |
| 106 | + expectSnippet(` |
| 107 | + const actions$ = {} as Actions<ActionA | ActionB | ActionC | ActionD | ActionE | ActionF>; |
| 108 | + const effect = actions$.pipe(ofType(ACTION_A, ACTION_B, ACTION_C, ACTION_D, ACTION_E, ACTION_F)) |
| 109 | + `).toInfer('effect', 'Observable<Action>'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should infer to never when the action is not in Actions', () => { |
| 113 | + expectSnippet(` |
| 114 | + const actions$ = {} as Actions<ActionA>; |
| 115 | + const effect = actions$.pipe(ofType(ACTION_B)) |
| 116 | + `).toInfer('effect', 'Observable<never>'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('strings ofType generic', () => { |
| 121 | + const expectSnippet = expecter( |
| 122 | + code => ` |
| 123 | + import { Action } from '@ngrx/store'; |
| 124 | + import { Actions, ofType } from '@ngrx/effects'; |
| 125 | + import { of } from 'rxjs'; |
| 126 | +
|
| 127 | + const ACTION_A = 'ACTION A' |
| 128 | + const ACTION_B = 'ACTION B' |
| 129 | + const ACTION_C = 'ACTION C' |
| 130 | + const ACTION_D = 'ACTION D' |
| 131 | + const ACTION_E = 'ACTION E' |
| 132 | + const ACTION_F = 'ACTION F' |
| 133 | +
|
| 134 | + interface ActionA { type: typeof ACTION_A }; |
| 135 | + interface ActionB { type: typeof ACTION_B }; |
| 136 | + interface ActionC { type: typeof ACTION_C }; |
| 137 | + interface ActionD { type: typeof ACTION_D }; |
| 138 | + interface ActionE { type: typeof ACTION_E }; |
| 139 | + interface ActionF { type: typeof ACTION_F }; |
| 140 | +
|
| 141 | + ${code}`, |
| 142 | + compilerOptions() |
| 143 | + ); |
| 144 | + |
| 145 | + it('should infer correctly', () => { |
| 146 | + expectSnippet(` |
| 147 | + const actions$ = {} as Actions; |
| 148 | + const effect = actions$.pipe(ofType<ActionA>(ACTION_A)) |
| 149 | + `).toInfer('effect', 'Observable<ActionA>'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should infer correctly with multiple actions (with over 5 actions)', () => { |
| 153 | + expectSnippet(` |
| 154 | + const actions$ = {} as Actions; |
| 155 | + const effect = actions$.pipe(ofType<ActionA | ActionB | ActionC | ActionD | ActionE | ActionF>(ACTION_A, ACTION_B, ACTION_C, ACTION_D, ACTION_E, ACTION_F)) |
| 156 | + `).toInfer( |
| 157 | + 'effect', |
| 158 | + 'Observable<ActionA | ActionB | ActionC | ActionD | ActionE | ActionF>' |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should infer to the generic even if the generic is wrong', () => { |
| 163 | + expectSnippet(` |
| 164 | + const actions$ = {} as Actions; |
| 165 | + const effect = actions$.pipe(ofType<ActionA>(ACTION_B)) |
| 166 | + `).toInfer('effect', 'Observable<ActionA>'); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments