|
1
|
import { |
|
2
|
RenderResult, |
|
3
|
cleanup, |
|
4
|
fireEvent, |
|
5
|
render, |
|
6
|
} from "@testing-library/react" |
|
7
|
import * as React from "react" |
|
8
|
|
|
9
|
import { Prompt } from "./prompt" |
|
10
|
|
|
11
|
import { Button } from "@/components/button" |
|
12
|
|
|
13
|
const TRIGGER_TEXT = "Open" |
|
14
|
const TITLE_TEXT = "Delete something" |
|
15
|
const DESCRIPTION_TEXT = "Are you sure? This cannot be undone." |
|
16
|
const CANCEL_TEXT = "Cancel" |
|
17
|
const CONFIRM_TEXT = "Confirm" |
|
18
|
|
|
19
|
describe("Prompt", () => { |
|
20
|
let rendered: RenderResult |
|
21
|
let trigger: HTMLElement |
|
22
|
|
|
23
|
beforeEach(() => { |
|
24
|
rendered = render( |
|
25
|
<Prompt> |
|
26
|
<Prompt.Trigger asChild> |
|
27
|
<Button>{TRIGGER_TEXT}</Button> |
|
28
|
</Prompt.Trigger> |
|
29
|
<Prompt.Content> |
|
30
|
<Prompt.Header> |
|
31
|
<Prompt.Title>{TITLE_TEXT}</Prompt.Title> |
|
32
|
<Prompt.Description>{DESCRIPTION_TEXT}</Prompt.Description> |
|
33
|
</Prompt.Header> |
|
34
|
<Prompt.Footer> |
|
35
|
<Prompt.Cancel>{CANCEL_TEXT}</Prompt.Cancel> |
|
36
|
<Prompt.Action>{CONFIRM_TEXT}</Prompt.Action> |
|
37
|
</Prompt.Footer> |
|
38
|
</Prompt.Content> |
|
39
|
</Prompt> |
|
40
|
) |
|
41
|
|
|
42
|
trigger = rendered.getByText(TRIGGER_TEXT) |
|
43
|
}) |
|
44
|
|
|
45
|
afterEach(() => { |
|
46
|
cleanup() |
|
47
|
}) |
|
48
|
|
|
49
|
it("renders a basic alert dialog when the trigger is clicked", async () => { |
|
50
|
fireEvent.click(trigger) |
|
51
|
|
|
52
|
const title = await rendered.findByText(TITLE_TEXT) |
|
53
|
const description = await rendered.findByText(DESCRIPTION_TEXT) |
|
54
|
|
|
55
|
expect(title).toBeInTheDocument() |
|
56
|
expect(description).toBeInTheDocument() |
|
57
|
}) |
|
58
|
|
|
59
|
it("close the dialog when the cancel button is clicked", async () => { |
|
60
|
fireEvent.click(trigger) |
|
61
|
|
|
62
|
const title = rendered.queryByText(TITLE_TEXT) |
|
63
|
const description = rendered.queryByText(DESCRIPTION_TEXT) |
|
64
|
|
|
65
|
expect(title).toBeInTheDocument() |
|
66
|
expect(description).toBeInTheDocument() |
|
67
|
|
|
68
|
const cancelButton = await rendered.findByText(CANCEL_TEXT) |
|
69
|
|
|
70
|
fireEvent.click(cancelButton) |
|
71
|
|
|
72
|
expect(title).not.toBeInTheDocument() |
|
73
|
expect(description).not.toBeInTheDocument() |
|
74
|
}) |
|
75
|
|
|
76
|
it("close the dialog when the confirm button is clicked", async () => { |
|
77
|
fireEvent.click(trigger) |
|
78
|
|
|
79
|
const title = rendered.queryByText(TITLE_TEXT) |
|
80
|
const description = rendered.queryByText(DESCRIPTION_TEXT) |
|
81
|
|
|
82
|
expect(title).toBeInTheDocument() |
|
83
|
expect(description).toBeInTheDocument() |
|
84
|
|
|
85
|
const confirmButton = await rendered.findByText(CONFIRM_TEXT) |
|
86
|
|
|
87
|
fireEvent.click(confirmButton) |
|
88
|
|
|
89
|
expect(title).not.toBeInTheDocument() |
|
90
|
expect(description).not.toBeInTheDocument() |
|
91
|
}) |
|
92
|
}) |