-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElementAssertion.test.tsx
More file actions
374 lines (327 loc) · 14.2 KB
/
ElementAssertion.test.tsx
File metadata and controls
374 lines (327 loc) · 14.2 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
import { AssertionError, expect } from "@assertive-ts/core";
import { fireEvent, render } from "@testing-library/react-native";
import { useState, useCallback, ReactElement } from "react";
import {
View,
TextInput,
Text,
Modal,
Button,
} from "react-native";
import { ElementAssertion } from "../../src/lib/ElementAssertion";
function SimpleToggleText(): ReactElement {
const [isVisible, setIsVisible] = useState(true);
const handleToggle = useCallback((): void => {
setIsVisible(prev => !prev);
}, []);
return (
<View>
<Text style={{ display: isVisible ? "flex" : "none" }}>
{"Toggle me!"}
</Text>
<Button
title="Toggle Text"
onPress={handleToggle}
/>
</View>
);
}
describe("[Unit] ElementAssertion.test.ts", () => {
describe(".toBeDisabled", () => {
context("when the element is TextInput", () => {
context("and the element is not editable", () => {
it("returns the assertion instance", () => {
const element = render(
<TextInput testID="id" editable={false} />,
);
const test = new ElementAssertion(element.getByTestId("id"));
expect(test.toBeDisabled()).toBe(test);
expect(test.not.toBeEnabled()).toBeEqual(test);
expect(() => test.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <TextInput ... /> to be enabled.");
});
});
context("and the element is editable", () => {
it("throws an error", () => {
const reactElement = render(<TextInput editable={true} testID="id" />);
const test = new ElementAssertion(reactElement.getByTestId("id"));
expect(() => test.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <TextInput ... /> to be disabled.");
expect(() => test.not.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <TextInput ... /> NOT to be enabled.");
});
});
});
context("when the parent has property aria-disabled", () => {
context("if parent aria-disabled = true", () => {
it("returns assertion instance for parent and child element", () => {
const element = render(
<View aria-disabled={true} testID="parentId">
<View testID="childId">
<TextInput />
</View>
</View>,
);
const parent = new ElementAssertion(element.getByTestId("parentId"));
const child = new ElementAssertion(element.getByTestId("childId"));
expect(parent.toBeDisabled()).toBe(parent);
expect(child.toBeDisabled()).toBe(child);
expect(() => parent.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be enabled.");
expect(() => parent.not.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be disabled.");
});
});
context("if parent aria-disabled = false", () => {
it("throws an error for parent and child element", () => {
const element = render(
<View aria-disabled={false} testID="parentId">
<View testID="childId">
<TextInput />
</View>
</View>,
);
const parent = new ElementAssertion(element.getByTestId("parentId"));
const child = new ElementAssertion(element.getByTestId("childId"));
expect(parent.toBeEnabled()).toBeEqual(parent);
expect(parent.not.toBeDisabled()).toBeEqual(parent);
expect(() => parent.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be disabled.");
expect(() => parent.not.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
expect(() => child.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be disabled.");
expect(() => child.not.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
});
});
});
context("when the element contains property aria-disabled", () => {
const element = render(
<View testID="parentId">
<View aria-disabled={true} testID="childId">
<TextInput />
</View>
</View>,
);
const parent = new ElementAssertion(element.getByTestId("parentId"));
const child = new ElementAssertion(element.getByTestId("childId"));
context("if child contains aria-disabled = true", () => {
it("returns assertion instance for child element", () => {
expect(child.toBeDisabled()).toBe(child);
expect(() => child.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be enabled.");
expect(() => child.not.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be disabled.");
});
it("returns error for parent element", () => {
expect(parent.toBeEnabled()).toBeEqual(parent);
expect(() => parent.toBeDisabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be disabled.");
expect(() => parent.not.toBeEnabled())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
});
});
});
});
describe(".toBeEmpty", () => {
context("when the element is empty", () => {
it("returns the assertion instance", () => {
const element = render(<View testID="id" />);
const test = new ElementAssertion(element.getByTestId("id"));
expect(test.toBeEmpty()).toBe(test);
expect(() => test.not.toBeEmpty())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be empty.");
});
});
context("when the element is NOT empty", () => {
it("throws an error", () => {
const element = render(
<View testID="id">
<Text>{"Not empty"}</Text>
</View>,
);
const test = new ElementAssertion(element.getByTestId("id"));
expect(test.not.toBeEmpty()).toBeEqual(test);
expect(() => test.toBeEmpty())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be empty.");
});
});
});
describe (".toBeVisible", () => {
context("when the modal is visible", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<Modal testID="id" visible={true} />,
);
const test = new ElementAssertion(getByTestId("id"));
expect(test.toBeVisible()).toBe(test);
expect(() => test.not.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <Modal ... /> NOT to be visible.");
});
});
context("when the element contains 'display' property", () => {
context("and display = none", () => {
it("throws an error", () => {
const { getByText, getByRole } = render(
<SimpleToggleText />,
);
const textElement = new ElementAssertion(getByText("Toggle me!"));
expect(textElement.toBeVisible()).toBeEqual(textElement);
const toggleButton = getByRole("button", { name: "Toggle Text" });
fireEvent.press(toggleButton);
expect(textElement.not.toBeVisible()).toBeEqual(textElement);
});
});
context("and display = flex", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<View testID="id" style={{ display: "flex" }} />,
);
const test = new ElementAssertion(getByTestId("id"));
expect(test.toBeVisible()).toBe(test);
expect(() => test.not.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
});
});
});
context("when the element contains 'accessibilityElementsHidden' property", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<View testID="id" accessibilityElementsHidden={false} />,
);
const test = new ElementAssertion(getByTestId("id"));
expect(test.toBeVisible()).toBe(test);
expect(() => test.not.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
});
});
context("when the element contains 'importantForAccessibility' property", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(
<View testID="id" importantForAccessibility={"yes"} />,
);
const test = new ElementAssertion(getByTestId("id"));
expect(test.toBeVisible()).toBe(test);
expect(() => test.not.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
});
});
context("when the parent element contains 'opacity' property", () => {
context("and parent opacity = 0", () => {
const { getByTestId } = render(
<View testID="parentId" style={{ opacity: 0 }} >
<View testID="childId" style={{ opacity: 1 }} />
</View>,
);
const parent = new ElementAssertion(getByTestId("parentId"));
const child = new ElementAssertion(getByTestId("childId"));
it("returns assertion instance for NOT visible elements", () => {
expect(parent.not.toBeVisible()).toBeEqual(parent);
expect(child.not.toBeVisible()).toBeEqual(child);
});
it("throws an error for visible elements", () => {
expect(() => parent.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be visible.");
expect(() => child.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be visible.");
});
});
context("and child opacity = 0", () => {
const { getByTestId } = render(
<View testID="parentId" style={{ opacity: 1 }} >
<View testID="childId" style={{ opacity: 0 }} />
</View>,
);
const parent = new ElementAssertion(getByTestId("parentId"));
const child = new ElementAssertion(getByTestId("childId"));
it("returns assertion instance for visible parent and NOT visible child", () => {
expect(parent.toBeVisible()).toBeEqual(parent);
expect(child.not.toBeVisible()).toBeEqual(child);
});
it("throws an error for NOT visible parent and visible child", () => {
expect(() => parent.not.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
expect(() => child.toBeVisible())
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to be visible.");
});
});
});
});
describe (".toContainElement", () => {
const element = render(
<View testID="grandParentId">
<View testID="parentId">
<View testID="childId" />
</View>
<Text testID="textId" />
</View>,
);
const container = element.getByTestId("grandParentId");
const containerElementAssertion = new ElementAssertion(container);
const parent = element.getByTestId("parentId");
const parentElementAssertion = new ElementAssertion(parent);
const child = element.getByTestId("childId");
const text = element.getByTestId("textId");
const textElementAssertion = new ElementAssertion(text);
context("when the element has children", () => {
context("and the target element is found in the children's element", () => {
it("returns the assertion instance", () => {
expect(containerElementAssertion.toContainElement(parent)).toBe(containerElementAssertion);
expect(containerElementAssertion.toContainElement(child)).toBe(containerElementAssertion);
expect(containerElementAssertion.toContainElement(text)).toBe(containerElementAssertion);
expect(parentElementAssertion.toContainElement(child)).toBe(parentElementAssertion);
});
it("throws an error for negative assertion", () => {
expect(() => containerElementAssertion.not.toContainElement(parent))
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to contain element <View ... />.");
expect(() => containerElementAssertion.not.toContainElement(text))
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> NOT to contain element <Text ... />.");
});
});
context("and the target element is NOT found in the children's element", () => {
it("throws an error", () => {
expect(() => parentElementAssertion.toContainElement(text))
.toThrowError(AssertionError)
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
});
it("returns the assertion instance for negative assertion", () => {
expect(parentElementAssertion.not.toContainElement(text)).toBeEqual(parentElementAssertion);
expect(parentElementAssertion.not.toContainElement(container)).toBeEqual(parentElementAssertion);
});
});
});
context("when the element does NOT have children", () => {
it("throws an error", () => {
expect(() => textElementAssertion.toContainElement(parent))
.toThrowError(AssertionError)
.toHaveMessage("Expected element <Text ... /> to contain element <View ... />.");
});
});
});
});