-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElementAssertion.test.tsx
More file actions
414 lines (339 loc) · 16.8 KB
/
ElementAssertion.test.tsx
File metadata and controls
414 lines (339 loc) · 16.8 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { AssertionError, expect } from "@assertive-ts/core";
import { render } from "@testing-library/react";
import { ElementAssertion } from "../../../src/lib/ElementAssertion";
import { FocusTestComponent } from "./fixtures/focusTestComponent";
import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent";
import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent";
import { SimpleTestComponent } from "./fixtures/simpleTestComponent";
import { WithAttributesTestComponent } from "./fixtures/withAttributesTestComponent";
describe("[Unit] ElementAssertion.test.ts", () => {
describe(".toBeInTheDocument", () => {
context("when the element is in the document", () => {
it("returns the assertion instance", async () => {
const { findByRole } = render(<SimpleTestComponent />);
const button = await findByRole("button", { name: "click me" });
const test = new ElementAssertion(button);
expect(test.toBeInTheDocument()).toBeEqual(test);
expect(() => test.not.toBeInTheDocument())
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to NOT be in the document");
});
});
context("when the element is not in the document", () => {
it("throws an assertion error", () => {
const detachedElement = document.createElement("div");
const test = new ElementAssertion(detachedElement);
expect(() => test.toBeInTheDocument())
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to be in the document");
expect(test.not.toBeInTheDocument()).toBeEqual(test);
});
});
});
describe(".toContainElement", () => {
context("when the descendant element is contained in the ancestor element", () => {
context("and it is a direct child", () => {
it("returns the assertion instance", async () => {
const { findByTestId } = render(<NestedElementsTestComponent />);
const grandparent = await findByTestId("grandparent");
const parent = await findByTestId("parent");
const child = await findByTestId("child");
const svgElement = await findByTestId("svg-element");
const grandparentTest = new ElementAssertion(grandparent);
const parentTest = new ElementAssertion(parent);
expect(grandparentTest.toContainElement(parent));
expect(grandparentTest.toContainElement(svgElement));
expect(parentTest.toContainElement(child));
expect(() => grandparentTest.not.toContainElement(parent))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
expect(() => grandparentTest.not.toContainElement(svgElement))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
expect(() => parentTest.not.toContainElement(child))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
});
});
context("and it is an indirect child", () => {
it("returns the assertion instance", async () => {
const { findByTestId } = render(<NestedElementsTestComponent />);
const grandparent = await findByTestId("grandparent");
const child = await findByTestId("child");
const grandparentTest = new ElementAssertion(grandparent);
expect(grandparentTest.toContainElement(child));
expect(() => grandparentTest.not.toContainElement(child))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
});
});
context("and it is a deeply nested child", () => {
it("returns the assertion instance", async () => {
const { findByTestId } = render(<NestedElementsTestComponent />);
const grandparent = await findByTestId("grandparent");
const deepChild = await findByTestId("deep-child");
const grandparentTest = new ElementAssertion(grandparent);
expect(grandparentTest.toContainElement(deepChild));
expect(() => grandparentTest.not.toContainElement(deepChild))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
});
});
});
context("when element is NOT contained in ancestor element", () => {
it("throws an assertion error", async () => {
const notChildElement = document.createElement("span");
const { findByTestId } = render(<NestedElementsTestComponent />);
const grandparent = await findByTestId("grandparent");
const grandparentTest = new ElementAssertion(grandparent);
expect(() => grandparentTest.toContainElement(notChildElement))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");
expect(grandparentTest.not.toContainElement(notChildElement)).toBeEqual(grandparentTest);
});
});
});
describe(".toHaveAttribute", () => {
context("when the element has the attribute with the expected value", () => {
it("returns the assertion instance", async () => {
const { findByRole } = render(<WithAttributesTestComponent />);
const button = await findByRole("button", { name: "click me" });
const test = new ElementAssertion(button);
expect(test.toHaveAttribute("type", "submit")).toBeEqual(test);
expect(() => test.not.toHaveAttribute("type", "submit"))
.toThrowError(AssertionError)
.toHaveMessage("Expected to NOT have attribute \"type\" with value \"submit\", but received \"submit\"");
});
});
context("when the element has the attribute with a not expected value", () => {
it("throws an assertion error", async () => {
const { findByRole } = render(<WithAttributesTestComponent />);
const button = await findByRole("button", { name: "click me" });
const test = new ElementAssertion(button);
expect(() => test.toHaveAttribute("type", "different value"))
.toThrowError(AssertionError)
.toHaveMessage("Expected to have attribute \"type\" with value \"different value\", but received \"submit\"",
);
expect(test.not.toHaveAttribute("type", "different value")).toBeEqual(test);
});
});
context("when the element has the attribute without checking value", () => {
it("returns the assertion instance", async () => {
const { findByRole } = render(<WithAttributesTestComponent />);
const button = await findByRole("button", { name: "click me" });
const test = new ElementAssertion(button);
expect(test.toHaveAttribute("disabled")).toBeEqual(test);
expect(() => test.not.toHaveAttribute("disabled"))
.toThrowError(AssertionError)
.toHaveMessage("Expected to NOT have attribute \"disabled\"");
});
});
context("when the element does not have the attribute", () => {
it("throws an assertion error", async () => {
const { findByRole } = render(<WithAttributesTestComponent />);
const button = await findByRole("button", { name: "click me" });
const test = new ElementAssertion(button);
expect(() => test.toHaveAttribute("non-existent"))
.toThrowError(AssertionError)
.toHaveMessage("Expected to have attribute \"non-existent\"");
expect(test.not.toHaveAttribute("non-existent")).toBeEqual(test);
});
});
});
describe(".toHaveClass", () => {
context("when the element has the expected class", () => {
it("returns the assertion instance", () => {
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
const divTest = getByText("Test text inside a div");
const test = new ElementAssertion(divTest);
expect(test.toHaveClass("foo")).toBeEqual(test);
expect(() => test.not.toHaveClass("foo"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to NOT have class: "foo"');
});
});
context("when the element does not have the expected class", () => {
it("throws an assertion error", () => {
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
const divTest = getByText("Test text inside a div");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveClass("baz"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to have class: "baz"');
expect(test.not.toHaveClass("baz")).toBeEqual(test);
});
});
});
describe(".toHaveAnyClass", () => {
context("when the element has at least one of the expected classes", () => {
it("returns the assertion instance", () => {
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
const divTest = getByText("Test text inside a div");
const test = new ElementAssertion(divTest);
expect(test.toHaveAnyClass("bar", "baz")).toBeEqual(test);
expect(() => test.not.toHaveAnyClass("bar", "baz"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to NOT have any of these classes: "bar baz"');
});
});
context("when the element does not have any of the expected classes", () => {
it("throws an assertion error", () => {
const { getByText } = render(<HaveClassTestComponent className="foo" />);
const divTest = getByText("Test text inside a div");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveAnyClass("bar", "baz"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to have at least one of these classes: "bar baz"');
expect(test.not.toHaveAnyClass("bar", "baz")).toBeEqual(test);
});
});
});
describe(".toHaveAllClasses", () => {
context("when the element has all the expected classes", () => {
it("returns the assertion instance", () => {
const { getByText } = render(<HaveClassTestComponent className="foo bar baz" />);
const divTest = getByText("Test text inside a div");
const test = new ElementAssertion(divTest);
expect(test.toHaveAllClasses("foo", "bar")).toBeEqual(test);
expect(() => test.not.toHaveAllClasses("foo", "bar"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to NOT have all of these classes: "foo bar"');
});
});
context("when the element does not have all the expected classes", () => {
it("throws an assertion error", () => {
const { getByText } = render(<HaveClassTestComponent className="foo bar" />);
const divTest = getByText("Test text inside a div");
divTest.classList.add("foo", "bar");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveAllClasses("foo", "bar", "baz"))
.toThrowError(AssertionError)
.toHaveMessage('Expected the element to have all of these classes: "foo bar baz"');
expect(test.not.toHaveAllClasses("foo", "bar", "baz")).toBeEqual(test);
});
});
});
describe(".toHaveFocus", () => {
context("when the element has focus", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(<FocusTestComponent />);
const input1 = getByTestId("input1");
input1.focus();
const test = new ElementAssertion(input1);
expect(test.toHaveFocus()).toBeEqual(test);
expect(() => test.not.toHaveFocus())
.toThrowError(AssertionError)
.toHaveMessage("Expected the element NOT to be focused");
});
});
context("when the element does not have focus", () => {
it("throws an assertion error", () => {
const { getByTestId } = render(<FocusTestComponent />);
const input1 = getByTestId("input1");
const test = new ElementAssertion(input1);
expect(() => test.toHaveFocus())
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to be focused");
expect(test.not.toHaveFocus()).toBeEqual(test);
});
});
});
describe(".toHaveStyle", () => {
context("when the element has the expected style", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<div
className="foo bar test"
style={{ border: "1px solid black", color: "red", display: "flex" }}
data-testid="test-div"
/>);
const divTest = getByTestId("test-div");
const test = new ElementAssertion(divTest);
expect(test.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
expect(() => test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" }))
.toThrowError(AssertionError)
.toHaveMessage(
// eslint-disable-next-line max-len
'Expected the element to NOT match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
);
});
});
context("when the element does not have the expected style", () => {
it("throws an assertion error", () => {
const { getByTestId } = render(
<div
className="foo bar test"
style={{ color: "blue", display: "block" }}
data-testid="test-div"
/>,
);
const divTest = getByTestId("test-div");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveStyle(({ border: "1px solid black", color: "red", display: "flex" })))
.toThrowError(AssertionError)
.toHaveMessage(
// eslint-disable-next-line max-len
'Expected the element to match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
);
expect(test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
});
});
context("when the element partially match the style", () => {
it("throws an assertion error", () => {
const { getByTestId } = render(
<div
className="foo bar test"
style={{ border: "1px solid black", color: "blue", display: "block" }}
data-testid="test-div"
/>,
);
const divTest = getByTestId("test-div");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveStyle(({ color: "red", display: "flex" })))
.toThrowError(AssertionError)
.toHaveMessage(
// eslint-disable-next-line max-len
'Expected the element to match the following style:\n{\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
);
expect(test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
});
});
});
describe(".toHaveSomeStyle", () => {
context("when the element contains one or more expected styles", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<div
style={{ color: "blue", maxHeight: "3rem", width: "2rem" }}
data-testid="test-div"
/>,
);
const divTest = getByTestId("test-div");
const test = new ElementAssertion(divTest);
expect(test.toHaveSomeStyle({ color: "red", display: "flex", height: "3rem", width: "2rem" })).toBeEqual(test);
expect(() => test.not.toHaveSomeStyle({ color: "blue" }))
.toThrowError(AssertionError)
// eslint-disable-next-line max-len
.toHaveMessage("Expected the element NOT to match some of the following styles:\n{\n \"color\": \"rgb(0, 0, 255)\"\n}");
});
});
context("when the element does not contain any of the expected styles", () => {
it("throws an assertion error", () => {
const { getByTestId } = render(
<div
className="foo bar test"
style={{ border: "1px solid black", color: "blue", display: "block" }}
data-testid="test-div"
/>,
);
const divTest = getByTestId("test-div");
const test = new ElementAssertion(divTest);
expect(() => test.toHaveSomeStyle({ color: "red", display: "flex" }))
.toThrowError(AssertionError)
// eslint-disable-next-line max-len
.toHaveMessage("Expected the element to match some of the following styles:\n{\n \"color\": \"rgb(255, 0, 0)\",\n \"display\": \"flex\"\n}");
expect(test.not.toHaveSomeStyle({ border: "1px solid blue", color: "red", display: "flex" })).toBeEqual(test);
});
});
});
});