fetch user again after modification

This commit is contained in:
Maren Süwer
2018-11-05 13:14:52 +01:00
parent bbfea08b8e
commit ff1dc25c5c
3 changed files with 53 additions and 13 deletions

View File

@@ -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<Props> {
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));

View File

@@ -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));
});

View File

@@ -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);
});
});