|
| 1 | +import React from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { Dialog } from ".."; |
| 5 | +import { DialogHeader } from "../DialogHeader"; |
| 6 | + |
| 7 | +describe("Dialog component", () => { |
| 8 | + it("should render the basic dialog without header, footer and body", () => { |
| 9 | + render( |
| 10 | + <Dialog isOpen={true}> |
| 11 | + <p>This is some dialog content</p> |
| 12 | + </Dialog>, |
| 13 | + ); |
| 14 | + |
| 15 | + const dialog = screen.getByText("This is some dialog content"); |
| 16 | + |
| 17 | + expect(dialog).toBeInTheDocument(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should render dialog header", () => { |
| 21 | + render( |
| 22 | + <Dialog isOpen={true}> |
| 23 | + <Dialog.Header hideCloseIcon={false} title={"Dialog Title"} /> |
| 24 | + </Dialog>, |
| 25 | + ); |
| 26 | + |
| 27 | + const dialogHeader = screen.getByText("Dialog Title"); |
| 28 | + const dialogCloseIcon = screen.queryByTestId("dt-close-icon"); |
| 29 | + |
| 30 | + expect(dialogHeader).toBeInTheDocument(); |
| 31 | + expect(dialogCloseIcon).toBeDefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should render the dialog header without close icon ", () => { |
| 35 | + render( |
| 36 | + <Dialog isOpen={true}> |
| 37 | + <Dialog.Header hideCloseIcon={true} title={"Dialog Title"} /> |
| 38 | + </Dialog>, |
| 39 | + ); |
| 40 | + |
| 41 | + const dialogHeader = screen.getByText("Dialog Title"); |
| 42 | + const dialogCloseIcon = screen.queryByTestId("dt-close-icon"); |
| 43 | + |
| 44 | + expect(dialogHeader).toBeInTheDocument(); |
| 45 | + expect(dialogCloseIcon).not.toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | +it("should render dialog Footer", () => { |
| 50 | + render( |
| 51 | + <Dialog isOpen={true}> |
| 52 | + <Dialog.Footer>Dialog Footer</Dialog.Footer> |
| 53 | + </Dialog>, |
| 54 | + ); |
| 55 | + |
| 56 | + const dialogFooter = screen.getByText("Dialog Footer"); |
| 57 | + |
| 58 | + expect(dialogFooter).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | +it("should close the modal when the close button in header is clicked", async () => { |
| 62 | + render( |
| 63 | + <Dialog isOpen={true}> |
| 64 | + <DialogHeader title={" Modal Title"} hideCloseIcon={false}></DialogHeader> |
| 65 | + <Dialog.Body> |
| 66 | + <p>This is some dialog content</p> |
| 67 | + </Dialog.Body> |
| 68 | + </Dialog>, |
| 69 | + ); |
| 70 | + |
| 71 | + const modal = await screen.getByText("This is some dialog content"); |
| 72 | + |
| 73 | + const modalCloseIcon = screen.getByTestId("dt-close-icon"); |
| 74 | + |
| 75 | + await userEvent.click(modalCloseIcon); |
| 76 | + |
| 77 | + expect(modal).toBeInTheDocument(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments