mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
added DELETE_USER and DELETE_USER_FAILURE in reducer including tests
This commit is contained in:
@@ -158,37 +158,40 @@ function updateUserFailure(user: User, error: Error) {
|
||||
};
|
||||
}
|
||||
|
||||
function requestDeleteUser(url: string) {
|
||||
export function requestDeleteUser(user: User) {
|
||||
return {
|
||||
type: DELETE_USER,
|
||||
url
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
function deleteUserSuccess() {
|
||||
function deleteUserSuccess(user: User) {
|
||||
return {
|
||||
type: DELETE_USER_SUCCESS
|
||||
type: DELETE_USER_SUCCESS,
|
||||
payload: user
|
||||
};
|
||||
}
|
||||
|
||||
function deleteUserFailure(url: string, err: Error) {
|
||||
export function deleteUserFailure(user: User, error: Error) {
|
||||
return {
|
||||
type: DELETE_USER_FAILURE,
|
||||
payload: err,
|
||||
url
|
||||
payload: {
|
||||
error,
|
||||
user
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteUser(link: string) {
|
||||
return function (dispatch: ThunkDispatch) {
|
||||
dispatch(requestDeleteUser(link));
|
||||
export function deleteUser(user: User) {
|
||||
return function (dispatch: any) {
|
||||
dispatch(requestDeleteUser(user));
|
||||
return apiClient
|
||||
.delete(link)
|
||||
.delete(user._links.delete.href)
|
||||
.then(() => {
|
||||
dispatch(deleteUserSuccess());
|
||||
dispatch(deleteUserSuccess(user));
|
||||
dispatch(fetchUsers());
|
||||
})
|
||||
.catch(err => dispatch(deleteUserFailure(link, err)));
|
||||
.catch(err => dispatch(deleteUserFailure(user, err)));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -235,6 +238,18 @@ export function editUser(user: User) {
|
||||
};
|
||||
}
|
||||
|
||||
const reduceUsersByNames = (state: any, username: string, newUserState: any) => {
|
||||
const newUsersByNames = {
|
||||
...state.usersByNames,
|
||||
[username] : newUserState
|
||||
};
|
||||
|
||||
return {
|
||||
...state,
|
||||
usersByNames: newUsersByNames
|
||||
};
|
||||
};
|
||||
|
||||
export default function reducer(state: any = {}, action: any = {}) {
|
||||
switch (action.type) {
|
||||
case FETCH_USERS:
|
||||
@@ -247,10 +262,11 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
}
|
||||
};
|
||||
case DELETE_USER:
|
||||
return {
|
||||
...state,
|
||||
users: null
|
||||
};
|
||||
return reduceUsersByNames(state, action.payload.name, {
|
||||
loading: true,
|
||||
error: null,
|
||||
entry: action.payload
|
||||
});
|
||||
case FETCH_USERS_SUCCESS:
|
||||
const users = action.payload._embedded.users;
|
||||
const userNames = users.map(user => user.name);
|
||||
@@ -269,6 +285,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
},
|
||||
usersByNames
|
||||
};
|
||||
|
||||
case FETCH_USERS_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
@@ -276,11 +293,11 @@ export default function reducer(state: any = {}, action: any = {}) {
|
||||
loading: false
|
||||
};
|
||||
case DELETE_USER_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
error: action.payload,
|
||||
loading: false
|
||||
};
|
||||
return reduceUsersByNames(state, action.payload.user.name, {
|
||||
loading: false,
|
||||
error: action.payload.error,
|
||||
entry: action.payload.user
|
||||
});
|
||||
case EDIT_USER:
|
||||
return {
|
||||
...state,
|
||||
|
||||
@@ -15,7 +15,9 @@ import {
|
||||
UPDATE_USER,
|
||||
UPDATE_USER_FAILURE,
|
||||
UPDATE_USER_SUCCESS,
|
||||
EDIT_USER
|
||||
EDIT_USER,
|
||||
requestDeleteUser,
|
||||
deleteUserFailure
|
||||
} from "./users";
|
||||
|
||||
import reducer from "./users";
|
||||
@@ -215,6 +217,81 @@ describe("reducer tests", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("delete user requested", () => {
|
||||
const state = {
|
||||
usersByNames : {
|
||||
"zaphod": {
|
||||
loading: false,
|
||||
error: null,
|
||||
entry: userZaphod
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const newState = reducer(state, requestDeleteUser(userZaphod));
|
||||
const zaphod = newState.usersByNames["zaphod"];
|
||||
expect(zaphod.loading).toBeTruthy();
|
||||
expect(zaphod.entry).toBe(userZaphod);
|
||||
})
|
||||
|
||||
it("should not effect other users if one user will be deleted", () => {
|
||||
const state = {
|
||||
usersByNames : {
|
||||
"zaphod": {
|
||||
loading: false,
|
||||
error: null,
|
||||
entry: userZaphod
|
||||
},
|
||||
"ford": {
|
||||
loading: false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const newState = reducer(state, requestDeleteUser(userZaphod));
|
||||
const ford = newState.usersByNames["ford"];
|
||||
expect(ford.loading).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should set the error of user which could not be deleted", () => {
|
||||
const state = {
|
||||
usersByNames : {
|
||||
"zaphod": {
|
||||
loading: false,
|
||||
error: null,
|
||||
entry: userZaphod
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const error = new Error("error");
|
||||
const newState = reducer(state, deleteUserFailure(userZaphod, error));
|
||||
const zaphod = newState.usersByNames["zaphod"];
|
||||
expect(zaphod.loading).toBeFalsy();
|
||||
expect(zaphod.error).toBe(error);
|
||||
});
|
||||
|
||||
it("should not effect other users if one user could not be deleted", () => {
|
||||
const state = {
|
||||
usersByNames : {
|
||||
"zaphod": {
|
||||
loading: false,
|
||||
error: null,
|
||||
entry: userZaphod
|
||||
},
|
||||
"ford": {
|
||||
loading: false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const error = new Error("error");
|
||||
const newState = reducer(state, deleteUserFailure(userZaphod, error));
|
||||
const ford = newState.usersByNames["ford"];
|
||||
expect(ford.loading).toBeFalsy();
|
||||
});
|
||||
|
||||
|
||||
test("reducer does not replace whole usersByNames map", () => {
|
||||
const oldState = {
|
||||
usersByNames: {
|
||||
@@ -232,6 +309,7 @@ describe("reducer tests", () => {
|
||||
expect(newState.usersByNames["ford"]).toBeDefined();
|
||||
});
|
||||
|
||||
|
||||
test("edit user", () => {
|
||||
const newState = reducer(
|
||||
{},
|
||||
|
||||
Reference in New Issue
Block a user