From 3267f05fdfde83f3bba87d533af793f51b1c1ad0 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 26 Jul 2018 08:32:46 +0200 Subject: [PATCH] removed fetchUsers on modify and introduced callbacks for modify and delete --- scm-ui/src/users/modules/users.js | 11 ++++++-- scm-ui/src/users/modules/users.test.js | 39 ++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index 9cefdaec37..f6c01e7939 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -173,14 +173,16 @@ export function createUserFailure(user: User, err: Error): Action { //modify user -export function modifyUser(user: User) { +export function modifyUser(user: User, callback?: () => void) { return function(dispatch: Dispatch) { dispatch(modifyUserPending(user)); return apiClient .putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER) .then(() => { dispatch(modifyUserSuccess(user)); - dispatch(fetchUsers()); + if (callback) { + callback(); + } }) .catch(err => { dispatch(modifyUserFailure(user, err)); @@ -214,13 +216,16 @@ export function modifyUserFailure(user: User, error: Error): Action { //delete user -export function deleteUser(user: User) { +export function deleteUser(user: User, callback?: () => void) { return function(dispatch: any) { dispatch(deleteUserPending(user)); return apiClient .delete(user._links.delete.href) .then(() => { dispatch(deleteUserSuccess(user)); + if (callback) { + callback(); + } }) .catch(cause => { const error = new Error( diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index 0dcca7e465..4c8a09d66d 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -258,15 +258,29 @@ describe("users fetch()", () => { fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { 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.length).toBe(2); expect(actions[0].type).toEqual(MODIFY_USER_PENDING); expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS); - expect(actions[2].type).toEqual(FETCH_USERS_PENDING); + }); + }); + + it("should call callback, after successful modified user", () => { + fetchMock.putOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { + status: 204 + }); + + let called = false; + const callMe = () => { + called = true; + }; + + const store = mockStore({}); + return store.dispatch(modifyUser(userZaphod, callMe)).then(() => { + expect(called).toBeTruthy(); }); }); @@ -288,18 +302,33 @@ describe("users fetch()", () => { fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { status: 204 }); - // after update, the users are fetched again - fetchMock.getOnce(USERS_URL, response); const store = mockStore({}); return store.dispatch(deleteUser(userZaphod)).then(() => { const actions = store.getActions(); + expect(actions.length).toBe(2); expect(actions[0].type).toEqual(DELETE_USER); expect(actions[0].payload).toBe(userZaphod); expect(actions[1].type).toEqual(DELETE_USER_SUCCESS); }); }); + it("should call the callback, after successful delete", () => { + fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { + status: 204 + }); + + let called = false; + const callMe = () => { + called = true; + }; + + const store = mockStore({}); + return store.dispatch(deleteUser(userZaphod, callMe)).then(() => { + expect(called).toBeTruthy(); + }); + }); + it("should fail to delete user zaphod", () => { fetchMock.deleteOnce("http://localhost:8081/scm/api/rest/v2/users/zaphod", { status: 500