Skip to content

Commit

Permalink
more component unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Aug 14, 2023
1 parent 1d053e0 commit c50f20f
Showing 1 changed file with 127 additions and 11 deletions.
138 changes: 127 additions & 11 deletions app/static/tests/unit/components/sensitivity/sensitivityTab.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { mount, shallowMount } from "@vue/test-utils";
import {mount} from "@vue/test-utils";
import Vuex from "vuex";
import { ModelState } from "../../../../src/app/store/model/state";
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 { SensitivityGetter } from "../../../../src/app/store/sensitivity/getters";
import {SensitivityGetter} 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 {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 {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 {SensitivityMutation} from "../../../../src/app/store/sensitivity/mutations";
import DownloadOutput from "../../../../src/app/components/DownloadOutput.vue";
import {nextTick} from "vue";

jest.mock("plotly.js-basic-dist-min", () => {});

describe("SensitivityTab", () => {
const mockRunSensitivity = jest.fn();
const mockSetLoading = jest.fn();
const mockSetUserSummaryDownloadFileName = jest.fn();
const mockDownloadSummary = jest.fn();
const mockSetPlotTime = jest.fn();

const getWrapper = (appType = AppType.Basic, modelState: Partial<ModelState> = {},
sensitivityState: Partial<SensitivityState> = {}, batchPars: any = {}, hasRunner = true) => {
Expand All @@ -39,6 +44,12 @@ describe("SensitivityTab", () => {
[ModelGetter.hasRunner]: () => hasRunner
}
},
run: {
namespaced: true,
state: {
endTime: 100
}
},
sensitivity: {
namespaced: true,
state: {
Expand All @@ -57,21 +68,26 @@ describe("SensitivityTab", () => {
error: null
},
plotSettings: {
plotType: SensitivityPlotType.TraceOverTime
plotType: SensitivityPlotType.TraceOverTime,
time: null
},
paramSettings: {
numberOfRuns: 5
},
userSummaryDownloadFileName: "",
...sensitivityState
},
getters: {
[SensitivityGetter.batchPars]: () => batchPars
},
actions: {
[SensitivityAction.RunSensitivity]: mockRunSensitivity
[SensitivityAction.RunSensitivity]: mockRunSensitivity,
[SensitivityAction.DownloadSummary]: mockDownloadSummary
},
mutations: {
[SensitivityMutation.SetLoading]: mockSetLoading
[SensitivityMutation.SetLoading]: mockSetLoading,
[SensitivityMutation.SetUserSummaryDownloadFileName]: mockSetUserSummaryDownloadFileName,
[SensitivityMutation.SetPlotTime]: mockSetPlotTime
}
}
}
Expand Down Expand Up @@ -135,6 +151,73 @@ describe("SensitivityTab", () => {
expect(wrapper.findComponent(SensitivityTracesPlot).exists()).toBe(false);
});

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

// enabled if not downloading, and no update required, and batch result exists
const noUpdateRequiredReasons = {
modelChanged: false,
parameterValueChanged: false,
endTimeChanged: false,
sensitivityOptionsChanged: false,
numberOfReplicatesChanged: false
};
const enabledResult = {
inputs: {},
batch: {
solutions: [ "test solution" ] as any,
errors: []
},
error: null
};
const enabledState = {
downloading: false,
sensitivityUpdateRequired: noUpdateRequiredReasons,
result: enabledResult
} as any;
expectDownloadButtonEnabled(enabledState, true);

// disabled if downloading
expectDownloadButtonEnabled({ ...enabledState, downloading: true }, false);

// disabled if update required
expectDownloadButtonEnabled({
...enabledState,
sensitivityUpdateRequired: { modelChanged: true}
}, false);

// disabled if no batch result
expectDownloadButtonEnabled({
...enabledState,
result: {...enabledResult, batch: null}
}, false);
});

it("renders DownloadOutput as expected", () => {
const wrapper = getWrapper(AppType.Basic, {}, { userSummaryDownloadFileName: "test.xlsx" });
const downloadOutput = wrapper.findComponent(DownloadOutput);
expect(downloadOutput.props().open).toBe(false);
expect(downloadOutput.props().downloadType).toBe("Sensitivity Summary");
expect(downloadOutput.props().includePoints).toBe(false);
expect(downloadOutput.props().userFileName).toBe("test.xlsx");
});

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

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

it("renders error", () => {
const testError = { error: "Test Error", detail: "test error detail" };
const sensitivityState = {
Expand Down Expand Up @@ -269,4 +352,37 @@ describe("SensitivityTab", () => {
expect(mockRunSensitivity).toHaveBeenCalledTimes(1);
expect(mockSetLoading).toHaveBeenCalledTimes(1);
});

it("opens dialog on click download button", async () => {
const wrapper = getWrapper();
await wrapper.find("button#download-summary-btn").trigger("click");
expect(wrapper.findComponent(DownloadOutput).props().open).toBe(true);
});

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");
});

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.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);
});
});

0 comments on commit c50f20f

Please sign in to comment.