Skip to content

Commit 310f1a2

Browse files
author
Li Juen Chang
committed
Add some tests for InformationModal
1 parent 903dcb5 commit 310f1a2

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

draft-packages/modal/KaizenDraft/Modal/Presets/ConfirmationModal.spec.tsx

+19-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("<ConfirmationModal />", () => {
4949
expect(queryByText("Confirm")).toBeNull()
5050
})
5151

52-
it("closes the modal when escape key pressed", () => {
52+
it("supports a dismiss action when escape key is pressed", () => {
5353
const handleConfirm = jest.fn()
5454
const handleDismiss = jest.fn()
5555
const document = render(
@@ -65,7 +65,23 @@ describe("<ConfirmationModal />", () => {
6565
expect(handleConfirm).toHaveBeenCalledTimes(0)
6666
})
6767

68-
it("closes the modal when cancel button is pressed", () => {
68+
it("supports a dismiss action when dismiss button is pressed", () => {
69+
const handleConfirm = jest.fn()
70+
const handleDismiss = jest.fn()
71+
const { getByTitle } = render(
72+
<ConfirmationModalWrapper
73+
onConfirm={handleConfirm}
74+
onDismiss={handleDismiss}
75+
>
76+
Example modal body
77+
</ConfirmationModalWrapper>
78+
)
79+
fireEvent.click(getByTitle(/Dismiss/i))
80+
expect(handleConfirm).toHaveBeenCalledTimes(0)
81+
expect(handleDismiss).toHaveBeenCalledTimes(1)
82+
})
83+
84+
it("supports a dismiss action when cancel button is pressed", () => {
6985
const handleConfirm = jest.fn()
7086
const handleDismiss = jest.fn()
7187
const { getByText } = render(
@@ -81,7 +97,7 @@ describe("<ConfirmationModal />", () => {
8197
expect(handleDismiss).toHaveBeenCalledTimes(1)
8298
})
8399

84-
it("closes the modal when confirm button is pressed", () => {
100+
it("supports a confirm action when confirm button is pressed", () => {
85101
const handleConfirm = jest.fn()
86102
const handleDismiss = jest.fn()
87103
const { getByText } = render(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
})

draft-packages/modal/KaizenDraft/Modal/Primitives/GenericModal.spec.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("<GenericModal />", () => {
2121
expect(() => getByText("Example")).toThrow()
2222
})
2323

24-
it("closes the modal when the escape key is pressed", () => {
24+
it("closes the modal when escape key is pressed", () => {
2525
const handleDismiss = jest.fn()
2626
const document = render(
2727
<GenericModal isOpen={true} onEscapeKeyup={handleDismiss}>

0 commit comments

Comments
 (0)