Skip to content

Commit

Permalink
store tests
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Kusumgar committed Nov 5, 2024
1 parent 5c238b8 commit 80b12d9
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 254 deletions.
164 changes: 81 additions & 83 deletions app/static/tests/unit/store/appState/actions.test.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/static/tests/unit/store/appState/getters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppStateGetter, getters } from "../../../../src/app/store/appState/getters";
import { AppStateGetter, getters } from "../../../../src/store/appState/getters";
import { mockBasicState } from "../../../mocks";

describe("AppState getters", () => {
Expand Down
8 changes: 4 additions & 4 deletions app/static/tests/unit/store/appState/mutations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { appStateMutations } from "../../../../src/app/store/appState/mutations";
import { VisualisationTab } from "../../../../src/app/store/appState/state";
import { Language } from "../../../../src/app/types/languageTypes";
import { appStateMutations } from "../../../../src/store/appState/mutations";
import { VisualisationTab } from "../../../../src/store/appState/state";
import { Language } from "../../../../src/types/languageTypes";
import { mockBasicState } from "../../../mocks";

describe("AppState mutations", () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("AppState mutations", () => {
});

it("clears queued state upload", () => {
const spyClearInterval = jest.spyOn(window, "clearInterval");
const spyClearInterval = vi.spyOn(window, "clearInterval");
const state = mockBasicState({ queuedStateUploadIntervalId: 99 });
appStateMutations.ClearQueuedStateUpload(state);
expect(state.queuedStateUploadIntervalId).toBe(-1);
Expand Down
122 changes: 122 additions & 0 deletions app/static/tests/unit/store/baseSensitivity/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { actions, BaseSensitivityAction } from "@/store/sensitivity/actions";
import { BaseSensitivityMutation } from "@/store/sensitivity/mutations";
import { nextTick } from "vue";

const {
mockDownload,
mockWodinSensitivitySummaryDownload
} = vi.hoisted(() => {
const mockDownload = vi.fn();
return {
mockDownload,
mockWodinSensitivitySummaryDownload: vi.fn().mockReturnValue({ download: mockDownload })
}
});

vi.mock("../../../../src/excel/wodinSensitivitySummaryDownload", () => ({
WodinSensitivitySummaryDownload: mockWodinSensitivitySummaryDownload
}))

describe("BaseSensitivity actions", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("ComputeNext dispatches call to itself if batch is not complete", async () => {
const commit = vi.fn();
const dispatch = vi.fn();
const state = {
result: {
inputs: {},
batch: {}
}
};
const rootState = {
run: {
runRequired: true
}
};
const batch = {
compute: () => false
};
vi.useFakeTimers();
(actions[BaseSensitivityAction.ComputeNext] as any)(
{
state,
rootState,
commit,
dispatch
},
batch
);
vi.advanceTimersByTime(1);
vi.useRealTimers();
expect(commit).toHaveBeenCalledTimes(1);
expect(commit.mock.calls[0][0]).toBe(BaseSensitivityMutation.SetResult);
expect(commit.mock.calls[0][1]).toStrictEqual({ ...state.result, batch });
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch.mock.calls[0][0]).toBe(BaseSensitivityAction.ComputeNext);
expect(dispatch.mock.calls[0][1]).toBe(batch);
});

it("ComputeNext commits running false, if batch is complete", async () => {
const commit = vi.fn();
const dispatch = vi.fn();
const state = {
result: {
inputs: {},
batch: {}
}
};
const rootState = {
run: {
runRequired: true
}
};
const batch = {
compute: () => true
};
(actions[BaseSensitivityAction.ComputeNext] as any)(
{
state,
rootState,
commit,
dispatch
},
batch
);
expect(commit).toHaveBeenCalledTimes(2);
expect(commit.mock.calls[0][0]).toBe(BaseSensitivityMutation.SetResult);
expect(commit.mock.calls[0][1]).toStrictEqual({ ...state.result, batch });
expect(commit.mock.calls[1][0]).toBe(BaseSensitivityMutation.SetRunning);
expect(commit.mock.calls[1][1]).toBe(false);
await nextTick();
expect(dispatch).toHaveBeenCalledTimes(0);
});

it("downloads sensitivity summary", async () => {
const commit = vi.fn();
const state = {
result: {}
};
const context = { commit, state };
const payload = "myFile.xlsx";
vi.useFakeTimers();
(actions[BaseSensitivityAction.DownloadSummary] as any)(context, payload);
expect(commit).toHaveBeenCalledTimes(1);
expect(commit.mock.calls[0][0]).toBe(BaseSensitivityMutation.SetDownloading);
expect(commit.mock.calls[0][1]).toBe(true);
vi.advanceTimersByTime(5);
vi.useRealTimers();

expect(mockWodinSensitivitySummaryDownload).toHaveBeenCalledOnce();
expect(mockWodinSensitivitySummaryDownload.mock.calls[0][0]).toBe(context);
expect(mockWodinSensitivitySummaryDownload.mock.calls[0][1]).toBe("myFile.xlsx");
expect(mockDownload).toHaveBeenCalledTimes(1);
expect(mockDownload.mock.calls[0][0]).toBe(state.result);

expect(commit).toHaveBeenCalledTimes(2);
expect(commit.mock.calls[1][0]).toBe(BaseSensitivityMutation.SetDownloading);
expect(commit.mock.calls[1][1]).toBe(false);
});
});
8 changes: 3 additions & 5 deletions app/static/tests/unit/store/basic/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { localStorageManager } from "../../../../src/app/localStorageManager";

jest.mock("../../../../src/app/utils", () => {
vi.mock("../../../../src/utils", () => {
return {
newUid: jest.fn().mockReturnValue("12345")
newUid: vi.fn().mockReturnValue("12345")
};
});

describe("basic", () => {
it("generates and saves sessionId", async () => {
const { storeOptions } = await import("../../../../src/app/store/basic/basic");
const { storeOptions } = await import("../../../../src/store/basic/basic");
const state = storeOptions.state as any;
expect(state.sessionId).toBe("12345");
});
Expand Down
15 changes: 6 additions & 9 deletions app/static/tests/unit/store/multiSensitivity/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { actions, MultiSensitivityAction } from "../../../../src/app/store/multiSensitivity/actions";
import { actions, MultiSensitivityAction } from "../../../../src/store/multiSensitivity/actions";
import {
defaultAdvanced,
mockBatch,
Expand All @@ -8,15 +8,12 @@ import {
rootGetters,
testCommonRunSensitivity
} from "../sensitivity/actions.test";
import { AppType } from "../../../../src/app/store/appState/state";
import { BaseSensitivityMutation } from "../../../../src/app/store/sensitivity/mutations";
import { SensitivityAction } from "../../../../src/app/store/sensitivity/actions";

jest.mock("../../../../src/app/excel/wodinSensitivitySummaryDownload");
import { AppType } from "../../../../src/store/appState/state";
import { BaseSensitivityMutation } from "../../../../src/store/sensitivity/mutations";

describe("multiSensitivity actions", () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

testCommonRunSensitivity(actions[MultiSensitivityAction.RunMultiSensitivity]);
Expand All @@ -33,8 +30,8 @@ describe("multiSensitivity actions", () => {
batchPars: mockBatchPars
};

const commit = jest.fn();
const dispatch = jest.fn();
const commit = vi.fn();
const dispatch = vi.fn();

(actions[MultiSensitivityAction.RunMultiSensitivity] as any)({
rootState,
Expand Down
10 changes: 5 additions & 5 deletions app/static/tests/unit/store/multiSensitivity/getters.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SensitivityScaleType, SensitivityVariationType } from "../../../../src/app/store/sensitivity/state";
import { SensitivityScaleType, SensitivityVariationType } from "../../../../src/store/sensitivity/state";
import { mockBatchParsDisplace, mockBatchParsRange, mockMultiSensitivityState } from "../../../mocks";
import { getters } from "../../../../src/app/store/multiSensitivity/getters";
import { BaseSensitivityGetter } from "../../../../src/app/store/sensitivity/getters";
import { getters } from "../../../../src/store/multiSensitivity/getters";
import { BaseSensitivityGetter } from "../../../../src/store/sensitivity/getters";

describe("MultiSensitivity getters", () => {
const paramSettings = [
Expand Down Expand Up @@ -45,8 +45,8 @@ describe("MultiSensitivity getters", () => {
}
} as any;

const mockSpyDisplace = jest.spyOn(odinRunnerOde, "batchParsDisplace");
const mockSpyRange = jest.spyOn(odinRunnerOde, "batchParsRange");
const mockSpyDisplace = vi.spyOn(odinRunnerOde, "batchParsDisplace");
const mockSpyRange = vi.spyOn(odinRunnerOde, "batchParsRange");

const result = getters[BaseSensitivityGetter.batchPars](state, getters, rootState, {} as any);
expect(result).toStrictEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockMultiSensitivityState } from "../../../mocks";
import { mutations, MultiSensitivityMutation } from "../../../../src/app/store/multiSensitivity/mutations";
import { mutations, MultiSensitivityMutation } from "../../../../src/store/multiSensitivity/mutations";

describe("MultiSensitivity mutations", () => {
it("Sets parameter settings", () => {
Expand Down
Loading

0 comments on commit 80b12d9

Please sign in to comment.