Skip to content

Commit

Permalink
Merge pull request #15 from fga-eps-mds/tests/cobertura-front
Browse files Browse the repository at this point in the history
Tests/cobertura front
  • Loading branch information
clara-ribeiro authored Dec 17, 2024
2 parents 2a8d3ba + 80b93ce commit b67a30b
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Components/Checkfield/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import CheckField from "./index";

describe("CheckField Component", () => {
it("should render the component with the correct label", () => {
render(<CheckField label="Test Label" />);

const labelElement = screen.getByText("Test Label");
expect(labelElement).toBeInTheDocument();
});

it("should render as checked when 'checked' prop is true", () => {
render(<CheckField label="Test Label" checked={true} />);

const checkbox = screen.getByRole("checkbox");
expect(checkbox).toBeChecked();
});

it("should render as unchecked when 'checked' prop is false", () => {
render(<CheckField label="Test Label" checked={false} />);

const checkbox = screen.getByRole("checkbox");
expect(checkbox).not.toBeChecked();
});

it("should toggle the checkbox when clicked", () => {
render(<CheckField label="Test Label" checked={false} />);

const checkbox = screen.getByRole("checkbox");
expect(checkbox).not.toBeChecked();

fireEvent.click(checkbox);

expect(checkbox).toBeChecked();

fireEvent.click(checkbox);

expect(checkbox).not.toBeChecked();
});

it("should call the 'onChange' function when clicked", () => {
const onChangeMock = vi.fn();
render(<CheckField label="Test Label" onChange={onChangeMock} />);

const checkboxContainer = screen.getByText("Test Label");

fireEvent.click(checkboxContainer);

expect(onChangeMock).toHaveBeenCalledWith(true);

fireEvent.click(checkboxContainer);

expect(onChangeMock).toHaveBeenCalledWith(false);
});

it("should update the state when 'checked' prop changes", () => {
const { rerender } = render(
<CheckField label="Test Label" checked={false} />
);

const checkbox = screen.getByRole("checkbox");
expect(checkbox).not.toBeChecked();

rerender(<CheckField label="Test Label" checked={true} />);
expect(checkbox).toBeChecked();
});
});
135 changes: 135 additions & 0 deletions src/Components/Checklist/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import "@testing-library/jest-dom";
import {
render,
screen,
fireEvent,
waitFor,
within,
} from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import CheckList from "./index";

vi.mock("../BigModal", () => ({
__esModule: true,
default: ({ show, handleClose, children }) => (
<div data-testid="big-modal" style={{ display: show ? "block" : "none" }}>
<button onClick={handleClose}>Close</button>
{children}
</div>
),
}));

describe("CheckList Component", () => {
const mockOnChange = vi.fn();
const items = [
{
_id: "1",
name: "John Doe",
email: "[email protected]",
religion: "Christian",
phone: "123456789",
cpf: "123.456.789-00",
birthDate: "1990-01-01",
sex: "Male",
address: "123 Main St",
naturalness: "City A",
uf_naturalidade: "SP",
marialStatus: "Single",
education: "Bachelor",
lotacao: "Office",
position: "Manager",
shipperOrganization: "Org A",
hiringDate: "2022-01-01",
dependents: [1, 2],
},
{
_id: "2",
name: "Jane Doe",
email: "[email protected]",
religion: "Catholic",
phone: "987654321",
cpf: "987.654.321-00",
birthDate: "1995-05-15",
sex: "Female",
address: "456 Elm St",
naturalness: "City B",
uf_naturalidade: "RJ",
marialStatus: "Married",
education: "Master",
lotacao: "HQ",
position: "Director",
shipperOrganization: "Org B",
hiringDate: "2023-06-01",
dependents: [],
},
];

const selectedValues = ["1"];

beforeEach(() => {
vi.clearAllMocks();
});

it("should render the list of items correctly", () => {
render(
<CheckList items={items} value={selectedValues} onChange={mockOnChange} />
);

expect(screen.getByText("John Doe")).toBeInTheDocument();
expect(screen.getByText("Jane Doe")).toBeInTheDocument();

const checkboxes = screen.getAllByRole("checkbox");
expect(checkboxes).toHaveLength(2);
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
});

it("should call onChange with updated values when a checkbox is clicked", () => {
render(
<CheckList items={items} value={selectedValues} onChange={mockOnChange} />
);

const secondCheckbox = screen.getAllByRole("checkbox")[1];
fireEvent.click(secondCheckbox);

expect(mockOnChange).toHaveBeenCalledWith(["1", "2"]);
});

it("should open the modal and display user details when an item is clicked", async () => {
render(
<CheckList items={items} value={selectedValues} onChange={mockOnChange} />
);

fireEvent.click(screen.getByText("John Doe"));

const modal = screen.getByTestId("big-modal");
expect(modal).toBeVisible();

const modalContent = within(modal);

expect(modalContent.getByText("Nome:")).toBeInTheDocument();
expect(modalContent.getByText("John Doe")).toBeInTheDocument();
expect(modalContent.getByText("Email:")).toBeInTheDocument();
expect(modalContent.getByText("[email protected]")).toBeInTheDocument();
expect(modalContent.getByText("Religião:")).toBeInTheDocument();
expect(modalContent.getByText("Christian")).toBeInTheDocument();
expect(modalContent.getByText("Dependentes:")).toBeInTheDocument();
expect(modalContent.getByText("2")).toBeInTheDocument();
});

it("should close the modal when the close button is clicked", async () => {
render(
<CheckList items={items} value={selectedValues} onChange={mockOnChange} />
);

fireEvent.click(screen.getByText("John Doe"));
const modal = screen.getByTestId("big-modal");
expect(modal).toBeVisible();

fireEvent.click(screen.getByText("Close"));

await waitFor(() => {
expect(modal).not.toBeVisible();
});
});
});
55 changes: 55 additions & 0 deletions src/Components/Footer/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import Footer from "./index";
import { describe, it, expect } from "vitest";

