Last active
April 19, 2018 16:11
-
-
Save Tinusw/6392f3a268808981698cc669b6934ea1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]", | |
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: "[email protected]", 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: "[email protected]", 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