From 8df43c5c18dec75609b5833f5f6d64a5f3b2d5ce Mon Sep 17 00:00:00 2001 From: Clara Ribeiro Date: Tue, 17 Dec 2024 06:39:47 -0300 Subject: [PATCH] tests: adiciona testes em componentes --- package-lock.json | 9 +- package.json | 2 +- src/Components/AdvantagesCard/index.test.jsx | 34 ++++++ src/Components/AdvantagesModal/index.test.jsx | 71 ++++++++++++ .../Public/ChangePasswordPage/index.test.jsx | 48 +++++++++ src/Services/memberShipService.test.js | 102 ------------------ 6 files changed, 158 insertions(+), 108 deletions(-) create mode 100644 src/Components/AdvantagesCard/index.test.jsx create mode 100644 src/Components/AdvantagesModal/index.test.jsx create mode 100644 src/Pages/Public/ChangePasswordPage/index.test.jsx delete mode 100644 src/Services/memberShipService.test.js diff --git a/package-lock.json b/package-lock.json index 83bc3192..4ed501ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,7 +34,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", @@ -1696,13 +1696,12 @@ } }, "node_modules/@testing-library/jest-dom": { - "version": "6.4.8", - "resolved": "https://registry.npmjs.org/@testing-library/jest-dom/-/jest-dom-6.4.8.tgz", - "integrity": "sha512-JD0G+Zc38f5MBHA4NgxQMR5XtO5Jx9g86jqturNTt2WUfRmLDIY7iKkWHDCCTiDuFMre6nxAD5wHw9W5kI4rGw==", + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/@testing-library/jest-dom/-/jest-dom-6.6.3.tgz", + "integrity": "sha512-IteBhl4XqYNkM54f4ejhLRJiZNqcSCoXUOG2CPK7qbD322KjQozM4kHQOfkG2oln9b9HTYqs+Sae8vBATubxxA==", "dev": true, "dependencies": { "@adobe/css-tools": "^4.4.0", - "@babel/runtime": "^7.9.2", "aria-query": "^5.0.0", "chalk": "^3.0.0", "css.escape": "^1.5.1", diff --git a/package.json b/package.json index ec2ec0ee..0290754b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Components/AdvantagesCard/index.test.jsx b/src/Components/AdvantagesCard/index.test.jsx new file mode 100644 index 00000000..f38cd9e6 --- /dev/null +++ b/src/Components/AdvantagesCard/index.test.jsx @@ -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( {}} />); + + 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(); + + const linkElement = screen.getByText("Saber mais"); + fireEvent.click(linkElement); + + expect(mockOnClick).toHaveBeenCalled(); + }); + + it("should render without crashing when no props are provided", () => { + render(); + const linkElement = screen.getByText("Saber mais"); + + expect(linkElement).toBeInTheDocument(); + }); +}); diff --git a/src/Components/AdvantagesModal/index.test.jsx b/src/Components/AdvantagesModal/index.test.jsx new file mode 100644 index 00000000..4596c637 --- /dev/null +++ b/src/Components/AdvantagesModal/index.test.jsx @@ -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( + + ); + + const titleElement = screen.getByText(mockTitle); + expect(titleElement).toBeInTheDocument(); + }); + + it("should render the description correctly", () => { + render( + + ); + + const descriptionElement = screen.getByText(mockDescription); + expect(descriptionElement).toBeInTheDocument(); + }); + + it("should call onClose when the close button is clicked", () => { + render( + + ); + + const closeButton = screen.getByText("x"); + fireEvent.click(closeButton); + + expect(mockOnClose).toHaveBeenCalledTimes(1); + }); + + it("should render the contact information", () => { + render( + + ); + + 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(); + }); + +}); diff --git a/src/Pages/Public/ChangePasswordPage/index.test.jsx b/src/Pages/Public/ChangePasswordPage/index.test.jsx new file mode 100644 index 00000000..0acfaf7c --- /dev/null +++ b/src/Pages/Public/ChangePasswordPage/index.test.jsx @@ -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( + + + } /> + + + ); + }; + + 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"); + }); +}); \ No newline at end of file diff --git a/src/Services/memberShipService.test.js b/src/Services/memberShipService.test.js deleted file mode 100644 index 01e18f24..00000000 --- a/src/Services/memberShipService.test.js +++ /dev/null @@ -1,102 +0,0 @@ -import { describe, it, expect, vi, beforeEach } from "vitest"; -import { - createMemberShip, - getMemberShip, - getMemberShipById, - updateMemberStatus, - updateMembership, - deleteMember, -} from "./memberShipService"; -import { APIUsers } from "./BaseService"; - -vi.mock("../services/BaseService", () => ({ - APIUsers: { - post: vi.fn(), - get: vi.fn(), - patch: vi.fn(), - delete: vi.fn(), - }, -})); - -describe("MembershipService", () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - it("deve retornar o status correto ao criar uma filiação", async () => { - APIUsers.post.mockResolvedValueOnce({ status: 201 }); - - const result = await createMemberShip({ name: "John Doe" }); - - expect(result).toBe(201); - expect(APIUsers.post).toHaveBeenCalledWith("membership/create", { - formData: { name: "John Doe" }, - }); - }); - - it("deve retornar dados da filiação com base no status", async () => { - const mockData = [{ id: 1, status: "active" }]; - APIUsers.get.mockResolvedValueOnce({ data: mockData }); - - const result = await getMemberShip("active"); - - expect(result).toEqual(mockData); - expect(APIUsers.get).toHaveBeenCalledWith("membership", { - params: { status: "active" }, - }); - }); - - it("deve retornar dados da filiação pelo ID", async () => { - const mockData = { id: 1, name: "John Doe" }; - APIUsers.get.mockResolvedValueOnce({ data: mockData }); - - const result = await getMemberShipById(1); - - expect(result).toEqual(mockData); - expect(APIUsers.get).toHaveBeenCalledWith("membership/1"); - }); - - it("deve atualizar o status da filiação e retornar dados", async () => { - const mockResponse = { success: true }; - APIUsers.patch.mockResolvedValueOnce({ data: mockResponse }); - - const result = await updateMemberStatus(1, { status: "inactive" }); - - expect(result).toEqual(mockResponse); - expect(APIUsers.patch).toHaveBeenCalledWith("membership/updateStatus/1", { - formData: { status: "inactive" }, - }); - }); - - it("deve retornar false quando atualizar a filiação com sucesso", async () => { - APIUsers.patch.mockResolvedValueOnce({}); - - const result = await updateMembership(1, { name: "John Updated" }); - - expect(result).toBe(false); - expect(APIUsers.patch).toHaveBeenCalledWith("membership/update/1", { - formData: { name: "John Updated" }, - }); - }); - - it("deve retornar false ao deletar a filiação com sucesso", async () => { - APIUsers.delete.mockResolvedValueOnce({}); - - const result = await deleteMember(1); - - expect(result).toBe(false); - expect(APIUsers.delete).toHaveBeenCalledWith("membership/delete/1"); - }); - - it("deve retornar erro ao falhar no createMemberShip", async () => { - const mockError = { response: { data: { erro: "Erro na requisição" } } }; - APIUsers.post.mockRejectedValueOnce(mockError); - - const result = await createMemberShip({ name: "John Doe" }); - - expect(result).toBe("Erro na requisição"); - expect(APIUsers.post).toHaveBeenCalledWith("membership/create", { - formData: { name: "John Doe" }, - }); - }); -});