Skip to content

Commit

Permalink
Restore axios MockAdapter in ToolForm.test.js
Browse files Browse the repository at this point in the history
Unfortunately there is an issue with the jest polyfills and axios requests get always an empty string "data" in response.
  • Loading branch information
davelopez committed Aug 7, 2024
1 parent 17897ba commit e2b6922
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions client/src/components/Tool/ToolForm.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
import MockCurrentHistory from "components/providers/MockCurrentHistory";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import flushPromises from "flush-promises";
import { createPinia } from "pinia";
import { useHistoryStore } from "stores/historyStore";
import { useUserStore } from "stores/userStore";
import { getLocalVue } from "tests/jest/helpers";

import { useServerMock } from "@/api/client/__mocks__";
import { HttpResponse, useServerMock } from "@/api/client/__mocks__";
import MockCurrentHistory from "@/components/providers/MockCurrentHistory";
import { useHistoryStore } from "@/stores/historyStore";
import { useUserStore } from "@/stores/userStore";

import ToolForm from "./ToolForm";
import ToolForm from "./ToolForm.vue";

const { server, http } = useServerMock();

Expand All @@ -17,40 +20,34 @@ const pinia = createPinia();

describe("ToolForm", () => {
let wrapper;
let axiosMock;
let userStore;
let historyStore;

beforeEach(() => {
server.use(
http.get("/api/configuration", ({ response }) => {
return response(200).json({
enable_tool_source_display: false,
object_store_allows_id_selection: false,
});
}),

http.get("/api/tools/{tool_id}/build", ({ response }) => {
return response(200).json({
id: "tool_id",
name: "tool_name",
version: "version",
inputs: [],
help: "help_text",
creator: [
{ class: "Person", givenName: "FakeName", familyName: "FakeSurname", email: "fakeEmail" },
],
});
}),

http.get("/api/tools/{tool_id}/citations", ({ response }) => {
return response(200).json([]);
}),

http.get("/api/webhooks", ({ response }) => {
return response(200).json([]);
return response.untyped(
HttpResponse.json({
enable_tool_source_display: false,
object_store_allows_id_selection: false,
})
);
})
);

axiosMock = new MockAdapter(axios);
axiosMock.onGet(`/api/tools/tool_id/build?tool_version=version`).reply(200, {
id: "tool_id",
name: "tool_name",
version: "version",
inputs: [],
help: "help_text",
creator: [{ class: "Person", givenName: "FakeName", familyName: "FakeSurname", email: "fakeEmail" }],
});
axiosMock.onGet(`/api/webhooks`).reply(200, []);
axiosMock.onGet(`/api/tools/tool_id/citations`).reply(200, []);

wrapper = mount(ToolForm, {
propsData: {
id: "tool_id",
Expand All @@ -64,12 +61,18 @@ describe("ToolForm", () => {
pinia,
});
userStore = useUserStore();
userStore.currentUser = { id: "fakeUser" };
userStore.currentUser = getFakeRegisteredUser({ id: "fakeUser" });

historyStore = useHistoryStore();
historyStore.setHistories([{ id: "fakeHistory" }]);
historyStore.setCurrentHistoryId("fakeHistory");
});

afterEach(() => {
axiosMock.restore();
axiosMock.reset();
});

it("shows props", async () => {
await flushPromises();
const button = wrapper.find(".btn-primary");
Expand Down

0 comments on commit e2b6922

Please sign in to comment.