Skip to content

Commit

Permalink
test: corrected the modal testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
sanuj21 committed Oct 16, 2024
1 parent dbf2c49 commit d20d2f2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/components/modal/__tests__/modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from "react";
import {render, fireEvent} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {Button} from "@nextui-org/button";

import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter} from "../src";
import {Button} from "../../button/src";
import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter, useDisclosure} from "../src";

// e.g. console.error Warning: Function components cannot be given refs.
// Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Expand Down Expand Up @@ -92,31 +92,36 @@ describe("Modal", () => {
expect(onClose).toHaveBeenCalled();
});

test("should fire 'onOpenChange' callback when open button is clicked", async () => {
const onOpen = jest.fn();
const ModalWrapper = ({onOpenChange}) => {
const {isOpen, onOpen} = useDisclosure();

const {getByLabelText} = render(
return (
<>
<Button aria-label="Open" onClick={onOpen}>
Open Modal
</Button>
<Modal isOpen>
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
<ModalContent>
<ModalHeader>Modal header</ModalHeader>
<ModalBody>Modal body</ModalBody>
<ModalFooter>Modal footer</ModalFooter>
</ModalContent>
</Modal>
</>,
</>
);
};

const openButton = getByLabelText("Open");
test("should fire 'onOpenChange' callback when open button is clicked and modal opens", async () => {
const onOpenChange = jest.fn();

const {getByLabelText} = render(<ModalWrapper onOpenChange={onOpenChange} />);

const openButton = getByLabelText("Open");
const user = userEvent.setup();

await user.click(openButton);

expect(onOpen).toHaveBeenCalled();
expect(onOpenChange).toHaveBeenCalled();
});

it("should hide the modal when pressing the escape key", () => {
Expand Down

0 comments on commit d20d2f2

Please sign in to comment.