Skip to content

Commit

Permalink
webapp/tests: return full Response from fetch stub
Browse files Browse the repository at this point in the history
  • Loading branch information
tharvik committed Dec 2, 2024
1 parent d6c9e65 commit 287482c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
2 changes: 1 addition & 1 deletion webapp/src/components/testing/__tests__/Testing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ it("shows stored models", async () => {
it("allows to download server's models", async () => {
vi.stubGlobal("fetch", async (url: string | URL) => {
if (url.toString() === "http://localhost:8080/tasks")
return { json: () => Promise.resolve([TASK]) };
return new Response(JSON.stringify([TASK]));
throw new Error(`unhandled get: ${url}`);
});
afterEach(() => {
Expand Down
34 changes: 5 additions & 29 deletions webapp/src/components/training/__tests__/Trainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,15 @@ import { loadCSV } from "@epfml/discojs-web";
import Trainer from "../Trainer.vue";
import TrainingInformation from "../TrainingInformation.vue";

vi.mock("axios", async () => {
async function get(url: string) {
if (url === "http://localhost:8080/tasks/titanic/model.json") {
return {
data: await serialization.model.encode(
await defaultTasks.titanic.getModel(),
),
};
}
throw new Error("unhandled get");
}

const axios = await vi.importActual<typeof import("axios")>("axios");
return {
...axios,
default: {
...axios.default,
get,
},
};
});

async function setupForTask() {
const provider = defaultTasks.titanic;

vi.stubGlobal("fetch", async (url: string | URL) => {
if (url.toString() === "http://localhost:8080/tasks/titanic/model.json")
return {
arrayBuffer: async () => {
const model = await provider.getModel();
return await serialization.model.encode(model);
},
};
if (url.toString() === "http://localhost:8080/tasks/titanic/model.json") {
const model = await provider.getModel();
const encoded = await serialization.model.encode(model);
return new Response(encoded);
}
throw new Error(`unhandled get: ${url}`);
});
afterEach(() => {
Expand Down

0 comments on commit 287482c

Please sign in to comment.