From f4f2748d2200f1d325b453977f1679fd1481988d Mon Sep 17 00:00:00 2001 From: Mantra Date: Tue, 5 Nov 2024 17:50:45 +0000 Subject: [PATCH 1/2] batch of tests --- .../sensitivitySummaryDownload.test.ts | 34 ++++---- .../sensitivitySummaryPlot.test.ts | 55 +++++++------ .../sensitivity/sensitivityTab.test.ts | 42 +++++----- .../sensitivity/sensitivityTracesPlot.test.ts | 55 ++++++++----- .../components/sensitivity/support.test.ts | 8 +- .../sessions/editSessionLabel.test.ts | 9 +-- .../components/sessions/sessionsPage.test.ts | 81 +++++++++---------- .../components/versions/appHeader.test.ts | 8 +- .../components/versions/versionMenu.test.ts | 10 +-- 9 files changed, 159 insertions(+), 143 deletions(-) diff --git a/app/static/tests/unit/components/sensitivity/sensitivitySummaryDownload.test.ts b/app/static/tests/unit/components/sensitivity/sensitivitySummaryDownload.test.ts index cc8ddf85..8b56346b 100644 --- a/app/static/tests/unit/components/sensitivity/sensitivitySummaryDownload.test.ts +++ b/app/static/tests/unit/components/sensitivity/sensitivitySummaryDownload.test.ts @@ -1,26 +1,26 @@ import Vuex from "vuex"; import { shallowMount, VueWrapper } from "@vue/test-utils"; import { nextTick } from "vue"; -import DownloadOutput from "../../../../src/app/components/DownloadOutput.vue"; -import { AppState } from "../../../../src/app/store/appState/state"; +import DownloadOutput from "../../../../src/components/DownloadOutput.vue"; +import { AppState } from "../../../../src/store/appState/state"; import { BaseSensitivityState, SensitivityPlotType, SensitivityState -} from "../../../../src/app/store/sensitivity/state"; -import SensitivitySummaryDownload from "../../../../src/app/components/sensitivity/SensitivitySummaryDownload.vue"; -import { BaseSensitivityAction, SensitivityAction } from "../../../../src/app/store/sensitivity/actions"; -import { BaseSensitivityMutation, SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; -import LoadingSpinner from "../../../../src/app/components/LoadingSpinner.vue"; -import { ModelGetter } from "../../../../src/app/store/model/getters"; -import { getters as graphsGetters } from "../../../../src/app/store/graphs/getters"; +} from "../../../../src/store/sensitivity/state"; +import SensitivitySummaryDownload from "../../../../src/components/sensitivity/SensitivitySummaryDownload.vue"; +import { BaseSensitivityAction } from "../../../../src/store/sensitivity/actions"; +import { BaseSensitivityMutation, SensitivityMutation } from "../../../../src/store/sensitivity/mutations"; +import LoadingSpinner from "../../../../src/components/LoadingSpinner.vue"; +import { ModelGetter } from "../../../../src/store/model/getters"; +import { getters as graphsGetters } from "../../../../src/store/graphs/getters"; import { mockGraphsState } from "../../../mocks"; -import { defaultGraphSettings } from "../../../../src/app/store/graphs/state"; +import { defaultGraphSettings } from "../../../../src/store/graphs/state"; describe("SensitivitySummaryDownload", () => { - const mockSetUserSummaryDownloadFileName = jest.fn(); - const mockDownloadSummary = jest.fn(); - const mockSetPlotTime = jest.fn(); + const mockSetUserSummaryDownloadFileName = vi.fn(); + const mockDownloadSummary = vi.fn(); + const mockSetPlotTime = vi.fn(); const getWrapper = (multiSens = false, state: Partial = {}) => { const plotSettings = { @@ -48,7 +48,7 @@ describe("SensitivitySummaryDownload", () => { }, mutations: { [BaseSensitivityMutation.SetUserSummaryDownloadFileName]: mockSetUserSummaryDownloadFileName, - [SensitivityMutation.SetPlotTime]: multiSens ? jest.fn() : mockSetPlotTime + [SensitivityMutation.SetPlotTime]: multiSens ? vi.fn() : mockSetPlotTime } }; const minimalSensitivity = { @@ -116,7 +116,7 @@ describe("SensitivitySummaryDownload", () => { ) => { let wrapper = getWrapper(false, state); test(wrapper, false); - jest.clearAllMocks(); + vi.clearAllMocks(); wrapper = getWrapper(true, state); test(wrapper, true); }; @@ -127,13 +127,13 @@ describe("SensitivitySummaryDownload", () => { ) => { let wrapper = getWrapper(false, state); await test(wrapper, false); - jest.clearAllMocks(); + vi.clearAllMocks(); wrapper = getWrapper(true, state); await test(wrapper, true); }; afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); it("enables download button when expected", () => { diff --git a/app/static/tests/unit/components/sensitivity/sensitivitySummaryPlot.test.ts b/app/static/tests/unit/components/sensitivity/sensitivitySummaryPlot.test.ts index 5b5ec00e..8ae0e194 100644 --- a/app/static/tests/unit/components/sensitivity/sensitivitySummaryPlot.test.ts +++ b/app/static/tests/unit/components/sensitivity/sensitivitySummaryPlot.test.ts @@ -3,7 +3,7 @@ import Vuex, { Store } from "vuex"; import { shallowMount, VueWrapper } from "@vue/test-utils"; import * as plotly from "plotly.js-basic-dist-min"; import { nextTick } from "vue"; -import { BasicState } from "../../../../src/app/store/basic/state"; +import { BasicState } from "../../../../src/store/basic/state"; import { mockBasicState } from "../../../mocks"; import { SensitivityParameterSettings, @@ -11,33 +11,32 @@ import { SensitivityPlotSettings, SensitivityPlotType, SensitivityScaleType -} from "../../../../src/app/store/sensitivity/state"; -import { SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; -import SensitivitySummaryPlot from "../../../../src/app/components/sensitivity/SensitivitySummaryPlot.vue"; -import { RunGetter } from "../../../../src/app/store/run/getters"; -import WodinPlotDataSummary from "../../../../src/app/components/WodinPlotDataSummary.vue"; -import { GraphConfig } from "../../../../src/app/store/graphs/state"; - -jest.mock("plotly.js-basic-dist-min", () => ({ - newPlot: jest.fn(), +} from "../../../../src/store/sensitivity/state"; +import { SensitivityMutation } from "../../../../src/store/sensitivity/mutations"; +import SensitivitySummaryPlot from "../../../../src/components/sensitivity/SensitivitySummaryPlot.vue"; +import { RunGetter } from "../../../../src/store/run/getters"; +import WodinPlotDataSummary from "../../../../src/components/WodinPlotDataSummary.vue"; + +vi.mock("plotly.js-basic-dist-min", () => ({ + newPlot: vi.fn(), Plots: { - resize: jest.fn() + resize: vi.fn() } })); describe("SensitivitySummaryPlot", () => { - const mockPlotlyNewPlot = jest.spyOn(plotly, "newPlot"); + const mockPlotlyNewPlot = vi.spyOn(plotly, "newPlot"); - const mockObserve = jest.fn(); - const mockDisconnect = jest.fn(); + const mockObserve = vi.fn(); + const mockDisconnect = vi.fn(); function mockResizeObserver(this: any) { this.observe = mockObserve; this.disconnect = mockDisconnect; } (global.ResizeObserver as any) = mockResizeObserver; - const mockSetPlotTime = jest.fn(); - const mockSetLoading = jest.fn(); + const mockSetPlotTime = vi.fn(); + const mockSetLoading = vi.fn(); const mockData = { x: [{ beta: 1 }, { beta: 1.1 }], @@ -75,26 +74,26 @@ describe("SensitivitySummaryPlot", () => { ] }; - const mockValueAtTime = jest.fn().mockReturnValue(mockData); - const mockExtreme = jest.fn().mockReturnValue(mockData); + const mockValueAtTime = vi.fn().mockReturnValue(mockData); + const mockExtreme = vi.fn().mockReturnValue(mockData); const mockBatch = { valueAtTime: mockValueAtTime, extreme: mockExtreme }; const mockBatchSet1 = { - valueAtTime: jest.fn().mockReturnValue(mockDataSet1), - extreme: jest.fn().mockReturnValue(mockDataSet1) + valueAtTime: vi.fn().mockReturnValue(mockDataSet1), + extreme: vi.fn().mockReturnValue(mockDataSet1) }; const mockBatchSet2 = { - valueAtTime: jest.fn().mockReturnValue(mockDataSet2), - extreme: jest.fn().mockReturnValue(mockDataSet2) + valueAtTime: vi.fn().mockReturnValue(mockDataSet2), + extreme: vi.fn().mockReturnValue(mockDataSet2) }; const mockBatchSet3 = { - valueAtTime: jest.fn().mockReturnValue(mockDataSet3), - extreme: jest.fn().mockReturnValue(mockDataSet3) + valueAtTime: vi.fn().mockReturnValue(mockDataSet3), + extreme: vi.fn().mockReturnValue(mockDataSet3) }; let store: Store | null = null; @@ -220,7 +219,7 @@ describe("SensitivitySummaryPlot", () => { }; afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); const expectDataToHaveBeenPlotted = (wrapper: VueWrapper, layout: any = {}, includesParameterSets = false) => { @@ -524,7 +523,7 @@ describe("SensitivitySummaryPlot", () => { it("redraws plot if data changes", async () => { // update store's time value to force re-compute of plotData, and then redraw - const wrapper = getWrapper(); + getWrapper(); store!.state.sensitivity.plotSettings.time = 50; await nextTick(); expect(mockPlotlyNewPlot).toHaveBeenCalledTimes(2); @@ -564,7 +563,7 @@ describe("SensitivitySummaryPlot", () => { }); it("commits set loading when plotData is computed, if loading is true", async () => { - const wrapper = getWrapper( + getWrapper( true, defaultPlotSettings, false, @@ -582,7 +581,7 @@ describe("SensitivitySummaryPlot", () => { }); it("does not commit set loading when plotData is computed, if loading is false", async () => { - const wrapper = getWrapper(); + getWrapper(); store!.state.sensitivity.plotSettings.time = 50; expect(mockSetLoading).not.toHaveBeenCalled(); }); diff --git a/app/static/tests/unit/components/sensitivity/sensitivityTab.test.ts b/app/static/tests/unit/components/sensitivity/sensitivityTab.test.ts index ac2cd175..fd9ad714 100644 --- a/app/static/tests/unit/components/sensitivity/sensitivityTab.test.ts +++ b/app/static/tests/unit/components/sensitivity/sensitivityTab.test.ts @@ -1,28 +1,28 @@ import { shallowMount } from "@vue/test-utils"; import Vuex from "vuex"; -import { ModelState } from "../../../../src/app/store/model/state"; -import SensitivityTab from "../../../../src/app/components/sensitivity/SensitivityTab.vue"; -import ActionRequiredMessage from "../../../../src/app/components/ActionRequiredMessage.vue"; -import { BaseSensitivityGetter } from "../../../../src/app/store/sensitivity/getters"; -import SensitivityTracesPlot from "../../../../src/app/components/sensitivity/SensitivityTracesPlot.vue"; -import { SensitivityPlotType, SensitivityState } from "../../../../src/app/store/sensitivity/state"; -import { SensitivityAction } from "../../../../src/app/store/sensitivity/actions"; -import SensitivitySummaryPlot from "../../../../src/app/components/sensitivity/SensitivitySummaryPlot.vue"; -import ErrorInfo from "../../../../src/app/components/ErrorInfo.vue"; -import { AppState, AppType } from "../../../../src/app/store/appState/state"; -import { ModelGetter } from "../../../../src/app/store/model/getters"; -import LoadingSpinner from "../../../../src/app/components/LoadingSpinner.vue"; -import { SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; -import SensitivitySummaryDownload from "../../../../src/app/components/sensitivity/SensitivitySummaryDownload.vue"; -import LoadingButton from "../../../../src/app/components/LoadingButton.vue"; -import { getters as graphsGetters } from "../../../../src/app/store/graphs/getters"; +import { ModelState } from "../../../../src/store/model/state"; +import SensitivityTab from "../../../../src/components/sensitivity/SensitivityTab.vue"; +import ActionRequiredMessage from "../../../../src/components/ActionRequiredMessage.vue"; +import { BaseSensitivityGetter } from "../../../../src/store/sensitivity/getters"; +import SensitivityTracesPlot from "../../../../src/components/sensitivity/SensitivityTracesPlot.vue"; +import { SensitivityPlotType, SensitivityState } from "../../../../src/store/sensitivity/state"; +import { SensitivityAction } from "../../../../src/store/sensitivity/actions"; +import SensitivitySummaryPlot from "../../../../src/components/sensitivity/SensitivitySummaryPlot.vue"; +import ErrorInfo from "../../../../src/components/ErrorInfo.vue"; +import { AppState, AppType } from "../../../../src/store/appState/state"; +import { ModelGetter } from "../../../../src/store/model/getters"; +import LoadingSpinner from "../../../../src/components/LoadingSpinner.vue"; +import { SensitivityMutation } from "../../../../src/store/sensitivity/mutations"; +import SensitivitySummaryDownload from "../../../../src/components/sensitivity/SensitivitySummaryDownload.vue"; +import LoadingButton from "../../../../src/components/LoadingButton.vue"; +import { getters as graphsGetters } from "../../../../src/store/graphs/getters"; -jest.mock("plotly.js-basic-dist-min", () => {}); +vi.mock("plotly.js-basic-dist-min", () => ({})); describe("SensitivityTab", () => { - const mockRunSensitivity = jest.fn(); - const mockSetLoading = jest.fn(); - const mockSetPlotTime = jest.fn(); + const mockRunSensitivity = vi.fn(); + const mockSetLoading = vi.fn(); + const mockSetPlotTime = vi.fn(); const getWrapper = ( appType = AppType.Basic, @@ -122,7 +122,7 @@ describe("SensitivityTab", () => { }; afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); it("renders as expected when Trace over Time", () => { diff --git a/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts b/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts index 0a584d49..e2b437e8 100644 --- a/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts +++ b/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts @@ -1,18 +1,17 @@ // Mock plotly before import RunTab, which indirectly imports plotly via WodinPlot -/* eslint-disable import/first */ import Vuex from "vuex"; import { shallowMount } from "@vue/test-utils"; -import { AppState, AppType } from "../../../../src/app/store/appState/state"; -import WodinPlot from "../../../../src/app/components/WodinPlot.vue"; -import SensitivityTracesPlot from "../../../../src/app/components/sensitivity/SensitivityTracesPlot.vue"; -import { FitDataGetter } from "../../../../src/app/store/fitData/getters"; +import { AppState, AppType } from "../../../../src/store/appState/state"; +import WodinPlot from "../../../../src/components/WodinPlot.vue"; +import SensitivityTracesPlot from "../../../../src/components/sensitivity/SensitivityTracesPlot.vue"; +import { FitDataGetter } from "../../../../src/store/fitData/getters"; import { mockRunState } from "../../../mocks"; -import { getters as runGetters } from "../../../../src/app/store/run/getters"; -import { SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; +import { getters as runGetters } from "../../../../src/store/run/getters"; +import { SensitivityMutation } from "../../../../src/store/sensitivity/mutations"; -jest.mock("plotly.js-basic-dist-min", () => {}); +vi.mock("plotly.js-basic-dist-min", () => ({})); -const mockSln1 = jest.fn().mockReturnValue({ +const mockSln1 = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [5, 6, 7] }, @@ -21,7 +20,7 @@ const mockSln1 = jest.fn().mockReturnValue({ ] }); -const mockSln2 = jest.fn().mockReturnValue({ +const mockSln2 = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [50, 60, 70] }, @@ -30,7 +29,7 @@ const mockSln2 = jest.fn().mockReturnValue({ ] }); -const mockCentralSln = jest.fn().mockReturnValue({ +const mockCentralSln = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [15, 16, 17] }, @@ -39,7 +38,7 @@ const mockCentralSln = jest.fn().mockReturnValue({ ] }); -const mockCentralStochasticSln = jest.fn().mockReturnValue({ +const mockCentralStochasticSln = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", description: "Mean", y: [22, 23, 24] }, @@ -48,7 +47,7 @@ const mockCentralStochasticSln = jest.fn().mockReturnValue({ ] }); -const mockParameterSetSln1 = jest.fn().mockReturnValue({ +const mockParameterSetSln1 = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [51, 61, 71] }, @@ -57,7 +56,7 @@ const mockParameterSetSln1 = jest.fn().mockReturnValue({ ] }); -const mockParameterSetSln2 = jest.fn().mockReturnValue({ +const mockParameterSetSln2 = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [52, 62, 72] }, @@ -66,7 +65,7 @@ const mockParameterSetSln2 = jest.fn().mockReturnValue({ ] }); -const mockParameterSetCentralSln = jest.fn().mockReturnValue({ +const mockParameterSetCentralSln = vi.fn().mockReturnValue({ x: [0, 0.5, 1], values: [ { name: "y", y: [151, 161, 171] }, @@ -393,7 +392,24 @@ const mockParameterSetCentralResults = { "Set 2": { solution: mockParameterSetCentralSln } } as any; -const mockSetLoading = jest.fn(); +const mockSetLoading = vi.fn(); + +const getMockResult = (sensitivityHasSolutions = true, sensitivityHasData = false) => { + return { + batch: { + solutions: sensitivityHasSolutions ? mockSolutions : null, + allFitData: sensitivityHasData ? mockAllFitData : undefined, + pars: { + varying: [ + { + name: "alpha", + values: [1.11111, 2.22222] + } + ] + } + } + }; +} describe("SensitivityTracesPlot", () => { const getWrapper = ( @@ -485,7 +501,7 @@ describe("SensitivityTracesPlot", () => { }; afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); it("renders as expected when there are sensitivity solutions", () => { @@ -496,6 +512,7 @@ describe("SensitivityTracesPlot", () => { expect(wodinPlot.props("endTime")).toBe(1); expect(wodinPlot.props("redrawWatches")).toStrictEqual([ ...mockSolutions, + getMockResult(), undefined, selectedVariables, {}, @@ -521,6 +538,7 @@ describe("SensitivityTracesPlot", () => { expect(wodinPlot.props("endTime")).toBe(1); expect(wodinPlot.props("redrawWatches")).toStrictEqual([ ...mockSolutions, + getMockResult(true, true), mockAllFitData, selectedVariables, {}, @@ -546,6 +564,7 @@ describe("SensitivityTracesPlot", () => { expect(wodinPlot.props("endTime")).toBe(1); expect(wodinPlot.props("redrawWatches")).toStrictEqual([ ...mockSolutions, + getMockResult(true, true), mockAllFitData, selectedVariables, { "Set 1": mockParameterSetBatch1 }, @@ -576,7 +595,7 @@ describe("SensitivityTracesPlot", () => { expect(wodinPlot.props("fadePlot")).toBe(false); expect(wodinPlot.props("placeholderMessage")).toBe("Sensitivity has not been run."); expect(wodinPlot.props("endTime")).toBe(1); - expect(wodinPlot.props("redrawWatches")).toStrictEqual([undefined, selectedVariables, {}, [], 0]); + expect(wodinPlot.props("redrawWatches")).toStrictEqual([getMockResult(false), undefined, selectedVariables, {}, [], 0]); const plotData = wodinPlot.props("plotData"); const data = plotData(0, 1, 100); diff --git a/app/static/tests/unit/components/sensitivity/support.test.ts b/app/static/tests/unit/components/sensitivity/support.test.ts index e3ff81e7..d81adaf7 100644 --- a/app/static/tests/unit/components/sensitivity/support.test.ts +++ b/app/static/tests/unit/components/sensitivity/support.test.ts @@ -1,9 +1,9 @@ import { sensitivityUpdateRequiredExplanation, verifyValidPlotSettingsTime -} from "../../../../src/app/components/sensitivity/support"; +} from "../../../../src/components/sensitivity/support"; import { mockBasicState, mockRunState, mockSensitivityState } from "../../../mocks"; -import { SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; +import { SensitivityMutation } from "../../../../src/store/sensitivity/mutations"; describe("construct actionable fit update messages from fit state changes", () => { const base = { @@ -90,10 +90,10 @@ describe("verifies valid plot settings time", () => { }); }; - const commit = jest.fn(); + const commit = vi.fn(); beforeEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); const expectPlotSettingsCommit = (expectedValue: number) => { diff --git a/app/static/tests/unit/components/sessions/editSessionLabel.test.ts b/app/static/tests/unit/components/sessions/editSessionLabel.test.ts index 0604f2a3..87b2c7be 100644 --- a/app/static/tests/unit/components/sessions/editSessionLabel.test.ts +++ b/app/static/tests/unit/components/sessions/editSessionLabel.test.ts @@ -1,12 +1,11 @@ import { shallowMount } from "@vue/test-utils"; import Vuex from "vuex"; -import EditSessionLabel from "../../../../src/app/components/sessions/EditSessionLabel.vue"; +import EditSessionLabel from "../../../../src/components/sessions/EditSessionLabel.vue"; import { mockBasicState } from "../../../mocks"; -import { BasicState } from "../../../../src/app/store/basic/state"; -import mock = jest.mock; +import { BasicState } from "../../../../src/store/basic/state"; describe("EditSessionLabel", () => { - const mockSaveSessionLabel = jest.fn(); + const mockSaveSessionLabel = vi.fn(); const getWrapper = async (open: boolean) => { const store = new Vuex.Store({ state: mockBasicState(), @@ -40,7 +39,7 @@ describe("EditSessionLabel", () => { }; beforeEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); it("renders as expected when opened", async () => { diff --git a/app/static/tests/unit/components/sessions/sessionsPage.test.ts b/app/static/tests/unit/components/sessions/sessionsPage.test.ts index 0b9b11e0..49645dd0 100644 --- a/app/static/tests/unit/components/sessions/sessionsPage.test.ts +++ b/app/static/tests/unit/components/sessions/sessionsPage.test.ts @@ -2,31 +2,32 @@ import { shallowMount } from "@vue/test-utils"; import Vuex from "vuex"; import VueFeather from "vue-feather"; import { nextTick } from "vue"; -import SessionsPage from "../../../../src/app/components/sessions/SessionsPage.vue"; -import ErrorsAlert from "../../../../src/app/components/ErrorsAlert.vue"; -import { BasicState } from "../../../../src/app/store/basic/state"; +import SessionsPage from "../../../../src/components/sessions/SessionsPage.vue"; +import ErrorsAlert from "../../../../src/components/ErrorsAlert.vue"; +import { BasicState } from "../../../../src/store/basic/state"; import { mockBasicState, mockSessionsState, mockUserPreferences } from "../../../mocks"; -import { SessionsAction } from "../../../../src/app/store/sessions/actions"; -import EditSessionLabel from "../../../../src/app/components/sessions/EditSessionLabel.vue"; -import ConfirmModal from "../../../../src/app/components/ConfirmModal.vue"; -import { AppStateAction } from "../../../../src/app/store/appState/actions"; -import { SessionsState } from "../../../../src/app/store/sessions/state"; +import { SessionsAction } from "../../../../src/store/sessions/actions"; +import EditSessionLabel from "../../../../src/components/sessions/EditSessionLabel.vue"; +import ConfirmModal from "../../../../src/components/ConfirmModal.vue"; +import { AppStateAction } from "../../../../src/store/appState/actions"; +import { SessionsState } from "../../../../src/store/sessions/state"; const mockRouter = { - push: jest.fn() + push: vi.fn() }; -jest.mock("vue-router", () => ({ - useRouter: jest.fn().mockImplementation(() => mockRouter) +vi.mock("vue-router", () => ({ + useRouter: vi.fn().mockImplementation(() => mockRouter), + RouterLink: null })); describe("SessionsPage", () => { - const mockGetSessions = jest.fn(); - const mockGenerateFriendlyId = jest.fn(); - const mockClipboardWriteText = jest.fn(); - const mockDeleteSession = jest.fn(); - const mockSaveUserPreferences = jest.fn(); - const mockLoadUserPreferences = jest.fn(); - const mockInitialiseSession = jest.fn(); + const mockGetSessions = vi.fn(); + const mockGenerateFriendlyId = vi.fn(); + const mockClipboardWriteText = vi.fn(); + const mockDeleteSession = vi.fn(); + const mockSaveUserPreferences = vi.fn(); + const mockLoadUserPreferences = vi.fn(); + const mockInitialiseSession = vi.fn(); Object.assign(window.navigator, { clipboard: { @@ -35,7 +36,7 @@ describe("SessionsPage", () => { }); beforeEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); const currentSessionId = "abc"; @@ -317,25 +318,19 @@ describe("SessionsPage", () => { expect(wrapper.find("#current-session .session-copy-confirm").text()).toBe(""); }); - it("copy confirmation indicates if friendly id is being fetched, and could not be generated", (done) => { - const runAsync = async () => { - // the mock generate action won't mutate the state, so friendly id will still be null after it's done, - // and the component will assume the id could not be fetched - const wrapper = getWrapper({ sessionsMetadata }, currentSessionId); - const rows = wrapper.findAll(".previous-session-row"); - const session2Cells = rows.at(0)!.findAll("div.session-col-value"); - await session2Cells.at(5)!.find(".session-copy-code").trigger("click"); - // message will update to 'Fetching code...' while it calls the action - const confirm = session2Cells.at(5)!.find(".session-copy-confirm"); - expect(confirm.text()).toBe("Fetching code..."); - - // when action is completed, id has not been successfully updated - setTimeout(() => { - expect(confirm.text()).toBe("Error fetching code"); - done(); - }); - }; - runAsync(); + it("copy confirmation indicates if friendly id is being fetched, and could not be generated", async () => { + // the mock generate action won't mutate the state, so friendly id will still be null after it's done, + // and the component will assume the id could not be fetched + const wrapper = getWrapper({ sessionsMetadata }, currentSessionId); + const rows = wrapper.findAll(".previous-session-row"); + const session2Cells = rows.at(0)!.findAll("div.session-col-value"); + await session2Cells.at(5)!.find(".session-copy-code").trigger("click"); + // message will update to 'Fetching code...' while it calls the action + const confirm = session2Cells.at(5)!.find(".session-copy-confirm"); + expect(confirm.text()).toBe("Fetching code..."); + await nextTick(); + // when action is completed, id has not been successfully updated + expect(confirm.text()).toBe("Error fetching code"); }); it("opens and closes confirm delete dialog", async () => { @@ -363,7 +358,7 @@ describe("SessionsPage", () => { it("loads session from code", async () => { const realLocation = window.location; delete (window as any).location; - window.location = { ...realLocation, assign: jest.fn() }; + window.location = { ...realLocation, assign: vi.fn() }; const wrapper = getWrapper({ sessionsMetadata }, currentSessionId); await wrapper.find("#session-code-input").setValue("bad-dog"); @@ -376,14 +371,18 @@ describe("SessionsPage", () => { it("can save show unlabelled sessions preference", async () => { const wrapper = getWrapper({ sessionsMetadata }, currentSessionId); - await wrapper.find("input#show-unlabelled-check").trigger("click"); + const checkbox = wrapper.find("input#show-unlabelled-check"); + (checkbox.element as HTMLInputElement).checked = false; + await checkbox.trigger("change"); expect(mockSaveUserPreferences).toHaveBeenCalledTimes(1); expect(mockSaveUserPreferences.mock.calls[0][1]).toStrictEqual({ showUnlabelledSessions: false }); }); it("can save show duplicate sessions preference", async () => { const wrapper = getWrapper({ sessionsMetadata }, currentSessionId); - await wrapper.find("input#show-duplicates-check").trigger("click"); + const checkbox = wrapper.find("input#show-duplicates-check"); + (checkbox.element as HTMLInputElement).checked = true; + await checkbox.trigger("change"); expect(mockSaveUserPreferences).toHaveBeenCalledTimes(1); expect(mockSaveUserPreferences.mock.calls[0][1]).toStrictEqual({ showDuplicateSessions: true }); }); diff --git a/app/static/tests/unit/components/versions/appHeader.test.ts b/app/static/tests/unit/components/versions/appHeader.test.ts index 13fda830..b28af436 100644 --- a/app/static/tests/unit/components/versions/appHeader.test.ts +++ b/app/static/tests/unit/components/versions/appHeader.test.ts @@ -3,11 +3,11 @@ import { RouterLink } from "vue-router"; import Vuex from "vuex"; import VueFeather from "vue-feather"; import { nextTick } from "vue"; -import AppHeader from "../../../../src/app/components/header/AppHeader.vue"; -import EditSessionLabel from "../../../../src/app/components/sessions/EditSessionLabel.vue"; -import { BasicState } from "../../../../src/app/store/basic/state"; +import AppHeader from "../../../../src/components/header/AppHeader.vue"; +import EditSessionLabel from "../../../../src/components/sessions/EditSessionLabel.vue"; +import { BasicState } from "../../../../src/store/basic/state"; import { mockBasicState } from "../../../mocks"; -import VersionMenu from "../../../../src/app/components/header/VersionMenu.vue"; +import VersionMenu from "../../../../src/components/header/VersionMenu.vue"; import { LanguageSwitcher } from "../../../../translationPackage"; describe("AppHeader", () => { diff --git a/app/static/tests/unit/components/versions/versionMenu.test.ts b/app/static/tests/unit/components/versions/versionMenu.test.ts index 1aee25bf..d033db9b 100644 --- a/app/static/tests/unit/components/versions/versionMenu.test.ts +++ b/app/static/tests/unit/components/versions/versionMenu.test.ts @@ -1,12 +1,12 @@ import Vuex from "vuex"; import { mount } from "@vue/test-utils"; -import { BasicState } from "../../../../src/app/store/basic/state"; +import { BasicState } from "../../../../src/store/basic/state"; import { mockBasicState, mockVersionsState } from "../../../mocks"; -import VersionMenu from "../../../../src/app/components/header/VersionMenu.vue"; -import DropDown from "../../../../src/app/components/DropDown.vue"; +import VersionMenu from "../../../../src/components/header/VersionMenu.vue"; +import DropDown from "../../../../src/components/DropDown.vue"; describe("version menu component", () => { - const mockVersionsAction = jest.fn(); + const mockVersionsAction = vi.fn(); const versions = { dfoptim: "0.0.5", @@ -17,7 +17,7 @@ describe("version menu component", () => { }; beforeEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); const getWrapper = () => { From 5680001c4e463dd3ea433ac7550fdfbb3efedbca Mon Sep 17 00:00:00 2001 From: Mantra Date: Wed, 6 Nov 2024 12:18:17 +0000 Subject: [PATCH 2/2] less code dup --- .../sensitivity/sensitivityTracesPlot.test.ts | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts b/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts index e2b437e8..fbce7b2d 100644 --- a/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts +++ b/app/static/tests/unit/components/sensitivity/sensitivityTracesPlot.test.ts @@ -458,20 +458,7 @@ describe("SensitivityTracesPlot", () => { paramSettings: { parameterToVary: "alpha" }, - result: { - batch: { - solutions: sensitivityHasSolutions ? mockSolutions : null, - allFitData: sensitivityHasData ? mockAllFitData : undefined, - pars: { - varying: [ - { - name: "alpha", - values: [1.11111, 2.22222] - } - ] - } - } - }, + result: getMockResult(sensitivityHasSolutions, sensitivityHasData), parameterSetResults: hasParameterSets ? mockParameterSetResults : {} }, mutations: {