import { fireEvent, render } from "@testing-library/react"; import Modal, { IModalProps } from "../../components/modal"; const iconProps: IModalProps = { element: "icon", icon: "bug_report", title: "Test Modal", onClose: jest.fn(), children:

Hello

, }; const invalidIconProps: IModalProps = { element: "icon", title: "Test Modal", onClose: jest.fn(), children:

Hello

, }; const textProps: IModalProps = { element: "text", text: "Test", title: "Test Modal", onClose: jest.fn(), children:

Hello

, }; const noneProps: IModalProps = { element: "none", title: "Test Modal", children:

Hello

, }; const setup = (props: IModalProps) => { const modal = render( {props.children} , ); return modal; }; describe("modal.tsx", () => { it("Tests modal with icon button", () => { const modal = setup(iconProps); expect(modal.asFragment).toMatchSnapshot(); fireEvent.click(modal.getByTestId("toggle-button")); fireEvent.click(modal.getByTestId("close-button")); expect(iconProps.onClose).toHaveBeenCalledTimes(2); }); it("Tests modal with text button", () => { const modal = setup(textProps); expect(modal.asFragment).toMatchSnapshot(); fireEvent.click(modal.getByTestId("toggle-button")); fireEvent.click(modal.getByTestId("close-button")); expect(textProps.onClose).toHaveBeenCalledTimes(2); }); it("Tests modal with neither", () => { const modal = setup(noneProps); expect(modal.asFragment).toMatchSnapshot(); }); it("Tests modal with icon", () => { const modal = setup(invalidIconProps); expect(modal.asFragment).toMatchSnapshot(); fireEvent.click(modal.getByTestId("toggle-button")); fireEvent.click(modal.getByTestId("close-button")); expect(invalidIconProps.onClose).toHaveBeenCalledTimes(2); }); });