-
Notifications
You must be signed in to change notification settings - Fork 57
/
useConfirm.test.js
261 lines (215 loc) · 7.26 KB
/
useConfirm.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React, { useState } from "react";
import {
render,
fireEvent,
waitForElementToBeRemoved,
} from "@testing-library/react";
import { ConfirmProvider, useConfirm } from "../src/index";
describe("useConfirm", () => {
const deleteConfirmed = jest.fn();
const deleteCancelled = jest.fn();
const DeleteButton = ({ confirmOptions }) => {
const confirm = useConfirm();
return (
<button
onClick={() =>
confirm(confirmOptions).then(deleteConfirmed).catch(deleteCancelled)
}
>
Delete
</button>
);
};
const TestComponent = ({ confirmOptions }) => (
<ConfirmProvider>
<DeleteButton confirmOptions={confirmOptions} />
</ConfirmProvider>
);
test("resolves the promise on confirm", async () => {
const { getByText, queryByText } = render(<TestComponent />);
expect(queryByText("Are you sure?")).toBeFalsy();
fireEvent.click(getByText("Delete"));
expect(queryByText("Are you sure?")).toBeTruthy();
fireEvent.click(getByText("Ok"));
await waitForElementToBeRemoved(() => queryByText("Are you sure?"));
expect(deleteConfirmed).toHaveBeenCalled();
expect(deleteCancelled).not.toHaveBeenCalled();
});
test("rejects the promise on cancel", async () => {
const { getByText, queryByText } = render(<TestComponent />);
expect(queryByText("Are you sure?")).toBeFalsy();
fireEvent.click(getByText("Delete"));
expect(queryByText("Are you sure?")).toBeTruthy();
fireEvent.click(getByText("Cancel"));
await waitForElementToBeRemoved(() => queryByText("Are you sure?"));
expect(deleteConfirmed).not.toHaveBeenCalled();
expect(deleteCancelled).toHaveBeenCalled();
});
describe("options", () => {
test("accepts custom text", () => {
const { getByText, queryByText } = render(
<TestComponent
confirmOptions={{
title: "Remove this item?",
description: "This will permanently remove the item.",
cancellationText: "No way",
confirmationText: "Yessir",
}}
/>,
);
fireEvent.click(getByText("Delete"));
expect(queryByText("Remove this item?")).toBeTruthy();
expect(
queryByText("This will permanently remove the item."),
).toBeTruthy();
expect(queryByText("No way")).toBeTruthy();
expect(queryByText("Yessir")).toBeTruthy();
});
test("accepts custom content", () => {
const { getByText, queryByText } = render(
<TestComponent
confirmOptions={{
content: <div>Arbitrary content</div>,
}}
/>,
);
fireEvent.click(getByText("Delete"));
expect(queryByText("Arbitrary content")).toBeTruthy();
});
});
test("honours default options passed to the provider", () => {
const { getByText, queryByText } = render(
<ConfirmProvider
defaultOptions={{
confirmationText: "Yessir",
cancellationText: "No way",
}}
>
<DeleteButton confirmOptions={{ cancellationText: "Nope" }} />
</ConfirmProvider>,
);
fireEvent.click(getByText("Delete"));
expect(queryByText("Yessir")).toBeTruthy();
expect(queryByText("Nope")).toBeTruthy();
});
test("merges default options with local options in a deep manner", () => {
const { getByText } = render(
<ConfirmProvider
defaultOptions={{
confirmationButtonProps: { "aria-label": "Confirm" },
}}
>
<DeleteButton
confirmOptions={{
confirmationText: "Yes",
confirmationButtonProps: { disabled: true },
}}
/>
</ConfirmProvider>,
);
fireEvent.click(getByText("Delete"));
const button = getByText("Yes");
expect(button.disabled).toBe(true);
expect(button.getAttribute("aria-label")).toEqual("Confirm");
});
test("respects updates to default options", () => {
function App() {
const [confirmationText, setConfirmationText] = useState("Yes");
return (
<ConfirmProvider defaultOptions={{ confirmationText }}>
<DeleteButton />
<button onClick={() => setConfirmationText("Ok")}>Change text</button>
</ConfirmProvider>
);
}
const { getByText, queryByText } = render(<App />);
fireEvent.click(getByText("Delete"));
expect(getByText("Yes")).toBeTruthy();
expect(queryByText("Ok")).toBeFalsy();
fireEvent.click(getByText("Change text"));
expect(queryByText("Yes")).toBeFalsy();
expect(getByText("Ok")).toBeTruthy();
});
describe("confirmation keyword", () => {
test("renders textfield when confirmation keyword is set", () => {
const { getByText, getAllByText } = render(
<TestComponent
confirmOptions={{
confirmationKeyword: "DELETE",
}}
/>,
);
fireEvent.click(getByText("Delete"));
const textfield = getAllByText(
(content, element) => element.tagName.toLowerCase() === "input",
)[0];
const confirmationButton = getByText("Ok");
expect(textfield).toBeTruthy();
expect(confirmationButton.disabled).toBe(true);
fireEvent.change(textfield, { target: { value: "DELETE" } });
expect(confirmationButton.disabled).toBe(false);
});
test("resets the input value on every open", () => {
const { getByText, getAllByText } = render(
<TestComponent
confirmOptions={{
confirmationKeyword: "DELETE",
}}
/>,
);
fireEvent.click(getByText("Delete"));
let textfield = getAllByText(
(content, element) => element.tagName.toLowerCase() === "input",
)[0];
expect(textfield).toBeTruthy();
fireEvent.change(textfield, { target: { value: "DELETE" } });
fireEvent.click(getByText("Ok"));
fireEvent.click(getByText("Delete"));
textfield = getAllByText(
(content, element) => element.tagName.toLowerCase() === "input",
)[0];
expect(textfield.value).toEqual("");
});
});
test("renders textfield with custom props", () => {
const { getByText, queryByPlaceholderText } = render(
<TestComponent
confirmOptions={{
confirmationKeyword: "DELETE",
confirmationKeywordTextFieldProps: {
placeholder: "Custom placeholder",
},
}}
/>,
);
fireEvent.click(getByText("Delete"));
const textfield = queryByPlaceholderText("Custom placeholder");
expect(textfield).toBeTruthy();
});
describe("hide cancel button", () => {
test("renders cancel button when hideCancelButton is false", () => {
const { getByText } = render(
<TestComponent
confirmOptions={{
hideCancelButton: false,
}}
/>,
);
fireEvent.click(getByText("Delete"));
const cancelButton = getByText("Cancel");
expect(cancelButton).toBeTruthy();
});
test("does not render cancel button when hideCancelButton is true", () => {
const { getByText, queryByText } = render(
<TestComponent
confirmOptions={{
hideCancelButton: true,
}}
/>,
);
fireEvent.click(getByText("Delete"));
const cancelButton = queryByText("Cancel");
expect(cancelButton).toBeFalsy();
});
});
});