describe("Footer Component", () => {
it("should render the Sindpol logo", () => {
render(<Footer />);

const logo = screen.getByAltText("Sindpol Logo");
expect(logo).toBeInTheDocument();
expect(logo).toHaveAttribute("src", "/src/assets/sindpol-logo.png");
});

it("should render the copyright text", () => {
render(<Footer />);

expect(
screen.getByText(
"Copyright © 2024 • Sindpol-DF • CNPJ 11.236.674/0001-06"
)
).toBeInTheDocument();

expect(
screen.getByText(
"Setor de Diversões Sul (SDS), Conjunto Baracat Bloco F 27, Salas 313/315 • Asa Sul"
)
).toBeInTheDocument();

expect(screen.getByText("Brasília/DF • CEP 70392-900")).toBeInTheDocument();
});

it("should render all social media links with correct URLs", () => {
render(<Footer />);

const expectedLinks = [
"https://www.facebook.com/sindpoldf",
"https://x.com/sindpoldf",
"https://www.youtube.com/@sindpoldf/videos",
"https://api.whatsapp.com/send/?phone=556133211949",
"https://www.instagram.com/sindpoldf/",
];

const links = screen.getAllByRole("link");

expectedLinks.forEach((expectedHref) => {
const link = links.find(
(link) => link.getAttribute("href") === expectedHref
);
expect(link).toBeInTheDocument();
});

expect(links).toHaveLength(5);
});
});
115 changes: 115 additions & 0 deletions src/Components/SideBar/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { MemoryRouter, useNavigate } from "react-router-dom";
import SideBar from "./index";
import AuthContext from "../../Context/auth";
import { usePermissions } from "../../Utils/permission";

vi.mock("react-router-dom", async () => {
const actual = await vi.importActual("react-router-dom");
return {
...actual,
useNavigate: vi.fn(),
};
});

vi.mock("../../Utils/permission", () => ({
usePermissions: vi.fn(),
checkModule: vi.fn(() => true),
}));

describe("SideBar Component", () => {
const mockNavigate = vi.fn();
const mockLogout = vi.fn();

const renderSideBar = (user = null) => {
useNavigate.mockReturnValue(mockNavigate);
usePermissions.mockReturnValue([]);

render(
<MemoryRouter>
<AuthContext.Provider
value={{
user,
Logout: mockLogout,
}}
>
<SideBar />
</AuthContext.Provider>
</MemoryRouter>
);
};

beforeEach(() => {
vi.clearAllMocks();
});

it("should open sidebar when clicking the first button (menu icon)", () => {
renderSideBar();

const menuIcon = screen.getAllByRole("button")[0];
fireEvent.click(menuIcon);

expect(screen.getByText("PÁGINA INICIAL")).toBeInTheDocument();
});

it("should navigate to 'PÁGINA INICIAL' when button is clicked", () => {
renderSideBar();

const menuIcon = screen.getAllByRole("button")[0];
fireEvent.click(menuIcon);

const homeButton = screen.getByText("PÁGINA INICIAL");
fireEvent.click(homeButton);

expect(mockNavigate).toHaveBeenCalledWith("/home");
});

it("should display user information when user is logged in", () => {
const mockUser = { name: "John Doe", role: "admin" };
renderSideBar(mockUser);

expect(
screen.getByText("Você está logado como John Doe")
).toBeInTheDocument();
});

it("should call Logout and navigate to '/' on logout", () => {
const mockUser = { name: "John Doe" };
renderSideBar(mockUser);

const logoutButton = screen.getByText("LOGOUT");
fireEvent.click(logoutButton);

expect(mockLogout).toHaveBeenCalled();
expect(mockNavigate).toHaveBeenCalledWith("/");
});

it("should navigate to 'FILIAÇÃO' when button is clicked", () => {
renderSideBar();

const menuIcon = screen.getAllByRole("button")[0];
fireEvent.click(menuIcon);

const filiationButton = screen.getByText("FILIAÇÃO");
fireEvent.click(filiationButton);

expect(mockNavigate).toHaveBeenCalledWith("/filiacao");
});

it("should hide restricted buttons if user has no permissions", () => {
usePermissions.mockReturnValue([]);

renderSideBar();

const menuIcon = screen.getAllByRole("button")[0];
fireEvent.click(menuIcon);

const cadastrosButton = screen.getByText("CADASTROS");
expect(cadastrosButton).toBeInTheDocument();

const beneficiosButton = screen.getByText("BENEFÍCIOS");
expect(beneficiosButton).toBeInTheDocument();
});
});
Loading

0 comments on commit b67a30b

Please sign in to comment.