Skip to content

Commit

Permalink
Merge pull request #2877 from metabrainz/selective-fetch-mock
Browse files Browse the repository at this point in the history
Enable fetch-mock selectively
  • Loading branch information
MonkeyDo committed May 21, 2024
2 parents d931d72 + fb8280f commit 0bc0bc6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33 deletions.
1 change: 0 additions & 1 deletion enzyme.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { enableFetchMocks } from "jest-fetch-mock";

Enzyme.configure({ adapter: new Adapter() });

enableFetchMocks();

// In Node > v15 unhandled promise rejections will terminate the process
if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe("BrainzPlayer", () => {
window.location = {
href: "http://nevergonnagiveyouup.com",
} as Window["location"];
fetchMock.enableMocks();
});

it("renders correctly", () => {
Expand Down
24 changes: 12 additions & 12 deletions frontend/js/tests/common/listens/ListensControls.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ const props: ListensProps = {
user,
};

fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);

fetchMock.mockIf(
(input) => input.url.endsWith("/delete-listen"),
() => Promise.resolve({ status: 200, statusText: "ok" })
);

// const userEventSession = userEvent.setup();

// eslint-disable-next-line jest/no-disabled-tests
xdescribe("ListensControls", () => {
describe("removeListenFromListenList", () => {
beforeAll(() => {
fetchMock.enableMocks();
fetchMock.doMock();
fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);

fetchMock.mockIf(
(input) => input.url.endsWith("/delete-listen"),
() => Promise.resolve({ status: 200, statusText: "ok" })
);
});
it("updates the listens state for particular recording", async () => {});
// it("updates the listens state for particular recording", async () => {
Expand Down
1 change: 1 addition & 0 deletions frontend/js/tests/lastfm/LastFMImporter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("LastFMImporter", () => {

describe("getNumberOfPages", () => {
beforeAll(()=>{
fetchMock.enableMocks();
// Mock function for fetch
fetchMock.mockResponse(JSON.stringify(page));
})
Expand Down
28 changes: 15 additions & 13 deletions frontend/js/tests/recent/RecentListens.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,22 @@ const propsOneListen = {
...recentListensPropsOneListen,
};

fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);
fetchMock.mockIf(
(input) => input.url.startsWith("https://api.spotify.com"),
() => {
return Promise.resolve(JSON.stringify({}));
}
);

describe("Recentlistens", () => {
beforeAll(() => {
fetchMock.enableMocks();
fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);
fetchMock.mockIf(
(input) => input.url.startsWith("https://api.spotify.com"),
() => {
return Promise.resolve(JSON.stringify({}));
}
);
});
it("renders the page correctly", () => {
const wrapper = mount<RecentListens>(
<GlobalAppContext.Provider value={mountOptions.context}>
Expand Down
13 changes: 7 additions & 6 deletions frontend/js/tests/user/Dashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ const propsOneListen = {
...recentListensPropsOneListen,
};

fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);
const getComponent = (componentProps: ListensProps) => (
<BrowserRouter>
<Listens />
Expand All @@ -75,6 +69,13 @@ xdescribe("Listens page", () => {
let userEventSession: UserEvent;
beforeAll(async () => {
userEventSession = await userEvent.setup();
fetchMock.enableMocks();
fetchMock.mockIf(
(input) => input.url.endsWith("/listen-count"),
() => {
return Promise.resolve(JSON.stringify({ payload: { count: 42 } }));
}
);
});
it("renders correctly on the profile page", async () => {});

Expand Down
8 changes: 7 additions & 1 deletion frontend/js/tests/user/taste/UserFeedback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as React from "react";
import fetchMock from "jest-fetch-mock";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import UserFeedback, { UserFeedbackProps } from "../../../src/user/taste/components/UserFeedback";
import UserFeedback, {
UserFeedbackProps,
} from "../../../src/user/taste/components/UserFeedback";
import * as userFeedbackProps from "../../__mocks__/userFeedbackProps.json";
import * as userFeedbackAPIResponse from "../../__mocks__/userFeedbackAPIResponse.json";

Expand All @@ -26,6 +28,10 @@ const props: UserFeedbackProps = {
jest.spyOn(global.Math, "random").mockImplementation(() => 0);

describe("UserFeedback", () => {
beforeAll(() => {
fetchMock.enableMocks();
});

it("renders ListenCard items for each feedback item", async () => {
render(<UserFeedback {...props} />);
const listensContainer = screen.getByTestId("userfeedback-listens");
Expand Down

0 comments on commit 0bc0bc6

Please sign in to comment.