Skip to content

Instantly share code, notes, and snippets.

@Tinusw
Last active April 19, 2018 16:11
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/6392f3a268808981698cc669b6934ea1 to your computer and use it in GitHub Desktop.
Save Tinusw/6392f3a268808981698cc669b6934ea1 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, signoutUser, signUpUser, fetchCampaigns } from "../../src/actions/index";
import { AUTH_USER, AUTH_ERROR, UNAUTH_USER, FETCH_CAMPAIGNS } from "../../src/actions/types";
global.localStorage = storageMock();
// Fake success response
const AuthSuccess = {
data: {
token: "1234"
}
};
// Fake failure response
const AuthFailure = {
response: {
data: "Unauthorized",
status: 401
}
};
// Fake Data
const data = {
email: "test@test1.com",
password: "1234"
};
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", done => {
moxios.stubRequest(url, {
status: 200,
response: {
data: {
token: "sample_token"
}
}
});
const expectedAction = { type: AUTH_USER };
let testData = { email: "test1@test.com", password: "1234" };
store.dispatch(signinUser(testData)).then(() => {
const actualAction = store.getActions();
expect(localStorage.setItem()).to.be.called.with('token', "sample_token")
expect(actualAction).to.eql(expectedAction);
});
done();
});
it("returns an error on AUTH_ERROR with 401", done => {
moxios.stubRequest(url, {
status: 401,
response: {
data: "Unauthorized",
status: 401
}
});
const expectedAction = { type: AUTH_ERROR, payload: AuthFailure };
let testData = { email: "test1@test.com", password: "124" };
store.dispatch(signinUser(testData)).then(() => {
const actualAction = store.getActions();
expect(actualAction).to.eql(expectedAction);
});
done();
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment