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 {
|
return {
|
||||||
type: DELETE_USER,
|
type: DELETE_USER,
|
||||||
url
|
payload: user
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteUserSuccess() {
|
function deleteUserSuccess(user: User) {
|
||||||
return {
|
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 {
|
return {
|
||||||
type: DELETE_USER_FAILURE,
|
type: DELETE_USER_FAILURE,
|
||||||
payload: err,
|
payload: {
|
||||||
url
|
error,
|
||||||
|
user
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteUser(link: string) {
|
export function deleteUser(user: User) {
|
||||||
return function (dispatch: ThunkDispatch) {
|
return function (dispatch: any) {
|
||||||
dispatch(requestDeleteUser(link));
|
dispatch(requestDeleteUser(user));
|
||||||
return apiClient
|
return apiClient
|
||||||
.delete(link)
|
.delete(user._links.delete.href)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
dispatch(deleteUserSuccess());
|
dispatch(deleteUserSuccess(user));
|
||||||
dispatch(fetchUsers());
|
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 = {}) {
|
export default function reducer(state: any = {}, action: any = {}) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case FETCH_USERS:
|
case FETCH_USERS:
|
||||||
@@ -247,10 +262,11 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
case DELETE_USER:
|
case DELETE_USER:
|
||||||
return {
|
return reduceUsersByNames(state, action.payload.name, {
|
||||||
...state,
|
loading: true,
|
||||||
users: null
|
error: null,
|
||||||
};
|
entry: action.payload
|
||||||
|
});
|
||||||
case FETCH_USERS_SUCCESS:
|
case FETCH_USERS_SUCCESS:
|
||||||
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);
|
||||||
@@ -269,6 +285,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
},
|
},
|
||||||
usersByNames
|
usersByNames
|
||||||
};
|
};
|
||||||
|
|
||||||
case FETCH_USERS_FAILURE:
|
case FETCH_USERS_FAILURE:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -276,11 +293,11 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
loading: false
|
loading: false
|
||||||
};
|
};
|
||||||
case DELETE_USER_FAILURE:
|
case DELETE_USER_FAILURE:
|
||||||
return {
|
return reduceUsersByNames(state, action.payload.user.name, {
|
||||||
...state,
|
loading: false,
|
||||||
error: action.payload,
|
error: action.payload.error,
|
||||||
loading: false
|
entry: action.payload.user
|
||||||
};
|
});
|
||||||
case EDIT_USER:
|
case EDIT_USER:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ import {
|
|||||||
UPDATE_USER,
|
UPDATE_USER,
|
||||||
UPDATE_USER_FAILURE,
|
UPDATE_USER_FAILURE,
|
||||||
UPDATE_USER_SUCCESS,
|
UPDATE_USER_SUCCESS,
|
||||||
EDIT_USER
|
EDIT_USER,
|
||||||
|
requestDeleteUser,
|
||||||
|
deleteUserFailure
|
||||||
} from "./users";
|
} from "./users";
|
||||||
|
|
||||||
import reducer 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", () => {
|
test("reducer does not replace whole usersByNames map", () => {
|
||||||
const oldState = {
|
const oldState = {
|
||||||
usersByNames: {
|
usersByNames: {
|
||||||
@@ -232,6 +309,7 @@ describe("reducer tests", () => {
|
|||||||
expect(newState.usersByNames["ford"]).toBeDefined();
|
expect(newState.usersByNames["ford"]).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
test("edit user", () => {
|
test("edit user", () => {
|
||||||
const newState = reducer(
|
const newState = reducer(
|
||||||
{},
|
{},
|
||||||
|
|||||||
Reference in New Issue
Block a user