fetch repo after modifying it

This commit is contained in:
Maren Süwer
2018-11-05 13:52:46 +01:00
parent c4d57b1ce8
commit bb92041e09
3 changed files with 86 additions and 15 deletions

View File

@@ -2,7 +2,7 @@
import React from "react";
import {
deleteRepo,
fetchRepo,
fetchRepoByName,
getFetchRepoFailure,
getRepository,
isFetchRepoPending
@@ -45,7 +45,7 @@ type Props = {
repoLink: string,
// dispatch functions
fetchRepo: (link: string, namespace: string, name: string) => void,
fetchRepoByName: (link: string, namespace: string, name: string) => void,
deleteRepo: (repository: Repository, () => void) => void,
// context props
@@ -56,9 +56,9 @@ type Props = {
class RepositoryRoot extends React.Component<Props> {
componentDidMount() {
const { fetchRepo, namespace, name, repoLink } = this.props;
const { fetchRepoByName, namespace, name, repoLink } = this.props;
fetchRepo(repoLink, namespace, name);
fetchRepoByName(repoLink, namespace, name);
}
stripEndingSlash = (url: string) => {
@@ -227,8 +227,8 @@ const mapStateToProps = (state, ownProps) => {
const mapDispatchToProps = dispatch => {
return {
fetchRepo: (link: string, namespace: string, name: string) => {
dispatch(fetchRepo(link, namespace, name));
fetchRepoByName: (link: string, namespace: string, name: string) => {
dispatch(fetchRepoByName(link, namespace, name));
},
deleteRepo: (repository: Repository, callback: () => void) => {
dispatch(deleteRepo(repository, callback));

View File

@@ -99,13 +99,20 @@ export function fetchReposFailure(err: Error): Action {
}
// fetch repo
export function fetchRepoByLink(repo: Repository) {
return fetchRepo(repo._links.self.href, repo.namespace, repo.name);
}
export function fetchRepo(link: string, namespace: string, name: string) {
export function fetchRepoByName(link: string, namespace: string, name: string) {
const repoUrl = link.endsWith("/") ? link : link + "/";
return fetchRepo(`${repoUrl}${namespace}/${name}`, namespace, name);
}
function fetchRepo(link: string, namespace: string, name: string) {
return function(dispatch: any) {
dispatch(fetchRepoPending(namespace, name));
return apiClient
.get(`${repoUrl}${namespace}/${name}`)
.get(link)
.then(response => response.json())
.then(repository => {
dispatch(fetchRepoSuccess(repository));
@@ -213,6 +220,9 @@ export function modifyRepo(repository: Repository, callback?: () => void) {
callback();
}
})
.then(() => {
dispatch(fetchRepoByLink(repository));
})
.catch(cause => {
const error = new Error(`failed to modify repo: ${cause.message}`);
dispatch(modifyRepoFailure(repository, error));
@@ -347,8 +357,6 @@ export default function reducer(
switch (action.type) {
case FETCH_REPOS_SUCCESS:
return normalizeByNamespaceAndName(action.payload);
case MODIFY_REPO_SUCCESS:
return reducerByNames(state, action.payload);
case FETCH_REPO_SUCCESS:
return reducerByNames(state, action.payload);
default:

View File

@@ -15,7 +15,8 @@ import reducer, {
fetchReposByLink,
fetchReposByPage,
FETCH_REPO,
fetchRepo,
fetchRepoByLink,
fetchRepoByName,
FETCH_REPO_PENDING,
FETCH_REPO_SUCCESS,
FETCH_REPO_FAILURE,
@@ -323,7 +324,7 @@ describe("repos fetch", () => {
});
});
it("should successfully fetch repo slarti/fjords", () => {
it("should successfully fetch repo slarti/fjords by name", () => {
fetchMock.getOnce(REPOS_URL + "/slarti/fjords", slartiFjords);
const expectedActions = [
@@ -343,18 +344,66 @@ describe("repos fetch", () => {
];
const store = mockStore({});
return store.dispatch(fetchRepo(URL, "slarti", "fjords")).then(() => {
return store.dispatch(fetchRepoByName(URL, "slarti", "fjords")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should dispatch FETCH_REPO_FAILURE, it the request for slarti/fjords fails", () => {
it("should dispatch FETCH_REPO_FAILURE, if the request for slarti/fjords by name fails", () => {
fetchMock.getOnce(REPOS_URL + "/slarti/fjords", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchRepo(URL, "slarti", "fjords")).then(() => {
return store.dispatch(fetchRepoByName(URL, "slarti", "fjords")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_REPO_PENDING);
expect(actions[1].type).toEqual(FETCH_REPO_FAILURE);
expect(actions[1].payload.namespace).toBe("slarti");
expect(actions[1].payload.name).toBe("fjords");
expect(actions[1].payload.error).toBeDefined();
expect(actions[1].itemId).toBe("slarti/fjords");
});
});
it("should successfully fetch repo slarti/fjords", () => {
fetchMock.getOnce(
"http://localhost:8081/api/v2/repositories/slarti/fjords",
slartiFjords
);
const expectedActions = [
{
type: FETCH_REPO_PENDING,
payload: {
namespace: "slarti",
name: "fjords"
},
itemId: "slarti/fjords"
},
{
type: FETCH_REPO_SUCCESS,
payload: slartiFjords,
itemId: "slarti/fjords"
}
];
const store = mockStore({});
return store.dispatch(fetchRepoByLink(slartiFjords)).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should dispatch FETCH_REPO_FAILURE, it the request for slarti/fjords fails", () => {
fetchMock.getOnce(
"http://localhost:8081/api/v2/repositories/slarti/fjords",
{
status: 500
}
);
const store = mockStore({});
return store.dispatch(fetchRepoByLink(slartiFjords)).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_REPO_PENDING);
expect(actions[1].type).toEqual(FETCH_REPO_FAILURE);
@@ -485,6 +534,12 @@ describe("repos fetch", () => {
fetchMock.putOnce(slartiFjords._links.update.href, {
status: 204
});
fetchMock.getOnce(
"http://localhost:8081/api/v2/repositories/slarti/fjords",
{
status: 500
}
);
let editedFjords = { ...slartiFjords };
editedFjords.description = "coast of africa";
@@ -495,6 +550,7 @@ describe("repos fetch", () => {
const actions = store.getActions();
expect(actions[0].type).toEqual(MODIFY_REPO_PENDING);
expect(actions[1].type).toEqual(MODIFY_REPO_SUCCESS);
expect(actions[2].type).toEqual(FETCH_REPO_PENDING);
});
});
@@ -502,6 +558,12 @@ describe("repos fetch", () => {
fetchMock.putOnce(slartiFjords._links.update.href, {
status: 204
});
fetchMock.getOnce(
"http://localhost:8081/api/v2/repositories/slarti/fjords",
{
status: 500
}
);
let editedFjords = { ...slartiFjords };
editedFjords.description = "coast of africa";
@@ -517,6 +579,7 @@ describe("repos fetch", () => {
const actions = store.getActions();
expect(actions[0].type).toEqual(MODIFY_REPO_PENDING);
expect(actions[1].type).toEqual(MODIFY_REPO_SUCCESS);
expect(actions[2].type).toEqual(FETCH_REPO_PENDING);
expect(called).toBe(true);
});
});