diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index e198cccfb3..148cc2ece4 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -88,7 +88,9 @@ function fetchUsersSuccess(users: any) { function requestUser(name: string) { return { type: FETCH_USER, - payload: { name } + payload: { + name + } }; } @@ -186,7 +188,7 @@ function updateUserSuccess() { }; } -function updateUserFailure(user: User, error: Error) { +export function updateUserFailure(user: User, error: Error) { return { type: UPDATE_USER_FAILURE, payload: error, @@ -294,8 +296,10 @@ export default function reducer(state: any = {}, action: any = {}) { switch (action.type) { case FETCH_USERS: return { - loading: true, - error: null + ...state, + users: { + loading: true + } }; case DELETE_USER: return reduceUsersByNames(state, action.payload.name, { @@ -344,11 +348,18 @@ export default function reducer(state: any = {}, action: any = {}) { case FETCH_USERS_FAILURE: case DELETE_USER_FAILURE: - return reduceUsersByNames(state, action.payload.user.name, { + const newState = reduceUsersByNames(state, action.payload.user.name, { loading: false, error: action.payload.error, entry: action.payload.user }); + return { + ...newState, + users: { + ...newState.users, + error: action.payload.error + } + }; case EDIT_USER: return { ...state, diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index 56e2a9e0de..7ec5b8c409 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -22,7 +22,8 @@ import { DELETE_USER, DELETE_USER_SUCCESS, DELETE_USER_FAILURE, - deleteUser + deleteUser, + updateUserFailure } from "./users"; import reducer from "./users"; @@ -118,7 +119,9 @@ const responseBody = { }; const response = { - headers: { "content-type": "application/json" }, + headers: { + "content-type": "application/json" + }, responseBody }; @@ -132,8 +135,9 @@ describe("users fetch()", () => { it("should successfully fetch users", () => { fetchMock.getOnce("/scm/api/rest/v2/users", response); - const expectedActions = [ - { type: FETCH_USERS }, + const expectedActions = [{ + type: FETCH_USERS + }, { type: FETCH_USERS_SUCCESS, payload: response @@ -257,17 +261,20 @@ describe("users fetch()", () => { }); describe("users reducer", () => { - test("should update state correctly according to FETCH_USERS action", () => { - const newState = reducer({}, { type: FETCH_USERS }); - expect(newState.loading).toBeTruthy(); - expect(newState.error).toBeNull(); + + it("should update state correctly according to FETCH_USERS action", () => { + const newState = reducer({}, { + type: FETCH_USERS + }); + expect(newState.users.loading).toBeTruthy(); + expect(newState.users.error).toBeFalsy(); }); it("should update state correctly according to FETCH_USERS_SUCCESS action", () => { - const newState = reducer( - {}, - { type: FETCH_USERS_SUCCESS, payload: responseBody } - ); + const newState = reducer({}, { + type: FETCH_USERS_SUCCESS, + payload: responseBody + }); expect(newState.users).toEqual({ entries: ["zaphod", "ford"], @@ -358,6 +365,44 @@ describe("users reducer", () => { expect(ford.loading).toBeFalsy(); }); + it("should set global error state if one user could not be deleted", () => { + const state = { + users: { + error: null + }, + usersByNames: { + zaphod: { + loading: false, + error: null, + entry: userZaphod + } + } + }; + + const error = new Error("could not delete user zaphod: .."); + const newState = reducer(state, deleteUserFailure(userZaphod, error)); + expect(newState.users.error).toBe(error); + }); + + it("should not set global error state if one user could not be edited", () => { + const state = { + users: { + error: null + }, + usersByNames: { + zaphod: { + loading: false, + error: null, + entry: userZaphod + } + } + }; + + const error = new Error("could not edit user zaphod: .."); + const newState = reducer(state, updateUserFailure(userZaphod, error)); + expect(newState.users.error).toBe(null); + }); + it("should not replace whole usersByNames map when fetching users", () => { const oldState = { usersByNames: { @@ -376,13 +421,10 @@ describe("users reducer", () => { }); it("should update state correctly according to EDIT_USER action", () => { - const newState = reducer( - {}, - { - type: EDIT_USER, - user: userZaphod - } - ); + const newState = reducer({}, { + type: EDIT_USER, + user: userZaphod + }); expect(newState.editUser).toEqual(userZaphod); }); });