fixed get branch, added tests

This commit is contained in:
Florian Scholdei
2019-04-01 13:12:34 +02:00
parent bb5e3c9c58
commit eaf3641d6f
3 changed files with 72 additions and 24 deletions

View File

@@ -8,6 +8,7 @@ import { translate } from "react-i18next";
import { withRouter } from "react-router-dom";
import {
fetchBranchByName,
getBranchByName,
getFetchBranchFailure,
isFetchBranchPending
} from "../../modules/branches";
@@ -70,11 +71,14 @@ class BranchView extends React.Component<Props> {
const mapStateToProps = (state, ownProps) => {
const { repository } = ownProps;
const branchName = decodeURIComponent(ownProps.match.params.branch);
const loading = isFetchBranchPending(state, repository, branchName);
const error = getFetchBranchFailure(state, repository, branchName);
const branch = getBranchByName(state, branchName);
console.log(branchName + " Branch:",branch);
const loading = isFetchBranchPending(state, branchName);
const error = getFetchBranchFailure(state, branchName);
return {
repository,
branchName,
branch,
loading,
error
};

View File

@@ -34,6 +34,14 @@ export function fetchBranchPending(name: string): Action {
};
}
export function fetchBranchSuccess(branch: Branch): Action {
return {
type: FETCH_BRANCH_SUCCESS,
payload: branch,
itemId: branch.name
};
}
export function fetchBranchFailure(name: string, error: Error): Action {
return {
type: FETCH_BRANCH_FAILURE,
@@ -59,32 +67,19 @@ export function fetchBranch(link: string, name: string) {
};
}
export function getFetchBranchFailure(
state: Object,
repository: string,
branchName: string
) {
return getFailure(
state,
FETCH_BRANCH,
repository + "/branches/" + branchName
);
export function getBranchByName(state: Object, name: string) {
console.log("State:", state);
if (state.branches) {
return state.branches[name];
}
}
export function isFetchBranchPending(
state: Object,
repository: string,
branchName: string
) {
return isPending(state, FETCH_BRANCH, repository + "/branches/" + branchName);
export function isFetchBranchPending(state: Object, name: string) {
return isPending(state, FETCH_BRANCH, name);
}
export function fetchBranchSuccess(branch: Branch): Action {
return {
type: FETCH_BRANCH_SUCCESS,
payload: branch,
itemId: branch.name
};
export function getFetchBranchFailure(state: Object, name: string) {
return getFailure(state, FETCH_BRANCH, name);
}
export function fetchBranches(repository: Repository) {
@@ -154,6 +149,12 @@ export default function reducer(
...state,
[key]: extractBranchesFromPayload(payload.data)
};
case FETCH_BRANCH_SUCCESS:
return {
...state,
[action.payload.name]: action.payload
};
default:
return state;
}

View File

@@ -6,7 +6,14 @@ import reducer, {
FETCH_BRANCHES_FAILURE,
FETCH_BRANCHES_PENDING,
FETCH_BRANCHES_SUCCESS,
FETCH_BRANCH,
FETCH_BRANCH_PENDING,
FETCH_BRANCH_SUCCESS,
FETCH_BRANCH_FAILURE,
fetchBranches,
fetchBranchByName,
fetchBranchSuccess,
fetchBranch,
getBranch,
getBranches,
getFetchBranchesFailure,
@@ -88,6 +95,32 @@ describe("branches", () => {
expect(store.getActions()[1].type).toEqual(FETCH_BRANCHES_FAILURE);
});
});
it("should successfully fetch single branch", () => {
fetchMock.getOnce(URL + "/branch1", branch1);
const store = mockStore({});
return store.dispatch(fetchBranchByName(URL, "branch1")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_BRANCH_PENDING);
expect(actions[1].type).toEqual(FETCH_BRANCH_SUCCESS);
expect(actions[1].payload).toBeDefined();
});
});
it("should fail fetching single branch on HTTP 500", () => {
fetchMock.getOnce(URL + "/branch2", {
status: 500
});
const store = mockStore({});
return store.dispatch(fetchBranchByName(URL, "branch2")).then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(FETCH_BRANCH_PENDING);
expect(actions[1].type).toEqual(FETCH_BRANCH_FAILURE);
expect(actions[1].payload).toBeDefined();
});
});
});
describe("branches reducer", () => {
@@ -123,6 +156,16 @@ describe("branches", () => {
expect(newState["hitchhiker/heartOfGold"]).toContain(branch3);
});
it("should update state according to FETCH_BRANCH_SUCCESS action", () => {
const newState = reducer({}, fetchBranchSuccess(branch3));
expect(newState["branch3"]).toBe(branch3);
});
it("should update state according to FETCH_BRANCH_SUCCESS action", () => {
const newState = reducer({}, fetchBranch(URL + "/branch1", "branch1"));
expect(newState["branch1"]).toBe(branch1);
});
});
describe("branch selectors", () => {