Skip to content

Commit

Permalink
teste do componente FieldFile criado
Browse files Browse the repository at this point in the history
  • Loading branch information
daniso0412 committed Feb 17, 2025
1 parent 7a98cf1 commit 0f11f14
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Components/FieldFile/index.test.jsx
Original file line number Diff line number Diff line change
@@ -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", () => {

Check failure on line 6 in src/Components/FieldFile/index.test.jsx

View workflow job for this annotation

GitHub Actions / lint

'describe' is not defined
const mockOnChange = vi.fn();

it("deve renderizar corretamente o campo com o label", () => {

Check failure on line 9 in src/Components/FieldFile/index.test.jsx

View workflow job for this annotation

GitHub Actions / lint

'it' is not defined
render(<FieldFile label="Upload File" onChange={mockOnChange} />);

// Verifica se o label é renderizado corretamente
expect(screen.getByLabelText("Upload File")).toBeInTheDocument();

Check failure on line 13 in src/Components/FieldFile/index.test.jsx

View workflow job for this annotation

GitHub Actions / lint

'expect' is not defined

expect(screen.getByRole("button")).toBeInTheDocument();

Check failure on line 15 in src/Components/FieldFile/index.test.jsx

View workflow job for this annotation

GitHub Actions / lint

'expect' is not defined
});

it("deve exibir o nome do arquivo e a pré-visualização após o upload de um arquivo", async () => {

Check failure on line 18 in src/Components/FieldFile/index.test.jsx

View workflow job for this annotation

GitHub Actions / lint

'it' is not defined
const file = new File(["file content"], "example.jpg", { type: "image/jpeg" });
render(<FieldFile label="Upload File" onChange={mockOnChange} />);

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(<FieldFile label="Upload File" onChange={mockOnChange} />);

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(<FieldFile label="Upload File" onChange={mockOnChange} value={imageUrl} />);

// Verifica se a imagem de pré-visualização está sendo renderizada
expect(screen.getByAltText("Preview")).toHaveAttribute("src", imageUrl);
});
});

0 comments on commit 0f11f14

Please sign in to comment.