From 7c6f1ef3776a9f907158cede7d1cb45e2a2b4a56 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:52:36 +0200 Subject: [PATCH] Convert tests to Typescript + refactor --- ...askMonitor.test.js => taskMonitor.test.ts} | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) rename client/src/composables/{taskMonitor.test.js => taskMonitor.test.ts} (70%) diff --git a/client/src/composables/taskMonitor.test.js b/client/src/composables/taskMonitor.test.ts similarity index 70% rename from client/src/composables/taskMonitor.test.js rename to client/src/composables/taskMonitor.test.ts index 596fcfdad687..ad3f3a446204 100644 --- a/client/src/composables/taskMonitor.test.js +++ b/client/src/composables/taskMonitor.test.ts @@ -1,26 +1,38 @@ -import axios from "axios"; -import MockAdapter from "axios-mock-adapter"; -import { useTaskMonitor } from "composables/taskMonitor"; import flushPromises from "flush-promises"; +import { useTaskMonitor } from "@/composables/taskMonitor"; +import { mockFetcher } from "@/schema/__mocks__"; + +jest.mock("@/schema"); + const PENDING_TASK_ID = "pending-fake-task-id"; const COMPLETED_TASK_ID = "completed-fake-task-id"; const FAILED_TASK_ID = "failed-fake-task-id"; const REQUEST_FAILED_TASK_ID = "request-failed-fake-task-id"; -describe("useTaskMonitor", () => { - let axiosMock; +function getMockedTaskStatus({ task_id }: { task_id: string }) { + switch (task_id) { + case PENDING_TASK_ID: + return { data: "PENDING", status: 200 }; - beforeEach(async () => { - axiosMock = new MockAdapter(axios); - }); + case COMPLETED_TASK_ID: + return { data: "SUCCESS", status: 200 }; - afterEach(async () => { - axiosMock.reset(); - }); + case FAILED_TASK_ID: + return { data: "FAILURE", status: 200 }; + case REQUEST_FAILED_TASK_ID: + throw new Error("Request failed"); + + default: + return { data: "UNKNOWN", status: 404 }; + } +} + +mockFetcher.path("/api/tasks/{task_id}/state").method("get").mock(getMockedTaskStatus); + +describe("useTaskMonitor", () => { it("should indicate the task is running when it is still pending", async () => { - axiosMock.onGet(`/api/tasks/${PENDING_TASK_ID}/state`).reply(200, "PENDING"); const { waitForTask, isRunning } = useTaskMonitor(); expect(isRunning.value).toBe(false); @@ -30,7 +42,6 @@ describe("useTaskMonitor", () => { }); it("should indicate the task is successfully completed when the state is SUCCESS", async () => { - axiosMock.onGet(`/api/tasks/${COMPLETED_TASK_ID}/state`).reply(200, "SUCCESS"); const { waitForTask, isRunning, isCompleted } = useTaskMonitor(); expect(isCompleted.value).toBe(false); @@ -41,7 +52,6 @@ describe("useTaskMonitor", () => { }); it("should indicate the task has failed when the state is FAILED", async () => { - axiosMock.onGet(`/api/tasks/${FAILED_TASK_ID}/state`).reply(200, "FAILURE"); const { waitForTask, isRunning, hasFailed } = useTaskMonitor(); expect(hasFailed.value).toBe(false); @@ -52,7 +62,6 @@ describe("useTaskMonitor", () => { }); it("should indicate the task status request failed when the request failed", async () => { - axiosMock.onGet(`/api/tasks/${REQUEST_FAILED_TASK_ID}/state`).reply(400, "UNKNOWN"); const { waitForTask, requestHasFailed } = useTaskMonitor(); expect(requestHasFailed.value).toBe(false);