From 8abbd6609a6194a99f1552164fc71cc0688a31fc Mon Sep 17 00:00:00 2001 From: ChromaticPanic <78883122+ChromaticPanic@users.noreply.github.com> Date: Tue, 25 Jun 2024 04:33:28 +0000 Subject: [PATCH] fix tests --- src/common/api.ts | 4 +- src/common/index.ts | 8 ++ .../StorageDirectoryContainer.tsx | 5 +- .../directory_list/StorageDirectoryView.tsx | 4 +- .../tests/StorageDirectoryContainer.test.tsx | 6 +- .../body/microscope_feed/MicroscopeFeed.tsx | 9 +- src/root/body/body.test.tsx | 103 ++++++++++-------- src/root/body/body.tsx | 6 +- 8 files changed, 85 insertions(+), 60 deletions(-) diff --git a/src/common/api.ts b/src/common/api.ts index 13b056e1..9b0aa038 100755 --- a/src/common/api.ts +++ b/src/common/api.ts @@ -44,7 +44,9 @@ const handleAxios = async (request: { export const readAzureStorageDir = async ( backendUrl: string, uuid: string, -): Promise => { +): Promise<{ + [key: string]: number; +}> => { if (backendUrl === "" || backendUrl == null) { throw new ValueError("Backend URL is null or empty"); } diff --git a/src/common/index.ts b/src/common/index.ts index fc1ce409..1abae8b7 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -11,6 +11,10 @@ import { deleteAzureStorageDir, inferenceRequest, fetchModelMetadata, + requestClassList, + sendNegativeFeedback, + sendPositiveFeedback, + requestUUID, } from "./api"; import { loadToCanvas, @@ -42,4 +46,8 @@ export { loadResultsToCache, getLabelOccurrence, getScaledBounds, + requestClassList, + sendNegativeFeedback, + sendPositiveFeedback, + requestUUID, }; diff --git a/src/components/body/directory_list/StorageDirectoryContainer.tsx b/src/components/body/directory_list/StorageDirectoryContainer.tsx index c406cd11..5d35dc35 100644 --- a/src/components/body/directory_list/StorageDirectoryContainer.tsx +++ b/src/components/body/directory_list/StorageDirectoryContainer.tsx @@ -1,7 +1,7 @@ import StorageDirectoryView from "./StorageDirectoryView"; export interface params { - azureStorageDir: any; + azureStorageDir: { [key: string]: number }; curDir: string; setCurDir: React.Dispatch>; setCreateDirectoryOpen: React.Dispatch>; @@ -28,7 +28,8 @@ const StorageDirectoryContainer: React.FC = (props): JSX.Element => { return ( void; handleDelete: (folder: string) => void; diff --git a/src/components/body/directory_list/tests/StorageDirectoryContainer.test.tsx b/src/components/body/directory_list/tests/StorageDirectoryContainer.test.tsx index 8bcdfd4c..e38dd247 100644 --- a/src/components/body/directory_list/tests/StorageDirectoryContainer.test.tsx +++ b/src/components/body/directory_list/tests/StorageDirectoryContainer.test.tsx @@ -6,9 +6,9 @@ import StorageDirectoryContainer from "../StorageDirectoryContainer"; describe("StorageDirectoryContainer", () => { const mockProps = { azureStorageDir: { - testDir1: "testDir1", - testDir2: "testDir2", - testDir3: "testDir3", + testDir1: 0, + testDir2: 1, + testDir3: 2, }, curDir: "testDir", setCurDir: vi.fn(), diff --git a/src/components/body/microscope_feed/MicroscopeFeed.tsx b/src/components/body/microscope_feed/MicroscopeFeed.tsx index eaa65536..014ecbc9 100755 --- a/src/components/body/microscope_feed/MicroscopeFeed.tsx +++ b/src/components/body/microscope_feed/MicroscopeFeed.tsx @@ -28,7 +28,7 @@ import { requestClassList, sendNegativeFeedback, sendPositiveFeedback, -} from "../../../common/api"; +} from "../../../common"; import { FreeformBox, NegativeFeedbackForm } from "../feedback_form"; import { getUnscaledCoordinates } from "../../../common/imageutils"; interface MicroscopeFeedProps { @@ -144,10 +144,9 @@ const MicroscopeFeed = (props: MicroscopeFeedProps): JSX.Element => { const classList: ClassData[] = useMemo(() => { const classes: ClassData[] = []; - const getClasses = () => { - return requestClassList(backendUrl).then((response) => { - return response.seeds; - }); + const getClasses = async () => { + const response = await requestClassList(backendUrl); + return response.seeds; }; getClasses().then((data) => { for (let i = 0; i < data.length; i++) { diff --git a/src/root/body/body.test.tsx b/src/root/body/body.test.tsx index 0e7a3900..803cbe95 100755 --- a/src/root/body/body.test.tsx +++ b/src/root/body/body.test.tsx @@ -1,10 +1,66 @@ import { render, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import Body from "./body"; -import axios from "axios"; process.env.VITE_BACKEND_URL = "somebackendurl"; +vi.mock("../../common", async (importOriginal) => { + const mod = await importOriginal(); + return { + ...mod, + readAzureStorageDir: vi.fn(() => { + return Promise.resolve({ + testDir1: 0, + testDir2: 1, + testDir3: 2, + }); + }), + createAzureStorageDir: vi.fn(), + deleteAzureStorageDir: vi.fn(), + inferenceRequest: vi.fn(), + fetchModelMetadata: vi.fn(() => { + return Promise.resolve([ + { + created_by: "Wayne Gretzky", + creation_date: "2023-12-01", + dataset: "", + default: false, + description: + "trained using 6 seed images per image of 14of15 tagarno", + job_name: "neat_cartoon_k0y4m0vz", + model_name: "9000 Seed Detector", + models: ["m-14of15seeds-6seedsmag"], + pipeline_id: "123", + pipeline_name: "9000 Seed Detector", + version: "1", + }, + ]); + }), + requestClassList: vi.fn(() => { + return Promise.resolve({ + seeds: [ + { + seed_id: "1", + seed_name: "seed_name1", + }, + { + seed_id: "2", + seed_name: "seed_name2", + }, + { + seed_id: "3", + seed_name: "seed_name3", + }, + ], + }); + }), + requestUUID: vi.fn(() => { + return Promise.resolve({ + uuid: "1234", + }); + }), + }; +}); const mockProps = { windowSize: { width: 1000, @@ -40,51 +96,6 @@ Object.defineProperty(global.window, "alert", { value: vi.fn(), }); -vi.mock("../../common", async (importOriginal) => { - const mod = await importOriginal(); - return { - ...mod, - readAzureStorageDir: vi.fn(async () => { - return new Promise((resolve) => { - resolve(true); - }); - }), - createAzureStorageDir: vi.fn(), - deleteAzureStorageDir: vi.fn(), - inferenceRequest: vi.fn(), - requestModelMetadata: vi.fn(), - requestClassList: vi.fn(async () => { - return new Promise((resolve) => { - resolve([ - { - seed_id: "1", - seed_name: "seed_name1", - }, - { - seed_id: "2", - seed_name: "seed_name2", - }, - { - seed_id: "3", - seed_name: "seed_name3", - }, - ]); - }); - }), - }; -}); - -vi.mock("axios"); -vi.mocked(axios).mockResolvedValue({ - ok: true, - status: 200, - data: { - testDir1: "testDir1", - testDir2: "testDir2", - testDir3: "testDir3", - }, -}); - describe("Body", () => { it("renders Body component", async () => { render(); diff --git a/src/root/body/body.tsx b/src/root/body/body.tsx index f4c67960..bb523c80 100755 --- a/src/root/body/body.tsx +++ b/src/root/body/body.tsx @@ -21,10 +21,10 @@ import { fetchModelMetadata, inferenceRequest, readAzureStorageDir, + requestUUID, } from "../../common"; import { Images, LabelOccurrences, ModelMetadata } from "../../common/types"; import Cookies from "js-cookie"; -import { requestUUID } from "../../common/api"; interface params { windowSize: { @@ -63,7 +63,9 @@ const Body: React.FC = (props) => { ); const [curDir, setCurDir] = useState("General"); const [readAzureStorage, setReadAzureStorage] = useState(false); - const [azureStorageDir, setAzureStorageDir] = useState({}); + const [azureStorageDir, setAzureStorageDir] = useState<{ + [key: string]: number; + }>({}); const [delDirectoryOpen, setDelDirectoryOpen] = useState(false); const [selectedModel, setSelectedModel] = useState("Swin transformer"); const [modelDisplayName, setModelDisplayName] = useState("");