Skip to content

Commit

Permalink
tests: adiciona testes em componentes
Browse files Browse the repository at this point in the history
  • Loading branch information
clara-ribeiro committed Dec 17, 2024
1 parent 535dbe5 commit 8df43c5
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 108 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"vitest-sonar-reporter": "^2.0.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/user-event": "^14.5.2",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
Expand Down
34 changes: 34 additions & 0 deletions src/Components/AdvantagesCard/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import AdvantagesCard from "./index";

describe("AdvantagesCard", () => {
it("should render the title correctly", () => {
const mockTitle = "Benefícios exclusivos";

render(<AdvantagesCard title={mockTitle} onClick={() => {}} />);

const titleElement = screen.getByText(mockTitle);
expect(titleElement).toBeInTheDocument();
});

it("should call onClick when 'Saber mais' is clicked", () => {
const mockOnClick = vi.fn();
const mockTitle = "Benefícios exclusivos";

render(<AdvantagesCard title={mockTitle} onClick={mockOnClick} />);

const linkElement = screen.getByText("Saber mais");
fireEvent.click(linkElement);

expect(mockOnClick).toHaveBeenCalled();
});

it("should render without crashing when no props are provided", () => {
render(<AdvantagesCard />);
const linkElement = screen.getByText("Saber mais");

expect(linkElement).toBeInTheDocument();
});
});
71 changes: 71 additions & 0 deletions src/Components/AdvantagesModal/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import AdvantagesModal from "./index";

describe("AdvantagesModal", () => {
const mockTitle = "Vantagens do Sindicato";
const mockDescription = "Este é o detalhamento das vantagens oferecidas.";
const mockOnClose = vi.fn();

it("should render the title correctly", () => {
render(
<AdvantagesModal
title={mockTitle}
description={mockDescription}
onClose={mockOnClose}
/>
);

const titleElement = screen.getByText(mockTitle);
expect(titleElement).toBeInTheDocument();
});

it("should render the description correctly", () => {
render(
<AdvantagesModal
title={mockTitle}
description={mockDescription}
onClose={mockOnClose}
/>
);

const descriptionElement = screen.getByText(mockDescription);
expect(descriptionElement).toBeInTheDocument();
});

it("should call onClose when the close button is clicked", () => {
render(
<AdvantagesModal
title={mockTitle}
description={mockDescription}
onClose={mockOnClose}
/>
);

const closeButton = screen.getByText("x");
fireEvent.click(closeButton);

expect(mockOnClose).toHaveBeenCalledTimes(1);
});

it("should render the contact information", () => {
render(
<AdvantagesModal
title={mockTitle}
description={mockDescription}
onClose={mockOnClose}
/>
);

const contactInfo = screen.getByText((content) =>
content.includes("Para mais informações, entre em contato com o Sindicato pelo número")
);

const phoneNumber = screen.getByText("(61) 3321-1949");

expect(contactInfo).toBeInTheDocument();
expect(phoneNumber).toBeInTheDocument();
});

});
48 changes: 48 additions & 0 deletions src/Pages/Public/ChangePasswordPage/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "@testing-library/jest-dom";
import { render, screen, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import ChangePasswordPage from "./index";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { verifyToken } from "../../../Services/userService";

// Mock das funções do userService
vi.mock("../../../Services/userService", () => ({
verifyToken: vi.fn(),
changePasswordById: vi.fn(),
}));

describe("ChangePasswordPage", () => {

const renderComponent = (token) => {
render(
<MemoryRouter initialEntries={[`/change-password/${token}`]}>
<Routes>
<Route path="/change-password/:token" element={<ChangePasswordPage />} />
</Routes>
</MemoryRouter>
);
};

beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(window, "alert").mockImplementation(() => {});
});

afterEach(() => {
window.alert.mockRestore();
});

it("should render the expired message when the token is invalid", async () => {
verifyToken.mockRejectedValueOnce(new Error("Token inválido"));

renderComponent("invalid-token");

await waitFor(() => {
expect(
screen.getByText("Essa página expirou ou não existe...")
).toBeInTheDocument();
});

expect(verifyToken).toHaveBeenCalledWith("invalid-token");
});
});
102 changes: 0 additions & 102 deletions src/Services/memberShipService.test.js

This file was deleted.

0 comments on commit 8df43c5

Please sign in to comment.