Skip to content

Commit 752c318

Browse files
committed
fix(Support): extract impure helpers and remove all eslint-disable comments
1 parent cf7ff01 commit 752c318

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

src/components/Support/Support.data.test.jsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
// eslint-disable-next-line import/no-extraneous-dependencies
5-
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
64
import { act, fireEvent, render, screen } from "@testing-library/react";
75
import SmallIcon from "../../assets/icon-square-small-slack.png";
86

7+
import Support from "./Support.jsx";
8+
99
// Data must be inline - jest.mock is hoisted before const declarations (TDZ)
1010
jest.mock("./AdditionalSupporters.mjs", () => []);
1111
jest.mock(
@@ -25,9 +25,6 @@ jest.mock(
2525
{ virtual: true },
2626
);
2727

28-
// eslint-disable-next-line import/first
29-
import Support from "./Support.jsx";
30-
3128
const AVATAR_URL = "https://example.com/avatar.png";
3229

3330
describe("Support with supporter data", () => {

src/components/Support/Support.jsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ function handleImgError(event) {
101101
imgNode.setAttribute("src", SmallIcon);
102102
}
103103

104+
function filterByAge(result, age) {
105+
const now = Date.now();
106+
return result.filter(
107+
(item) =>
108+
item.firstDonation && now - new Date(item.firstDonation).getTime() < age,
109+
);
110+
}
111+
112+
function shuffleSlice(arr, n) {
113+
const result = [...arr];
114+
for (let i = 0; i < n; i++) {
115+
const other = Math.floor(Math.random() * (result.length - i));
116+
const temp = result[other];
117+
result[other] = result[i];
118+
result[i] = temp;
119+
}
120+
return result.slice(0, n);
121+
}
122+
104123
export default function Support({ rank, type }) {
105124
const [inView, setInView] = useState(false);
106125
const containerRef = useRef(null);
@@ -156,30 +175,15 @@ export default function Support({ rank, type }) {
156175
}
157176

158177
if (typeof age === "number") {
159-
// eslint-disable-next-line react-hooks/purity
160-
const now = Date.now();
161-
result = result.filter(
162-
(item) =>
163-
item.firstDonation &&
164-
now - new Date(item.firstDonation).getTime() < age,
165-
);
178+
result = filterByAge(result, age);
166179
}
167180

168181
if (typeof lim === "number") {
169182
result = result.slice(0, lim);
170183
}
171184

172185
if (typeof rand === "number" && result.length >= rand) {
173-
// Pick n random items
174-
result = [...result];
175-
for (let i = 0; i < rand; i++) {
176-
// eslint-disable-next-line react-hooks/purity
177-
const other = Math.floor(Math.random() * (result.length - i));
178-
const temp = result[other];
179-
result[other] = result[i];
180-
result[i] = temp;
181-
}
182-
result = result.slice(0, rand);
186+
result = shuffleSlice(result, rand);
183187
}
184188

185189
// resort to keep order

src/components/Support/Support.test.jsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
// eslint-disable-next-line import/no-extraneous-dependencies
5-
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
64
import { render, screen } from "@testing-library/react";
75

6+
import Support from "./Support.jsx";
7+
88
jest.mock("./AdditionalSupporters.mjs", () => []);
99
jest.mock("./_supporters.json", () => [], { virtual: true });
1010

11-
// eslint-disable-next-line import/first
12-
import Support from "./Support.jsx";
13-
1411
describe("Support", () => {
1512
let mockObserve;
1613
let mockDisconnect;

0 commit comments

Comments
 (0)