|
| 1 | +import { cleanup, render, fireEvent } from "@testing-library/react" |
| 2 | +import * as React from "react" |
| 3 | +import InformationModal, { InformationModalProps } from "./InformationModal" |
| 4 | + |
| 5 | +afterEach(cleanup) |
| 6 | + |
| 7 | +const InformationModalWrapper = (props: Partial<InformationModalProps>) => ( |
| 8 | + <InformationModal |
| 9 | + isOpen={true} |
| 10 | + title="Example modal title" |
| 11 | + onConfirm={() => undefined} |
| 12 | + onDismiss={() => undefined} |
| 13 | + children="Example modal body" |
| 14 | + secondaryLabel="Example secondary" |
| 15 | + onSecondaryAction={() => undefined} |
| 16 | + {...props} |
| 17 | + /> |
| 18 | +) |
| 19 | + |
| 20 | +describe("<InformationModal />", () => { |
| 21 | + it("renders an open modal with the provided content", () => { |
| 22 | + const { getByText } = render( |
| 23 | + <InformationModalWrapper>Example modal body</InformationModalWrapper> |
| 24 | + ) |
| 25 | + expect(getByText("Example modal body")).toBeTruthy() |
| 26 | + }) |
| 27 | + |
| 28 | + it("supports a dismiss action when escape key is pressed", () => { |
| 29 | + const handleDismiss = jest.fn() |
| 30 | + const document = render( |
| 31 | + <InformationModalWrapper onDismiss={handleDismiss}> |
| 32 | + Example modal body |
| 33 | + </InformationModalWrapper> |
| 34 | + ) |
| 35 | + fireEvent.keyUp(document.container, { key: "Escape", code: "Escape" }) |
| 36 | + expect(handleDismiss).toHaveBeenCalledTimes(1) |
| 37 | + }) |
| 38 | + |
| 39 | + it("supports a dismiss action when dismiss button is pressed", () => { |
| 40 | + const handleConfirm = jest.fn() |
| 41 | + const handleDismiss = jest.fn() |
| 42 | + const { getByTitle } = render( |
| 43 | + <InformationModalWrapper |
| 44 | + onConfirm={handleConfirm} |
| 45 | + onDismiss={handleDismiss} |
| 46 | + > |
| 47 | + Example modal body |
| 48 | + </InformationModalWrapper> |
| 49 | + ) |
| 50 | + fireEvent.click(getByTitle(/Dismiss/i)) |
| 51 | + expect(handleConfirm).toHaveBeenCalledTimes(0) |
| 52 | + expect(handleDismiss).toHaveBeenCalledTimes(1) |
| 53 | + }) |
| 54 | + |
| 55 | + it("supports a confirm action when confirm button is pressed", () => { |
| 56 | + const handleConfirm = jest.fn() |
| 57 | + const handleDismiss = jest.fn() |
| 58 | + const { getByText } = render( |
| 59 | + <InformationModalWrapper |
| 60 | + onConfirm={handleConfirm} |
| 61 | + onDismiss={handleDismiss} |
| 62 | + > |
| 63 | + Example modal body |
| 64 | + </InformationModalWrapper> |
| 65 | + ) |
| 66 | + fireEvent.click(getByText(/Confirm/i)) |
| 67 | + expect(handleConfirm).toHaveBeenCalledTimes(1) |
| 68 | + expect(handleDismiss).toHaveBeenCalledTimes(0) |
| 69 | + }) |
| 70 | + |
| 71 | + it("supports a secondary action when secondary button is pressed", () => { |
| 72 | + const handleConfirm = jest.fn() |
| 73 | + const handleSecondary = jest.fn() |
| 74 | + const handleDismiss = jest.fn() |
| 75 | + const { getByText } = render( |
| 76 | + <InformationModalWrapper |
| 77 | + onConfirm={handleConfirm} |
| 78 | + onDismiss={handleDismiss} |
| 79 | + onSecondaryAction={handleSecondary} |
| 80 | + > |
| 81 | + Example modal body |
| 82 | + </InformationModalWrapper> |
| 83 | + ) |
| 84 | + fireEvent.click(getByText(/Example secondary/i)) |
| 85 | + expect(handleSecondary).toHaveBeenCalledTimes(1) |
| 86 | + expect(handleConfirm).toHaveBeenCalledTimes(0) |
| 87 | + expect(handleDismiss).toHaveBeenCalledTimes(0) |
| 88 | + }) |
| 89 | +}) |
0 commit comments