Skip to content

Instantly share code, notes, and snippets.

@Tinusw
Created April 23, 2018 11:10
Show Gist options
  • Select an option

  • Save Tinusw/9f82451a46e71664dbbffd7b62593bde to your computer and use it in GitHub Desktop.

Select an option

Save Tinusw/9f82451a46e71664dbbffd7b62593bde to your computer and use it in GitHub Desktop.

Revisions

  1. Tinusw created this gist Apr 23, 2018.
    80 changes: 80 additions & 0 deletions auth_action_test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    import { expect } from "../test_helper";
    import configureMockStore from "redux-mock-store";
    import thunk from "redux-thunk";
    import moxios from "moxios";

    import { storageMock } from "./mock_local_storage";

    import { signinUser } from "../../src/actions/index";

    import { AUTH_USER, AUTH_ERROR } from "../../src/actions/types";

    global.localStorage = storageMock();

    const middlewares = [thunk];

    const mockStore = configureMockStore(middlewares);

    let store;
    let url;

    describe("AUTH ACTION CREATORS", () => {
    beforeEach(() => {
    moxios.install();
    store = mockStore({});
    url = "http://localhost:3030";
    });

    afterEach(() => {
    moxios.uninstall();
    });

    describe("signinUser()", () => {
    it("create a token on AUTH USER", () => {
    moxios.wait(() => {
    let request = moxios.requests.mostRecent();
    request.respondWith({
    status: 200,
    response: {
    data: {
    token: "sample_token"
    }
    }
    });
    });
    const expectedAction = { type: AUTH_USER };
    let testData = { email: "[email protected]", password: "1234" };
    return store.dispatch(signinUser(testData)).then(() => {
    const actualAction = store.getActions();
    expect(actualAction[0]).to.eql(expectedAction);
    });
    });

    it("returns an error on AUTH_ERROR with 401", () => {
    moxios.wait(() => {
    let request = moxios.requests.mostRecent();
    request.respondWith({
    status: 401,
    response: {
    data: "Unauthorized",
    status: 401
    }
    });
    });

    const expectedAction = {
    type: AUTH_ERROR,
    payload: "Error: Request failed with status code 401"
    };

    let testData = { email: "[email protected]", password: "124" };
    return store.dispatch(signinUser(testData)).then(() => {
    const actualAction = store.getActions();
    expect(actualAction[0].type).to.eql(expectedAction.type);
    expect(actualAction[0].payload.toString()).to.eql(
    expectedAction.payload
    );
    });
    });
    });
    });