Skip to content

Commit

Permalink
Refactorizando codigo
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Apr 16, 2024
1 parent 8612551 commit f0363bf
Showing 1 changed file with 48 additions and 49 deletions.
97 changes: 48 additions & 49 deletions webapp/src/pages/Stats/Stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ const columnHeaders = [
"Tiempo por pregunta (s)",
];

const checkTableHeader = (headerText) => {
const headerElement = screen.getByText(headerText);
expect(headerElement).toBeInTheDocument();
};

const checkCellValue = (key, value) => {
if (key !== "username" && key !== "_id" && key !== "gamemode" && key!=="__v") {
const formattedValue =
key === "avgPoints" || key === "avgTime"
? value.toFixed(2)
: key === "ratioCorrect"
? value.toFixed(2) + "%"
: value.toString();
const valueElements = screen.getAllByText(formattedValue);
expect(valueElements.length).toBeGreaterThan(0);
}
};

const renderComponentWithRouter = () => {
render(
<I18nextProvider i18n={i18n}>
Expand Down Expand Up @@ -60,30 +78,46 @@ describe("Stats component", () => {

test("fetches user statistics and displays them", async () => {
localStorage.setItem("username", "testUser");

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(userData),
});

renderComponentWithRouter();

await waitFor(async () => {
const table = await screen.findByRole("table");
expect(table).toBeInTheDocument();
});

columnHeaders.forEach((headerText) => {
const headerElement = screen.getByText(headerText);
expect(headerElement).toBeInTheDocument();

columnHeaders.forEach(checkTableHeader);
Object.entries(userData).forEach(([key, value]) => {
checkCellValue(key, value);
});
});

test("updates user statistics when game mode changes", async () => {
localStorage.setItem("username", "testUser");

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(userData),
});

renderComponentWithRouter();

await waitFor(() => {
expect(screen.queryByText("Cargando ...")).not.toBeInTheDocument();
});

const modeButton = screen.getByTestId("calculator-button");
userEvent.click(modeButton);

const table = await screen.findByRole("table");
expect(table).toBeInTheDocument();

columnHeaders.forEach(checkTableHeader);
Object.entries(userData).forEach(([key, value]) => {
if (key !== "username" && key!=="_id") {
if (key === "avgPoints" || key === "avgTime") {
expect(screen.getByText(value.toFixed(2))).toBeInTheDocument();
} else if (key === "ratioCorrect") {
expect(screen.getByText(value.toFixed(2) + "%")).toBeInTheDocument();
}
}
checkCellValue(key, value);
});
});

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

test("updates user statistics when game mode changes", async () => {
localStorage.setItem("username", "testUser");

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(userData),
});

renderComponentWithRouter();

await waitFor(() => {
expect(screen.queryByText("Cargando ...")).not.toBeInTheDocument();
});

const modeButton = screen.getByTestId("calculator-button");
userEvent.click(modeButton);

const table = await screen.findByRole("table");
expect(table).toBeInTheDocument();

columnHeaders.forEach((headerText) => {
const headerElement = screen.getByText(headerText);
expect(headerElement).toBeInTheDocument();
});

Object.entries(userData).forEach(([key, value]) => {
if (key !== "username" && key!=="_id") {
if (key === "avgPoints" || key === "avgTime") {
expect(screen.getByText(value.toFixed(2))).toBeInTheDocument();
} else if (key === "ratioCorrect") {
expect(screen.getByText(value.toFixed(2) + "%")).toBeInTheDocument();
}
}
});
});

test("fetches and displays user statistics for Human Calculator mode", async () => {
localStorage.setItem("username", "testUser");

Expand Down

0 comments on commit f0363bf

Please sign in to comment.