Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Last active June 19, 2020 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonsodev/0aa11556579f1cd36dc03ec1ccd55890 to your computer and use it in GitHub Desktop.
Save alfonsodev/0aa11556579f1cd36dc03ec1ccd55890 to your computer and use it in GitHub Desktop.
Attempting to test redux-observable epics in typescript
import { submitSignUpFormEpic } from "../epics";
import { TestScheduler } from "rxjs/testing";
import { FirebaseUser } from "../logic/User";
import { submitSignUpForm } from "../actions/authActions";
import { of, from } from "rxjs";
import { ActionsObservable, StateObservable } from "redux-observable";
import { Observable } from "redux";
import { map } from "rxjs/operators";
import { push } from "connected-react-router";
var fs = require("fs");
jest.mock("../logic/user");
const testScheduler = new TestScheduler((actual, expected) => {
// somehow assert the two objects are equal
// e.g. with chai `expect(actual).deep.equal(expected)`
expect(actual).toBe;
});
describe("Authentication Epics ", () => {
test("Signup Form", (done: any) => {
testScheduler.run((helpers) => {
const { hot, expectObservable } = helpers;
const user = { signUp: jest.fn() };
const username = "jhon";
const password = "123456";
const email = "jhon@doe.com";
const state$ = new StateObservable(hot("-a", {}), undefined);
const action$ = new ActionsObservable(
hot("-a", {
"-a": submitSignUpForm({ email, username, password }),
})
);
action$.pipe(map((action) => expect(action.type).toBe("f"))).subscribe();
const output$ = submitSignUpFormEpic(action$, state$, { user });
// expect(user.signUp).toBeCalledTimes(1);
fs.writeFileSync("sync.txt", "anni", { mode: 0o755 });
expectObservable(output$).toBe("-a", { a: push("welcome") });
done();
});
});
});
export {};
import { ActionType, isActionOf } from "typesafe-actions";
import { Epic } from "redux-observable";
import { from, of, Observable } from "rxjs";
import { push } from "connected-react-router";
import { switchMap, filter, map, catchError } from "rxjs/operators";
import "../../app/bootstrap";
import * as actions from "../actions/authActions";
type Action = ActionType<typeof actions>;
type Params = {
payload: {
email: string;
password: string;
};
};
export const submitSignUpFormEpic: Epic = (
action$: Observable<Action>,
undefined,
{ user }
) =>
action$.pipe(
filter(isActionOf(actions.submitSignUpForm)),
switchMap(({ payload }: Params) => {
return from(user.signUp(payload.email, payload.password)).pipe(
map((user) => {
if (user) {
return push("/welcome");
}
}),
catchError((error: Error) => of(actions.submitSignUpFormFailure(error)))
);
})
);
export default submitSignUpFormEpic;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment