mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
restructured code/added tests
This commit is contained in:
@@ -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 = "scm/users/FETCH_USER";
|
||||||
export const FETCH_USER_SUCCESS = "scm/users/FETCH_USER_SUCCESS";
|
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 = "scm/users/ADD";
|
||||||
export const ADD_USER_SUCCESS = "scm/users/ADD_SUCCESS";
|
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 {
|
return {
|
||||||
type: FETCH_USER_SUCCESS,
|
type: FETCH_USER_SUCCESS,
|
||||||
payload: user
|
payload: user
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fetchUserFailure(user: User, error: Error) {
|
||||||
|
return {
|
||||||
|
type: FETCH_USER_FAILURE,
|
||||||
|
user,
|
||||||
|
error
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function requestAddUser(user: User) {
|
export function requestAddUser(user: User) {
|
||||||
return {
|
return {
|
||||||
type: ADD_USER,
|
type: ADD_USER,
|
||||||
@@ -173,8 +182,8 @@ export function updateUser(user: User) {
|
|||||||
dispatch(fetchUsers());
|
dispatch(fetchUsers());
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err)
|
console.log(err);
|
||||||
dispatch(updateUserFailure(user, err))
|
dispatch(updateUserFailure(user, err));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -305,6 +314,7 @@ const reduceUsersByNames = (
|
|||||||
|
|
||||||
export default function reducer(state: any = {}, action: any = {}) {
|
export default function reducer(state: any = {}, action: any = {}) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
// fetch user list cases
|
||||||
case FETCH_USERS:
|
case FETCH_USERS:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -312,17 +322,6 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
loading: true
|
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:
|
case FETCH_USERS_SUCCESS:
|
||||||
// return red(state, action.payload._embedded.users);
|
// return red(state, action.payload._embedded.users);
|
||||||
const users = action.payload._embedded.users;
|
const users = action.payload._embedded.users;
|
||||||
@@ -342,6 +341,22 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
},
|
},
|
||||||
usersByNames
|
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:
|
case FETCH_USER_SUCCESS:
|
||||||
const ubn = extractUsersByNames(
|
const ubn = extractUsersByNames(
|
||||||
[action.payload],
|
[action.payload],
|
||||||
@@ -357,6 +372,19 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
},
|
},
|
||||||
usersByNames: ubn
|
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:
|
case DELETE_USER_SUCCESS:
|
||||||
const newUserByNames = deleteUserInUsersByNames(state.usersByNames, [
|
const newUserByNames = deleteUserInUsersByNames(state.usersByNames, [
|
||||||
action.payload.name
|
action.payload.name
|
||||||
@@ -372,15 +400,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
},
|
},
|
||||||
usersByNames: newUserByNames
|
usersByNames: newUserByNames
|
||||||
};
|
};
|
||||||
case FETCH_USERS_FAILURE:
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
users: {
|
|
||||||
...state.users,
|
|
||||||
loading: false,
|
|
||||||
error: action.payload.error
|
|
||||||
}
|
|
||||||
};
|
|
||||||
case DELETE_USER_FAILURE:
|
case DELETE_USER_FAILURE:
|
||||||
const newState = reduceUsersByNames(state, action.payload.user.name, {
|
const newState = reduceUsersByNames(state, action.payload.user.name, {
|
||||||
loading: false,
|
loading: false,
|
||||||
@@ -394,6 +414,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
error: action.payload.error
|
error: action.payload.error
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// Add single user cases
|
||||||
case ADD_USER:
|
case ADD_USER:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -412,15 +433,6 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
error: null
|
error: null
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
case ADD_USER_SUCCESS:
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
users: {
|
|
||||||
...state.users,
|
|
||||||
loading: false,
|
|
||||||
error: null
|
|
||||||
}
|
|
||||||
};
|
|
||||||
case ADD_USER_FAILURE:
|
case ADD_USER_FAILURE:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -430,6 +442,7 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
error: action.payload
|
error: action.payload
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// Update single user cases
|
||||||
case UPDATE_USER:
|
case UPDATE_USER:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -454,7 +467,6 @@ export default function reducer(state: any = {}, action: any = {}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ import {
|
|||||||
addUserFailure,
|
addUserFailure,
|
||||||
updateUserFailure,
|
updateUserFailure,
|
||||||
deleteUserSuccess,
|
deleteUserSuccess,
|
||||||
failedToFetchUsers
|
failedToFetchUsers,
|
||||||
|
requestUser,
|
||||||
|
fetchUserFailure
|
||||||
} from "./users";
|
} from "./users";
|
||||||
|
|
||||||
import reducer from "./users";
|
import reducer from "./users";
|
||||||
@@ -404,4 +406,17 @@ describe("users reducer", () => {
|
|||||||
expect(newState.users.loading).toBeFalsy();
|
expect(newState.users.loading).toBeFalsy();
|
||||||
expect(newState.users.error).toEqual(new Error("kaputt kaputt"));
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user