Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Jun 26, 2024
1 parent eb9297a commit 5fdf149
Show file tree
Hide file tree
Showing 28 changed files with 520 additions and 336 deletions.
3 changes: 2 additions & 1 deletion app/static/src/app/excel/wodinModelOutputDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class WodinModelOutputDownload extends WodinExcelDownload {
tEnd: end,
nPoints: this._points
});
const selectedVariables = this._state.graphs.config.flatMap((c) => c.selectedVariables);
const selectedVariables = this._rootGetters[`graphs/${GraphsGetter.allSelectedVariables}`];
console.log(`selectedVariables are ${JSON.stringify(selectedVariables)}`)

const worksheet = WodinModelOutputDownload._generateModelledOutput(
selectedVariables,
Expand Down
26 changes: 16 additions & 10 deletions app/static/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SensitivityVariationType
} from "../src/app/store/sensitivity/state";
import { VersionsState } from "../src/app/store/versions/state";
import { GraphSettingsState } from "../src/app/store/graphs/state";
import { GraphsState } from "../src/app/store/graphs/state";
import { LanguageState } from "../translationPackage/store/state";
import { Language } from "../src/app/types/languageTypes";
import { noSensitivityUpdateRequired } from "../src/app/store/sensitivity/sensitivity";
Expand Down Expand Up @@ -72,8 +72,6 @@ export const mockModelState = (state: Partial<ModelState> = {}): ModelState => {
compileRequired: false,
paletteModel: null,
odinModelCodeError: null,
selectedVariables: [],
unselectedVariables: [],
...state
};
};
Expand Down Expand Up @@ -124,11 +122,19 @@ export const mockVersionsState = (states: Partial<VersionsState> = {}): Versions
};
};

