/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import React from "react";
import { render, screen, fireEvent, act, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import BackendService from "../../services/BackendService";
import StorageService from "../../services/StorageService";
import EditImage from "../EditImage";
jest.mock("../../services/BackendService");
jest.mock("../../services/StorageService");
jest.mock("../../hooks");
beforeEach(() => {
BackendService.editWidget = jest.fn();
StorageService.downloadFile = jest.fn();
window.URL.createObjectURL = jest.fn();
});
test("renders title", async () => {
render(, { wrapper: MemoryRouter });
expect(await screen.findByRole("heading", { name: "Edit Image" })).toBeInTheDocument();
});
test("renders Image title", async () => {
render(, { wrapper: MemoryRouter });
expect(await screen.findByLabelText("Image title")).toBeInTheDocument();
});
test("renders element descriptions", async () => {
const { getByText } = render(, { wrapper: MemoryRouter });
expect(getByText("Give your image a descriptive title.")).toBeInTheDocument();
expect(getByText("Must be a PNG, JPEG, or SVG file")).toBeInTheDocument();
expect(
getByText(
"Provide a short description of the image for users with visual impairments using a screen reader. This description will not display on the dashboard.",
),
).toBeInTheDocument();
expect(
getByText(
"Give your image a description to explain it in more depth. It can also be read by screen readers to describe the image for those with visual impairments. This field supports markdown.",
),
).toBeInTheDocument();
});
test("renders a file upload input", async () => {
render(, { wrapper: MemoryRouter });
expect(await screen.findByLabelText("File upload")).toBeInTheDocument();
});
test("renders an image size/scale input", async () => {
render(, { wrapper: MemoryRouter });
expect(
await screen.findByText("How your image will display in the dashboard."),
).toBeInTheDocument();
expect(await screen.findByText("As uploaded (do not scale)")).toBeInTheDocument();
});
test("on submit, it calls editWidget api and uploads dataset", async () => {
const { getByRole, getByText, getByLabelText } = render(, {
wrapper: MemoryRouter,
});
const submitButton = getByRole("button", { name: "Save" });
fireEvent.input(getByLabelText("Image title"), {
target: {
value: "Test Image",
},
});
fireEvent.input(getByLabelText("Image alt text"), {
target: {
value: "Test alt text",
},
});
const file = new File(["dummy content"], "filename.png", {
type: "image/png",
});
const uploadFile = getByLabelText("File upload");
Object.defineProperty(uploadFile, "files", { value: [file] });
Object.defineProperty(uploadFile, "value", {
value: file.name,
});
fireEvent.change(uploadFile);
fireEvent.change(getByLabelText("75%"), {
target: {
value: ["75%"],
},
});
await waitFor(() => expect(submitButton).toBeEnabled());
await waitFor(() => {
expect(getByLabelText("Image alt text")).toBeInTheDocument();
expect(
getByText(
"Provide a short description of the image for users with visual impairments using a screen reader. This description will not display on the dashboard.",
),
).toBeInTheDocument();
expect(getByText("Image description (optional)")).toBeInTheDocument();
expect(
getByText(
"Give your image a description to explain it in more depth. It can also be read by screen readers to describe the image for those with visual impairments. This field supports markdown.",
),
).toBeInTheDocument();
});
await act(async () => {
fireEvent.click(submitButton);
});
expect(BackendService.editWidget).toHaveBeenCalled();
expect(StorageService.uploadImage).toHaveBeenCalled();
});
test("renders the expand preview button", async () => {
render(, { wrapper: MemoryRouter });
expect(screen.getByRole("button", { name: "Expand preview" })).toBeInTheDocument();
});