diff --git a/app/actions/authActions.js b/app/actions/authActions.js
index 82f4248..2af9f4b 100644
--- a/app/actions/authActions.js
+++ b/app/actions/authActions.js
@@ -1,13 +1,23 @@
import alt from '../alt';
+import login from '../services/authService';
class AuthActions {
constructor() {
this.generateActions(
'loginBegin',
'loginSuccess',
- 'loginFailed',
+ 'onError',
);
}
+
+ login(data) {
+ this.loginBegin();
+
+ login(data)
+ .then((response) => {
+ this.loginSuccess(response);
+ }, error => this.onError(error));
+ }
}
export default alt.createActions(AuthActions);
diff --git a/app/actions/authActions.test.js b/app/actions/authActions.test.js
deleted file mode 100644
index c6b0aa0..0000000
--- a/app/actions/authActions.test.js
+++ /dev/null
@@ -1,98 +0,0 @@
-import configureMockStore from 'redux-mock-store';
-import thunk from 'redux-thunk';
-import * as authActions from './authActions';
-import { AUTH } from './actionTypes';
-import * as authService from '../services/authService';
-
-const middlewares = [thunk];
-const mockStore = configureMockStore(middlewares);
-
-describe('authActions', () => {
- it('should create an action to start a login request', () => {
- // Arrange
- const expectedAction = {
- type: AUTH.LOGIN_BEGIN,
- };
-
- // Act
- const result = authActions.loginRequest();
-
- // Assert
- expect(result).toEqual(expectedAction);
- });
-
- it('should create an action to handle a successfully login', () => {
- // Arrange
- const user = {
- name: 'John',
- };
- const expectedAction = {
- type: AUTH.LOGIN_SUCCESS,
- user,
- };
-
- // Act
- const result = authActions.loginSuccess(user);
-
- // Assert
- expect(result).toEqual(expectedAction);
- });
-
- it('should create an action to handle a failed login', () => {
- // Arrange
- const message = 'Testing an error.';
- const expectedAction = {
- type: AUTH.LOGIN_FAILED,
- message,
- };
-
- // Act
- const result = authActions.loginFailed(message);
-
- // Assert
- expect(result).toEqual(expectedAction);
- });
-
- it('should handle successful login when valid credentials provided', (done) => {
- // Arrange
- authService.login = jest.fn(() => Promise.resolve({ name: 'John' }));
- const username = 'username';
- const password = 'password';
- const user = {
- name: 'John',
- };
- const store = mockStore({
- auth: {},
- });
-
- // Act
- return store.dispatch(authActions.login({ username, password })).then(() => {
- const actions = store.getActions();
- // Assert
- expect(actions[0]).toEqual({ type: AUTH.LOGIN_BEGIN });
- expect(actions[1]).toEqual({ type: AUTH.LOGIN_SUCCESS, user });
- done();
- });
- });
-
- it('should handle failed login when invalid credentials provided', (done) => {
- // Arrange
- authService.login = jest.fn(() => Promise.reject('Invalid credentials.'));
-
- const username = 'invalid';
- const password = 'invalid';
- const message = 'Invalid credentials.';
- const store = mockStore({
- auth: {},
- });
-
- // Act
- return store.dispatch(authActions.login(username, password)).then(() => {
- const actions = store.getActions();
- // Assert
- expect(actions[0]).toEqual({ type: AUTH.LOGIN_BEGIN });
- expect(actions[1]).toEqual({ type: AUTH.LOGIN_FAILED, message });
- done();
- });
- });
-});
diff --git a/app/actions/usersActions.js b/app/actions/usersActions.js
index 77166e2..1ef1510 100644
--- a/app/actions/usersActions.js
+++ b/app/actions/usersActions.js
@@ -6,10 +6,7 @@ import {
deleteUsers,
} from '../services/userService';
import { omit } from '../utils/functions';
-import {
- DEFAULT_USER_VALID_ID_PATHS,
- DEFAULT_PAGINATION_QUERY,
-} from '../constants';
+import { DEFAULT_USER_VALID_ID_PATHS } from '../constants';
import getUserId from '../utils/user';
class UserActions {
diff --git a/app/actions/usersActions.test.js b/app/actions/usersActions.test.js
deleted file mode 100644
index 90e2c6a..0000000
--- a/app/actions/usersActions.test.js
+++ /dev/null
@@ -1,349 +0,0 @@
-import configureStore from 'redux-mock-store';
-import thunkMiddleware from 'redux-thunk';
-import { omit } from '../utils/functions';
-import {
- createUser,
- getUsers,
- selectUser,
- updateUser,
- deleteUser,
-} from './usersActions';
-import * as userService from '../services/userService';
-import { USERS } from './actionTypes';
-
-describe('usersActions', () => {
- const middlewares = [
- thunkMiddleware,
- ];
- const mockStore = configureStore(middlewares);
- const defaultReponseStatusProps = {
- status: 200,
- statusText: 'OK',
- ok: true,
- };
- const defaultPaginatedResponse = {
- count: 1,
- page: 0,
- limit: 0,
- totalPages: 1,
- docs: [{ foo: 'bar' }],
- };
- let store;
-
- function createResponse(response) {
- return {
- ...omit(response, ['data']),
- data: { ...response.data },
- };
- }
-
- describe('When service call is successful', () => {
- beforeEach(() => {
- store = mockStore({});
-
- userService.createUsers = jest.fn(data => Promise.resolve(createResponse({
- ...defaultReponseStatusProps,
- data,
- })));
-
- userService.deleteUsers = jest.fn(() => Promise.resolve(createResponse({
- ...defaultReponseStatusProps,
- statusCode: 204,
- })));
-
- userService.fetchUsers = jest.fn(() => Promise.resolve(createResponse({
- ...defaultReponseStatusProps,
- data: defaultPaginatedResponse,
- })));
-
- userService.updateUsers = jest.fn((userId, user) => Promise.resolve(createResponse({
- ...defaultReponseStatusProps,
- data: {
- _id: userId,
- ...user,
- },
- })));
- });
-
- afterEach(() => {
- userService.createUsers.mockClear();
- userService.deleteUsers.mockClear();
- userService.fetchUsers.mockClear();
- userService.updateUsers.mockClear();
- });
- describe('getUsers', () => {
- it('should be defined', () => {
- expect(userService.fetchUsers).toBeDefined();
- });
-
- it('should be a function', () => {
- expect(userService.fetchUsers).toEqual(expect.any(Function));
- });
-
- describe('when the service call is successful', () => {
- it('should create an action to get users', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- { type: USERS.LOADING_COMPLETE },
- {
- type: USERS.GET_ALL_SUCCESS,
- payload: {
- count: 1,
- page: 0,
- limit: 0,
- totalPages: 1,
- users: [{ foo: 'bar' }],
- },
- },
- ];
-
- // Act
- await store.dispatch(getUsers());
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
-
- describe('selectUser', () => {
- describe('when the service call is successful', () => {
- it('should create an action to select a user', async () => {
- // Arrange
- const expectedActions = [{
- payload: {
- _id: 'fake.id.john',
- name: 'John Doe',
- },
- type: USERS.SELECT_SUCCESS,
- }];
-
- // Act
- await store.dispatch(selectUser({
- _id: 'fake.id.john',
- name: 'John Doe',
- }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
-
- describe('createUsers', () => {
- it('should be defined', () => {
- expect(userService.createUsers).toBeDefined();
- });
-
- it('should be a function', () => {
- expect(userService.createUsers).toEqual(expect.any(Function));
- });
-
- describe('when the service call is successful', () => {
- it('should create an action to create a user', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- { type: USERS.LOADING_COMPLETE },
- {
- type: USERS.CREATE_SUCCESS,
- payload: {
- name: 'John Doe',
- },
- },
- ];
- // Act
- await store.dispatch(createUser({ name: 'John Doe' }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
-
- describe('updateUsers', () => {
- it('should be defined', () => {
- expect(userService.updateUsers).toBeDefined();
- });
-
- it('should be a function', () => {
- expect(userService.updateUsers).toEqual(expect.any(Function));
- });
-
- describe('when the service call is successful', () => {
- it('should create an action to update a user', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- { type: USERS.LOADING_COMPLETE },
- {
- type: USERS.UPDATE_SUCCESS,
- payload: {
- _id: 'fake.id.john',
- name: 'John Doe Jr.',
- },
- },
- ];
-
- // Act
- await store.dispatch(updateUser({
- _id: 'fake.id.john',
- name: 'John Doe Jr.',
- }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
-
- describe('deleteUsers', () => {
- it('should be defined', () => {
- expect(userService.deleteUsers).toBeDefined();
- });
-
- it('should be a function', () => {
- expect(userService.deleteUsers).toEqual(expect.any(Function));
- });
-
- describe('when the service call is successful', () => {
- it('should create an action to delete a user', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- { type: USERS.LOADING_COMPLETE },
- {
- type: USERS.DELETE_SUCCESS,
- payload: {
- _id: 'fake.id.john',
- name: 'John Doe',
- },
- },
- ];
-
- // Act
- await store.dispatch(deleteUser({
- _id: 'fake.id.john',
- name: 'John Doe',
- }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
- });
-
- describe('when the service call fails', () => {
- beforeEach(() => {
- store = mockStore({});
-
- userService.createUsers = jest.fn(() => Promise.reject('Test Error'));
-
- userService.deleteUsers = jest.fn(() => Promise.reject('Test Error'));
-
- userService.fetchUsers = jest.fn(() => Promise.reject('Test Error'));
-
- userService.updateUsers = jest.fn(() => Promise.reject('Test Error'));
- });
-
- afterEach(() => {
- userService.createUsers.mockClear();
- userService.deleteUsers.mockClear();
- userService.fetchUsers.mockClear();
- userService.updateUsers.mockClear();
- });
-
- describe('getUsers service fails', () => {
- it('should inform when the getUser request fails', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- {
- type: USERS.LOADING_FAILED,
- payload: {
- error: 'Test Error',
- },
- },
- ];
-
- // Act
- const getUsersActionResult = getUsers();
- await store.dispatch(getUsersActionResult);
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
-
- describe('createUsers service fails', () => {
- it('should inform when the createUsers request fails', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- {
- type: USERS.LOADING_FAILED,
- payload: {
- error: 'Test Error',
- },
- },
- ];
-
- // Act
- await store.dispatch(createUser({ name: 'John Doe' }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
-
- describe('updateUsers service fails', () => {
- it('should inform when the updateUsers request fails', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- {
- type: USERS.LOADING_FAILED,
- payload: {
- error: 'Test Error',
- },
- },
- ];
-
- // Act;
- await store.dispatch(updateUser({
- _id: 'fake.id.john',
- name: 'John Doe Jr.',
- }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
-
- describe('deleteUsers service fails', () => {
- it('should inform when the deleteUsers request fails', async () => {
- // Arrange
- const expectedActions = [
- { type: USERS.LOADING_BEGIN },
- {
- type: USERS.LOADING_FAILED,
- payload: {
- error: 'Test Error',
- },
- },
- ];
-
- // Act
- await store.dispatch(deleteUser({
- _id: 'fake.id.john',
- name: 'John Doe Jr.',
- }));
-
- // Assert
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
-});
diff --git a/app/components/App.test.js b/app/components/App.test.js
index f2373ca..7d3cb39 100644
--- a/app/components/App.test.js
+++ b/app/components/App.test.js
@@ -9,17 +9,11 @@ function setup(props) {
describe('