From ff1dc25c5c4554b72cf8ce1d6977485d58cf66a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maren=20S=C3=BCwer?= Date: Mon, 5 Nov 2018 13:14:52 +0100 Subject: [PATCH] fetch user again after modification --- scm-ui/src/users/containers/SingleUser.js | 10 +++--- scm-ui/src/users/modules/users.js | 15 +++++++-- scm-ui/src/users/modules/users.test.js | 41 +++++++++++++++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/scm-ui/src/users/containers/SingleUser.js b/scm-ui/src/users/containers/SingleUser.js index f581874742..ed5d6df0c3 100644 --- a/scm-ui/src/users/containers/SingleUser.js +++ b/scm-ui/src/users/containers/SingleUser.js @@ -15,7 +15,7 @@ import EditUser from "./EditUser"; import type { User } from "@scm-manager/ui-types"; import type { History } from "history"; import { - fetchUser, + fetchUserByName, deleteUser, getUserByName, isFetchUserPending, @@ -37,7 +37,7 @@ type Props = { // dispatcher functions deleteUser: (user: User, callback?: () => void) => void, - fetchUser: (string, string) => void, + fetchUserByName: (string, string) => void, // context objects t: string => string, @@ -47,7 +47,7 @@ type Props = { class SingleUser extends React.Component { componentDidMount() { - this.props.fetchUser(this.props.usersLink, this.props.name); + this.props.fetchUserByName(this.props.usersLink, this.props.name); } userDeleted = () => { @@ -138,8 +138,8 @@ const mapStateToProps = (state, ownProps) => { const mapDispatchToProps = dispatch => { return { - fetchUser: (link: string, name: string) => { - dispatch(fetchUser(link, name)); + fetchUserByName: (link: string, name: string) => { + dispatch(fetchUserByName(link, name)); }, deleteUser: (user: User, callback?: () => void) => { dispatch(deleteUser(user, callback)); diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index 80d8107b3e..3e9420d485 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -87,12 +87,20 @@ export function fetchUsersFailure(url: string, error: Error): Action { } //fetch user -export function fetchUser(link: string, name: string) { +export function fetchUserByName(link: string, name: string) { const userUrl = link.endsWith("/") ? link + name : link + "/" + name; + return fetchUser(userUrl, name); +} + +export function fetchUserByLink(user: User) { + return fetchUser(user._links.self.href, user.name); +} + +function fetchUser(link: string, name: string) { return function(dispatch: any) { dispatch(fetchUserPending(name)); return apiClient - .get(userUrl) + .get(link) .then(response => { return response.json(); }) @@ -195,6 +203,9 @@ export function modifyUser(user: User, callback?: () => void) { callback(); } }) + .then(() => { + dispatch(fetchUserByLink(user)); + }) .catch(err => { dispatch(modifyUserFailure(user, err)); }); diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index 03da4658c2..9803e5af52 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -20,7 +20,8 @@ import reducer, { FETCH_USERS_FAILURE, FETCH_USERS_PENDING, FETCH_USERS_SUCCESS, - fetchUser, + fetchUserByLink, + fetchUserByName, fetchUserSuccess, getFetchUserFailure, fetchUsers, @@ -166,11 +167,37 @@ describe("users fetch()", () => { }); }); - it("should sucessfully fetch single user", () => { + it("should sucessfully fetch single user by name", () => { fetchMock.getOnce(USERS_URL + "/zaphod", userZaphod); const store = mockStore({}); - return store.dispatch(fetchUser(URL, "zaphod")).then(() => { + return store.dispatch(fetchUserByName(URL, "zaphod")).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(FETCH_USER_PENDING); + expect(actions[1].type).toEqual(FETCH_USER_SUCCESS); + expect(actions[1].payload).toBeDefined(); + }); + }); + + it("should fail fetching single user by name on HTTP 500", () => { + fetchMock.getOnce(USERS_URL + "/zaphod", { + status: 500 + }); + + const store = mockStore({}); + return store.dispatch(fetchUserByName(URL, "zaphod")).then(() => { + const actions = store.getActions(); + expect(actions[0].type).toEqual(FETCH_USER_PENDING); + expect(actions[1].type).toEqual(FETCH_USER_FAILURE); + expect(actions[1].payload).toBeDefined(); + }); + }); + + it("should sucessfully fetch single user", () => { + fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", userZaphod); + + const store = mockStore({}); + return store.dispatch(fetchUserByLink(userZaphod)).then(() => { const actions = store.getActions(); expect(actions[0].type).toEqual(FETCH_USER_PENDING); expect(actions[1].type).toEqual(FETCH_USER_SUCCESS); @@ -179,12 +206,12 @@ describe("users fetch()", () => { }); it("should fail fetching single user on HTTP 500", () => { - fetchMock.getOnce(USERS_URL + "/zaphod", { + fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", { status: 500 }); const store = mockStore({}); - return store.dispatch(fetchUser(URL, "zaphod")).then(() => { + return store.dispatch(fetchUserByLink(userZaphod)).then(() => { const actions = store.getActions(); expect(actions[0].type).toEqual(FETCH_USER_PENDING); expect(actions[1].type).toEqual(FETCH_USER_FAILURE); @@ -245,13 +272,15 @@ describe("users fetch()", () => { fetchMock.putOnce("http://localhost:8081/api/v2/users/zaphod", { status: 204 }); + fetchMock.getOnce("http://localhost:8081/api/v2/users/zaphod", userZaphod); const store = mockStore({}); return store.dispatch(modifyUser(userZaphod)).then(() => { const actions = store.getActions(); - expect(actions.length).toBe(2); + expect(actions.length).toBe(3); expect(actions[0].type).toEqual(MODIFY_USER_PENDING); expect(actions[1].type).toEqual(MODIFY_USER_SUCCESS); + expect(actions[2].type).toEqual(FETCH_USER_PENDING); }); });