Last active
January 7, 2020 20:43
-
-
Save evertbouw/d4c6e588c6ffd46febfdee6ac9d33878 to your computer and use it in GitHub Desktop.
Writing tests for epics with state$
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
import { Action } from "redux"; | |
import { ActionsObservable, Epic, StateObservable } from "redux-observable"; | |
import { Subject } from "rxjs"; | |
import { toArray } from "rxjs/operators"; | |
const stateInput$ = new Subject(); | |
const state$ = new StateObservable<any>(stateInput$, undefined); | |
interface IEpicTest<A extends Action, S, D> { | |
(params: { | |
epic: Epic<A, S, D>; | |
inputActions: A[]; | |
expectedActions?: A[]; | |
state?: S; | |
dependencies?: D; | |
}): Promise<A[]>; | |
} | |
export const epicTest: IEpicTest<Action, {}, {}> = ({ | |
epic, | |
inputActions, | |
expectedActions, | |
state = {}, | |
dependencies = {}, | |
}) => { | |
stateInput$.next(state); | |
const action$ = ActionsObservable.from(inputActions); | |
return epic(action$, state$, dependencies) | |
.pipe(toArray()) | |
.toPromise() | |
.then(actualActions => { | |
if (expectedActions !== undefined) { | |
expect(actualActions).toEqual(expectedActions); | |
} | |
return actualActions; | |
}); | |
}; | |
/* then in your actual test file */ | |
import { epicTest } from "./epicTestFunction.ts"; | |
import { myEpic } from "./myEpic.ts"; | |
describe("myEpic", () => { | |
it("should asdf", () => epicTest({ | |
epic: myEpic, | |
inputActions: [/* actions */], | |
expectedActions: [/* expected actions */], | |
state: {/* state */}, | |
}).then(output => { | |
// do something extra like take snapshots | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment