mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Added unit tests
This commit is contained in:
@@ -180,7 +180,6 @@ export function modifyUser(user: User) {
|
||||
.putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER)
|
||||
.then(() => {
|
||||
dispatch(modifyUserSuccess(user));
|
||||
dispatch(fetchUsers());
|
||||
})
|
||||
.catch(err => {
|
||||
dispatch(modifyUserFailure(user, err));
|
||||
@@ -292,7 +291,7 @@ function extractUsersByNames(
|
||||
}
|
||||
return usersByNames;
|
||||
}
|
||||
function deleteUserInUsersByNames(users: {}, userName: any) {
|
||||
function deleteUserInUsersByNames(users: {}, userName: string) {
|
||||
let newUsers = {};
|
||||
for (let username in users) {
|
||||
if (username !== userName) newUsers[username] = users[username];
|
||||
@@ -300,7 +299,7 @@ function deleteUserInUsersByNames(users: {}, userName: any) {
|
||||
return newUsers;
|
||||
}
|
||||
|
||||
function deleteUserInEntries(users: [], userName: any) {
|
||||
function deleteUserInEntries(users: [], userName: string) {
|
||||
let newUsers = [];
|
||||
for (let user of users) {
|
||||
if (user !== userName) newUsers.push(user);
|
||||
@@ -331,11 +330,13 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
}
|
||||
};
|
||||
case FETCH_USERS_SUCCESS:
|
||||
// return red(state, action.payload._embedded.users);
|
||||
const users = action.payload._embedded.users;
|
||||
const userNames = users.map(user => user.name);
|
||||
const byNames = extractUsersByNames(users, userNames, state.byNames);
|
||||
return {
|
||||
...state,
|
||||
userCreatePermission: action.payload._links.create ? true : false,
|
||||
list: {
|
||||
error: null,
|
||||
entries: userNames,
|
||||
@@ -382,12 +383,14 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
});
|
||||
|
||||
case DELETE_USER_SUCCESS:
|
||||
const newUserByNames = deleteUserInUsersByNames(state.byNames, [
|
||||
const newUserByNames = deleteUserInUsersByNames(
|
||||
state.byNames,
|
||||
action.payload.name
|
||||
]);
|
||||
const newUserEntries = deleteUserInEntries(state.list.entries, [
|
||||
);
|
||||
const newUserEntries = deleteUserInEntries(
|
||||
state.list.entries,
|
||||
action.payload.name
|
||||
]);
|
||||
);
|
||||
return {
|
||||
...state,
|
||||
list: {
|
||||
|
||||
@@ -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 = {
|
||||
page: 0,
|
||||
pageTotal: 1,
|
||||
@@ -259,14 +237,12 @@ describe("users fetch()", () => {
|
||||
status: 204
|
||||
});
|
||||
// after update, the users are fetched again
|
||||
fetchMock.getOnce(USERS_URL, response);
|
||||
|
||||
const store = mockStore({});
|
||||
return store.dispatch(modifyUser(userZaphod)).then(() => {
|
||||
const actions = store.getActions();
|
||||
expect(actions[0].type).toEqual(MODIFY_USER_PENDING);
|
||||
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();
|
||||
});
|
||||
|
||||
test("should update state correctly according to DELETE_USER action", () => {
|
||||
test("should update state correctly according to DELETE_USER_PENDING action", () => {
|
||||
const state = {
|
||||
usersByNames: {
|
||||
zaphod: {
|
||||
@@ -364,7 +340,7 @@ describe("users reducer", () => {
|
||||
|
||||
it("should not effect other users if one user will be deleted", () => {
|
||||
const state = {
|
||||
usersByNames: {
|
||||
byNames: {
|
||||
zaphod: {
|
||||
loading: false,
|
||||
error: null,
|
||||
@@ -377,7 +353,7 @@ describe("users reducer", () => {
|
||||
};
|
||||
|
||||
const newState = reducer(state, deleteUserPending(userZaphod));
|
||||
const ford = newState.usersByNames["ford"];
|
||||
const ford = newState.byNames["ford"];
|
||||
expect(ford.loading).toBeFalsy();
|
||||
});
|
||||
|
||||
@@ -400,7 +376,7 @@ describe("users reducer", () => {
|
||||
|
||||
it("should not effect other users if one user could not be deleted", () => {
|
||||
const state = {
|
||||
usersByNames: {
|
||||
byNames: {
|
||||
zaphod: {
|
||||
loading: false,
|
||||
error: null,
|
||||
@@ -414,10 +390,35 @@ describe("users reducer", () => {
|
||||
|
||||
const error = new Error("error");
|
||||
const newState = reducer(state, deleteUserFailure(userZaphod, error));
|
||||
const ford = newState.usersByNames["ford"];
|
||||
const ford = newState.byNames["ford"];
|
||||
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", () => {
|
||||
const oldState = {
|
||||
byNames: {
|
||||
@@ -432,6 +433,21 @@ describe("users reducer", () => {
|
||||
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", () => {
|
||||
const newState = reducer({}, fetchUsersSuccess(responseBody));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user