Skip to content

Commit 8564b94

Browse files
committed
Add tests
1 parent e95975f commit 8564b94

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @jest-environment jsdom
3+
* @jest-environment-options {"url": "http://localhost/users/42"}
4+
*/
5+
import React from "react";
6+
7+
import "@testing-library/jest-dom";
8+
import { configureStore, combineReducers } from "@reduxjs/toolkit";
9+
import { render, waitFor } from "@testing-library/react";
10+
import { Provider } from "react-redux";
11+
12+
import UserProfile from "../widgets/pages/profile";
13+
import reducers from "../widgets/slices";
14+
15+
jest.mock(
16+
"gon",
17+
() => {
18+
const gonParams = { local: "en", current_user: { sound_settings: {} } };
19+
return { getAsset: (type) => gonParams[type] };
20+
},
21+
{ virtual: true },
22+
);
23+
24+
jest.mock("../i18n", () => ({
25+
__esModule: true,
26+
getLocale: jest.fn(() => "en"),
27+
getSupportedLocale: jest.fn((locale) => locale || "en"),
28+
default: {
29+
language: "en",
30+
t: jest.fn((key, params = {}) =>
31+
key.replace(/%\{(\w+)\}/g, (_, name) => String(params[name] ?? `%{${name}}`)),
32+
),
33+
},
34+
}));
35+
36+
jest.mock("../widgets/components/LanguageIcon", () => () => <span>lang-icon</span>);
37+
jest.mock("../widgets/components/Loading", () => ({ small }) => (
38+
<div>{small ? "loading-small" : "loading"}</div>
39+
));
40+
jest.mock("../widgets/pages/profile/Heatmap", () => () => <div>heatmap</div>);
41+
jest.mock("../widgets/pages/profile/UserStatCharts", () => () => <div>charts</div>);
42+
jest.mock("../widgets/pages/profile/UserTournaments", () => () => <div>tournaments</div>);
43+
jest.mock("../widgets/pages/lobby/CompletedGames", () => () => <div>completed-games</div>);
44+
45+
const reducer = combineReducers(reducers);
46+
47+
describe("UserProfile", () => {
48+
beforeEach(() => {
49+
global.fetch = jest
50+
.fn()
51+
.mockResolvedValueOnce({
52+
ok: true,
53+
json: async () => ({
54+
active_game_id: null,
55+
achievements: [],
56+
metrics: {
57+
game_stats: { won: 3, lost: 1, gave_up: 0 },
58+
language_stats: { js: 2, ts: 2 },
59+
tournaments_stats: {
60+
rookie_wins: 0,
61+
challenger_wins: 0,
62+
pro_wins: 0,
63+
elite_wins: 0,
64+
masters_wins: 0,
65+
grand_slam_wins: 0,
66+
},
67+
},
68+
season_results: [],
69+
stats: { games: { won: 3, lost: 1, gave_up: 0 }, all: [] },
70+
user: {
71+
id: 42,
72+
name: "Kleria",
73+
avatar_url: "/assets/images/logo.svg",
74+
lang: "js",
75+
clan: "",
76+
clan_id: null,
77+
github_name: "Kleria",
78+
inserted_at: "2026-01-01T12:00:00Z",
79+
rating: 1500,
80+
rank: 10,
81+
points: 100,
82+
is_bot: false,
83+
},
84+
}),
85+
})
86+
.mockResolvedValueOnce({
87+
ok: true,
88+
json: async () => ({
89+
top_rivals: [],
90+
}),
91+
});
92+
});
93+
94+
test("does not render or request holopin resources on the profile page", async () => {
95+
const store = configureStore({ reducer });
96+
const { container, getByLabelText, queryByText } = render(
97+
<Provider store={store}>
98+
<UserProfile />
99+
</Provider>,
100+
);
101+
102+
await waitFor(() => {
103+
expect(getByLabelText("Github account")).toHaveAttribute("href", "https://github.com/Kleria");
104+
});
105+
106+
expect(global.fetch).toHaveBeenNthCalledWith(1, "/api/v1/user/42/stats");
107+
expect(global.fetch).toHaveBeenNthCalledWith(2, "/api/v1/user/42/rivals");
108+
expect(queryByText("Holopins")).not.toBeInTheDocument();
109+
expect(container.querySelector('a[href^="https://holopin.io/@"]')).not.toBeInTheDocument();
110+
expect(container.querySelector('img[src^="https://holopin.me/@"]')).not.toBeInTheDocument();
111+
});
112+
});

0 commit comments

Comments
 (0)