|
| 1 | +import store from "store"; |
| 2 | +import React from "react"; |
| 3 | +import { Provider } from "react-redux"; |
| 4 | +import type { ImageComponentProps } from "./"; |
| 5 | +import ImageComponent from "./"; |
| 6 | +import { render, fireEvent, waitFor } from "@testing-library/react"; |
| 7 | +import { urlToBase64 } from "../helper"; |
| 8 | + |
| 9 | +let container: HTMLDivElement | null; |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + container = document.createElement("div"); |
| 13 | + document.body.appendChild(container); |
| 14 | +}); |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + document.body.removeChild(container as Node); |
| 18 | + container = null; |
| 19 | +}); |
| 20 | + |
| 21 | +const mockData = "base64data"; |
| 22 | + |
| 23 | +jest.mock("../helper.ts", () => ({ |
| 24 | + ...jest.requireActual("../helper.ts"), |
| 25 | + urlToBase64: jest.fn(async (url) => { |
| 26 | + if (!url) return Promise.resolve(""); |
| 27 | + return Promise.resolve(`data:image/jpeg;base64,${mockData}`); |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +describe("<ImageComponent />", () => { |
| 32 | + const imageUrl = "https://assets.appsmith.com/widgets/default.png"; |
| 33 | + const defaultImageProps: ImageComponentProps = { |
| 34 | + imageUrl: imageUrl, |
| 35 | + enableDownload: true, |
| 36 | + defaultImageUrl: imageUrl, |
| 37 | + isLoading: false, |
| 38 | + maxZoomLevel: 0, |
| 39 | + objectFit: "", |
| 40 | + disableDrag: () => {}, |
| 41 | + borderRadius: "", |
| 42 | + widgetId: "", |
| 43 | + }; |
| 44 | + |
| 45 | + it("1. renders the image", async () => { |
| 46 | + const { container } = render( |
| 47 | + <Provider store={store}> |
| 48 | + <ImageComponent {...defaultImageProps} /> |
| 49 | + </Provider>, |
| 50 | + ); |
| 51 | + const imageContainer = container.querySelector("img"); |
| 52 | + expect(imageContainer).not.toBeNull(); |
| 53 | + expect(imageContainer?.getAttribute("src")).toBe(imageUrl); |
| 54 | + }); |
| 55 | + |
| 56 | + it("2. renders the download button on hover", async () => { |
| 57 | + const { container, getByTestId } = render( |
| 58 | + <Provider store={store}> |
| 59 | + <ImageComponent {...defaultImageProps} /> |
| 60 | + </Provider>, |
| 61 | + ); |
| 62 | + const imageContainer = container.querySelector("img"); |
| 63 | + expect(imageContainer).not.toBeNull(); |
| 64 | + expect(imageContainer?.getAttribute("src")).toBe(imageUrl); |
| 65 | + fireEvent.mouseEnter(imageContainer as Element); |
| 66 | + expect(getByTestId("t--image-download")).not.toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("3. downloads the image on click", async () => { |
| 70 | + const { container, getByTestId } = render( |
| 71 | + <Provider store={store}> |
| 72 | + <ImageComponent {...defaultImageProps} /> |
| 73 | + </Provider>, |
| 74 | + ); |
| 75 | + |
| 76 | + const imageContainer = container.querySelector("img"); |
| 77 | + expect(imageContainer).not.toBeNull(); |
| 78 | + expect(imageContainer?.getAttribute("src")).toBe(imageUrl); |
| 79 | + fireEvent.mouseEnter(imageContainer as Element); |
| 80 | + const downloadButton = getByTestId( |
| 81 | + "t--image-download", |
| 82 | + ) as HTMLAnchorElement; |
| 83 | + expect(downloadButton).not.toBeNull(); |
| 84 | + |
| 85 | + // Wait for the state to be updated |
| 86 | + await waitFor(() => expect(urlToBase64).toHaveBeenCalled()); |
| 87 | + |
| 88 | + fireEvent.click(downloadButton); |
| 89 | + expect(downloadButton.href).toContain(`data:image/jpeg;base64,${mockData}`); |
| 90 | + }); |
| 91 | + |
| 92 | + it("4. does not render download button if both image URL and default image URL is empty", async () => { |
| 93 | + const emptyUrlProps: ImageComponentProps = { |
| 94 | + ...defaultImageProps, |
| 95 | + imageUrl: "", |
| 96 | + defaultImageUrl: "", |
| 97 | + }; |
| 98 | + |
| 99 | + const { container, queryByTestId } = render( |
| 100 | + <Provider store={store}> |
| 101 | + <ImageComponent {...emptyUrlProps} /> |
| 102 | + </Provider>, |
| 103 | + ); |
| 104 | + |
| 105 | + const imageContainer = container.querySelector("img"); |
| 106 | + expect(imageContainer).not.toBeNull(); |
| 107 | + expect(imageContainer?.getAttribute("src")).toBe(""); |
| 108 | + |
| 109 | + fireEvent.mouseEnter(imageContainer as Element); |
| 110 | + |
| 111 | + await waitFor(() => expect(urlToBase64).toHaveBeenCalledWith("")); |
| 112 | + |
| 113 | + expect(queryByTestId("t--image-download")).toBeNull(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments