Skip to content

Instantly share code, notes, and snippets.

@Tinusw
Created April 23, 2018 11:10
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 Tinusw/9f82451a46e71664dbbffd7b62593bde to your computer and use it in GitHub Desktop.
Save Tinusw/9f82451a46e71664dbbffd7b62593bde to your computer and use it in GitHub Desktop.
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
);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment