Skip to content

Commit

Permalink
Fix UserReports tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkeyDo committed May 10, 2024
1 parent ced8901 commit cf0e318
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 83 deletions.
20 changes: 4 additions & 16 deletions frontend/js/tests/user/stats/UserArtistMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ describe.each([
});
await waitForComponentToPaint(wrapper);

expect(wrapper.getDOMNode()).toHaveTextContent("Invalid range: invalid_range");
expect(wrapper.getDOMNode()).toHaveTextContent(
"Invalid range: invalid_range"
);
expect(wrapper.find(CustomChoropleth)).toHaveLength(0);
});
});
Expand Down Expand Up @@ -123,23 +125,9 @@ describe.each([
expect(wrapper.state()).toMatchObject({
loading: false,
hasError: true,
errorMessage: "There are no statistics available for this user for this period",
errorMessage: "NO CONTENT",
});
});

it("throws error", async () => {
const wrapper = shallow<UserArtistMap>(<UserArtistMap {...props} />);
const instance = wrapper.instance();

const spy = jest.spyOn(instance.APIService, "getUserArtistMap");
const notFoundError = new APIError("NOT FOUND");
notFoundError.response = {
status: 404,
} as Response;
spy.mockImplementation(() => Promise.reject(notFoundError));

await expect(instance.getData()).rejects.toThrow("NOT FOUND");
});
});

describe("processData", () => {
Expand Down
22 changes: 4 additions & 18 deletions frontend/js/tests/user/stats/UserDailyActivity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe("UserDailyActivity", () => {
});
await waitForComponentToPaint(wrapper);

expect(wrapper.getDOMNode()).toHaveTextContent("Invalid range: invalid_range");
expect(wrapper.getDOMNode()).toHaveTextContent(
"Invalid range: invalid_range"
);
expect(wrapper.find(Heatmap)).toHaveLength(0);
});

Expand Down Expand Up @@ -121,25 +123,9 @@ describe("UserDailyActivity", () => {
expect(wrapper.state()).toMatchObject({
loading: false,
hasError: true,
errorMessage: "There are no statistics available for this user for this period",
errorMessage: "NO CONTENT",
});
});

it("throws error", async () => {
const wrapper = mount<UserDailyActivity>(
<UserDailyActivity {...props} />
);
const instance = wrapper.instance();

const spy = jest.spyOn(instance.APIService, "getUserDailyActivity");
const notFoundError = new APIError("NOT FOUND");
notFoundError.response = {
status: 404,
} as Response;
spy.mockImplementation(() => Promise.reject(notFoundError));

await expect(instance.getData()).rejects.toThrow("NOT FOUND");
});
});