export const mockGraphSettingsState = (state: Partial<GraphSettingsState> = {}): GraphSettingsState => {
export const mockGraphsState = (state: Partial<GraphsState> = {}): GraphsState => {
return {
logScaleYAxis: false,
lockYAxis: false,
yAxisRange: [0, 0],
config: [
{
selectedVariables: [],
unselectedVariables: []
}
],
settings: {
logScaleYAxis: false,
lockYAxis: false,
yAxisRange: [0, 0]
},
...state
};
};
Expand Down Expand Up @@ -229,7 +235,7 @@ export const mockBasicState = (state: Partial<BasicState> = {}): BasicState => {
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
versions: mockVersionsState(),
graphSettings: mockGraphSettingsState(),
graphs: mockGraphsState(),
configured: false,
persisted: true,
language: mockLanguageState(),
Expand Down Expand Up @@ -285,7 +291,7 @@ export const mockFitState = (state: Partial<FitState> = {}): FitState => {
multiSensitivity: mockMultiSensitivityState(),
modelFit: mockModelFitState(),
versions: mockVersionsState(),
graphSettings: mockGraphSettingsState(),
graphs: mockGraphsState(),
configured: false,
persisted: false,
language: mockLanguageState(),
Expand Down Expand Up @@ -319,7 +325,7 @@ export const mockStochasticState = (state: Partial<StochasticState> = {}): Stoch
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
versions: mockVersionsState(),
graphSettings: mockGraphSettingsState(),
graphs: mockGraphsState(),
configured: false,
persisted: false,
language: mockLanguageState(),
Expand Down
8 changes: 5 additions & 3 deletions app/static/tests/unit/components/basic/basicApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { expectLeftWodinTabs, expectRightWodinTabs } from "../../../testUtils";
import HelpTab from "../../../../src/app/components/help/HelpTab.vue";
import BasicApp from "../../../../src/app/components/basic/BasicApp.vue";
import { BasicState } from "../../../../src/app/store/basic/state";
import { mockBasicState, mockGraphSettingsState, mockModelState, mockSensitivityState } from "../../../mocks";
import { mockBasicState, mockGraphsState, mockModelState, mockSensitivityState } from "../../../mocks";
import WodinApp from "../../../../src/app/components/WodinApp.vue";
import WodinPanels from "../../../../src/app/components/WodinPanels.vue";
import OptionsTab from "../../../../src/app/components/options/OptionsTab.vue";
Expand All @@ -28,6 +28,7 @@ import { ModelAction } from "../../../../src/app/store/model/actions";
import { VisualisationTab } from "../../../../src/app/store/appState/state";
import { AppStateMutation } from "../../../../src/app/store/appState/mutations";
import { AppConfig } from "../../../../src/app/types/responseTypes";
import { getters as graphsGetters } from "../../../../src/app/store/graphs/getters";

const mockTooltipDirective = jest.fn();

Expand Down Expand Up @@ -64,9 +65,10 @@ describe("BasicApp", () => {
errors: []
}
},
graphSettings: {
graphs: {
namespaced: true,
state: mockGraphSettingsState()
state: mockGraphsState(),
getters: graphsGetters
}
}
});
Expand Down
26 changes: 18 additions & 8 deletions app/static/tests/unit/components/code/selectedVariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { shallowMount } from "@vue/test-utils";
import { BasicState } from "../../../../src/app/store/basic/state";
import SelectedVariables from "../../../../src/app/components/code/SelectedVariables.vue";
import { ModelAction } from "../../../../src/app/store/model/actions";
import {GraphsAction} from "../../../../src/app/store/graphs/actions";

describe("SelectedVariables", () => {
const mockUpdateSelectedVariables = jest.fn();
Expand All @@ -11,6 +12,19 @@ describe("SelectedVariables", () => {
const store = new Vuex.Store<BasicState>({
state: {} as any,
modules: {
graphs: {
namespaced: true,
state: {
config: [
{
selectedVariables: ["S", "R"],
}
]
},
actions: {
[GraphsAction.UpdateSelectedVariables]: mockUpdateSelectedVariables
}
},
model: {
namespaced: true,
state: {
Expand All @@ -19,15 +33,11 @@ describe("SelectedVariables", () => {
variables: ["S", "I", "R"]
}
},
selectedVariables: ["S", "R"],
paletteModel: {
S: "#ff0000",
I: "#00ff00",
R: "#0000ff"
}
},
actions: {
[ModelAction.UpdateSelectedVariables]: mockUpdateSelectedVariables
}
}
}
Expand Down Expand Up @@ -63,28 +73,28 @@ describe("SelectedVariables", () => {
const s = wrapper.findAll(".selected-variables-panel span.variable").at(0)!;
await s.trigger("click");
expect(mockUpdateSelectedVariables).toBeCalledTimes(1);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual(["R"]);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual({index: 0, selectedVariables: ["R"]});
});

it("clicking an unselected variables selects it", async () => {
const wrapper = getWrapper();
const i = wrapper.findAll(".selected-variables-panel span.variable").at(1)!;
await i.trigger("click");
expect(mockUpdateSelectedVariables).toBeCalledTimes(1);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual(["S", "R", "I"]);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual({index: 0, selectedVariables: ["S", "R", "I"]});

Check failure on line 84 in app/static/tests/unit/components/code/selectedVariables.test.ts

View workflow job for this annotation

GitHub Actions / test

This line has a length of 123. Maximum allowed is 120
});

it("clicking select all link selects all variables", async () => {
const wrapper = getWrapper();
wrapper.find("span#select-variables-all").trigger("click");
expect(mockUpdateSelectedVariables).toBeCalledTimes(1);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual(["S", "I", "R"]);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual({index: 0, selectedVariables: ["S", "I", "R"]});

Check failure on line 91 in app/static/tests/unit/components/code/selectedVariables.test.ts

View workflow job for this annotation

GitHub Actions / test

This line has a length of 123. Maximum allowed is 120
});

it("clicking select none link unselects all variables", async () => {
const wrapper = getWrapper();
wrapper.find("span#select-variables-none").trigger("click");
expect(mockUpdateSelectedVariables).toBeCalledTimes(1);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual([]);
expect(mockUpdateSelectedVariables.mock.calls[0][1]).toStrictEqual({index: 0, selectedVariables: []});
});
});
8 changes: 5 additions & 3 deletions app/static/tests/unit/components/fit/fitApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { FitState } from "../../../../src/app/store/fit/state";
import {
mockFitDataState,
mockFitState,
mockGraphSettingsState,
mockGraphsState,
mockModelFitState,
mockModelState,
mockSensitivityState
Expand All @@ -40,6 +40,7 @@ import { VisualisationTab } from "../../../../src/app/store/appState/state";
import { AppStateMutation } from "../../../../src/app/store/appState/mutations";
import { ModelFitGetter } from "../../../../src/app/store/modelFit/getters";
import { AppConfig } from "../../../../src/app/types/responseTypes";
import { getters as graphsGetters } from "../../../../src/app/store/graphs/getters";

function mockResizeObserver(this: any) {
this.observe = jest.fn();
Expand Down Expand Up @@ -86,9 +87,10 @@ describe("FitApp", () => {
errors: []
}
},
graphSettings: {
graphs: {
namespaced: true,
state: mockGraphSettingsState()
state: mockGraphsState(),
getters: graphsGetters
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions app/static/tests/unit/components/fit/fitTab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FitState } from "../../../../src/app/store/fit/state";
import ActionRequiredMessage from "../../../../src/app/components/ActionRequiredMessage.vue";
import LoadingSpinner from "../../../../src/app/components/LoadingSpinner.vue";
import FitPlot from "../../../../src/app/components/fit/FitPlot.vue";
import { mockFitState, mockGraphSettingsState } from "../../../mocks";
import { mockFitState, mockGraphsState } from "../../../mocks";
import { WodinError } from "../../../../src/app/types/responseTypes";
import ErrorInfo from "../../../../src/app/components/ErrorInfo.vue";

Expand Down Expand Up @@ -38,7 +38,7 @@ describe("Fit Tab", () => {
},
graphSettings: {
namespaced: true,
state: mockGraphSettingsState()
state: mockGraphsState()
},
modelFit: {
namespaced: true,
Expand Down
29 changes: 21 additions & 8 deletions app/static/tests/unit/components/mixins/baseSensitivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Vuex from "vuex";
import { nextTick } from "vue";
import { ModelState } from "../../../../src/app/store/model/state";
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 { BaseSensitivityAction } from "../../../../src/app/store/sensitivity/actions";
import { getters as graphGetters } from "../../../../src/app/store/graphs/getters";
import mock = jest.mock;
import {mockGraphsState} from "../../../mocks";

describe("baseSensitivity mixin", () => {
const mockSensSetUserSummaryDownloadFileName = jest.fn();
Expand All @@ -20,11 +21,19 @@ describe("baseSensitivity mixin", () => {
hasRunner = true,
modelState: Partial<ModelState> = {},
sensitivityState: Partial<BaseSensitivityState> = {},
multiSensitivityState: Partial<BaseSensitivityState> = {}
multiSensitivityState: Partial<BaseSensitivityState> = {},
selectedVariables: string[] = ["A"]
) => {
return new Vuex.Store<AppState>({
state: {} as any,
modules: {
graphs: {
namespaced: true,
state: mockGraphsState({
config: [ { selectedVariables, unselectedVariables: [] } ]
}),
getters: graphGetters
},
model: {
namespaced: true,
state: {
Expand Down Expand Up @@ -121,30 +130,32 @@ describe("baseSensitivity mixin", () => {
hasRunner: true,
modelState: Partial<ModelState>,
sensState: Partial<BaseSensitivityState>,
selectedVariables: string[],
expectedSensMsg: string,
expectedMultiSensMsg: string
expectedMultiSensMsg: string,
) => {
const sensStore = getStore(hasRunner, modelState, sensState);
const sensStore = getStore(hasRunner, modelState, sensState, {}, selectedVariables);
const sensSut = baseSensitivity(sensStore, false);
expect(sensSut.updateMsg.value).toBe(expectedSensMsg);
const multiSensStore = getStore(hasRunner, modelState, {}, sensState);
const multiSensStore = getStore(hasRunner, modelState, {}, sensState, selectedVariables);
const multiSensSut = baseSensitivity(multiSensStore, true);
expect(multiSensSut.updateMsg.value).toBe(expectedMultiSensMsg);
};

it("returns empty update message when no update required", () => {
expectUpdateMsgForSensAndMultiSens(true, {}, {}, "", "");
expectUpdateMsgForSensAndMultiSens(true, {}, {}, ["A"], "", "");
});

it("returns empty update message when update required, but there has been no run yet", () => {
expectUpdateMsgForSensAndMultiSens(true, { compileRequired: true }, { result: null }, "", "");
expectUpdateMsgForSensAndMultiSens(true, { compileRequired: true }, { result: null }, ["A"], "", "");
});

it("returns expected update message when compile required", () => {
expectUpdateMsgForSensAndMultiSens(
true,
{ compileRequired: true },
{},
["A"],
"Model code has been updated. Compile code and Run Sensitivity to update.",
"Model code has been updated. Compile code and Run Multi-sensitivity to update."
);
Expand All @@ -153,8 +164,9 @@ describe("baseSensitivity mixin", () => {
it("returns expected update message when there are no selected variables", () => {
expectUpdateMsgForSensAndMultiSens(
true,
{ selectedVariables: [] },
{},
{},
[],
"Please select at least one variable.",
"Please select at least one variable."
);
Expand All @@ -173,6 +185,7 @@ describe("baseSensitivity mixin", () => {
true,
{},
{ sensitivityUpdateRequired },
["A"],
"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."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ActionRequiredMessage from "../../../../src/app/components/ActionRequired
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";
import { getters as graphsGetters} from "../../../../src/app/store/graphs/getters";

describe("MultiSensitivityTab", () => {
const mockRunMultiSensitivity = jest.fn();
Expand All @@ -22,12 +23,22 @@ describe("MultiSensitivityTab", () => {
const store = new Vuex.Store<AppState>({
state: {} as any,
modules: {
graphs: {
namespaced: true,
state: {
config: [
{
selectedVariables: ["A"]
}
]
},
getters: graphsGetters
},
model: {
namespaced: true,
state: {
odin: {},
compileRequired: false,
selectedVariables: ["A"],
...modelState
},
getters: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ describe("GraphSettings", () => {
const getWrapper = (logScaleYAxis = true, lockYAxis = true) => {
const store = new Vuex.Store<BasicState>({
modules: {
graphSettings: {
graphs: {
namespaced: true,
state: {
logScaleYAxis,
lockYAxis
settings: {
logScaleYAxis,
lockYAxis
}
} as any,
mutations: {
[GraphsMutation.SetLogScaleYAxis]: mockSetLogScaleYAxis,
Expand Down
Loading

0 comments on commit 5fdf149

Please sign in to comment.