Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Sep 7, 2023
1 parent 25aa128 commit 3965fd9
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 51 deletions.
19 changes: 14 additions & 5 deletions app/static/src/app/components/mixins/baseSensitivity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { Store } from "vuex";
import { computed } from "vue";
import {Store} from "vuex";
import {computed, ComputedRef, WritableComputedRef} from "vue";
import { AppState } from "../../store/appState/state";
import { ModelGetter } from "../../store/model/getters";
import userMessages from "../../userMessages";
import { anyTrue } from "../../utils";
import {sensitivityUpdateRequiredExplanation, verifyValidPlotSettingsTime} from "../sensitivity/support";
import { Dict } from "../../types/utilTypes";
import {BaseSensitivityMutation} from "../../store/sensitivity/mutations";
import {BaseSensitivityAction, SensitivityAction} from "../../store/sensitivity/actions";
import {BaseSensitivityAction} from "../../store/sensitivity/actions";

export default (store: Store<AppState>, multiSensitivity: boolean) => {
export interface BaseSensitivityMixin {
sensitivityPrerequisitesReady: ComputedRef<boolean>,
updateMsg: ComputedRef<string>,
downloading: ComputedRef<boolean>,
canDownloadSummary: ComputedRef<boolean>,
downloadSummaryUserFileName: WritableComputedRef<string>,
downloadSummary: (payload: { fileName: string }) => void
}

export default (store: Store<AppState>, multiSensitivity: boolean): BaseSensitivityMixin => {
const namespace = multiSensitivity ? "multiSensitivity" : "sensitivity";

const hasRunner = computed(() => store.getters[`model/${ModelGetter.hasRunner}`]);
Expand Down Expand Up @@ -42,7 +51,7 @@ export default (store: Store<AppState>, multiSensitivity: boolean) => {
const downloading = computed(() => sensModule.downloading);

// only allow download if update not required, and if we have run sensitivity
const canDownloadSummary = computed(() => !updateMsg.value && sensModule.result?.batch);
const canDownloadSummary = computed(() => !updateMsg.value && !!sensModule.result?.batch);

const downloadSummaryUserFileName = computed({
get: () => sensModule.userSummaryDownloadFileName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as XLSX from "xlsx";
import { WodinExcelDownload } from "./wodinExcelDownload";
import {BaseSensitivityState, SensitivityPlotExtreme, SensitivityPlotExtremePrefix} from "../store/sensitivity/state";
import { SensitivityPlotExtreme, SensitivityPlotExtremePrefix} from "../store/sensitivity/state";
import { OdinUserType, OdinUserTypeSeriesSet } from "../types/responseTypes";
import {OdinSensitivityResult} from "../types/wrapperTypes";

Expand Down
101 changes: 100 additions & 1 deletion app/static/tests/unit/components/mixins/baseSensitivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import Vuex from "vuex";
import { ModelState } from "../../../../src/app/store/model/state";
import baseSensitivity from "../../../../src/app/components/mixins/baseSensitivity";
import baseSensitivity, {BaseSensitivityMixin} from "../../../../src/app/components/mixins/baseSensitivity";
import { BaseSensitivityState } from "../../../../src/app/store/sensitivity/state";
import { noSensitivityUpdateRequired } from "../../../../src/app/store/sensitivity/sensitivity";
import { AppState } from "../../../../src/app/store/appState/state";
import {BaseSensitivityMutation} from "../../../../src/app/store/sensitivity/mutations";
import {nextTick} from "vue";
import {BaseSensitivityAction} from "../../../../src/app/store/sensitivity/actions";
import mock = jest.mock;

describe("baseSensitivity mixin", () => {
const mockSensSetUserSummaryDownloadFileName = jest.fn();
const mockMultiSensSetUserSummaryDownloadFileName = jest.fn();

const mockSensDownloadSummary = jest.fn();
const mockMultiSensDownloadSummary = jest.fn();

const getStore = (hasRunner = true, modelState: Partial<ModelState> = {},
sensitivityState: Partial<BaseSensitivityState> = {},
multiSensitivityState: Partial<BaseSensitivityState> = {}) => {
Expand Down Expand Up @@ -34,6 +44,13 @@ describe("baseSensitivity mixin", () => {
}
},
...multiSensitivityState
},
mutations: {
[BaseSensitivityMutation.SetUserSummaryDownloadFileName]:
mockMultiSensSetUserSummaryDownloadFileName
},
actions: {
[BaseSensitivityAction.DownloadSummary]: mockSensDownloadSummary
}
},
sensitivity: {
Expand All @@ -46,12 +63,24 @@ describe("baseSensitivity mixin", () => {
}
},
...sensitivityState
},
mutations: {
[BaseSensitivityMutation.SetUserSummaryDownloadFileName]:
mockSensSetUserSummaryDownloadFileName
},
actions: {
[BaseSensitivityAction.DownloadSummary]: mockSensDownloadSummary
}
}
} as any
});
};

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


it("sensitivityPrerequisitesReady returns true when all prerequisites have been met", () => {
const store = getStore();
const sut = baseSensitivity(store, false);
Expand Down Expand Up @@ -118,4 +147,74 @@ describe("baseSensitivity mixin", () => {
"Plot is out of date: model code has been recompiled. Run Sensitivity to update.",
"Status is out of date: model code has been recompiled. Run Multi-sensitivity to update.");
});

const testForSensAndMultiSens = (state: Partial<BaseSensitivityState>, test: (mixin: BaseSensitivityMixin) => void) => {
let store = getStore(true, {}, state, {});
test(baseSensitivity(store, false));
store = getStore(true, {}, {}, state);
test(baseSensitivity(store, true));
};

it("returns downloading", () => {
testForSensAndMultiSens({downloading: true}, (mixin) => {
expect(mixin.downloading.value).toBe(true);
});
testForSensAndMultiSens({}, (mixin) => {
expect(mixin.downloading.value).toBe(false);
});
});

it("can download summary when no update required and batch exists", () => {
testForSensAndMultiSens({}, (mixin) => {
expect(mixin.canDownloadSummary.value).toBe(true);
});
});

it("cannot download summary when update required", () => {
const state = {
sensitivityUpdateRequired: {
...noSensitivityUpdateRequired(),
modelChanged: true
}
};
testForSensAndMultiSens(state, (mixin) => {
expect(mixin.canDownloadSummary.value).toBe(false);
});
});

it("cannot download summary when no batch exists", () => {
const state = {
result: {}
} as any;
testForSensAndMultiSens(state, (mixin) => {
expect(mixin.canDownloadSummary.value).toBe(false);
});
});

it("returns download summary user file name", () => {
const state = { userSummaryDownloadFileName: "test.xlsx" };
testForSensAndMultiSens(state, (mixin) => {
expect(mixin.downloadSummaryUserFileName.value).toBe("test.xlsx");
});
});

it("writes download summary user file name", () => {
const sensSut = baseSensitivity(getStore(), false);
sensSut.downloadSummaryUserFileName.value = "new.xlsx";
expect(mockSensSetUserSummaryDownloadFileName.mock.calls[0][1]).toBe("new.xlsx");

const multiSensSut = baseSensitivity(getStore(), true);
multiSensSut.downloadSummaryUserFileName.value = "newMulti.xlsx";
expect(mockMultiSensSetUserSummaryDownloadFileName.mock.calls[0][1]).toBe("newMulti.xlsx");
});

it("downloadSummary dispatches action", () => {
const sensSut = baseSensitivity(getStore(), false);
sensSut.downloadSummary({ fileName: "test.xlsx" });
expect(mockSensDownloadSummary.mock.calls[0][1]).toBe("test.xlsx");

const multiSensSut = baseSensitivity(getStore(), true);
multiSensSut.downloadSummary({ fileName: "testMulti.xlsx" });
expect(mockMultiSensDownloadSummary.mock.calls[0][1]).toBe("testMulti.xlsx");
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { shallowMount } from "@vue/test-utils";
import Vuex from "vuex";
import MultiSensitivityTab from "../../../../src/app/components/multiSensitivity/MultiSensitivityTab.vue";
import { AppState } from "../../../../src/app/store/appState/state";
import {AppState, AppType} from "../../../../src/app/store/appState/state";
import LoadingButton from "../../../../src/app/components/LoadingButton.vue";
import { ModelState } from "../../../../src/app/store/model/state";
import { MultiSensitivityAction } from "../../../../src/app/store/multiSensitivity/actions";
import ActionRequiredMessage from "../../../../src/app/components/ActionRequiredMessage.vue";
import { MultiSensitivityState } from "../../../../src/app/store/multiSensitivity/state";
import ErrorInfo from "../../../../src/app/components/ErrorInfo.vue";
import SensitivitySummaryDownload from "../../../../src/app/components/sensitivity/SensitivitySummaryDownload.vue";

describe("MultiSensitivityTab", () => {
const mockRunMultiSensitivity = jest.fn();
Expand Down Expand Up @@ -141,4 +142,11 @@ describe("MultiSensitivityTab", () => {
});
expect(wrapper.findComponent(ErrorInfo).props("error")).toStrictEqual(error);
});

it("renders SensitivitySummaryDownload", () => {
const wrapper = getWrapper();
const download = wrapper.findComponent(SensitivitySummaryDownload);
expect(download.props("multiSensitivity")).toBe(true);
expect(download.props("downloadType")).toBe("Multi-sensitivity Summary");
});
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {mount, shallowMount} from "@vue/test-utils";
import {shallowMount} from "@vue/test-utils";
import Vuex from "vuex";
import { nextTick } from "vue";
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 { BaseSensitivityGetter, SensitivityGetter } from "../../../../src/app/store/sensitivity/getters";
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 {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 SensitivitySummaryDownload from "../../../../src/app/components/sensitivity/SensitivitySummaryDownload.vue";

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

Expand Down Expand Up @@ -145,6 +145,13 @@ describe("SensitivityTab", () => {
expect(wrapper.findComponent(SensitivityTracesPlot).exists()).toBe(false);
});

it("renders SensitivitySummaryDownload", () => {
const wrapper = getWrapper(AppType.Basic);
const download = wrapper.findComponent(SensitivitySummaryDownload);
expect(download.props("multiSensitivity")).toBe(false);
expect(download.props("downloadType")).toBe("Sensitivity Summary");
});

it("renders error", () => {
const testError = { error: "Test Error", detail: "test error detail" };
const sensitivityState = {
Expand Down
Loading

0 comments on commit 3965fd9

Please sign in to comment.