1 import { RenderResult, fireEvent, render } from "@testing-library/react"
2 import * as React from "react"
3
4 import { usePrompt } from "./use-prompt"
5
6 const OPEN_TEXT = "Open dialog"
7 const TITLE_TEXT = "Delete something"
8 const DESCRIPTION_TEXT = "Are you sure? This cannot be undone."
9 const CANCEL_TEXT = "Cancel"
10 const CONFIRM_TEXT = "Confirm"
11 const VERIFICATION_TEXT = "medusa-design-system"
12
13 const DialogTest = ({ verificationText }: { verificationText?: string }) => {
14 const dialog = usePrompt()
15
16 const handleAction = async () => {
17 await dialog({
18 title: TITLE_TEXT,
19 description: DESCRIPTION_TEXT,
20 cancelText: CANCEL_TEXT,
21 confirmText: CONFIRM_TEXT,
22 verificationText,
23 variant: "danger",
24 })
25 }
26
27 return (
28 <div>
29 <button onClick={handleAction}>{OPEN_TEXT}</button>
30 </div>
31 )
32 }
33
34 describe("usePrompt", () => {
35 let rendered: RenderResult
36 let trigger: HTMLElement
37
38 beforeEach(() => {
39 rendered = render(<DialogTest />)
40 trigger = rendered.getByText(OPEN_TEXT)
41 })
42
43 afterEach(() => {
44 // Try to find the cancel button and click it to close the dialog
45 // We need to do this a we are appending a div to the body and it will not be removed
46 // automatically by the cleanup
47 const cancelButton = rendered.queryByText(CANCEL_TEXT)
48
49 if (cancelButton) {
50 fireEvent.click(cancelButton)
51 }
52 })
53
54 it("renders a basic alert dialog when the trigger is clicked", async () => {
55 fireEvent.click(trigger)
56
57 const title = await rendered.findByText(TITLE_TEXT)
58 const description = await rendered.findByText(DESCRIPTION_TEXT)
59
60 expect(title).toBeInTheDocument()
61 expect(description).toBeInTheDocument()
62 })
63
64 it("unmounts the dialog when the cancel button is clicked", async () => {
65 fireEvent.click(trigger)
66
67 const cancelButton = await rendered.findByText(CANCEL_TEXT)
68
69 fireEvent.click(cancelButton)
70
71 const title = rendered.queryByText(TITLE_TEXT)
72 const description = rendered.queryByText(DESCRIPTION_TEXT)
73
74 expect(title).not.toBeInTheDocument()
75 expect(description).not.toBeInTheDocument()
76 })
77
78 it("unmounts the dialog when the confirm button is clicked", async () => {
79 fireEvent.click(trigger)
80
81 const confirmButton = await rendered.findByText(CONFIRM_TEXT)
82
83 fireEvent.click(confirmButton)
84
85 const title = rendered.queryByText(TITLE_TEXT)
86 const description = rendered.queryByText(DESCRIPTION_TEXT)
87
88 expect(title).not.toBeInTheDocument()
89 expect(description).not.toBeInTheDocument()
90 })
91
92 it("renders a verification input when verificationText is provided", async () => {
93 rendered.rerender(<DialogTest verificationText="delete" />)
94 fireEvent.click(trigger)
95
96 const input = await rendered.findByRole("textbox")
97
98 expect(input).toBeInTheDocument()
99 })
100
101 it("renders a disabled confirm button when verificationText is provided", async () => {
102 rendered.rerender(<DialogTest verificationText={VERIFICATION_TEXT} />)
103 fireEvent.click(trigger)
104
105 const button = await rendered.findByText(CONFIRM_TEXT)
106
107 expect(button).toBeDisabled()
108 })
109
110 it("renders an enabled confirm button when verificationText is provided and the input matches", async () => {
111 rendered.rerender(<DialogTest verificationText={VERIFICATION_TEXT} />)
112 fireEvent.click(trigger)
113
114 const input = await rendered.findByRole("textbox")
115 const button = await rendered.findByText(CONFIRM_TEXT)
116
117 fireEvent.change(input, { target: { value: VERIFICATION_TEXT } })
118
119 expect(button).toBeEnabled()
120 })
121 })