Skip to content

Commit

Permalink
fix(component-store): accept error type in tapResponse with strict ge…
Browse files Browse the repository at this point in the history
…neric checks (#3068)

Related to #3056
  • Loading branch information
markostanimirovic authored Jul 12, 2021
1 parent a39b278 commit 3e02e37
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
50 changes: 50 additions & 0 deletions modules/component-store/spec/tap-response.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EMPTY, noop, Observable, of, throwError } from 'rxjs';
import { tapResponse } from '@ngrx/component-store';
import { concatMap, finalize } from 'rxjs/operators';

describe('tapResponse', () => {
it('should invoke next callback on next', () => {
const nextCallback = jest.fn<void, [number]>();

of(1, 2, 3).pipe(tapResponse(nextCallback, noop)).subscribe();

expect(nextCallback.mock.calls).toEqual([[1], [2], [3]]);
});

it('should invoke error callback on error', () => {
const errorCallback = jest.fn<void, [{ message: string }]>();
const error = { message: 'error' };

throwError(error).pipe(tapResponse(noop, errorCallback)).subscribe();

expect(errorCallback).toHaveBeenCalledWith(error);
});

it('should invoke complete callback on complete', () => {
const completeCallback = jest.fn<void, []>();

EMPTY.pipe(tapResponse(noop, noop, completeCallback)).subscribe();

expect(completeCallback).toHaveBeenCalledWith();
});

it('should not unsubscribe from outer observable on inner observable error', () => {
const innerCompleteCallback = jest.fn<void, []>();
const outerCompleteCallback = jest.fn<void, []>();

new Observable((subscriber) => subscriber.next(1))
.pipe(
concatMap(() =>
throwError('error').pipe(
tapResponse(noop, noop),
finalize(innerCompleteCallback)
)
),
finalize(outerCompleteCallback)
)
.subscribe();

expect(innerCompleteCallback).toHaveBeenCalled();
expect(outerCompleteCallback).not.toHaveBeenCalled();
});
});
71 changes: 71 additions & 0 deletions modules/component-store/spec/types/tap-response.types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Expect, expecter } from 'ts-snippet';
import { compilerOptions } from './utils';

describe('tapResponse types', () => {
const snippetFactory = (code: string): string => `
import { tapResponse } from '@ngrx/component-store';
import { noop, of } from 'rxjs';
${code}
`;

function testWith(expectSnippet: (code: string) => Expect): void {
it('should infer next type', () => {
expectSnippet(`
of(1).pipe(
tapResponse((next) => {
const num = next;
}, noop)
);
`).toInfer('num', 'number');
});

it('should accept error type', () => {
expectSnippet(`
of(true).pipe(
tapResponse(noop, (error: { message: string }) => {
const err = error;
})
);
`).toInfer('err', '{ message: string; }');
});

it('should use unknown as default error type', () => {
expectSnippet(`
of(true).pipe(
tapResponse(noop, (error) => {
const err = error;
})
);
`).toInfer('err', 'unknown');
});
}

describe('strict mode', () => {
const expectSnippet = expecter(snippetFactory, {
...compilerOptions(),
strict: true,
});

testWith(expectSnippet);
});

describe('non-strict mode', () => {
const expectSnippet = expecter(snippetFactory, {
...compilerOptions(),
strict: false,
});

testWith(expectSnippet);
});

describe('non-strict mode with strict generic checks', () => {
const expectSnippet = expecter(snippetFactory, {
...compilerOptions(),
strict: false,
noStrictGenericChecks: false,
});

testWith(expectSnippet);
});
});
4 changes: 2 additions & 2 deletions modules/component-store/src/tap-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { catchError, tap } from 'rxjs/operators';
* });
* ```
*/
export function tapResponse<T>(
export function tapResponse<T, E = unknown>(
nextFn: (next: T) => void,
errorFn: <E = unknown>(error: E) => void,
errorFn: (error: E) => void,
completeFn?: () => void
): (source: Observable<T>) => Observable<T> {
return (source) =>
Expand Down

0 comments on commit 3e02e37

Please sign in to comment.