diff --git a/scm-ui/src/repos/modules/branches.js b/scm-ui/src/repos/modules/branches.js new file mode 100644 index 0000000000..dde5cb379b --- /dev/null +++ b/scm-ui/src/repos/modules/branches.js @@ -0,0 +1,53 @@ +import {FAILURE_SUFFIX, PENDING_SUFFIX, SUCCESS_SUFFIX} from "../../modules/types"; +import {apiClient} from "@scm-manager/ui-components"; + +export const FETCH_BRANCHES = "scm/repos/FETCH_BRANCHES"; +export const FETCH_BRANCHES_PENDING = `${FETCH_BRANCHES}_${PENDING_SUFFIX}`; +export const FETCH_BRANCHES_SUCCESS = `${FETCH_BRANCHES}_${SUCCESS_SUFFIX}`; +export const FETCH_BRANCHES_FAILURE = `${FETCH_BRANCHES}_${FAILURE_SUFFIX}`; + +const REPO_URL = "repositories"; + +// Fetching branches +export function fetchBranchesByNamespaceAndName(namespace: string, name: string) { + return function (dispatch: any) { + dispatch(fetchBranchesPending(namespace, name)); + return apiClient.get(REPO_URL + "/" + namespace + "/" + name + "/branches") + .then(response => response.json()) + .then(data => { + dispatch(fetchBranchesSuccess(data, namespace, name)) + }) + .catch(cause => { + dispatch(fetchBranchesFailure(namespace, name, cause)) + }) + } +} + +// Action creators +export function fetchBranchesPending(namespace: string, name: string) { + return { + type: FETCH_BRANCHES_PENDING, + payload: {namespace, name}, + itemId: namespace + "/" + name + } +} + +export function fetchBranchesSuccess(data: string, namespace: string, name: string) { + return { + type: FETCH_BRANCHES_SUCCESS, + payload: {data, namespace, name}, + itemId: namespace + "/" + name + } +} + +export function fetchBranchesFailure(namespace: string, name: string, error: Error) { + return { + type: FETCH_BRANCHES_FAILURE, + payload: {error, namespace, name}, + itemId: namespace + "/" + name + } +} + +// Reducers + +// Selectors diff --git a/scm-ui/src/repos/modules/branches.test.js b/scm-ui/src/repos/modules/branches.test.js new file mode 100644 index 0000000000..67c5950c5a --- /dev/null +++ b/scm-ui/src/repos/modules/branches.test.js @@ -0,0 +1,63 @@ +import configureMockStore from "redux-mock-store"; +import thunk from "redux-thunk"; +import fetchMock from "fetch-mock"; +import { + FETCH_BRANCHES_FAILURE, + FETCH_BRANCHES_PENDING, + FETCH_BRANCHES_SUCCESS, + fetchBranchesByNamespaceAndName +} from "./branches"; + +describe("fetch branches", () => { + const URL = "/api/rest/v2/repositories/foo/bar/branches"; + const mockStore = configureMockStore([thunk]); + + afterEach(() => { + fetchMock.reset(); + fetchMock.restore(); + }); + + + it("should fetch branches", () => { + const collection = {}; + + fetchMock.getOnce(URL, "{}"); + + const expectedActions = [ + {type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"}, + itemId: "foo/bar"}, + { + type: FETCH_BRANCHES_SUCCESS, + payload: {data: collection, namespace: "foo", name: "bar"}, + itemId: "foo/bar" + } + ]; + + const store = mockStore({}); + return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => { + expect(store.getActions()).toEqual(expectedActions); + }); + }); + + it("should fail fetching branches on HTTP 500", () => { + const collection = {}; + + fetchMock.getOnce(URL, 500); + + const expectedActions = [ + {type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"}, + itemId: "foo/bar"}, + { + type: FETCH_BRANCHES_FAILURE, + payload: {error: collection, namespace: "foo", name: "bar"}, + itemId: "foo/bar" + } + ]; + + const store = mockStore({}); + return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => { + expect(store.getActions()[0]).toEqual(expectedActions[0]); + expect(store.getActions()[1].type).toEqual(FETCH_BRANCHES_FAILURE); + }); + }) +});