-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(component-store): accept error type in tapResponse with strict ge…
- Loading branch information
1 parent
a39b278
commit 3e02e37
Showing
3 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
modules/component-store/spec/types/tap-response.types.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters