Skip to content

Commit

Permalink
Refactor UserPreferredObjectStore test to use server mock
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 9, 2024
1 parent 6383bd4 commit 02cc2c6
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions client/src/components/User/UserPreferredObjectStore.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 { getLocalVue } from "tests/jest/helpers";

import { useServerMock } from "@/api/client/__mocks__";
import { ROOT_COMPONENT } from "@/utils/navigation";

import { setupSelectableMock } from "../ObjectStore/mockServices";
Expand All @@ -14,9 +14,17 @@ setupSelectableMock();

const localVue = getLocalVue(true);

const { server, http } = useServerMock();

const TEST_USER_ID = "myTestUserId";

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

const wrapper = mount(UserPreferredObjectStore, {
propsData: { userId: TEST_USER_ID },
localVue,
Expand All @@ -26,16 +34,6 @@ function mountComponent() {
}

describe("UserPreferredObjectStore.vue", () => {
let axiosMock;

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

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

it("contains a localized link", async () => {
const wrapper = mountComponent();
expect(wrapper.vm.$refs["modal"].isHidden).toBeTruthy();
Expand All @@ -56,7 +54,13 @@ describe("UserPreferredObjectStore.vue", () => {
ROOT_COMPONENT.preferences.object_store_selection.option_button({ object_store_id: "__null__" }).selector
);
expect(galaxyDefaultOption.exists()).toBeTruthy();
axiosMock.onPut("/api/users/current", expect.objectContaining({ preferred_object_store_id: null })).reply(202);

server.use(
http.put("/api/users/{user_id}", ({ response }) => {
return response(200).json(getFakeRegisteredUser({ preferred_object_store_id: null }));
})
);

await galaxyDefaultOption.trigger("click");
await flushPromises();
const errorEl = wrapper.find(".object-store-selection-error");
Expand All @@ -72,9 +76,13 @@ describe("UserPreferredObjectStore.vue", () => {
.selector
);
expect(objectStore2Option.exists()).toBeTruthy();
axiosMock
.onPut("/api/users/current", expect.objectContaining({ preferred_object_store_id: "object_store_2" }))
.reply(202);

server.use(
http.put("/api/users/{user_id}", ({ response }) => {
return response(200).json(getFakeRegisteredUser({ preferred_object_store_id: "object_store_2" }));
})
);

await objectStore2Option.trigger("click");
await flushPromises();
const errorEl = wrapper.find(".object-store-selection-error");
Expand All @@ -89,9 +97,13 @@ describe("UserPreferredObjectStore.vue", () => {
ROOT_COMPONENT.preferences.object_store_selection.option_button({ object_store_id: "__null__" }).selector
);
expect(galaxyDefaultOption.exists()).toBeTruthy();
axiosMock
.onPut("/api/users/current", expect.objectContaining({ preferred_object_store_id: null }))
.reply(400, { err_msg: "problem with selection.." });

server.use(
http.put("/api/users/{user_id}", ({ response }) => {
return response("4XX").json({ err_msg: "problem with selection.." }, { status: 400 });
})
);

await galaxyDefaultOption.trigger("click");
await flushPromises();
const errorEl = await wrapper.find(".object-store-selection-error");
Expand Down

0 comments on commit 02cc2c6

Please sign in to comment.