Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mrc-5957 vitest pt 6 #224

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions app/static/tests/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import MockAdapter from "axios-mock-adapter";
import axios from "axios";
import { BasicState } from "../src/app/store/basic/state";
import { FitState } from "../src/app/store/fit/state";
import { StochasticState } from "../src/app/store/stochastic/state";
import { BasicState } from "../src/store/basic/state";
import { FitState } from "../src/store/fit/state";
import { StochasticState } from "../src/store/stochastic/state";
import {
AdvancedOptions,
BatchPars,
Expand All @@ -11,27 +11,27 @@ import {
ResponseSuccess,
VaryingPar,
WodinError
} from "../src/app/types/responseTypes";
import { ModelState } from "../src/app/store/model/state";
import { AdvancedComponentType, RunState } from "../src/app/store/run/state";
import { CodeState } from "../src/app/store/code/state";
import { FitDataState } from "../src/app/store/fitData/state";
import { AppType, VisualisationTab } from "../src/app/store/appState/state";
import { ModelFitState } from "../src/app/store/modelFit/state";
} from "../src/types/responseTypes";
import { ModelState } from "../src/store/model/state";
import { AdvancedComponentType, RunState } from "../src/store/run/state";
import { CodeState } from "../src/store/code/state";
import { FitDataState } from "../src/store/fitData/state";
import { AppType, VisualisationTab } from "../src/store/appState/state";
import { ModelFitState } from "../src/store/modelFit/state";
import {
SensitivityPlotExtreme,
SensitivityPlotType,
SensitivityScaleType,
SensitivityState,
SensitivityVariationType
} from "../src/app/store/sensitivity/state";
import { VersionsState } from "../src/app/store/versions/state";
import { defaultGraphSettings, GraphsState } from "../src/app/store/graphs/state";
} from "../src/store/sensitivity/state";
import { VersionsState } from "../src/store/versions/state";
import { GraphsState, defaultGraphSettings } from "../src/store/graphs/state";
import { LanguageState } from "../translationPackage/store/state";
import { Language } from "../src/app/types/languageTypes";
import { noSensitivityUpdateRequired } from "../src/app/store/sensitivity/sensitivity";
import { MultiSensitivityState } from "../src/app/store/multiSensitivity/state";
import { SessionsState } from "../src/app/store/sessions/state";
import { Language } from "../src/types/languageTypes";
import { noSensitivityUpdateRequired } from "../src/store/sensitivity/sensitivity";
import { MultiSensitivityState } from "../src/store/multiSensitivity/state";
import { SessionsState } from "../src/store/sessions/state";

export const mockAxios = new MockAdapter(axios);

Expand Down Expand Up @@ -377,12 +377,12 @@ export const mockBatchParsDisplace = (

export const mockRunnerOde = () => {
return {
wodinRun: jest.fn((odin, pars, start, end) => "test solution" as any)
wodinRun: vi.fn((odin, pars, start, end) => "test solution" as any)
} as any;
};

export const mockRunnerDiscrete = () => {
return {
wodinRunDiscrete: jest.fn(() => "test discrete result" as any)
wodinRunDiscrete: vi.fn(() => "test discrete result" as any)
} as any;
};
2 changes: 1 addition & 1 deletion app/static/tests/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VueWrapper } from "@vue/test-utils";
import WodinPanels from "../src/app/components/WodinPanels.vue";
import WodinPanels from "../src/components/WodinPanels.vue";

export const fileTimeout = 20;

Expand Down
84 changes: 42 additions & 42 deletions app/static/tests/unit/apiService.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from "../../src/app/apiService";
import { api } from "../../src/apiService";
import { mockAxios, mockError, mockFailure, mockSuccess, mockBasicState } from "../mocks";
import { freezer } from "../../src/app/utils";
import Mock = jest.Mock;
import { freezer } from "../../src/utils";
import { Mock } from "vitest";

