/* * Copyright 2023 Avaiga Private Limited * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import React from "react"; import { render } from "@testing-library/react"; import "@testing-library/jest-dom"; import userEvent from "@testing-library/user-event"; import axios from "axios"; import Dialog from "./Dialog"; import { TaipyContext } from "../../context/taipyContext"; import { TaipyState, INITIAL_STATE } from "../../context/taipyReducers"; import { HelmetProvider } from "react-helmet-async"; jest.mock("axios"); const mockedAxios = axios as jest.Mocked; mockedAxios.get.mockRejectedValue("Network error: Something went wrong"); mockedAxios.get.mockResolvedValue({ data: { jsx_no: '
' } }); jest.mock("react-router-dom", () => ({ ...jest.requireActual("react-router-dom"), useLocation: () => ({ pathname: "pathname", }), })); beforeEach(() => { mockedAxios.get.mockClear(); }); describe("Dialog Component", () => { it("renders when value is true", async () => { const { getByText } = render( ); const elt = getByText("Dialog-Test-Title"); expect(elt.tagName).toBe("H2"); expect(mockedAxios.get).toHaveBeenCalled(); }); it("renders nothing when value is false", async () => { const { queryAllByText } = render( ); expect(queryAllByText("Dialog-Test-Title")).toHaveLength(0); const divs = document.getElementsByTagName("div"); expect(divs).toHaveLength(1); expect(divs[0].childElementCount).toBe(0); }); it("displays the right info for class", async () => { const wrapper = render( ); const elt = document.querySelector(".MuiDialog-root"); expect(elt).toHaveClass("taipy-dialog"); }); it("displays the default value", async () => { const { getByText } = render( ); getByText("Dialog-Test-Title"); }); it("doesn't show a button by default", async () => { const { getAllByRole } = render( ); expect(getAllByRole("button")).toHaveLength(1); }); it("shows one button when a label set", async () => { const { getAllByRole } = render( ); expect(getAllByRole("button")).toHaveLength(2); }); it("shows 3 buttons when 3 labels set", async () => { const { getAllByRole } = render( ); expect(getAllByRole("button")).toHaveLength(4); }); it("is disabled", async () => { const { getByText } = render( ); expect(getByText("testValidate")).toBeDisabled(); expect(getByText("testCancel")).toBeDisabled(); }); it("is enabled by default", async () => { const { getByText } = render( ); expect(getByText("testValidate")).not.toBeDisabled(); expect(getByText("testCancel")).not.toBeDisabled(); }); it("is enabled by active", async () => { const { getByText } = render( ); expect(getByText("testValidate")).not.toBeDisabled(); expect(getByText("testCancel")).not.toBeDisabled(); }); it("dispatch a well formed message on close icon press", async () => { const dispatch = jest.fn(); const state: TaipyState = INITIAL_STATE; const { getByTitle } = render( ); await userEvent.click(getByTitle("Close")); expect(dispatch).toHaveBeenLastCalledWith({ name: "testId", payload: { action: "testValidateAction", args: [-1] }, type: "SEND_ACTION_ACTION", }); }); it("dispatch a well formed message on first button press", async () => { const dispatch = jest.fn(); const state: TaipyState = INITIAL_STATE; const { getByText } = render( ); await userEvent.click(getByText("testValidate")); expect(dispatch).toHaveBeenLastCalledWith({ name: "testId", payload: { action: "testValidateAction", args: [0] }, type: "SEND_ACTION_ACTION", }); }); it("dispatch a well formed message on third button press", async () => { const dispatch = jest.fn(); const state: TaipyState = INITIAL_STATE; const { getByText } = render( ); await userEvent.click(getByText("Another One")); expect(dispatch).toHaveBeenLastCalledWith({ name: "testId", payload: { action: "testValidateAction", args: [2] }, type: "SEND_ACTION_ACTION", }); }); });