From 45e0539fa48506eb08187fb166fd93d8b49946db Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Fri, 22 Mar 2024 16:44:50 +0100 Subject: [PATCH] Refactor MultipleView.test.js to improve test setup --- .../History/Multiple/MultipleView.test.js | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/client/src/components/History/Multiple/MultipleView.test.js b/client/src/components/History/Multiple/MultipleView.test.js index 6175c7c69bd..c8acf9c4fa4 100644 --- a/client/src/components/History/Multiple/MultipleView.test.js +++ b/client/src/components/History/Multiple/MultipleView.test.js @@ -1,7 +1,6 @@ import { mount } from "@vue/test-utils"; import axios from "axios"; import MockAdapter from "axios-mock-adapter"; -import MockUserHistories from "components/providers/MockUserHistories"; import flushPromises from "flush-promises"; import { createPinia } from "pinia"; import { useHistoryStore } from "stores/historyStore"; @@ -24,13 +23,24 @@ const getFakeHistorySummaries = (num, selectedIndex) => { const currentUser = { id: USER_ID }; describe("MultipleView", () => { - async function setUpWrapper(UserHistoriesMock, count, currentHistoryId) { - const axiosMock = new MockAdapter(axios); - axiosMock.onGet(`api/histories/${FIRST_HISTORY_ID}`).reply(200, {}); + let axiosMock; + + beforeEach(() => { + axiosMock = new MockAdapter(axios); + }); + + afterEach(() => { + axiosMock.reset(); + }); + + async function setUpWrapper(count, currentHistoryId) { + const fakeSummaries = getFakeHistorySummaries(count, 0); + for (const summary of fakeSummaries) { + axiosMock.onGet(`api/histories/${summary.id}`).reply(200, summary); + } const wrapper = mount(MultipleView, { pinia: createPinia(), stubs: { - UserHistories: UserHistoriesMock, HistoryPanel: true, icon: { template: "
" }, }, @@ -41,12 +51,12 @@ describe("MultipleView", () => { userStore.currentUser = currentUser; const historyStore = useHistoryStore(); - historyStore.setHistories(getFakeHistorySummaries(count, 0)); + historyStore.setHistories(fakeSummaries); historyStore.setCurrentHistoryId(currentHistoryId); await flushPromises(); - return { wrapper, axiosMock }; + return wrapper; } it("more than 4 histories should not show the current history", async () => { @@ -54,8 +64,9 @@ describe("MultipleView", () => { const currentHistoryId = FIRST_HISTORY_ID; // Set up UserHistories and wrapper - const UserHistoriesMock = MockUserHistories({ id: currentHistoryId }, getFakeHistorySummaries(count, 0), false); - const { wrapper, axiosMock } = await setUpWrapper(UserHistoriesMock, count, currentHistoryId); + const wrapper = await setUpWrapper(count, currentHistoryId); + + console.log(wrapper.html()); // Test: current (first) history should not be shown because only 4 latest are shown by default expect(wrapper.find("button[title='Current History']").exists()).toBeFalsy(); @@ -65,21 +76,16 @@ describe("MultipleView", () => { expect(wrapper.find("div[title='Currently showing 4 most recently updated histories']").exists()).toBeTruthy(); expect(wrapper.find("[data-description='open select histories modal']").exists()).toBeTruthy(); - - axiosMock.reset(); }); - it("less than or equal to 4 histories should not show the current history", async () => { + it("less than 4 histories should show the current history", async () => { const count = 3; const currentHistoryId = FIRST_HISTORY_ID; // Set up UserHistories and wrapper - const UserHistoriesMock = MockUserHistories({ id: currentHistoryId }, getFakeHistorySummaries(count, 0), false); - const { wrapper, axiosMock } = await setUpWrapper(UserHistoriesMock, count, currentHistoryId); + const wrapper = await setUpWrapper(count, currentHistoryId); // Test: current (first) history should be shown because only 4 latest are shown by default, and count = 3 expect(wrapper.find("button[title='Current History']").exists()).toBeTruthy(); - - axiosMock.reset(); }); });