Skip to content

Commit 9d25868

Browse files
committed
Drop axios
1 parent c83adb6 commit 9d25868

32 files changed

Lines changed: 553 additions & 438 deletions

apps/codebattle/assets/css/style.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ input[type='range'].cb-range {
366366
&.alert-info {
367367
background: linear-gradient(
368368
135deg,
369-
rgba(13, 110, 253, 0.9),
370-
rgba(13, 110, 253, 0.7)
369+
rgba(25, 135, 84, 0.9),
370+
rgba(25, 135, 84, 0.7)
371371
);
372372
color: #ffffff;
373-
border-left: 4px solid #0d6efd;
373+
border-left: 4px solid #198754;
374374
}
375375

376376
&.alert-success {

apps/codebattle/assets/js/__tests__/ContributorsList.test.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// import { configureStore, combineReducers } from '@reduxjs/toolkit';
44
// import { render } from '@testing-library/react';
55
import "@testing-library/jest-dom";
6-
import axios from "axios";
76
// import { Provider } from 'react-redux';
87
//
98
// import ContributorsList from '../widgets/pages/game/ContributorsList';
@@ -18,9 +17,13 @@ jest.mock(
1817
{ virtual: true },
1918
);
2019

21-
jest.mock("axios");
2220
const users = [];
23-
axios.get.mockResolvedValue({ data: users });
21+
beforeAll(() => {
22+
global.fetch = jest.fn().mockResolvedValue({
23+
ok: true,
24+
json: async () => users,
25+
});
26+
});
2427
//
2528
test("rendering ContributorsList", async () => {
2629
// const reducer = combineReducers(reducers);

apps/codebattle/assets/js/__tests__/CreateGameDialog.test.jsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { configureStore, combineReducers } from "@reduxjs/toolkit";
44
import { render, waitFor } from "@testing-library/react";
55
import userEvent from "@testing-library/user-event";
66
import "@testing-library/jest-dom";
7-
import axios from "axios";
87
import noop from "lodash/noop";
98
import omit from "lodash/omit";
109
import { Provider } from "react-redux";
@@ -29,8 +28,6 @@ jest.mock(
2928
{ virtual: true },
3029
);
3130

32-
jest.mock("axios");
33-
3431
const {
3532
elementaryTasksFromBackend,
3633
easyTasksFromBackend,
@@ -52,14 +49,6 @@ const users = [
5249
];
5350
const userData = { avatarUrl: "" };
5451

55-
axios.get.mockResolvedValue({
56-
data: {
57-
tasks: [...elementaryTasksFromBackend, ...easyTasksFromBackend],
58-
users,
59-
user: userData,
60-
},
61-
});
62-
6352
jest.mock("react-select");
6453
jest.mock("react-select/async");
6554
/*
@@ -118,6 +107,25 @@ const defaultGameParams = {
118107
let vdom;
119108

120109
beforeAll(() => {
110+
global.fetch = jest.fn((url) => {
111+
if (String(url).includes("/api/v1/tasks")) {
112+
return Promise.resolve({
113+
ok: true,
114+
json: async () => ({
115+
tasks: [...elementaryTasksFromBackend, ...easyTasksFromBackend],
116+
}),
117+
});
118+
}
119+
120+
return Promise.resolve({
121+
ok: true,
122+
json: async () => ({
123+
users,
124+
user: userData,
125+
}),
126+
});
127+
});
128+
121129
vdom = (
122130
<Provider store={store}>
123131
<CreateGameDialog hideModal={noop} />

apps/codebattle/assets/js/__tests__/Registration.test.jsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import React from "react";
77
import { render, waitFor } from "@testing-library/react";
88
import userEvent from "@testing-library/user-event";
99
import "@testing-library/jest-dom";
10-
import axios from "axios";
1110

1211
import Registration from "../widgets/pages/registration";
1312

1413
import { getTestData } from "./helpers";
1514

16-
jest.mock("axios");
17-
1815
const { invalidData, validData } = getTestData("signUpData.json");
1916
const { data, route, headers } = validData;
2017

@@ -39,6 +36,10 @@ describe("sign up", () => {
3936
document.head.innerHTML = '<meta name="csrf-token" content="test-csrf-token">';
4037
});
4138

39+
beforeEach(() => {
40+
global.fetch = jest.fn();
41+
});
42+
4243
test("render", () => {
4344
const { getByText } = setup(<Registration />);
4445

@@ -62,7 +63,11 @@ describe("sign up", () => {
6263
test("successful sign up", async () => {
6364
const { getByLabelText, user } = setup(<Registration />);
6465

65-
const signUpSpy = jest.spyOn(axios, "post").mockResolvedValueOnce({ data: {} });
66+
const signUpSpy = jest.fn().mockResolvedValueOnce({
67+
ok: true,
68+
json: async () => ({}),
69+
});
70+
global.fetch.mockImplementation(signUpSpy);
6671

6772
await userEvent.type(getByLabelText("name"), data.name);
6873
await userEvent.type(getByLabelText("email"), data.email);
@@ -73,7 +78,11 @@ describe("sign up", () => {
7378
await user.click(submitButton);
7479

7580
await waitFor(() => {
76-
expect(signUpSpy).toHaveBeenCalledWith(route, data, headers);
81+
expect(signUpSpy).toHaveBeenCalledWith(route, {
82+
method: "POST",
83+
headers: headers.headers,
84+
body: JSON.stringify(data),
85+
});
7786
});
7887
});
7988
});

apps/codebattle/assets/js/__tests__/RootContainer.test.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { configureStore, combineReducers } from "@reduxjs/toolkit";
55
import { render } from "@testing-library/react";
66
import userEvent from "@testing-library/user-event";
77
import "@testing-library/jest-dom";
8-
import axios from "axios";
98
import { Provider } from "react-redux";
109
import { createMachine } from "xstate";
1110

@@ -89,8 +88,6 @@ jest.mock(
8988
{ virtual: true },
9089
);
9190

92-
jest.mock("axios");
93-
9491
jest.mock(
9592
"../widgets/pages/game/EditorContainer",
9693
() =>
@@ -111,7 +108,12 @@ jest.mock("../widgets/utils/useStayScrolled", () => () => ({ stayScrolled: () =>
111108
virtual: true,
112109
});
113110

114-
axios.get.mockResolvedValue({ data: {} });
111+
beforeAll(() => {
112+
global.fetch = jest.fn().mockResolvedValue({
113+
ok: true,
114+
json: async () => ({}),
115+
});
116+
});
115117

116118
jest.mock("phoenix", () => {
117119
const originalModule = jest.requireActual("phoenix");

apps/codebattle/assets/js/__tests__/UserSettings.test.jsx

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "@testing-library/jest-dom";
44
import { configureStore, combineReducers } from "@reduxjs/toolkit";
55
import { render, waitFor } from "@testing-library/react";
66
import userEvent from "@testing-library/user-event";
7-
import axios from "axios";
87
import { Provider } from "react-redux";
98

109
import UserSettings from "../widgets/pages/settings";
@@ -26,8 +25,6 @@ jest.mock("react-bootstrap/Alert", () => ({
2625
) : null,
2726
}));
2827

29-
jest.mock("axios");
30-
3128
const reducer = combineReducers(reducers);
3229

3330
const preloadedState = {
@@ -73,6 +70,10 @@ describe("UserSettings test cases", () => {
7370
};
7471
}
7572

73+
beforeEach(() => {
74+
global.fetch = jest.fn();
75+
});
76+
7677
test("render main component", () => {
7778
const { getByText } = setup(
7879
<Provider store={store}>
@@ -83,7 +84,10 @@ describe("UserSettings test cases", () => {
8384
});
8485

8586
test("successfull user settings update", async () => {
86-
const settingUpdaterSpy = jest.spyOn(axios, "patch").mockResolvedValueOnce({ data: {} });
87+
const settingUpdaterSpy = global.fetch.mockResolvedValueOnce({
88+
ok: true,
89+
json: async () => ({}),
90+
});
8791
const { getByRole, getByLabelText, getByTestId, user } = setup(
8892
<Provider store={store}>
8993
<UserSettings />
@@ -100,27 +104,55 @@ describe("UserSettings test cases", () => {
100104

101105
await waitFor(() => {
102106
expect(settingUpdaterSpy).toHaveBeenCalledWith(
103-
expect.anything(),
104-
{
105-
clan: "",
106-
name: "Dmitry",
107-
lang: "js",
108-
locale: undefined,
109-
lang_view: "code",
110-
db_type: "",
111-
style_lang: "",
112-
sound_settings: {
113-
level: 6,
114-
tournament_level: 4,
115-
type: "standard",
116-
},
117-
},
118-
expect.anything(),
107+
"/api/v1/settings",
108+
expect.objectContaining({
109+
method: "PATCH",
110+
}),
119111
);
112+
113+
const [, requestOptions] = settingUpdaterSpy.mock.calls[0];
114+
expect(JSON.parse(requestOptions.body)).toEqual({
115+
clan: "",
116+
name: "Dmitry",
117+
lang: "js",
118+
lang_view: "code",
119+
db_type: "",
120+
style_lang: "",
121+
sound_settings: {
122+
level: 6,
123+
tournament_level: 4,
124+
type: "standard",
125+
},
126+
});
120127
expect(getByRole("alert")).toHaveClass("alert-success");
121128
});
122129
});
123130

131+
test("successfull locale change", async () => {
132+
const settingUpdaterSpy = global.fetch.mockResolvedValueOnce({
133+
ok: true,
134+
json: async () => ({}),
135+
});
136+
const { getByLabelText, getByTestId, findByText, user } = setup(
137+
<Provider store={store}>
138+
<UserSettings />
139+
</Provider>,
140+
);
141+
const submitButton = getByLabelText("SubmitForm");
142+
const localeSelect = getByTestId("localeSelect");
143+
144+
await user.click(localeSelect);
145+
await user.click(await findByText("Ru"));
146+
await user.click(submitButton);
147+
148+
await waitFor(() => {
149+
const [, requestOptions] = settingUpdaterSpy.mock.calls[0];
150+
expect(JSON.parse(requestOptions.body)).toMatchObject({
151+
locale: "ru",
152+
});
153+
});
154+
});
155+
124156
test("failed user settings update", async () => {
125157
const { getByTestId, getByLabelText, findByRole, findByText, user } = setup(
126158
<Provider store={store}>
@@ -144,14 +176,14 @@ describe("UserSettings test cases", () => {
144176
).toBeInTheDocument();
145177
expect(submitButton).toBeDisabled();
146178

147-
axios.patch.mockRejectedValueOnce({
148-
response: {
149-
data: {
150-
errors: {
151-
name: ["has already been taken"],
152-
},
179+
global.fetch.mockResolvedValueOnce({
180+
ok: false,
181+
status: 422,
182+
json: async () => ({
183+
errors: {
184+
name: ["has already been taken"],
153185
},
154-
},
186+
}),
155187
});
156188

157189
await user.clear(nameInput);
@@ -163,10 +195,7 @@ describe("UserSettings test cases", () => {
163195

164196
expect(await findByText(/Has already been taken/i)).toBeInTheDocument();
165197

166-
axios.patch.mockRejectedValueOnce({
167-
response: undefined,
168-
message: "Network Error",
169-
});
198+
global.fetch.mockRejectedValueOnce(new Error("Network Error"));
170199

171200
await user.clear(nameInput);
172201
await user.type(nameInput, "CoolUserName");

apps/codebattle/assets/js/widgets/components/FeedbackAlertNotification.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function FeedbackAlertNotification() {
5353
onClose={() => handleClose(key)}
5454
key={key}
5555
variant={result.status}
56-
className="row mb-0 rounded-0 alert alert-info alert-dismissible fade show"
56+
className="row mb-0 rounded-0 alert alert-info alert-dismissible fade show alert-dark-theme"
5757
>
5858
{result.message}
5959
</Alert>

apps/codebattle/assets/js/widgets/components/GamesHeatmap.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useState, useEffect } from "react";
22

3-
import axios from "axios";
43
import CalendarHeatmap from "react-calendar-heatmap";
54
import { useDispatch } from "react-redux";
65

@@ -14,10 +13,14 @@ function GamesHeatmap() {
1413
const dispatch = useDispatch();
1514

1615
useEffect(() => {
17-
axios
18-
.get("/api/v1/game_activity")
19-
.then((response) => {
20-
setActivities(response.data.activities);
16+
fetch("/api/v1/game_activity")
17+
.then(async (response) => {
18+
if (!response.ok) {
19+
throw new Error(`Request failed with status ${response.status}`);
20+
}
21+
22+
const data = await response.json();
23+
setActivities(data.activities);
2124
})
2225
.catch((error) => {
2326
dispatch(actions.setError(error));

apps/codebattle/assets/js/widgets/components/PlayerInsightsModal/index.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useState, useMemo, useEffect } from "react";
22

3-
import axios from "axios";
43
import cn from "classnames";
54
import Spinner from "react-bootstrap/Spinner";
65
import {
@@ -288,10 +287,14 @@ function PlayerInsightsModal({ show, onHide, player, allResults, season }) {
288287
setLoading(true);
289288
setError(null);
290289

291-
axios
292-
.get(`/api/v1/seasons/${season.id}/players/${player.user_id}/stats`)
293-
.then((response) => {
294-
setDetailedStats(response.data);
290+
fetch(`/api/v1/seasons/${season.id}/players/${player.user_id}/stats`)
291+
.then(async (response) => {
292+
if (!response.ok) {
293+
throw new Error(`Request failed with status ${response.status}`);
294+
}
295+
296+
const data = await response.json();
297+
setDetailedStats(data);
295298
setLoading(false);
296299
})
297300
.catch((err) => {

0 commit comments

Comments
 (0)