EvalueTonSavoir/client/src/__tests__/components/ImageGallery/ImageGallery.test.tsx

55 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-03-20 23:19:54 -04:00
import React, { act } from "react";
2025-03-06 20:33:57 -05:00
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
2025-03-20 23:19:54 -04:00
import ImageGallery from "../../../components/ImageGallery/ImageGallery";
2025-03-06 20:33:57 -05:00
import ApiService from "../../../services/ApiService";
import { Images } from "../../../Types/Images";
import "@testing-library/jest-dom";
jest.mock("../../../services/ApiService");
const mockImages: Images[] = [
{ id: "1", file_name: "image1.jpg", mime_type: "image/jpeg", file_content: "mockBase64Content1" },
{ id: "2", file_name: "image2.jpg", mime_type: "image/jpeg", file_content: "mockBase64Content2" },
{ id: "3", file_name: "image3.jpg", mime_type: "image/jpeg", file_content: "mockBase64Content3" },
];
2025-03-20 23:19:54 -04:00
beforeAll(() => {
Object.assign(navigator, {
clipboard: {
writeText: jest.fn(),
},
2025-03-06 20:33:57 -05:00
});
2025-03-20 23:19:54 -04:00
});
2025-03-06 20:33:57 -05:00
2025-03-20 23:19:54 -04:00
describe("ImageGallery", () => {
beforeEach(() => {
(ApiService.getUserImages as jest.Mock).mockResolvedValue({ images: mockImages, total: 3 });
(ApiService.deleteImage as jest.Mock).mockResolvedValue(true);
(ApiService.uploadImage as jest.Mock).mockResolvedValue('mockImageUrl');
2025-03-06 20:33:57 -05:00
2025-03-20 23:19:54 -04:00
render(<ImageGallery />);
2025-03-06 20:33:57 -05:00
});
2025-03-20 23:19:54 -04:00
it("should render images correctly", async () => {
2025-03-06 20:33:57 -05:00
await act(async () => {
2025-03-20 23:19:54 -04:00
await screen.findByText("Gallery");
});
2025-03-06 20:33:57 -05:00
2025-03-20 23:19:54 -04:00
expect(screen.getByAltText("Image image1.jpg")).toBeInTheDocument();
expect(screen.getByAltText("Image image2.jpg")).toBeInTheDocument();
2025-03-06 20:33:57 -05:00
});
2025-03-20 23:19:54 -04:00
it("should handle copy action", async () => {
const handleCopyMock = jest.fn();
2025-03-06 20:33:57 -05:00
2025-03-20 23:19:54 -04:00
render(<ImageGallery handleCopy={handleCopyMock} />);
const copyButtons = await waitFor(() => screen.findAllByTestId(/gallery-tab-copy-/));
await act(async () => {
fireEvent.click(copyButtons[0]);
2025-03-06 20:33:57 -05:00
});
2025-03-20 23:19:54 -04:00
expect(navigator.clipboard.writeText).toHaveBeenCalled();
2025-03-13 18:02:26 -04:00
});
2025-03-06 20:33:57 -05:00
});