From 990dc1eb2c08e96267d84a71efe37209e1f9eca2 Mon Sep 17 00:00:00 2001 From: guerler Date: Thu, 22 Sep 2022 05:18:40 -0400 Subject: [PATCH] Add history store client test case --- client/src/store/historyStore/historyStore.js | 2 +- .../store/historyStore/historyStore.test.js | 40 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/client/src/store/historyStore/historyStore.js b/client/src/store/historyStore/historyStore.js index 3b80e280d331..390b86832995 100644 --- a/client/src/store/historyStore/historyStore.js +++ b/client/src/store/historyStore/historyStore.js @@ -36,7 +36,7 @@ const mutations = { // histories, so we ensure that already available details are not getting lost. const enrichedHistories = newHistories.map((history) => { const historyState = state.histories[history.id] || {}; - return Object.assign({}, history, historyState); + return Object.assign({}, historyState, history); }); // Histories are provided as list but stored as map. const newMap = enrichedHistories.reduce((acc, h) => ({ ...acc, [h.id]: h }), {}); diff --git a/client/src/store/historyStore/historyStore.test.js b/client/src/store/historyStore/historyStore.test.js index 8afcbaed0f5a..55a91748fb56 100644 --- a/client/src/store/historyStore/historyStore.test.js +++ b/client/src/store/historyStore/historyStore.test.js @@ -7,9 +7,29 @@ let nHistories = 3; let currentHistory = 0; const histories = {}; for (let i = 0; i < nHistories; i++) { - histories[i] = { id: i }; + histories[i] = { id: i, name: `default` }; } +// add secondary sets of histories +const buildDetails = (key, count = 0) => { + const historyList = []; + for (let i = 0; i < nHistories + count; i++) { + const element = { id: i, name: `name_${key}` }; + element[key] = true; + historyList.push(element); + } + return historyList; +}; + +// detail counter +const countDetails = (histories, key, value = true) => { + let n = 0; + histories.forEach((h) => { + h[key] == value && n++; + }); + return n; +}; + describe("historyStore", () => { let axiosMock; @@ -37,8 +57,24 @@ describe("historyStore", () => { axiosMock.restore(); }); - it("store initialization", async () => { + it("history lists", async () => { expect(store.getters["history/currentHistoryId"]).toBe(null); + let hlist = store.getters["history/histories"]; + expect(hlist.length).toBe(0); + await store.commit("history/setHistories", buildDetails("a", 5)); + hlist = store.getters["history/histories"]; + expect(countDetails(hlist, "a")).toBe(8); + expect(countDetails(hlist, "b")).toBe(0); + expect(countDetails(hlist, "name", "name_a")).toBe(8); + await store.commit("history/setHistories", buildDetails("b")); + hlist = store.getters["history/histories"]; + expect(countDetails(hlist, "a")).toBe(8); + expect(countDetails(hlist, "b")).toBe(3); + expect(countDetails(hlist, "name", "name_a")).toBe(5); + expect(countDetails(hlist, "name", "name_b")).toBe(3); + }); + + it("store initialization", async () => { await store.dispatch("history/loadCurrentHistory"); expect(store.getters["history/currentHistoryId"]).toBe(0); await store.dispatch("history/loadHistoryById", 1);