mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
Added possibility to fetch changesets by branches
This commit is contained in:
@@ -50,4 +50,45 @@ export function fetchBranchesFailure(namespace: string, name: string, error: Err
|
||||
|
||||
// Reducers
|
||||
|
||||
export default function reducer(state: Object = {}, action: Action = {type: "UNKNOWN"}): Object {
|
||||
switch (action.type) {
|
||||
case FETCH_BRANCHES_SUCCESS:
|
||||
const key = action.payload.namespace + "/" + action.payload.name;
|
||||
let oldBranchesByNames = {[key]: {}};
|
||||
if (state[key] !== undefined) {
|
||||
oldBranchesByNames[key] = state[key]
|
||||
}
|
||||
return {
|
||||
[key]: {
|
||||
byNames: extractBranchesByNames(action.payload.data, oldBranchesByNames[key].byNames)
|
||||
}
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function extractBranchesByNames(data: any, oldBranchesByNames: any): Branch[] {
|
||||
const branches = data._embedded.branches;
|
||||
const branchesByNames = {};
|
||||
|
||||
for (let branch of branches) {
|
||||
branchesByNames[branch.name] = branch;
|
||||
}
|
||||
|
||||
for (let name in oldBranchesByNames) {
|
||||
branchesByNames[name] = oldBranchesByNames[name]
|
||||
}
|
||||
return branchesByNames;
|
||||
}
|
||||
|
||||
// Selectors
|
||||
|
||||
export function getBranchesForNamespaceAndNameFromState(namespace: string, name: string, state: Object) {
|
||||
const key = namespace + "/" + name;
|
||||
if (!state.branches[key]) {
|
||||
return null;
|
||||
}
|
||||
return Object.values(state.branches[key].byNames);
|
||||
}
|
||||
|
||||
@@ -5,13 +5,25 @@ import {
|
||||
FETCH_BRANCHES_FAILURE,
|
||||
FETCH_BRANCHES_PENDING,
|
||||
FETCH_BRANCHES_SUCCESS,
|
||||
fetchBranchesByNamespaceAndName
|
||||
fetchBranchesByNamespaceAndName, getBranchesForNamespaceAndNameFromState
|
||||
} from "./branches";
|
||||
import reducer from "./branches";
|
||||
|
||||
|
||||
const namespace = "foo";
|
||||
const name = "bar";
|
||||
const key = namespace + "/" + name;
|
||||
|
||||
const branch1 = {name: "branch1", revision: "revision1"};
|
||||
const branch2 = {name: "branch2", revision: "revision2"};
|
||||
const branch3 = {name: "branch3", revision: "revision3"};
|
||||
|
||||
|
||||
describe("fetch branches", () => {
|
||||
const URL = "/api/rest/v2/repositories/foo/bar/branches";
|
||||
const mockStore = configureMockStore([thunk]);
|
||||
|
||||
|
||||
afterEach(() => {
|
||||
fetchMock.reset();
|
||||
fetchMock.restore();
|
||||
@@ -24,17 +36,19 @@ describe("fetch branches", () => {
|
||||
fetchMock.getOnce(URL, "{}");
|
||||
|
||||
const expectedActions = [
|
||||
{type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"},
|
||||
itemId: "foo/bar"},
|
||||
{
|
||||
type: FETCH_BRANCHES_PENDING, payload: {namespace, name},
|
||||
itemId: key
|
||||
},
|
||||
{
|
||||
type: FETCH_BRANCHES_SUCCESS,
|
||||
payload: {data: collection, namespace: "foo", name: "bar"},
|
||||
itemId: "foo/bar"
|
||||
payload: {data: collection, namespace, name},
|
||||
itemId: key
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => {
|
||||
return store.dispatch(fetchBranchesByNamespaceAndName(namespace, name)).then(() => {
|
||||
expect(store.getActions()).toEqual(expectedActions);
|
||||
});
|
||||
});
|
||||
@@ -45,19 +59,75 @@ describe("fetch branches", () => {
|
||||
fetchMock.getOnce(URL, 500);
|
||||
|
||||
const expectedActions = [
|
||||
{type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"},
|
||||
itemId: "foo/bar"},
|
||||
{
|
||||
type: FETCH_BRANCHES_PENDING, payload: {namespace, name},
|
||||
itemId: key
|
||||
},
|
||||
{
|
||||
type: FETCH_BRANCHES_FAILURE,
|
||||
payload: {error: collection, namespace: "foo", name: "bar"},
|
||||
itemId: "foo/bar"
|
||||
payload: {error: collection, namespace, name},
|
||||
itemId: key
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => {
|
||||
return store.dispatch(fetchBranchesByNamespaceAndName(namespace, name)).then(() => {
|
||||
expect(store.getActions()[0]).toEqual(expectedActions[0]);
|
||||
expect(store.getActions()[1].type).toEqual(FETCH_BRANCHES_FAILURE);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
describe("branches reducer", () => {
|
||||
|
||||
const branches = {
|
||||
_embedded: {
|
||||
branches: [branch1, branch2]
|
||||
}
|
||||
};
|
||||
const action = {
|
||||
type: FETCH_BRANCHES_SUCCESS,
|
||||
payload: {
|
||||
namespace,
|
||||
name,
|
||||
data: branches
|
||||
}
|
||||
};
|
||||
|
||||
it("should update state according to successful fetch", () => {
|
||||
const newState = reducer({}, action);
|
||||
expect(newState).toBeDefined();
|
||||
expect(newState[key]).toBeDefined();
|
||||
expect(newState[key].byNames["branch1"]).toEqual(branch1);
|
||||
expect(newState[key].byNames["branch2"]).toEqual(branch2);
|
||||
});
|
||||
|
||||
it("should not delete existing branches from state", () => {
|
||||
const oldState = {
|
||||
"foo/bar": { byNames: {
|
||||
"branch3": branch3
|
||||
}}
|
||||
};
|
||||
|
||||
const newState = reducer(oldState, action);
|
||||
console.log(newState);
|
||||
expect(newState[key].byNames["branch1"]).toEqual(branch1);
|
||||
expect(newState[key].byNames["branch2"]).toEqual(branch2);
|
||||
expect(newState[key].byNames["branch3"]).toEqual(branch3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("branch selectors", () => {
|
||||
it("should get branches for namespace and name", () => {
|
||||
const state = {
|
||||
branches: {
|
||||
[key]: {
|
||||
byNames: {
|
||||
"branch1": branch1
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
getBranchesForNamespaceAndNameFromState(namespace, name, state);
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user