Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChromaticPanic committed Jun 25, 2024
1 parent 4b6d99a commit 8abbd66
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 60 deletions.
4 changes: 3 additions & 1 deletion src/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const handleAxios = async <T>(request: {
export const readAzureStorageDir = async (
backendUrl: string,
uuid: string,
): Promise<void> => {
): Promise<{
[key: string]: number;
}> => {
if (backendUrl === "" || backendUrl == null) {
throw new ValueError("Backend URL is null or empty");
}
Expand Down
8 changes: 8 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
deleteAzureStorageDir,
inferenceRequest,
fetchModelMetadata,
requestClassList,
sendNegativeFeedback,
sendPositiveFeedback,
requestUUID,
} from "./api";
import {
loadToCanvas,
Expand Down Expand Up @@ -42,4 +46,8 @@ export {
loadResultsToCache,
getLabelOccurrence,
getScaledBounds,
requestClassList,
sendNegativeFeedback,
sendPositiveFeedback,
requestUUID,
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import StorageDirectoryView from "./StorageDirectoryView";

export interface params {
azureStorageDir: any;
azureStorageDir: { [key: string]: number };
curDir: string;
setCurDir: React.Dispatch<React.SetStateAction<string>>;
setCreateDirectoryOpen: React.Dispatch<React.SetStateAction<boolean>>;
Expand All @@ -28,7 +28,8 @@ const StorageDirectoryContainer: React.FC<params> = (props): JSX.Element => {

return (
<StorageDirectoryView
{...props}
azureStorageDir={props.azureStorageDir}
curDir={props.curDir}
handleSelect={handleSelect}
handleDelete={handleDelete}
handleCreateDirectory={handleCreateDirectory}
Expand Down
4 changes: 3 additions & 1 deletion src/components/body/directory_list/StorageDirectoryView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import FolderIcon from "@mui/icons-material/Folder";
import FilterIcon from "@mui/icons-material/Filter";

interface params {
azureStorageDir: any;
azureStorageDir: {
[key: string]: number;
};
curDir: string;
handleSelect: (folder: string) => void;
handleDelete: (folder: string) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 4 additions & 5 deletions src/components/body/microscope_feed/MicroscopeFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++) {
Expand Down
103 changes: 57 additions & 46 deletions src/root/body/body.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof import("../../common")>();
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,
Expand Down Expand Up @@ -40,51 +96,6 @@ Object.defineProperty(global.window, "alert", {
value: vi.fn(),
});

vi.mock("../../common", async (importOriginal) => {
const mod = await importOriginal<typeof import("../../common")>();
return {
...mod,
readAzureStorageDir: vi.fn(async () => {
return new Promise<any>((resolve) => {
resolve(true);
});
}),
createAzureStorageDir: vi.fn(),
deleteAzureStorageDir: vi.fn(),
inferenceRequest: vi.fn(),
requestModelMetadata: vi.fn(),
requestClassList: vi.fn(async () => {
return new Promise<any>((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(<Body {...mockProps} />);
Expand Down
6 changes: 4 additions & 2 deletions src/root/body/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -63,7 +63,9 @@ const Body: React.FC<params> = (props) => {
);
const [curDir, setCurDir] = useState<string>("General");
const [readAzureStorage, setReadAzureStorage] = useState<boolean>(false);
const [azureStorageDir, setAzureStorageDir] = useState<any>({});
const [azureStorageDir, setAzureStorageDir] = useState<{
[key: string]: number;
}>({});
const [delDirectoryOpen, setDelDirectoryOpen] = useState<boolean>(false);
const [selectedModel, setSelectedModel] = useState("Swin transformer");
const [modelDisplayName, setModelDisplayName] = useState("");
Expand Down

0 comments on commit 8abbd66

Please sign in to comment.