Skip to content

Commit

Permalink
Refactor MultipleView.test.js to use server mock and update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 12, 2024
1 parent 3acc67d commit 102a2aa
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions client/src/components/History/Multiple/MultipleView.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { getFakeRegisteredUser } from "@tests/test-data";
import { mount } from "@vue/test-utils";
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 MultipleView from "./MultipleView";
import { useServerMock } from "@/api/client/__mocks__";
import { useHistoryStore } from "@/stores/historyStore";
import { useUserStore } from "@/stores/userStore";

import MultipleView from "./MultipleView.vue";

const USER_ID = "test-user-id";
const FIRST_HISTORY_ID = "test-history-id-0";

const getFakeHistorySummaries = (num, selectedIndex) => {
const { server, http } = useServerMock();

const getFakeHistorySummaries = (num) => {
return Array.from({ length: num }, (_, index) => ({
id: `test-history-id-${index}`,
name: `History-${index}`,
Expand All @@ -22,24 +24,32 @@ const getFakeHistorySummaries = (num, selectedIndex) => {
}));
};

const currentUser = getFakeRegisteredUser({ id: USER_ID });

describe("MultipleView", () => {
let axiosMock;

beforeEach(() => {
axiosMock = new MockAdapter(axios);
});

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

async function setUpWrapper(count, currentHistoryId) {
const fakeSummaries = getFakeHistorySummaries(count, 0);
for (const summary of fakeSummaries) {
axiosMock.onGet(`api/histories/${summary.id}`).reply(200, summary);
}
const fakeSummaries = getFakeHistorySummaries(count);

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

http.get("/api/histories/{history_id}", ({ response, params }) => {
const { history_id } = params;
const summary = fakeSummaries.find((s) => s.id === history_id);
if (!summary) {
return response("4XX").json({ err_msg: "History not found", err_code: 404 }, { status: 404 });
}
return response(200).json(summary);
}),

http.get("/api/histories/{history_id}/contents", ({ response }) => {
return response(200).json({
stats: { total_matches: 0 },
contents: [],
});
})
);

const wrapper = mount(MultipleView, {
pinia: createPinia(),
stubs: {
Expand All @@ -50,7 +60,7 @@ describe("MultipleView", () => {
});

const userStore = useUserStore();
userStore.currentUser = currentUser;
userStore.currentUser = getFakeRegisteredUser({ id: USER_ID });

const historyStore = useHistoryStore();
historyStore.setHistories(fakeSummaries);
Expand Down

0 comments on commit 102a2aa

Please sign in to comment.