|
| 1 | +import { expecter } from 'ts-snippet'; |
| 2 | +import { compilerOptions } from './utils'; |
| 3 | + |
| 4 | +describe('ComponentStore types', () => { |
| 5 | + describe('effect', () => { |
| 6 | + const expectSnippet = expecter( |
| 7 | + (code) => ` |
| 8 | + import { ComponentStore } from '@ngrx/component-store'; |
| 9 | + import { of, EMPTY, Observable } from 'rxjs'; |
| 10 | + import { concatMap } from 'rxjs/operators'; |
| 11 | +
|
| 12 | + const number$: Observable<number> = of(5); |
| 13 | + const string$: Observable<string> = of('string'); |
| 14 | +
|
| 15 | + const componentStore = new ComponentStore(); |
| 16 | + ${code} |
| 17 | + `, |
| 18 | + compilerOptions() |
| 19 | + ); |
| 20 | + |
| 21 | + describe('infers Subscription', () => { |
| 22 | + it('when argument type is specified and a variable with corresponding type is passed', () => { |
| 23 | + expectSnippet( |
| 24 | + `const eff = componentStore.effect((e: Observable<string>) => number$)('string');` |
| 25 | + ).toInfer('eff', 'Subscription'); |
| 26 | + }); |
| 27 | + |
| 28 | + it( |
| 29 | + 'when argument type is specified, returns EMPTY and ' + |
| 30 | + 'a variable with corresponding type is passed', |
| 31 | + () => { |
| 32 | + expectSnippet( |
| 33 | + `const eff = componentStore.effect((e: Observable<string>) => EMPTY)('string');` |
| 34 | + ).toInfer('eff', 'Subscription'); |
| 35 | + } |
| 36 | + ); |
| 37 | + |
| 38 | + it('when argument type is specified and an Observable with corresponding type is passed', () => { |
| 39 | + expectSnippet( |
| 40 | + `const eff = componentStore.effect((e: Observable<string>) => EMPTY)(string$);` |
| 41 | + ).toInfer('eff', 'Subscription'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('when argument type is specified as Observable<unknown> and any type is passed', () => { |
| 45 | + expectSnippet( |
| 46 | + `const eff = componentStore.effect((e: Observable<unknown>) => EMPTY)(5);` |
| 47 | + ).toInfer('eff', 'Subscription'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('when generic type is specified and a variable with corresponding type is passed', () => { |
| 51 | + expectSnippet( |
| 52 | + `const eff = componentStore.effect<string>((e) => number$)('string');` |
| 53 | + ).toInfer('eff', 'Subscription'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('when generic type is specified as unknown and a variable with any type is passed', () => { |
| 57 | + expectSnippet( |
| 58 | + `const eff = componentStore.effect<unknown>((e) => number$)('string');` |
| 59 | + ).toInfer('eff', 'Subscription'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('when generic type is specified as unknown and origin can still be piped', () => { |
| 63 | + expectSnippet( |
| 64 | + `const eff = componentStore.effect<unknown>((e) => e.pipe(concatMap(() => of())))('string');` |
| 65 | + ).toInfer('eff', 'Subscription'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('when generic type is specified as unknown and origin can still be piped', () => { |
| 69 | + expectSnippet( |
| 70 | + `const eff = componentStore.effect<unknown>((e) => e.pipe(concatMap(() => of())))('string');` |
| 71 | + ).toInfer('eff', 'Subscription'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('infers void', () => { |
| 76 | + it('when argument type is specified as Observable<void> and nothing is passed', () => { |
| 77 | + expectSnippet( |
| 78 | + `const eff = componentStore.effect((e: Observable<void>) => string$)();` |
| 79 | + ).toInfer('eff', 'void'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('when type is not specified and origin can still be piped', () => { |
| 83 | + expectSnippet( |
| 84 | + // treated as Observable<void> 👇 |
| 85 | + `const eff = componentStore.effect((e) => e.pipe(concatMap(() => of())))();` |
| 86 | + ).toInfer('eff', 'void'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('when generic type is specified as void and origin can still be piped', () => { |
| 90 | + expectSnippet( |
| 91 | + `const eff = componentStore.effect<void>((e) => e.pipe(concatMap(() => number$)))();` |
| 92 | + ).toInfer('eff', 'void'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('catches improper usage', () => { |
| 97 | + it('when type is specified and argument is not passed', () => { |
| 98 | + expectSnippet( |
| 99 | + `componentStore.effect((e: Observable<string>) => of())();` |
| 100 | + ).toFail(/Expected 1 arguments, but got 0/); |
| 101 | + }); |
| 102 | + |
| 103 | + it('when type is specified and argument of incorrect type is passed', () => { |
| 104 | + expectSnippet( |
| 105 | + `componentStore.effect((e: Observable<string>) => number$)(5);` |
| 106 | + ).toFail( |
| 107 | + /Argument of type '5' is not assignable to parameter of type 'string \| Observable<string>'./ |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('when type is specified and Observable argument of incorrect type is passed', () => { |
| 112 | + expectSnippet( |
| 113 | + `componentStore.effect((e: Observable<string>) => string$)(number$);` |
| 114 | + ).toFail( |
| 115 | + /Argument of type 'Observable<number>' is not assignable to parameter of type 'string \| Observable<string>'/ |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('when argument type is specified as Observable<unknown> and type is not passed', () => { |
| 120 | + expectSnippet( |
| 121 | + `componentStore.effect((e: Observable<unknown>) => EMPTY)();` |
| 122 | + ).toFail(/Expected 1 arguments, but got 0/); |
| 123 | + }); |
| 124 | + |
| 125 | + it('when generic type is specified and a variable with incorrect type is passed', () => { |
| 126 | + expectSnippet( |
| 127 | + `componentStore.effect<string>((e) => number$)(5);` |
| 128 | + ).toFail( |
| 129 | + /Argument of type '5' is not assignable to parameter of type 'string \| Observable<string>'/ |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('when generic type is specified as unknown and a variable is not passed', () => { |
| 134 | + expectSnippet( |
| 135 | + `componentStore.effect<unknown>((e) => number$)();` |
| 136 | + ).toFail(/Expected 1 arguments, but got 0/); |
| 137 | + }); |
| 138 | + |
| 139 | + it('when argument type is specified as Observable<void> and anything is passed', () => { |
| 140 | + expectSnippet( |
| 141 | + `componentStore.effect((e: Observable<void>) => string$)(5);` |
| 142 | + ).toFail(/Expected 0 arguments, but got 1/); |
| 143 | + }); |
| 144 | + |
| 145 | + it('when type is not specified and anything is passed', () => { |
| 146 | + expectSnippet( |
| 147 | + `const eff = componentStore.effect((e) => EMPTY)('string');` |
| 148 | + ).toFail(/Expected 0 arguments, but got 1/); |
| 149 | + }); |
| 150 | + |
| 151 | + it('when generic type is specified and anything is passed', () => { |
| 152 | + expectSnippet( |
| 153 | + `componentStore.effect<void>((e) => EMPTY)(undefined);` |
| 154 | + ).toFail(/Expected 0 arguments, but got 1/); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments