Skip to content

Commit f0363bf

Browse files
committed
Refactorizando codigo
1 parent 8612551 commit f0363bf

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

webapp/src/pages/Stats/Stats.test.js

+48-49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ const columnHeaders = [
3030
"Tiempo por pregunta (s)",
3131
];
3232

33+
const checkTableHeader = (headerText) => {
34+
const headerElement = screen.getByText(headerText);
35+
expect(headerElement).toBeInTheDocument();
36+
};
37+
38+
const checkCellValue = (key, value) => {
39+
if (key !== "username" && key !== "_id" && key !== "gamemode" && key!=="__v") {
40+
const formattedValue =
41+
key === "avgPoints" || key === "avgTime"
42+
? value.toFixed(2)
43+
: key === "ratioCorrect"
44+
? value.toFixed(2) + "%"
45+
: value.toString();
46+
const valueElements = screen.getAllByText(formattedValue);
47+
expect(valueElements.length).toBeGreaterThan(0);
48+
}
49+
};
50+
3351
const renderComponentWithRouter = () => {
3452
render(
3553
<I18nextProvider i18n={i18n}>
@@ -60,30 +78,46 @@ describe("Stats component", () => {
6078

6179
test("fetches user statistics and displays them", async () => {
6280
localStorage.setItem("username", "testUser");
63-
81+
6482
global.fetch = jest.fn().mockResolvedValue({
6583
json: jest.fn().mockResolvedValue(userData),
6684
});
67-
85+
6886
renderComponentWithRouter();
69-
87+
7088
await waitFor(async () => {
7189
const table = await screen.findByRole("table");
7290
expect(table).toBeInTheDocument();
7391
});
74-
75-
columnHeaders.forEach((headerText) => {
76-
const headerElement = screen.getByText(headerText);
77-
expect(headerElement).toBeInTheDocument();
92+
93+
columnHeaders.forEach(checkTableHeader);
94+
Object.entries(userData).forEach(([key, value]) => {
95+
checkCellValue(key, value);
96+
});
97+
});
98+
99+
test("updates user statistics when game mode changes", async () => {
100+
localStorage.setItem("username", "testUser");
101+
102+
global.fetch = jest.fn().mockResolvedValue({
103+
json: jest.fn().mockResolvedValue(userData),
104+
});
105+
106+
renderComponentWithRouter();
107+
108+
await waitFor(() => {
109+
expect(screen.queryByText("Cargando ...")).not.toBeInTheDocument();
78110
});
111+
112+
const modeButton = screen.getByTestId("calculator-button");
113+
userEvent.click(modeButton);
114+
115+
const table = await screen.findByRole("table");
116+
expect(table).toBeInTheDocument();
117+
118+
columnHeaders.forEach(checkTableHeader);
79119
Object.entries(userData).forEach(([key, value]) => {
80-
if (key !== "username" && key!=="_id") {
81-
if (key === "avgPoints" || key === "avgTime") {
82-
expect(screen.getByText(value.toFixed(2))).toBeInTheDocument();
83-
} else if (key === "ratioCorrect") {
84-
expect(screen.getByText(value.toFixed(2) + "%")).toBeInTheDocument();
85-
}
86-
}
120+
checkCellValue(key, value);
87121
});
88122
});
89123

@@ -124,41 +158,6 @@ describe("Stats component", () => {
124158
expect(fetch).toHaveBeenCalledWith(expect.stringContaining(newUsername));
125159
});
126160

127-
test("updates user statistics when game mode changes", async () => {
128-
localStorage.setItem("username", "testUser");
129-
130-
global.fetch = jest.fn().mockResolvedValue({
131-
json: jest.fn().mockResolvedValue(userData),
132-
});
133-
134-
renderComponentWithRouter();
135-
136-
await waitFor(() => {
137-
expect(screen.queryByText("Cargando ...")).not.toBeInTheDocument();
138-
});
139-
140-
const modeButton = screen.getByTestId("calculator-button");
141-
userEvent.click(modeButton);
142-
143-
const table = await screen.findByRole("table");
144-
expect(table).toBeInTheDocument();
145-
146-
columnHeaders.forEach((headerText) => {
147-
const headerElement = screen.getByText(headerText);
148-
expect(headerElement).toBeInTheDocument();
149-
});
150-
151-
Object.entries(userData).forEach(([key, value]) => {
152-
if (key !== "username" && key!=="_id") {
153-
if (key === "avgPoints" || key === "avgTime") {
154-
expect(screen.getByText(value.toFixed(2))).toBeInTheDocument();
155-
} else if (key === "ratioCorrect") {
156-
expect(screen.getByText(value.toFixed(2) + "%")).toBeInTheDocument();
157-
}
158-
}
159-
});
160-
});
161-
162161
test("fetches and displays user statistics for Human Calculator mode", async () => {
163162
localStorage.setItem("username", "testUser");
164163

0 commit comments

Comments
 (0)