From 0f11f14bf6749b1c52760ba815eba3c42126e933 Mon Sep 17 00:00:00 2001 From: daniso0412 <180015222@aluno.unb.br> Date: Sun, 16 Feb 2025 22:32:46 -0300 Subject: [PATCH] teste do componente FieldFile criado --- src/Components/FieldFile/index.test.jsx | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Components/FieldFile/index.test.jsx diff --git a/src/Components/FieldFile/index.test.jsx b/src/Components/FieldFile/index.test.jsx new file mode 100644 index 00000000..e761b3c7 --- /dev/null +++ b/src/Components/FieldFile/index.test.jsx @@ -0,0 +1,60 @@ +import { render, screen, fireEvent, act } from "@testing-library/react"; +import FieldFile from "../../Components/FieldFile"; +import { vi } from "vitest"; +import "@testing-library/jest-dom"; + +describe("FieldFile Component", () => { + const mockOnChange = vi.fn(); + + it("deve renderizar corretamente o campo com o label", () => { + render(); + + // Verifica se o label é renderizado corretamente + expect(screen.getByLabelText("Upload File")).toBeInTheDocument(); + + expect(screen.getByRole("button")).toBeInTheDocument(); + }); + + it("deve exibir o nome do arquivo e a pré-visualização após o upload de um arquivo", async () => { + const file = new File(["file content"], "example.jpg", { type: "image/jpeg" }); + render(); + + const inputFile = screen.getByLabelText("Upload File").nextElementSibling.querySelector("input"); + + // Simula a mudança do arquivo + await act(async () => { + fireEvent.change(inputFile, { target: { files: [file] } }); + }); + + // Verifica se o nome do arquivo foi exibido + expect(screen.getByDisplayValue("example.jpg")).toBeInTheDocument(); + + await screen.findByAltText("Preview"); + expect(screen.getByAltText("Preview")).toBeInTheDocument(); + }); + + it("deve chamar onChange com o arquivo correto quando o arquivo for alterado", async () => { + const file = new File(["file content"], "example.jpg", { type: "image/jpeg" }); + render(); + + const inputFile = screen.getByLabelText("Upload File").nextElementSibling.querySelector("input"); + + // Simula a mudança do arquivo + await act(async () => { + fireEvent.change(inputFile, { target: { files: [file] } }); + }); + + // Verifica se a função onChange foi chamada com o arquivo correto + expect(mockOnChange).toHaveBeenCalledWith(file); + }); + + + + it("deve exibir a pré-visualização da imagem se o valor for uma URL de imagem", () => { + const imageUrl = "https://example.com/image.jpg"; + render(); + + // Verifica se a imagem de pré-visualização está sendo renderizada + expect(screen.getByAltText("Preview")).toHaveAttribute("src", imageUrl); + }); +});