Skip to content

Commit

Permalink
test SensitivitySummaryDownload when multisens
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Sep 7, 2023
1 parent cf0794a commit 25aa128
Showing 1 changed file with 66 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import DownloadOutput from "../../../../src/app/components/DownloadOutput.vue";
import {AppState, AppType} from "../../../../src/app/store/appState/state";
import {ModelState} from "../../../../src/app/store/model/state";
import {BaseSensitivityState, SensitivityPlotType, SensitivityState} from "../../../../src/app/store/sensitivity/state";
import Vuex from "vuex";
import {shallowMount} from "@vue/test-utils";
import SensitivityTab from "../../../../src/app/components/sensitivity/SensitivityTab.vue";
import {shallowMount, VueWrapper} from "@vue/test-utils";
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";
Expand Down Expand Up @@ -92,11 +90,32 @@ describe("SensitivitySummaryDownload", () => {
});
};

const testForSensAndMultiSens = (test: (wrapper: VueWrapper<any>, multiSens: boolean) => void, state: Partial<SensitivityState> = {}) => {
let wrapper = getWrapper(false, state);
test(wrapper, false);
jest.clearAllMocks();
wrapper = getWrapper(true, state);
test(wrapper, true);
};

const asyncTestForSensAndMultiSens = async (test: (wrapper: VueWrapper<any>, multiSens: boolean) => Promise<void>, state: Partial<SensitivityState> = {}) => {
let wrapper = getWrapper(false, state);
await test(wrapper, false);
jest.clearAllMocks();
wrapper = getWrapper(true, state);
await test(wrapper, true);
};

afterEach(() => {
jest.clearAllMocks();
});

it("enables download button when expected", () => {
const expectDownloadButtonEnabled = (state: Partial<SensitivityState>, expectButtonEnabled: boolean) => {
const wrapper = getWrapper(false, state);
const button = wrapper.find("button#download-summary-btn");
expect((button.element as HTMLButtonElement).disabled).toBe(!expectButtonEnabled);
const expectDownloadButtonEnabled = (state: Partial<BaseSensitivityState>, expectButtonEnabled: boolean) => {
testForSensAndMultiSens((wrapper) => {
const button = wrapper.find("button#download-summary-btn");
expect((button.element as HTMLButtonElement).disabled).toBe(!expectButtonEnabled);
}, state);
};

// enabled if not downloading, and no update required, and batch result exists
Expand Down Expand Up @@ -139,24 +158,29 @@ describe("SensitivitySummaryDownload", () => {
});

it("renders DownloadOutput as expected", () => {
const wrapper = getWrapper(false,{ userSummaryDownloadFileName: "test.xlsx" });
const downloadOutput = wrapper.findComponent(DownloadOutput);
expect(downloadOutput.props().open).toBe(false);
expect(downloadOutput.props().downloadType).toBe("Test Download Type");
expect(downloadOutput.props().includePoints).toBe(false);
expect(downloadOutput.props().userFileName).toBe("test.xlsx");
const state = {userSummaryDownloadFileName: "test.xlsx"};
testForSensAndMultiSens((wrapper) => {
const downloadOutput = wrapper.findComponent(DownloadOutput);
expect(downloadOutput.props().open).toBe(false);
expect(downloadOutput.props().downloadType).toBe("Test Download Type");
expect(downloadOutput.props().includePoints).toBe(false);
expect(downloadOutput.props().userFileName).toBe("test.xlsx");
}, state);
});

it("renders downloading as expected", () => {
// does not render if not downloading
let wrapper = getWrapper();
expect(wrapper.find("div#downloading").exists()).toBe(false);
testForSensAndMultiSens((wrapper) => {
// does not render if not downloading
expect(wrapper.find("div#downloading").exists()).toBe(false);
});

// does render if downloading
wrapper = getWrapper(false, { downloading: true });
const downloading = wrapper.find("div#downloading");
expect(downloading.text()).toBe("Downloading...");
expect(wrapper.findComponent(LoadingSpinner).props("size")).toBe("xs");
const state = { downloading: true };
testForSensAndMultiSens((wrapper) => {
const downloading = wrapper.find("div#downloading");
expect(downloading.text()).toBe("Downloading...");
expect(wrapper.findComponent(LoadingSpinner).props("size")).toBe("xs");
}, state);
});

it("opens dialog on click download button", async () => {
Expand All @@ -168,32 +192,33 @@ describe("SensitivitySummaryDownload", () => {
});

it("commits user download filename change", () => {
const wrapper = getWrapper();
const downloadOutput = wrapper.findComponent(DownloadOutput);
downloadOutput.vm.$emit("update:userFileName", "newFile.xlsx");
expect(mockSetUserSummaryDownloadFileName).toHaveBeenCalledTimes(1);
expect(mockSetUserSummaryDownloadFileName.mock.calls[0][1]).toBe("newFile.xlsx");
testForSensAndMultiSens((wrapper) => {
const downloadOutput = wrapper.findComponent(DownloadOutput);
downloadOutput.vm.$emit("update:userFileName", "newFile.xlsx");
expect(mockSetUserSummaryDownloadFileName).toHaveBeenCalledTimes(1);
expect(mockSetUserSummaryDownloadFileName.mock.calls[0][1]).toBe("newFile.xlsx");
});
});

it("verifies end time and dispatches action on download output emit", () => {
const wrapper = getWrapper();
const downloadOutput = wrapper.findComponent(DownloadOutput);
downloadOutput.vm.$emit("download", { fileName: "test.xlsx" });
// should have updated plot settings time to run end time
expect(mockSetPlotTime).toHaveBeenCalledTimes(1);
expect(mockSetPlotTime.mock.calls[0][1]).toBe(100);
expect(mockDownloadSummary.mock.calls[0][1]).toBe("test.xlsx");
testForSensAndMultiSens((wrapper) => {
const downloadOutput = wrapper.findComponent(DownloadOutput);
downloadOutput.vm.$emit("download", {fileName: "test.xlsx"});
// should have updated plot settings time to run end time
expect(mockSetPlotTime).toHaveBeenCalledTimes(1);
expect(mockSetPlotTime.mock.calls[0][1]).toBe(100);
expect(mockDownloadSummary.mock.calls[0][1]).toBe("test.xlsx");
});
});

it("closes output dialog on close emit", async () => {
const wrapper = getWrapper();
await wrapper.find("#download-summary-btn").trigger("click");
const downloadOutput = wrapper.findComponent(DownloadOutput);
expect(downloadOutput.props().open).toBe(true);
downloadOutput.vm.$emit("close");
await nextTick();
expect(downloadOutput.props().open).toBe(false);
await asyncTestForSensAndMultiSens(async (wrapper) => {
await wrapper.find("#download-summary-btn").trigger("click");
const downloadOutput = wrapper.findComponent(DownloadOutput);
expect(downloadOutput.props().open).toBe(true);
downloadOutput.vm.$emit("close");
await nextTick();
expect(downloadOutput.props().open).toBe(false);
});
});

// TODO: test all for multisens
});

0 comments on commit 25aa128

Please sign in to comment.