const BASE_URL = "http://localhost:3000";
const rootState = mockBasicState({ baseUrl: BASE_URL });
Expand All @@ -11,19 +11,19 @@ describe("ApiService", () => {
const TEST_BODY = "test body";

beforeEach(() => {
console.log = jest.fn();
console.warn = jest.fn();
console.log = vi.fn();
console.warn = vi.fn();
mockAxios.reset();
});

afterEach(() => {
(console.log as jest.Mock).mockClear();
(console.warn as jest.Mock).mockClear();
jest.clearAllMocks();
(console.log as Mock).mockClear();
(console.warn as Mock).mockClear();
vi.clearAllMocks();
});

const expectNoErrorHandlerMsgLogged = () => {
expect((console.warn as jest.Mock).mock.calls[0][0]).toBe(
expect((console.warn as Mock).mock.calls[0][0]).toBe(
`No error handler registered for request ${TEST_ROUTE}.`
);
};
Expand All @@ -48,27 +48,27 @@ describe("ApiService", () => {
it("console logs error on get", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("some error message"));

await api({ commit: jest.fn(), rootState } as any).get(TEST_ROUTE);
await api({ commit: vi.fn(), rootState } as any).get(TEST_ROUTE);

expectNoErrorHandlerMsgLogged();

expect((console.log as jest.Mock).mock.calls[0][0].errors[0].detail).toBe("some error message");
expect((console.log as Mock).mock.calls[0][0].errors[0].detail).toBe("some error message");
});

it("console logs error on post", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(500, mockFailure("some error message"));

await api({ commit: jest.fn(), rootState } as any).post(TEST_ROUTE, TEST_BODY);
await api({ commit: vi.fn(), rootState } as any).post(TEST_ROUTE, TEST_BODY);

expectNoErrorHandlerMsgLogged();

expect((console.log as jest.Mock).mock.calls[0][0].errors[0].detail).toBe("some error message");
expect((console.log as Mock).mock.calls[0][0].errors[0].detail).toBe("some error message");
});

it("commits the the first error message to errors module by default on get", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("some error message"));

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

await api({ commit, rootState } as any).get(TEST_ROUTE);

Expand All @@ -79,7 +79,7 @@ describe("ApiService", () => {
it("commits the the first error message to errors module by default on post", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(500, mockFailure("some error message"));

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

await api({ commit, rootState } as any).post(TEST_ROUTE, TEST_BODY);

Expand All @@ -95,7 +95,7 @@ describe("ApiService", () => {
};
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, failure);

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

await api({ commit, rootState } as any).get(TEST_ROUTE);

Expand All @@ -111,7 +111,7 @@ describe("ApiService", () => {
};
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(500, failure);

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

await api({ commit, rootState } as any).post(TEST_ROUTE, TEST_BODY);

Expand All @@ -122,7 +122,7 @@ describe("ApiService", () => {
it("commits the first error with the specified type if well formatted on get", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("some error message"));

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

await api({ commit, rootState } as any)
.withError("TEST_TYPE")
Expand All @@ -135,7 +135,7 @@ describe("ApiService", () => {
it("commits the first error with the specified type if well formatted on post", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(500, mockFailure("some error message"));

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

await api({ commit, rootState } as any)
.withError("TEST_TYPE")
Expand All @@ -148,7 +148,7 @@ describe("ApiService", () => {
it("commits the error type if the error detail is missing on get", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure(null as any));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withError("TEST_TYPE")
.get(TEST_ROUTE);
Expand All @@ -160,7 +160,7 @@ describe("ApiService", () => {
it("commits the error type if the error detail is missing on post", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(500, mockFailure(null as any));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withError("TEST_TYPE")
.post(TEST_ROUTE, TEST_BODY);
Expand All @@ -172,7 +172,7 @@ describe("ApiService", () => {
it("commits the success response with the specified type on get", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess("test data"));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.get(TEST_ROUTE);
Expand All @@ -185,7 +185,7 @@ describe("ApiService", () => {
it("commits the success response with the specified type on post", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(200, mockSuccess("test data"));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.post(TEST_ROUTE, TEST_BODY);
Expand All @@ -198,7 +198,7 @@ describe("ApiService", () => {
it("commits the success response with the specified type with root true", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess("test data"));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE", true)
.get(TEST_ROUTE);
Expand All @@ -211,7 +211,7 @@ describe("ApiService", () => {
it("commits the error response with the specified type with root true", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("TEST ERROR"));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withError("TEST_TYPE", true)
.get(TEST_ROUTE);
Expand All @@ -224,7 +224,7 @@ describe("ApiService", () => {
it("handles exception thrown on commit success response by adding expected error", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess("TEST SUCCESS"));

const commit = jest.fn().mockImplementation((type: string) => {
const commit = vi.fn().mockImplementation((type: string) => {
if (type === "TEST_TYPE") {
throw new Error("Test Success Mutation Exception");
}
Expand All @@ -249,7 +249,7 @@ describe("ApiService", () => {
it("handles exception thrown on commit error response by adding expected error", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("TEST FAILURE"));

const commit = jest.fn().mockImplementation((type: string) => {
const commit = vi.fn().mockImplementation((type: string) => {
if (type === "TEST_TYPE") {
throw new Error("Test Error Mutation Exception");
}
Expand All @@ -274,7 +274,7 @@ describe("ApiService", () => {
it("get returns the response object", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess("TEST"));

const commit = jest.fn();
const commit = vi.fn();
const response = await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.get(TEST_ROUTE);
Expand All @@ -285,7 +285,7 @@ describe("ApiService", () => {
it("post returns the response object", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(200, mockSuccess("TEST"));

const commit = jest.fn();
const commit = vi.fn();
const response = await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.post(TEST_ROUTE, TEST_BODY);
Expand All @@ -295,7 +295,7 @@ describe("ApiService", () => {

it("post sets default Content-Type header", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(200, mockSuccess("TEST"));
const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.post(TEST_ROUTE, TEST_BODY);
Expand All @@ -305,7 +305,7 @@ describe("ApiService", () => {

it("post sets requested Content-Type header", async () => {
mockAxios.onPost(`${BASE_URL}${TEST_ROUTE}`, TEST_BODY).reply(200, mockSuccess("TEST"));
const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.post(TEST_ROUTE, TEST_BODY, "text/plain");
Expand All @@ -318,9 +318,9 @@ describe("ApiService", () => {
const mockResponse = mockSuccess(fakeData);
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockResponse);

const spy = jest.spyOn(freezer, "deepFreeze");
const spy = vi.spyOn(freezer, "deepFreeze");

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.freezeResponse()
.withSuccess("TEST_TYPE")
Expand All @@ -336,9 +336,9 @@ describe("ApiService", () => {
const fakeData = { name: "d1" };
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess(fakeData));

const spy = jest.spyOn(freezer, "deepFreeze");
const spy = vi.spyOn(freezer, "deepFreeze");

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("TEST_TYPE")
.get(TEST_ROUTE);
Expand All @@ -350,7 +350,7 @@ describe("ApiService", () => {
});

async function expectCouldNotParseAPIResponseError() {
const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any).get(TEST_ROUTE);

expect(commit.mock.calls.length).toBe(1);
Expand Down Expand Up @@ -383,33 +383,33 @@ describe("ApiService", () => {
it("does nothing on error if ignoreErrors is true", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(500, mockFailure("some error message"));

const commit = jest.fn();
const commit = vi.fn();
await api({ commit, rootState } as any)
.withSuccess("whatever")
.ignoreErrors()
.get("/baseline/");

expect((console.warn as jest.Mock).mock.calls.length).toBe(0);
expect((console.warn as Mock).mock.calls.length).toBe(0);
expect(commit.mock.calls.length).toBe(0);
});

it("does not warn that no success handler if ignoring success", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess(true));

await api({ commit: jest.fn(), rootState } as any)
await api({ commit: vi.fn(), rootState } as any)
.ignoreSuccess()
.withError("whatever")
.get(TEST_ROUTE);

expect((console.warn as jest.Mock).mock.calls.length).toBe(0);
expect((console.warn as Mock).mock.calls.length).toBe(0);
});

it("warns if error and success handlers are not set if not ignoring", async () => {
mockAxios.onGet(`${BASE_URL}${TEST_ROUTE}`).reply(200, mockSuccess(true));

await api({ commit: jest.fn(), rootState } as any).get(TEST_ROUTE);
await api({ commit: vi.fn(), rootState } as any).get(TEST_ROUTE);

const warnings = (console.warn as jest.Mock).mock.calls;
const warnings = (console.warn as Mock).mock.calls;

expectNoErrorHandlerMsgLogged();
expect(warnings[1][0]).toBe(`No success handler registered for request ${TEST_ROUTE}.`);
Expand Down
Loading
Loading