Skip to content

Commit

Permalink
Add history store client test case
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Sep 22, 2022
1 parent d08196a commit 990dc1e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/src/store/historyStore/historyStore.js
Expand Up @@ -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 }), {});
Expand Down
40 changes: 38 additions & 2 deletions client/src/store/historyStore/historyStore.test.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 990dc1e

Please sign in to comment.