|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 3 | +import { Dialog } from "./Dialog"; |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | +import { createChangeHandler } from "@/plugins/mui/common.test"; |
| 6 | + |
| 7 | +describe("Dialog", () => { |
| 8 | + it("should render the Dialog component with title and content", () => { |
| 9 | + render( |
| 10 | + <Dialog |
| 11 | + id="test-dialog" |
| 12 | + type="Dialog" |
| 13 | + open={true} |
| 14 | + title="Test Title" |
| 15 | + content="Test Content" |
| 16 | + onChange={() => {}} |
| 17 | + />, |
| 18 | + ); |
| 19 | + |
| 20 | + expect(screen.getByRole("dialog")).toBeInTheDocument(); |
| 21 | + expect(screen.getByText("Test Title")).toBeInTheDocument(); |
| 22 | + expect(screen.getByText("Test Content")).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should not render the Dialog if open is false", () => { |
| 26 | + render( |
| 27 | + <Dialog |
| 28 | + id="test-dialog" |
| 29 | + type="Dialog" |
| 30 | + open={false} |
| 31 | + onChange={() => {}} |
| 32 | + />, |
| 33 | + ); |
| 34 | + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should handle onClose event and call onChange", () => { |
| 38 | + const { recordedEvents, onChange } = createChangeHandler(); |
| 39 | + |
| 40 | + render( |
| 41 | + <Dialog |
| 42 | + id="test-dialog" |
| 43 | + type="Dialog" |
| 44 | + open={true} |
| 45 | + title="Test Title" |
| 46 | + content="Test Content" |
| 47 | + onChange={onChange} |
| 48 | + />, |
| 49 | + ); |
| 50 | + |
| 51 | + const backdrop = screen.getByRole("dialog", { hidden: true }); |
| 52 | + expect(backdrop).toBeInTheDocument(); |
| 53 | + console.log(backdrop); |
| 54 | + fireEvent.click(backdrop); |
| 55 | + |
| 56 | + expect(recordedEvents.length).toBe(1); |
| 57 | + expect(recordedEvents[0]).toEqual({ |
| 58 | + componentType: "Dialog", |
| 59 | + id: "test-dialog", |
| 60 | + property: "value", |
| 61 | + value: expect.any(Object), // Expecting an event object |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should render children within DialogActions", () => { |
| 66 | + render( |
| 67 | + <Dialog |
| 68 | + id="test-dialog" |
| 69 | + type="Dialog" |
| 70 | + open={true} |
| 71 | + title="Test Title" |
| 72 | + content="Test Content" |
| 73 | + children={[<button data-testid="test-button">Test Button</button>]} |
| 74 | + onChange={() => {}} |
| 75 | + ></Dialog>, |
| 76 | + ); |
| 77 | + |
| 78 | + expect( |
| 79 | + screen.getByRole("button", { name: "Test Button" }), |
| 80 | + ).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle onClose event and call onChange with correct data", () => { |
| 84 | + const { recordedEvents, onChange } = createChangeHandler(); |
| 85 | + |
| 86 | + render( |
| 87 | + <Dialog |
| 88 | + id="test-dialog" |
| 89 | + type="Dialog" |
| 90 | + open={true} |
| 91 | + title="Test Title" |
| 92 | + content="Test Content" |
| 93 | + onChange={onChange} |
| 94 | + />, |
| 95 | + ); |
| 96 | + |
| 97 | + const backdrop = screen.getByRole("presentation"); |
| 98 | + expect(backdrop).toBeInTheDocument(); |
| 99 | + fireEvent.click(backdrop); |
| 100 | + |
| 101 | + expect(recordedEvents.length).toBe(1); |
| 102 | + expect(recordedEvents[0]).toEqual({ |
| 103 | + componentType: "Dialog", |
| 104 | + id: "test-dialog", |
| 105 | + property: "value", |
| 106 | + value: expect.any(Object), |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments