diff --git a/scm-ui/src/repos/modules/changesets.js b/scm-ui/src/repos/modules/changesets.js index 067f48391c..1ac83aba0a 100644 --- a/scm-ui/src/repos/modules/changesets.js +++ b/scm-ui/src/repos/modules/changesets.js @@ -129,15 +129,20 @@ export default function reducer( return state; } - let oldChangesets = { [key]: {} }; - if (state[key]) { - oldChangesets[key] = state[key]; + let oldByIds = {}; + if (state[key] && state[key].byId) { + oldByIds = state[key].byId; } + const byIds = extractChangesetsByIds(changesets); + return { ...state, [key]: { - byId: byIds, + byId: { + ...oldByIds, + ...byIds + }, list: { entries: changesetIds, entry: { @@ -170,10 +175,14 @@ export function getChangesets( branch?: Branch ) { const key = createItemId(repository, branch); - if (!state.changesets[key]) { + + const changesets = state.changesets[key]; + if (!changesets) { return null; } - return Object.values(state.changesets[key].byId); + return changesets.list.entries.map((id: string) => { + return changesets.byId[id]; + }); } export function isFetchChangesetsPending( diff --git a/scm-ui/src/repos/modules/changesets.test.js b/scm-ui/src/repos/modules/changesets.test.js index 6d88315b04..3b0410b635 100644 --- a/scm-ui/src/repos/modules/changesets.test.js +++ b/scm-ui/src/repos/modules/changesets.test.js @@ -224,6 +224,35 @@ describe("changesets", () => { entries: ["changeset1", "changeset2", "changeset3"] }); }); + + it("should not remove existing changesets", () => { + const state = { + "foo/bar": { + byId: { + id2: { id: "id2" }, + id1: { id: "id1" } + }, + list: { + entries: ["id1", "id2"] + } + } + }; + + const newState = reducer( + state, + fetchChangesetsSuccess(repository, undefined, responseBody) + ); + + const fooBar = newState["foo/bar"]; + + expect(fooBar.list.entries).toEqual([ + "changeset1", + "changeset2", + "changeset3" + ]); + expect(fooBar.byId["id2"]).toEqual({ id: "id2" }); + expect(fooBar.byId["id1"]).toEqual({ id: "id1" }); + }); }); describe("changeset selectors", () => { @@ -234,14 +263,17 @@ describe("changesets", () => { changesets: { "foo/bar": { byId: { - id1: { id: "id1" }, - id2: { id: "id2" } + id2: { id: "id2" }, + id1: { id: "id1" } + }, + list: { + entries: ["id1", "id2"] } } } }; const result = getChangesets(state, repository); - expect(result).toContainEqual({ id: "id1" }); + expect(result).toEqual([{ id: "id1" }, { id: "id2" }]); }); it("should return true, when fetching changesets is pending", () => {