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: "test1@test.com", 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: "test1@test.com", 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 ); }); }); }); });