restructured code/added tests

This commit is contained in:
Philipp Czora
2018-07-19 17:37:20 +02:00
parent 1d3d1bb41e
commit 9ab231477c
2 changed files with 61 additions and 34 deletions

View File

@@ -11,6 +11,7 @@ export const FETCH_USERS_NOTFOUND = "scm/users/FETCH_NOTFOUND";
export const FETCH_USER = "scm/users/FETCH_USER";
export const FETCH_USER_SUCCESS = "scm/users/FETCH_USER_SUCCESS";
export const FETCH_USER_FAILURE = "scm/users/FETCH_USER_FAILURE";
export const ADD_USER = "scm/users/ADD";
export const ADD_USER_SUCCESS = "scm/users/ADD_SUCCESS";
@@ -108,13 +109,21 @@ export function fetchUser(name: string) {
};
}
function fetchUserSuccess(user: User) {
export function fetchUserSuccess(user: User) {
return {
type: FETCH_USER_SUCCESS,
payload: user
};
}
export function fetchUserFailure(user: User, error: Error) {
return {
type: FETCH_USER_FAILURE,
user,
error
};
}
export function requestAddUser(user: User) {
return {
type: ADD_USER,
@@ -173,8 +182,8 @@ export function updateUser(user: User) {
dispatch(fetchUsers());
})
.catch(err => {
console.log(err)
dispatch(updateUserFailure(user, err))
console.log(err);
dispatch(updateUserFailure(user, err));
});
};
}
@@ -305,6 +314,7 @@ const reduceUsersByNames = (
export default function reducer(state: any = {}, action: any = {}) {
switch (action.type) {
// fetch user list cases
case FETCH_USERS:
return {
...state,
@@ -312,17 +322,6 @@ export default function reducer(state: any = {}, action: any = {}) {
loading: true
}
};
case DELETE_USER:
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null,
entry: action.payload
});
case FETCH_USER:
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null
});
case FETCH_USERS_SUCCESS:
// return red(state, action.payload._embedded.users);
const users = action.payload._embedded.users;
@@ -342,6 +341,22 @@ export default function reducer(state: any = {}, action: any = {}) {
},
usersByNames
};
case FETCH_USERS_FAILURE:
return {
...state,
users: {
...state.users,
loading: false,
error: action.payload.error
}
};
// Fetch single user cases
case FETCH_USER:
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null
});
case FETCH_USER_SUCCESS:
const ubn = extractUsersByNames(
[action.payload],
@@ -357,6 +372,19 @@ export default function reducer(state: any = {}, action: any = {}) {
},
usersByNames: ubn
};
case FETCH_USER_FAILURE:
return reduceUsersByNames(state, action.user.name, {
loading: true,
error: action.error
});
// Delete single user cases
case DELETE_USER:
return reduceUsersByNames(state, action.payload.name, {
loading: true,
error: null,
entry: action.payload
});
case DELETE_USER_SUCCESS:
const newUserByNames = deleteUserInUsersByNames(state.usersByNames, [
action.payload.name
@@ -372,15 +400,7 @@ export default function reducer(state: any = {}, action: any = {}) {
},
usersByNames: newUserByNames
};
case FETCH_USERS_FAILURE:
return {
...state,
users: {
...state.users,
loading: false,
error: action.payload.error
}
};
case DELETE_USER_FAILURE:
const newState = reduceUsersByNames(state, action.payload.user.name, {
loading: false,
@@ -394,6 +414,7 @@ export default function reducer(state: any = {}, action: any = {}) {
error: action.payload.error
}
};
// Add single user cases
case ADD_USER:
return {
...state,
@@ -412,15 +433,6 @@ export default function reducer(state: any = {}, action: any = {}) {
error: null
}
};
case ADD_USER_SUCCESS:
return {
...state,
users: {
...state.users,
loading: false,
error: null
}
};
case ADD_USER_FAILURE:
return {
...state,
@@ -430,6 +442,7 @@ export default function reducer(state: any = {}, action: any = {}) {
error: action.payload
}
};
// Update single user cases
case UPDATE_USER:
return {
...state,
@@ -454,7 +467,6 @@ export default function reducer(state: any = {}, action: any = {}) {
}
}
};
default:
return state;
}

View File

@@ -30,7 +30,9 @@ import {
addUserFailure,
updateUserFailure,
deleteUserSuccess,
failedToFetchUsers
failedToFetchUsers,
requestUser,
fetchUserFailure
} from "./users";
import reducer from "./users";
@@ -404,4 +406,17 @@ describe("users reducer", () => {
expect(newState.users.loading).toBeFalsy();
expect(newState.users.error).toEqual(new Error("kaputt kaputt"));
});
it("should update state according to FETCH_USER action", () => {
const newState = reducer({}, requestUser("zaphod"));
expect(newState.usersByNames["zaphod"].loading).toBeTruthy();
});
it("should uppdate state according to FETCH_USER_FAILURE action", () => {
const newState = reducer(
{},
fetchUserFailure(userFord, new Error("kaputt!"))
);
expect(newState.usersByNames["ford"].error).toBeTruthy;
});
});