From eaf3641d6f6cb64e7775c354808c638661aa2a90 Mon Sep 17 00:00:00 2001 From: Florian Scholdei Date: Mon, 1 Apr 2019 13:12:34 +0200 Subject: [PATCH] fixed get branch, added tests --- .../repos/branches/containers/BranchView.js | 8 +++- scm-ui/src/repos/modules/branches.js | 45 ++++++++++--------- scm-ui/src/repos/modules/branches.test.js | 43 ++++++++++++++++++ 3 files changed, 72 insertions(+), 24 deletions(-) diff --git a/scm-ui/src/repos/branches/containers/BranchView.js b/scm-ui/src/repos/branches/containers/BranchView.js index 3fe2985fb5..fd65f6eab0 100644 --- a/scm-ui/src/repos/branches/containers/BranchView.js +++ b/scm-ui/src/repos/branches/containers/BranchView.js @@ -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 { 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 }; diff --git a/scm-ui/src/repos/modules/branches.js b/scm-ui/src/repos/modules/branches.js index 6020ec54de..15dfb9c714 100644 --- a/scm-ui/src/repos/modules/branches.js +++ b/scm-ui/src/repos/modules/branches.js @@ -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; } diff --git a/scm-ui/src/repos/modules/branches.test.js b/scm-ui/src/repos/modules/branches.test.js index bcbae28611..1f33992b65 100644 --- a/scm-ui/src/repos/modules/branches.test.js +++ b/scm-ui/src/repos/modules/branches.test.js @@ -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", () => {