mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-16 10:16:16 +01:00
add reducer, selector and actions for getting one changeset
This commit is contained in:
@@ -8,6 +8,10 @@ import {
|
||||
FETCH_CHANGESETS_FAILURE,
|
||||
FETCH_CHANGESETS_PENDING,
|
||||
FETCH_CHANGESETS_SUCCESS,
|
||||
FETCH_CHANGESET,
|
||||
FETCH_CHANGESET_FAILURE,
|
||||
FETCH_CHANGESET_PENDING,
|
||||
FETCH_CHANGESET_SUCCESS,
|
||||
fetchChangesets,
|
||||
fetchChangesetsByBranchAndPage,
|
||||
fetchChangesetsByNamespaceNameAndBranch,
|
||||
@@ -15,7 +19,14 @@ import {
|
||||
fetchChangesetsSuccess,
|
||||
getChangesets,
|
||||
getFetchChangesetsFailure,
|
||||
isFetchChangesetsPending
|
||||
isFetchChangesetsPending,
|
||||
fetchChangeset,
|
||||
getChangeset,
|
||||
fetchChangesetIfNeeded,
|
||||
shouldFetchChangeset,
|
||||
isFetchChangesetPending,
|
||||
getFetchChangesetFailure,
|
||||
fetchChangesetSuccess
|
||||
} from "./changesets";
|
||||
import reducer from "./changesets";
|
||||
|
||||
@@ -33,6 +44,139 @@ describe("changesets", () => {
|
||||
fetchMock.restore();
|
||||
});
|
||||
|
||||
//********added for detailed view of changesets
|
||||
const changesetId = "aba876c0625d90a6aff1494f3d161aaa7008b958";
|
||||
|
||||
it("should fetch changeset", () => {
|
||||
fetchMock.getOnce(DEFAULT_BRANCH_URL + "/" + changesetId, "{}");
|
||||
|
||||
const expectedActions = [
|
||||
{
|
||||
type: FETCH_CHANGESET_PENDING,
|
||||
payload: {
|
||||
id: changesetId,
|
||||
namespace: "foo",
|
||||
repoName: "bar"
|
||||
},
|
||||
itemId: "foo/bar/" + changesetId
|
||||
},
|
||||
{
|
||||
type: FETCH_CHANGESET_SUCCESS,
|
||||
payload: {
|
||||
changeset: {},
|
||||
id: changesetId,
|
||||
namespace: "foo",
|
||||
repoName: "bar"
|
||||
},
|
||||
itemId: "foo/bar/" + changesetId
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store
|
||||
.dispatch(fetchChangeset("foo", "bar", changesetId))
|
||||
.then(() => {
|
||||
expect(store.getActions()).toEqual(expectedActions);
|
||||
});
|
||||
});
|
||||
|
||||
it("should fail fetching changeset on error", () => {
|
||||
fetchMock.getOnce(DEFAULT_BRANCH_URL + "/" + changesetId, 500);
|
||||
|
||||
const expectedActions = [
|
||||
{
|
||||
type: FETCH_CHANGESET_PENDING,
|
||||
payload: {
|
||||
id: changesetId,
|
||||
namespace: "foo",
|
||||
repoName: "bar"
|
||||
},
|
||||
itemId: "foo/bar/" + changesetId
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store
|
||||
.dispatch(fetchChangeset("foo", "bar", changesetId))
|
||||
.then(() => {
|
||||
expect(store.getActions()[0]).toEqual(expectedActions[0]);
|
||||
expect(store.getActions()[1].type).toEqual(FETCH_CHANGESET_FAILURE);
|
||||
expect(store.getActions()[1].payload).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
it("should fetch changeset if needed", () => {
|
||||
fetchMock.getOnce(DEFAULT_BRANCH_URL + "/" + "id3", "{}");
|
||||
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const expectedActions = [
|
||||
{
|
||||
type: FETCH_CHANGESET_PENDING,
|
||||
payload: {
|
||||
id: "id3",
|
||||
namespace: "foo",
|
||||
repoName: "bar"
|
||||
},
|
||||
itemId: "foo/bar/" + "id3"
|
||||
},
|
||||
{
|
||||
type: FETCH_CHANGESET_SUCCESS,
|
||||
payload: {
|
||||
changeset: {},
|
||||
id: "id3",
|
||||
namespace: "foo",
|
||||
repoName: "bar"
|
||||
},
|
||||
itemId: "foo/bar/" + "id3"
|
||||
}
|
||||
];
|
||||
|
||||
const store = mockStore({});
|
||||
return store
|
||||
.dispatch(fetchChangesetIfNeeded(state, "foo", "bar", "id3"))
|
||||
.then(() => {
|
||||
expect(store.getActions()).toEqual(expectedActions);
|
||||
});
|
||||
});
|
||||
|
||||
it("should not fetch changeset if not needed", () => {
|
||||
fetchMock.getOnce(DEFAULT_BRANCH_URL + "/" + "id1", 500);
|
||||
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const expectedActions = [];
|
||||
|
||||
const store = mockStore({});
|
||||
return expect(
|
||||
store.dispatch(fetchChangesetIfNeeded(state, "foo", "bar", "id1"))
|
||||
).toEqual(undefined);
|
||||
});
|
||||
|
||||
//********end of added for detailed view of changesets
|
||||
|
||||
it("should fetch changesets for default branch", () => {
|
||||
fetchMock.getOnce(DEFAULT_BRANCH_URL, "{}");
|
||||
|
||||
@@ -248,11 +392,172 @@ describe("changesets", () => {
|
||||
expect(newState.byKey["foo/bar"].byId["changeset2"]).toBeDefined();
|
||||
expect(newState.byKey["foo/bar"].byId["changeset1"]).toBeDefined();
|
||||
});
|
||||
|
||||
//********added for detailed view of changesets
|
||||
const responseBodySingleChangeset = {
|
||||
id: "id3",
|
||||
author: {
|
||||
mail: "z@phod.com",
|
||||
name: "zaphod"
|
||||
},
|
||||
date: "2018-09-13T08:46:22Z",
|
||||
description: "added testChangeset",
|
||||
_links: {},
|
||||
_embedded: {
|
||||
tags: [],
|
||||
branches: []
|
||||
}
|
||||
};
|
||||
|
||||
it("should add changeset to state", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
["id2"]: {
|
||||
id: "id2",
|
||||
author: { mail: "mail@author.com", name: "author" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
list: {
|
||||
entry: {
|
||||
page: 1,
|
||||
pageTotal: 10,
|
||||
_links: {}
|
||||
},
|
||||
entries: ["id2"]
|
||||
}
|
||||
},
|
||||
fetchChangesetSuccess(responseBodySingleChangeset, "foo", "bar", "id3")
|
||||
);
|
||||
expect(newState).toBeDefined();
|
||||
expect(newState.byKey["foo/bar"].byId["id3"].description).toEqual(
|
||||
"added testChangeset"
|
||||
);
|
||||
expect(newState.byKey["foo/bar"].byId["id3"].author.mail).toEqual(
|
||||
"z@phod.com"
|
||||
);
|
||||
expect(newState.byKey["foo/bar"].byId["id2"]).toBeDefined();
|
||||
expect(newState.byKey["foo/bar"].byId["id3"]).toBeDefined();
|
||||
expect(newState.list).toEqual({
|
||||
entry: {
|
||||
page: 1,
|
||||
pageTotal: 10,
|
||||
_links: {}
|
||||
},
|
||||
entries: ["id2", "id3"]
|
||||
});
|
||||
});
|
||||
//********end of added for detailed view of changesets
|
||||
});
|
||||
|
||||
describe("changeset selectors", () => {
|
||||
const error = new Error("Something went wrong");
|
||||
|
||||
//********added for detailed view of changesets
|
||||
|
||||
it("should return changeset", () => {
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const result = getChangeset(state, "foo", "bar", "id1");
|
||||
expect(result).toEqual({ id: "id1" });
|
||||
});
|
||||
|
||||
it("should return null if changeset does not exist", () => {
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const result = getChangeset(state, "foo", "bar", "id3");
|
||||
expect(result).toEqual(null);
|
||||
});
|
||||
|
||||
it("should return true if changeset does not exist", () => {
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const result = shouldFetchChangeset(state, "foo", "bar", "id3");
|
||||
expect(result).toEqual(true);
|
||||
});
|
||||
|
||||
it("should return false if changeset exists", () => {
|
||||
const state = {
|
||||
changesets: {
|
||||
byKey: {
|
||||
"foo/bar": {
|
||||
byId: {
|
||||
id1: { id: "id1" },
|
||||
id2: { id: "id2" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const result = shouldFetchChangeset(state, "foo", "bar", "id2");
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
|
||||
it("should return true, when fetching changeset is pending", () => {
|
||||
const state = {
|
||||
pending: {
|
||||
[FETCH_CHANGESET + "/foo/bar/id1"]: true
|
||||
}
|
||||
};
|
||||
|
||||
expect(isFetchChangesetPending(state, "foo", "bar", "id1")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should return false, when fetching changeset is not pending", () => {
|
||||
expect(isFetchChangesetPending({}, "foo", "bar", "id1")).toEqual(false);
|
||||
});
|
||||
|
||||
it("should return error if fetching changeset failed", () => {
|
||||
const state = {
|
||||
failure: {
|
||||
[FETCH_CHANGESET + "/foo/bar/id1"]: error
|
||||
}
|
||||
};
|
||||
|
||||
expect(getFetchChangesetFailure(state, "foo", "bar", "id1")).toEqual(
|
||||
error
|
||||
);
|
||||
});
|
||||
|
||||
it("should return false if fetching changeset did not fail", () => {
|
||||
expect(getFetchChangesetFailure({}, "foo", "bar", "id1")).toBeUndefined();
|
||||
});
|
||||
//********end of added for detailed view of changesets
|
||||
|
||||
it("should get all changesets for a given namespace and name", () => {
|
||||
const state = {
|
||||
changesets: {
|
||||
|
||||
Reference in New Issue
Block a user