describe("processData", () => {
Expand Down
31 changes: 10 additions & 21 deletions frontend/js/tests/user/stats/UserListeningActivity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from "react";
import { mount, ReactWrapper, shallow, ShallowWrapper } from "enzyme";

import { act } from "react-dom/test-utils";
import { ResponsiveBar } from "@nivo/bar";
import { Context as ResponsiveContext } from "react-responsive";
import UserListeningActivity, {
UserListeningActivityProps,
UserListeningActivityState,
Expand All @@ -16,8 +18,6 @@ import * as userListeningActivityProcessedDataMonth from "../../__mocks__/userLi
import * as userListeningActivityProcessedDataYear from "../../__mocks__/userListeningActivityProcessDataYear.json";
import * as userListeningActivityProcessedDataAllTime from "../../__mocks__/userListeningActivityProcessDataAllTime.json";
import { waitForComponentToPaint } from "../../test-utils";
import { ResponsiveBar } from "@nivo/bar";
import { Context as ResponsiveContext } from 'react-responsive'

const userProps: UserListeningActivityProps = {
user: {
Expand Down Expand Up @@ -124,13 +124,18 @@ describe.each([

it("renders corectly when range is invalid", async () => {
const wrapper = mount<UserListeningActivity>(
<UserListeningActivity {...props} range={"invalid_range" as UserStatsAPIRange } />
<UserListeningActivity
{...props}
range={"invalid_range" as UserStatsAPIRange}
/>
);
await waitForComponentToPaint(wrapper);

expect(wrapper.state().hasError).toBeTruthy();
expect(wrapper.find(ResponsiveBar)).toHaveLength(0);
expect(wrapper.getDOMNode()).toHaveTextContent("Invalid range: invalid_range");
expect(wrapper.getDOMNode()).toHaveTextContent(
"Invalid range: invalid_range"
);
});
});

Expand Down Expand Up @@ -205,25 +210,9 @@ describe.each([
expect(wrapper.state()).toMatchObject({
loading: false,
hasError: true,
errorMessage: "There are no statistics available for this user for this period",
errorMessage: "NO CONTENT",
});
});

it("throws error", async () => {
const wrapper = shallow<UserListeningActivity>(
<UserListeningActivity {...props} />
);
const instance = wrapper.instance();

const spy = jest.spyOn(instance.APIService, "getUserListeningActivity");
const notFoundError = new APIError("NOT FOUND");
notFoundError.response = {
status: 404,
} as Response;
spy.mockImplementation(() => Promise.reject(notFoundError));

await expect(instance.getData()).rejects.toThrow("NOT FOUND");
});
});

describe("getNumberOfDaysInMonth", () => {
Expand Down
19 changes: 1 addition & 18 deletions frontend/js/tests/user/stats/UserTopEntity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,9 @@ describe.each([
expect(childElement.state()).toMatchObject({
loading: false,
hasError: true,
errorMessage:
"There are no statistics available for this user for this period",
errorMessage: "NO CONTENT",
});
wrapper.unmount();
});

it("throws error", async () => {
const wrapper = shallow(getComponent(props));
const childElement = shallow(wrapper.find(UserTopEntity).get(0));
const instance = childElement.instance() as UserTopEntity;

const spy = jest.spyOn(instance.APIService, "getUserEntity");
const notFoundError = new APIError("NOT FOUND");
notFoundError.response = {
status: 404,
} as Response;
spy.mockImplementation(() => Promise.reject(notFoundError));

await expect(instance.getData()).rejects.toThrow("NOT FOUND");
wrapper.unmount();
});
});
});
20 changes: 10 additions & 10 deletions frontend/js/tests/utils/APIService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("submitListens", () => {
status: 200,
});
});
jest.useFakeTimers({advanceTimers: true});
jest.useFakeTimers({ advanceTimers: true });
});

it("calls fetch with correct parameters", async () => {
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("submitListens", () => {
},
},
]);

await jest.advanceTimersByTimeAsync(10000);

expect(spy).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -321,12 +321,12 @@ describe("getUserEntity", () => {
return Promise.resolve({
ok: true,
status: 204,
statusText: "NO CONTENT",
statusText: "Whatever error",
});
});

await expect(apiService.getUserEntity("foobar", "artist")).rejects.toThrow(
Error("HTTP Error NO CONTENT")
Error("There are no statistics available for this user for this period")
);
});

Expand Down Expand Up @@ -376,12 +376,12 @@ describe("getUserListeningActivity", () => {
return Promise.resolve({
ok: true,
status: 204,
statusText: "NO CONTENT",
statusText: "Whatever error",
});
});

await expect(apiService.getUserListeningActivity("foobar")).rejects.toThrow(
Error("HTTP Error NO CONTENT")
Error("There are no statistics available for this user for this period")
);
});

Expand Down Expand Up @@ -424,12 +424,12 @@ describe("getUserDailyActivity", () => {
return Promise.resolve({
ok: true,
status: 204,
statusText: "NO CONTENT",
statusText: "Whatever error",
});
});

await expect(apiService.getUserDailyActivity("foobar")).rejects.toThrow(
Error("HTTP Error NO CONTENT")
Error("There are no statistics available for this user for this period")
);
});

Expand Down Expand Up @@ -479,12 +479,12 @@ describe("getUserArtistMap", () => {
return Promise.resolve({
ok: true,
status: 204,
statusText: "NO CONTENT",
statusText: "Whatever error",
});
});

await expect(apiService.getUserArtistMap("foobar")).rejects.toThrow(
Error("HTTP Error NO CONTENT")
Error("There are no statistics available for this user for this period")
);
});

Expand Down

0 comments on commit cf0e318

Please sign in to comment.