Skip to content

Commit

Permalink
Add tests to create account action
Browse files Browse the repository at this point in the history
  • Loading branch information
robisson committed Dec 23, 2017
1 parent 80085d3 commit 1b08e33
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
26 changes: 0 additions & 26 deletions src/client/App.js

This file was deleted.

15 changes: 3 additions & 12 deletions src/client/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@ export function createAccount(email, password) {
return dispatch => {
dispatch({ type: actionsType.CREATE_ACCOUNT_SEND });

return BoardApi
.createAccount(email, password)
.then(({ data }) => {
dispatch(accountCreated(data));
})
.catch(error => {
dispatch(accountCreatedError(error));
});
return BoardApi.createAccount(email, password).then(({ data }) => {
dispatch(accountCreated(data));
});
};
}

export function accountCreated(response) {
return { type: actionsType.CREATE_ACCOUNT_SUCCESS, response };
}

export function accountCreatedError(error) {
return { type: actionsType.CREATE_ACCOUNT_ERROR, error };
}
3 changes: 1 addition & 2 deletions src/client/actions/actionsType.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const actionsType = {
LOADING_LOGIN: "LOADING_LOGIN",
LOGOUT_SUCCESS: "LOGOUT_SUCCESS",
CREATE_ACCOUNT_SEND: "CREATE_ACCOUNT_SEND",
CREATE_ACCOUNT_SUCCESS: "CREATE_ACCOUNT_SUCCESS",
CREATE_ACCOUNT_ERROR: "CREATE_ACCOUNT_ERROR"
CREATE_ACCOUNT_SUCCESS: "CREATE_ACCOUNT_SUCCESS"
};

export default actionsType;
44 changes: 41 additions & 3 deletions src/client/api/__tests__/BoardApi.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { login, logout } from "../../actions/Login";
import { createAccount } from "../../actions/User";
import configureStore from "../../configureStore";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import "jest-localstorage-mock";

describe("Action Login", () => {
describe("Board API requests", () => {
let store;
let mock;
beforeEach(() => {
Expand All @@ -17,7 +18,29 @@ describe("Action Login", () => {
mock.reset();
});

it("Shoud to authenticate a user and set token localstorage", () => {
it("Should to create account user", () => {
let response = {
success: true,
message: "User account created with successfull!"
};

mock.onPost("/api/v1/user").reply(200, response);

const expectedState = [
{
message: "User account created with successfull!",
showPreloader: false,
success: true,
user: {}
}
];

store.dispatch(createAccount("email@example.com", 12345)).then(() => {
expect([store.getState().userReducer]).toEqual(expectedState);
});
});

it("Should to authenticate a user and set token localstorage", () => {
let response = {
success: true,
message: "Login User successfull!",
Expand All @@ -44,9 +67,24 @@ describe("Action Login", () => {
});
});

it("Shoud logout a user and clean token and user in the localstorage", () => {
it("Should logout a user and clean token and user in the localstorage", () => {
store.dispatch(logout());
expect(localStorage.getItem("token")).toEqual(null);
expect(localStorage.getItem("user")).toEqual(null);
});

it("Should dispatch a network error", () => {
beforeEach(() => {
mock.restore();
mock.reset();
});

store.dispatch(login("example@example.com", 12345));
expect(store.getState().userReducer).toEqual({
message: "Loading...",
showPreloader: false,
success: false,
user: {}
});
});
});
1 change: 0 additions & 1 deletion src/client/reducers/userReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default function userReducer(state = initialState, action) {
};

case actionsType.CREATE_ACCOUNT_SUCCESS:
case actionsType.CREATE_ACCOUNT_ERROR:
return {
...state,
showPreloader: false,
Expand Down

0 comments on commit 1b08e33

Please sign in to comment.