Created
April 22, 2018 14:28
-
-
Save Tinusw/2bb8e128e463c12a1c5704d88b406859 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 } from "../../src/actions/index"; | |
import { AUTH_USER, AUTH_ERROR, UNAUTH_USER } 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 CREATOR", () => { | |
beforeEach(() => { | |
moxios.install(); | |
store = mockStore({}); | |
url = "http://localhost:3030"; | |
}); | |
afterEach(() => { | |
moxios.uninstall(); | |
}); | |
describe("signinUser()", () => { | |
it("create a token on AUTH USER", () => { | |
//Allow moxios to intercept our AXIOS request & mock a response | |
moxios.wait(() => { | |
let request = moxios.requests.mostRecent() | |
request.respondWith({ | |
status: 200, | |
response: { | |
data: { | |
token: "sample_token" | |
} | |
} | |
}) | |
}) | |
// This action has no payload | |
const expectedAction = { type: AUTH_USER }; | |
let testData = { email: "[email protected]", password: "1234" }; | |
return store.dispatch(signinUser(testData)).then(() => { | |
// actualAction will be an ARRAY of all actions | |
const actualAction = store.getActions(); | |
expect(actualAction[0].type).to.eql(expectedAction.type); | |
}); | |
}); | |
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); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment