Skip to content

Commit

Permalink
Resolving conflicts to merge to tdd redux action login
Browse files Browse the repository at this point in the history
  • Loading branch information
robisson committed Dec 20, 2017
2 parents a669b06 + 8f9219a commit e66940a
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ stats.json

#apidoc
apidoc

.vscode
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"apidoc": "^0.17.6",
"axios-mock-adapter": "^1.10.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.3",
Expand All @@ -68,9 +69,11 @@
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.5.1",
"eslint-watch": "^3.1.3",
"jest-localstorage-mock": "^2.1.0",
"mocha": "^4.0.1",
"mongoose": "^4.13.7",
"morgan": "^1.9.0",
"nock": "^9.1.5",
"node-sass-chokidar": "0.0.3",
"npm-check": "^5.5.2",
"npm-run-all": "^4.0.2",
Expand Down
21 changes: 15 additions & 6 deletions src/client/actions/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export function login(email, password) {
let { data: { success, message, token, user } } = response;

if (success) {
window.localStorage.setItem("token", JSON.stringify(token));
window.localStorage.setItem("user", JSON.stringify(user));
localStorage.setItem("token", JSON.stringify(token));
localStorage.setItem("user", JSON.stringify(user));
}

dispatch(loginSuccess(success, message, user));
})
.catch(error => {
throw error;
dispatch(loginError(false, error, {}));
});
};
}

export function logout() {
return dispatch => {
window.localStorage.removeItem("token");
window.localStorage.removeItem("user");
window.localStorage.removeItem("state");
localStorage.removeItem("token");
localStorage.removeItem("user");
localStorage.removeItem("state");

dispatch(logoutSuccess());
};
Expand All @@ -41,6 +41,15 @@ export function loginSuccess(success, message, user) {
};
}

export function loginError(success, message, user) {
return {
type: actionsType.LOGIN_ERROR,
success: success,
message: message,
user
};
}

export function logoutSuccess() {
return { type: actionsType.LOGOUT_SUCCESS };
}
1 change: 1 addition & 0 deletions src/client/actions/actionsType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const actionsType = {
LOAD_COLUMNS_SUCCESS: "LOAD_COLUMNS_SUCCESS",
LOADING_COLUMNS: "LOADING_COLUMNS",
LOGIN_SUCCESS: "LOGIN_SUCCESS",
LOGIN_ERROR: "LOGIN_ERROR",
LOADING_LOGIN: "LOADING_LOGIN",
LOGOUT_SUCCESS: "LOGOUT_SUCCESS",
CREATE_ACCOUNT_SEND: "CREATE_ACCOUNT_SEND",
Expand Down
52 changes: 52 additions & 0 deletions src/client/api/__tests__/BoardApi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { login, logout } from "../../actions/Login";
import configureStore from "../../configureStore";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import "jest-localstorage-mock";

describe("Action Login", () => {
let store;
let mock;
beforeEach(() => {
store = configureStore();
mock = new MockAdapter(axios);
});

afterEach(() => {
mock.restore();
mock.reset();
});

it("Shoud to authenticate a user and set token localstorage", () => {
let response = {
success: true,
message: "Login User successfull!",
token: "asijaosijas1109281029812",
user: { name: "Name example", email: "email@example.com" },
showPreloader: false
};

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

const expectedState = [
{
success: response.success,
message: response.message,
user: response.user,
showPreloader: response.showPreloader
}
];

store.dispatch(login(response.user.email, 12345)).then(() => {
expect([store.getState().userReducer]).toEqual(expectedState);
expect(response.token).toEqual(JSON.parse(localStorage.getItem("token")));
expect(response.user).toEqual(JSON.parse(localStorage.getItem("user")));
});
});

it("Shoud 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);
});
});
5 changes: 3 additions & 2 deletions src/client/components/User/__tests__/CreateAccount.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from "react";
import CreateAccount from "../CreateAccount";
import { shallow } from "enzyme";

// setup file
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import { shallow } from "enzyme";

configure({ adapter: new Adapter() });
export default configure({ adapter: new Adapter() });


describe("Create Account User", () => {
let createAccount;
Expand Down
1 change: 1 addition & 0 deletions src/client/reducers/userReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function userReducer(state = initialState, action) {
message: "Loading..."
};
case actionsType.LOGIN_SUCCESS:
case actionsType.LOGIN_ERROR:
var { message, success, user } = action;

return { ...state, message, success, user };
Expand Down
5 changes: 5 additions & 0 deletions src/client/setupTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// setup file
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

export default configure({ adapter: new Adapter() });

0 comments on commit e66940a

Please sign in to comment.