-
-
Notifications
You must be signed in to change notification settings - Fork 106
Add async epic helper #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apples-kksk
wants to merge
2
commits into
piotrwitek:master
Choose a base branch
from
apples-kksk:add-create-async-epic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,11 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`createAsyncEpic testType<Observable<FetchUserOutputAction>>( | ||
| epicCheck(of(fetchUserAsync.request('42')), state$, services) | ||
| ) (type) should match snapshot 1`] = `"Observable<FetchUserOutputAction>"`; | ||
|
|
||
| exports[`createAsyncEpic testType<Services>(service) (type) should match snapshot 1`] = `"Services"`; | ||
|
|
||
| exports[`createAsyncEpic testType<string>(action.payload) (type) should match snapshot 1`] = `"string"`; | ||
|
|
||
| exports[`createAsyncEpic testType<string>(state.value.token) (type) should match snapshot 1`] = `"string"`; |
This file contains hidden or 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,193 @@ | ||
| // tslint:disable:import-blacklist | ||
| import { Observable, of, Subject, throwError } from 'rxjs'; | ||
|
|
||
| import { createAsyncAction } from './create-async-action'; | ||
| import { createAsyncEpic } from './create-async-epic'; | ||
| import * as T from './type-helpers'; | ||
| import { testType } from './utils/testing'; | ||
|
|
||
| type User = { | ||
| id: string; | ||
| name: string; | ||
| }; | ||
|
|
||
| type State$ = { | ||
| value: { | ||
| token: string; | ||
| }; | ||
| }; | ||
|
|
||
| type Services = { | ||
| getUser: (id: string, token: string) => Observable<User>; | ||
| }; | ||
|
|
||
| const user: User = { | ||
| id: '42', | ||
| name: 'Piotr', | ||
| }; | ||
|
|
||
| const state$: State$ = { | ||
| value: { | ||
| token: 'secret', | ||
| }, | ||
| }; | ||
|
|
||
| const fetchUserAsync = createAsyncAction( | ||
| 'FETCH_USER_REQUEST', | ||
| 'FETCH_USER_SUCCESS', | ||
| 'FETCH_USER_FAILURE', | ||
| 'FETCH_USER_CANCEL' | ||
| )<string, User, Error, string>(); | ||
|
|
||
| const actions = { | ||
| fetchUserAsync, | ||
| }; | ||
|
|
||
| type RootAction = T.ActionType<typeof actions>; | ||
| type FetchUserOutputAction = | ||
| | ReturnType<typeof fetchUserAsync.success> | ||
| | ReturnType<typeof fetchUserAsync.failure>; | ||
|
|
||
| const services: Services = { | ||
| getUser: () => of(user), | ||
| }; | ||
|
|
||
| // @dts-jest:group createAsyncEpic | ||
| { | ||
| const epic = createAsyncEpic< | ||
| typeof fetchUserAsync, | ||
| RootAction, | ||
| State$, | ||
| Services | ||
| >(fetchUserAsync, (action, state, service) => { | ||
| // @dts-jest:pass:snap -> string | ||
| testType<string>(action.payload); | ||
| // @dts-jest:pass:snap -> string | ||
| testType<string>(state.value.token); | ||
| // @dts-jest:pass:snap -> Services | ||
| testType<Services>(service); | ||
|
|
||
| return service.getUser(action.payload, state.value.token); | ||
| }); | ||
|
|
||
| // @dts-jest:pass | ||
| const epicCheck: ( | ||
| action$: Observable<RootAction>, | ||
| state$: State$, | ||
| services: Services | ||
| ) => Observable<FetchUserOutputAction> = epic; | ||
|
|
||
| // @dts-jest:pass:snap -> Observable<FetchUserOutputAction> | ||
| testType<Observable<FetchUserOutputAction>>( | ||
| epicCheck(of(fetchUserAsync.request('42')), state$, services) | ||
| ); | ||
| } | ||
|
|
||
| describe('createAsyncEpic', () => { | ||
| it('maps request handler results to success actions', () => { | ||
| const epic = createAsyncEpic< | ||
| typeof fetchUserAsync, | ||
| RootAction, | ||
| State$, | ||
| Services | ||
| >(fetchUserAsync, (action, state, service) => | ||
| service.getUser(action.payload, state.value.token) | ||
| ); | ||
|
|
||
| const emitted: RootAction[] = []; | ||
|
|
||
| epic(of(fetchUserAsync.request('42')), state$, services).subscribe(action => | ||
| emitted.push(action) | ||
| ); | ||
|
|
||
| expect(emitted).toEqual([fetchUserAsync.success(user)]); | ||
| }); | ||
|
|
||
| it('waits for active promise handlers after the action stream completes', async () => { | ||
| const epic = createAsyncEpic< | ||
| typeof fetchUserAsync, | ||
| RootAction, | ||
| State$, | ||
| Services | ||
| >(fetchUserAsync, () => Promise.resolve(user)); | ||
|
|
||
| const emitted: RootAction[] = []; | ||
|
|
||
| await new Promise(resolve => { | ||
| epic(of(fetchUserAsync.request('42')), state$, services).subscribe({ | ||
| next: action => emitted.push(action), | ||
| complete: resolve, | ||
| }); | ||
| }); | ||
|
|
||
| expect(emitted).toEqual([fetchUserAsync.success(user)]); | ||
| }); | ||
|
|
||
| it('maps request handler errors to failure actions', () => { | ||
| const error = new Error('not found'); | ||
| const epic = createAsyncEpic< | ||
| typeof fetchUserAsync, | ||
| RootAction, | ||
| State$, | ||
| Services | ||
| >(fetchUserAsync, () => throwError(error)); | ||
|
|
||
| const emitted: RootAction[] = []; | ||
|
|
||
| epic(of(fetchUserAsync.request('42')), state$, services).subscribe(action => | ||
| emitted.push(action) | ||
| ); | ||
|
|
||
| expect(emitted).toEqual([fetchUserAsync.failure(error)]); | ||
| }); | ||
|
|
||
| it('supports mapping thrown errors before creating failure actions', () => { | ||
| const fetchUserWithMessageAsync = createAsyncAction( | ||
| 'FETCH_USER_WITH_MESSAGE_REQUEST', | ||
| 'FETCH_USER_WITH_MESSAGE_SUCCESS', | ||
| 'FETCH_USER_WITH_MESSAGE_FAILURE' | ||
| )<string, User, string>(); | ||
|
|
||
| const error = new Error('not found'); | ||
| const epic = createAsyncEpic( | ||
| fetchUserWithMessageAsync, | ||
| () => throwError(error), | ||
| { | ||
| mapError: value => (value as Error).message, | ||
| } | ||
| ); | ||
|
|
||
| const emitted: Array<T.ActionType<typeof fetchUserWithMessageAsync>> = []; | ||
|
|
||
| epic( | ||
| of(fetchUserWithMessageAsync.request('42')), | ||
| state$, | ||
| services | ||
| ).subscribe(action => emitted.push(action)); | ||
|
|
||
| expect(emitted).toEqual([fetchUserWithMessageAsync.failure(error.message)]); | ||
| }); | ||
|
|
||
| it('cancels pending request handlers when the cancel action is emitted', () => { | ||
| const action$ = new Subject<RootAction>(); | ||
| const response$ = new Subject<User>(); | ||
| const epic = createAsyncEpic< | ||
| typeof fetchUserAsync, | ||
| RootAction, | ||
| State$, | ||
| Services | ||
| >(fetchUserAsync, () => response$); | ||
| const emitted: RootAction[] = []; | ||
| const subscription = epic(action$, state$, services).subscribe(action => | ||
| emitted.push(action) | ||
| ); | ||
|
|
||
| action$.next(fetchUserAsync.request('42')); | ||
| action$.next(fetchUserAsync.cancel('user cancelled')); | ||
| response$.next(user); | ||
|
|
||
| expect(emitted).toEqual([]); | ||
|
|
||
| subscription.unsubscribe(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.