Added unit tests

This commit is contained in:
Philipp Czora
2018-07-25 16:42:52 +02:00
parent d1bbb6a14f
commit d0c8b66459
2 changed files with 55 additions and 36 deletions

View File

@@ -180,7 +180,6 @@ export function modifyUser(user: User) {
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER) .putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
.then(() => { .then(() => {
dispatch(modifyUserSuccess(user)); dispatch(modifyUserSuccess(user));
dispatch(fetchUsers());
}) })
.catch(err => { .catch(err => {
dispatch(modifyUserFailure(user, err)); dispatch(modifyUserFailure(user, err));
@@ -292,7 +291,7 @@ function extractUsersByNames(
} }
return usersByNames; return usersByNames;
} }
function deleteUserInUsersByNames(users: {}, userName: any) { function deleteUserInUsersByNames(users: {}, userName: string) {
let newUsers = {}; let newUsers = {};
for (let username in users) { for (let username in users) {
if (username !== userName) newUsers[username] = users[username]; if (username !== userName) newUsers[username] = users[username];
@@ -300,7 +299,7 @@ function deleteUserInUsersByNames(users: {}, userName: any) {
return newUsers; return newUsers;
} }
function deleteUserInEntries(users: [], userName: any) { function deleteUserInEntries(users: [], userName: string) {
let newUsers = []; let newUsers = [];
for (let user of users) { for (let user of users) {
if (user !== userName) newUsers.push(user); if (user !== userName) newUsers.push(user);
@@ -331,11 +330,13 @@ export default function reducer(state: any = {}, action: any = {}) {
} }
}; };
case FETCH_USERS_SUCCESS: case FETCH_USERS_SUCCESS:
// return red(state, action.payload._embedded.users);
const users = action.payload._embedded.users; const users = action.payload._embedded.users;
const userNames = users.map(user => user.name); const userNames = users.map(user => user.name);
const byNames = extractUsersByNames(users, userNames, state.byNames); const byNames = extractUsersByNames(users, userNames, state.byNames);
return { return {
...state, ...state,
userCreatePermission: action.payload._links.create ? true : false,
list: { list: {
error: null, error: null,
entries: userNames, entries: userNames,
@@ -382,12 +383,14 @@ export default function reducer(state: any = {}, action: any = {}) {
}); });
case DELETE_USER_SUCCESS: case DELETE_USER_SUCCESS:
const newUserByNames = deleteUserInUsersByNames(state.byNames, [ const newUserByNames = deleteUserInUsersByNames(
state.byNames,
action.payload.name action.payload.name
]); );
const newUserEntries = deleteUserInEntries(state.list.entries, [ const newUserEntries = deleteUserInEntries(
state.list.entries,
action.payload.name action.payload.name
]); );
return { return {
...state, ...state,
list: { list: {

View File

@@ -89,28 +89,6 @@ const userFord = {
} }
}; };
const responseBodyZaphod = {
page: 0,
pageTotal: 1,
_links: {
self: {
href: "http://localhost:3000/scm/api/rest/v2/users/?page=0&pageSize=10"
},
first: {
href: "http://localhost:3000/scm/api/rest/v2/users/?page=0&pageSize=10"
},
last: {
href: "http://localhost:3000/scm/api/rest/v2/users/?page=0&pageSize=10"
},
create: {
href: "http://localhost:3000/scm/api/rest/v2/users/"
}
},
_embedded: {
users: [userZaphod]
}
};
const responseBody = { const responseBody = {
page: 0, page: 0,
pageTotal: 1, pageTotal: 1,
@@ -259,14 +237,12 @@ describe("users fetch()", () => {
status: 204 status: 204
}); });
// after update, the users are fetched again // after update, the users are fetched again
fetchMock.getOnce(USERS_URL, response);
const store = mockStore({}); const store = mockStore({});
return store.dispatch(modifyUser(userZaphod)).then(() => { return store.dispatch(modifyUser(userZaphod)).then(() => {
const actions = store.getActions(); const actions = store.getActions();
expect(actions[0].type).toEqual(MODIFY_USER_PENDING); expect(actions[0].type).toEqual(MODIFY_USER_PENDING);
expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS); expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS);
expect(actions[2].type).toEqual(FETCH_USERS_PENDING);
}); });
}); });
@@ -345,7 +321,7 @@ describe("users reducer", () => {
expect(newState.list.userCreatePermission).toBeTruthy(); expect(newState.list.userCreatePermission).toBeTruthy();
}); });
test("should update state correctly according to DELETE_USER action", () => { test("should update state correctly according to DELETE_USER_PENDING action", () => {
const state = { const state = {
usersByNames: { usersByNames: {
zaphod: { zaphod: {
@@ -364,7 +340,7 @@ describe("users reducer", () => {
it("should not effect other users if one user will be deleted", () => { it("should not effect other users if one user will be deleted", () => {
const state = { const state = {
usersByNames: { byNames: {
zaphod: { zaphod: {
loading: false, loading: false,
error: null, error: null,
@@ -377,7 +353,7 @@ describe("users reducer", () => {
}; };
const newState = reducer(state, deleteUserPending(userZaphod)); const newState = reducer(state, deleteUserPending(userZaphod));
const ford = newState.usersByNames["ford"]; const ford = newState.byNames["ford"];
expect(ford.loading).toBeFalsy(); expect(ford.loading).toBeFalsy();
}); });
@@ -400,7 +376,7 @@ describe("users reducer", () => {
it("should not effect other users if one user could not be deleted", () => { it("should not effect other users if one user could not be deleted", () => {
const state = { const state = {
usersByNames: { byNames: {
zaphod: { zaphod: {
loading: false, loading: false,
error: null, error: null,
@@ -414,10 +390,35 @@ describe("users reducer", () => {
const error = new Error("error"); const error = new Error("error");
const newState = reducer(state, deleteUserFailure(userZaphod, error)); const newState = reducer(state, deleteUserFailure(userZaphod, error));
const ford = newState.usersByNames["ford"]; const ford = newState.byNames["ford"];
expect(ford.loading).toBeFalsy(); expect(ford.loading).toBeFalsy();
}); });
it("should remove user from state when delete succeeds", () => {
const state = {
list: {
entries: ["ford", "zaphod"]
},
byNames: {
zaphod: {
loading: true,
error: null,
entry: userZaphod
},
ford: {
loading: true,
error: null,
entry: userFord
}
}
};
const newState = reducer(state, deleteUserSuccess(userFord));
expect(newState.byNames["zaphod"]).toBeDefined();
expect(newState.list.entries).toEqual(["zaphod"]);
expect(newState.byNames["ford"]).toBeFalsy();
});
it("should not replace whole byNames map when fetching users", () => { it("should not replace whole byNames map when fetching users", () => {
const oldState = { const oldState = {
byNames: { byNames: {
@@ -432,6 +433,21 @@ describe("users reducer", () => {
expect(newState.byNames["ford"]).toBeDefined(); expect(newState.byNames["ford"]).toBeDefined();
}); });
it("should set error when fetching users failed", () => {
const oldState = {
list: {
loading: true
}
};
const error = new Error("kaputt");
const newState = reducer(oldState, fetchUsersFailure("url.com", error));
expect(newState.list.loading).toBeFalsy();
expect(newState.list.error).toEqual(error);
});
it("should set userCreatePermission to true if update link is present", () => { it("should set userCreatePermission to true if update link is present", () => {
const newState = reducer({}, fetchUsersSuccess(responseBody)); const newState = reducer({}, fetchUsersSuccess(responseBody));