-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy path3-jest-matchers.test.tsx
More file actions
483 lines (433 loc) · 15.8 KB
/
3-jest-matchers.test.tsx
File metadata and controls
483 lines (433 loc) · 15.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* React Native Testing Library Tutorial - Chapter 3: Jest Matchers
*
* This chapter introduces you to the powerful jest matchers provided by React Native Testing Library.
* These matchers extend Jest's built-in expect() function with React Native-specific assertions,
* making your tests more readable and providing better error messages.
*
* Jest matchers are the "assertions" in your tests - they check if your components behave as expected.
* Instead of writing complex conditional logic, you can use descriptive matchers that clearly
* express what you're testing for.
*
* All matchers can be negated using .not - for example:
* - expect(element).toBeOnTheScreen() - asserts element is rendered
* - expect(element).not.toBeOnTheScreen() - asserts element is NOT rendered
*/
import * as React from 'react';
import { render, screen } from '@testing-library/react-native';
import { Text, TextInput, View } from 'react-native';
/**
* MATCHER 1: toBeOnTheScreen()
*
* The most fundamental matcher - checks if an element is rendered in the component tree.
* This is equivalent to checking if an element exists and is not null.
*
* Usage:
* - expect(element).toBeOnTheScreen() - element should be rendered
* - expect(element).not.toBeOnTheScreen() - element should not be rendered
*/
test('showcase: toBeOnTheScreen', async () => {
await render(
<View>
<View testID="view" />
</View>,
);
// Assert that the view with testID "view" is rendered
expect(screen.getByTestId('view')).toBeOnTheScreen();
// Assert that a non-existent element is not rendered
// Note: queryByTestId returns null if element not found, getByTestId would throw
expect(screen.queryByTestId('non-existent')).not.toBeOnTheScreen();
});
/**
* MATCHER 2: toHaveTextContent()
*
* Checks if an element contains specific text content. This matcher is very flexible:
* - Supports exact string matching
* - Supports regular expressions for pattern matching
* - Supports partial matching with { exact: false } option
*
* Usage:
* - expect(element).toHaveTextContent('exact text')
* - expect(element).toHaveTextContent(/pattern/i) - regex with case-insensitive flag
* - expect(element).toHaveTextContent('partial', { exact: false })
*/
test('showcase: toHaveTextContent', async () => {
await render(
<View>
<Text testID="text">Hello World</Text>
</View>,
);
// Exact text match
expect(screen.getByTestId('text')).toHaveTextContent('Hello World');
// Regular expression match (case-insensitive)
expect(screen.getByTestId('text')).toHaveTextContent(/hello/i);
// Partial text match - useful when you only care about part of the text
expect(screen.getByTestId('text')).toHaveTextContent('Hello', { exact: false });
});
/**
* MATCHER 3: toContainElement()
*
* Checks if one element contains another element as a descendant.
* This is useful for testing component hierarchies and nested structures.
*
* Usage:
* - expect(parentElement).toContainElement(childElement)
* - expect(parentElement).not.toContainElement(unrelatedElement)
*/
test('showcase: toContainElement', async () => {
await render(
<View>
<View testID="outer">
<View testID="inner" />
</View>
<View testID="outer-2" />
</View>,
);
// Assert that "outer" contains "inner" as a descendant
expect(screen.getByTestId('outer')).toContainElement(screen.getByTestId('inner'));
// Assert that "outer" does not contain "outer-2" (they are siblings)
expect(screen.getByTestId('outer')).not.toContainElement(screen.getByTestId('outer-2'));
});
/**
* MATCHER 4: toBeEmptyElement()
*
* Checks if an element has no children (is empty).
* This is useful for testing loading states, empty lists, or components
* that should render nothing under certain conditions.
*
* Usage:
* - expect(element).toBeEmptyElement() - element has no children
* - expect(element).not.toBeEmptyElement() - element has children
*/
test('showcase: toBeEmptyElement', async () => {
await render(
<View>
<View testID="empty" />
<View testID="not-empty">
<Text testID="text">Hello World</Text>
</View>
</View>,
);
// Assert that the empty view has no children
expect(screen.getByTestId('empty')).toBeEmptyElement();
// Assert that the view with text content is not empty
expect(screen.getByTestId('not-empty')).not.toBeEmptyElement();
});
/**
* MATCHER 5: toHaveDisplayValue()
*
* Checks the display value of form elements like TextInput.
* This matcher is specifically designed for input elements and checks
* their current value, not their placeholder or other properties.
*
* Usage:
* - expect(inputElement).toHaveDisplayValue('expected value')
* - expect(inputElement).toHaveDisplayValue(['value1', 'value2']) - for multiple values
*/
test('showcase: toHaveDisplayValue', async () => {
await render(
<View>
<TextInput testID="input" value="Hello" />
</View>,
);
// Assert that the TextInput displays the expected value
expect(screen.getByTestId('input')).toHaveDisplayValue('Hello');
});
/**
* MATCHER 6: toHaveAccessibilityValue()
*
* Checks accessibility value properties of elements. This is crucial for testing
* accessibility features and ensuring your app works well with screen readers.
*
* The matcher can check:
* - text: aria-valuetext (string description of the value)
* - now: aria-valuenow (current numeric value)
* - min: aria-valuemin (minimum value)
* - max: aria-valuemax (maximum value)
*
* Usage:
* - expect(element).toHaveAccessibilityValue({ text: 'description' })
* - expect(element).toHaveAccessibilityValue({ now: 50, min: 0, max: 100 })
*/
test('showcase: toHaveAccessibilityValue', async () => {
await render(
<View>
<View
testID="view"
aria-valuetext="33%"
aria-valuenow={33}
aria-valuemax={100}
aria-valuemin={0}
/>
</View>,
);
// Check accessibility value text
expect(screen.getByTestId('view')).toHaveAccessibilityValue({ text: '33%' });
// Check current accessibility value
expect(screen.getByTestId('view')).toHaveAccessibilityValue({ now: 33 });
// Check multiple accessibility values at once
expect(screen.getByTestId('view')).toHaveAccessibilityValue({ now: 33, min: 0, max: 100 });
});
/**
* MATCHER 7: toBeEnabled() / toBeDisabled()
*
* Checks if an element is enabled or disabled based on the aria-disabled attribute.
* These matchers are particularly useful for testing form elements and interactive components.
*
* Usage:
* - expect(element).toBeEnabled() - element is interactive
* - expect(element).toBeDisabled() - element is disabled
* - expect(element).not.toBeEnabled() - same as toBeDisabled()
* - expect(element).not.toBeDisabled() - same as toBeEnabled()
*/
test('showcase: toBeEnabled/toBeDisabled', async () => {
await render(
<View>
<View testID="enabled" aria-disabled={false} />
<View testID="disabled" aria-disabled />
</View>,
);
// Test enabled element
expect(screen.getByTestId('enabled')).toBeEnabled();
expect(screen.getByTestId('enabled')).not.toBeDisabled();
// Test disabled element
expect(screen.getByTestId('disabled')).toBeDisabled();
expect(screen.getByTestId('disabled')).not.toBeEnabled();
});
/**
* MATCHER 8: toBeSelected()
*
* Checks if an element is selected based on the aria-selected attribute.
* This is useful for testing selectable lists, tabs, or other components
* where items can be selected.
*
* Usage:
* - expect(element).toBeSelected() - element is selected
* - expect(element).not.toBeSelected() - element is not selected
*/
test('showcase: toBeSelected', async () => {
await render(
<View>
<View testID="selected" aria-selected />
<View testID="not-selected" />
</View>,
);
// Test selected element
expect(screen.getByTestId('selected')).toBeSelected();
// Test non-selected element
expect(screen.getByTestId('not-selected')).not.toBeSelected();
});
/**
* MATCHER 9: toBeChecked() / toBePartiallyChecked()
*
* Checks the checked state of checkboxes and other checkable elements.
* These matchers work with elements that have role="checkbox" and aria-checked attributes.
*
* Checkbox states:
* - aria-checked={true} or aria-checked - fully checked
* - aria-checked="mixed" - partially checked (indeterminate)
* - aria-checked={false} or no aria-checked - unchecked
*
* Usage:
* - expect(checkbox).toBeChecked() - checkbox is checked
* - expect(checkbox).toBePartiallyChecked() - checkbox is in mixed state
* - expect(checkbox).not.toBeChecked() - checkbox is not checked
*/
test('showcase: toBeChecked/toBePartiallyChecked (role: checkbox)', async () => {
await render(
<View>
<View accessible role="checkbox" testID="checked" aria-checked />
<View accessible role="checkbox" testID="partially-checked" aria-checked="mixed" />
<View accessible role="checkbox" testID="not-checked" />
</View>,
);
// Test fully checked checkbox
expect(screen.getByTestId('checked')).toBeChecked();
expect(screen.getByTestId('checked')).not.toBePartiallyChecked();
// Test partially checked checkbox
expect(screen.getByTestId('partially-checked')).toBePartiallyChecked();
expect(screen.getByTestId('partially-checked')).not.toBeChecked();
// Test unchecked checkbox
expect(screen.getByTestId('not-checked')).not.toBeChecked();
expect(screen.getByTestId('not-checked')).not.toBePartiallyChecked();
});
/**
* MATCHER 10: toBeChecked() (for switches and radios)
*
* The toBeChecked matcher also works with other checkable elements like
* switches and radio buttons. Note that these don't support partial states.
*
* Usage:
* - expect(switch).toBeChecked() - switch is on
* - expect(radio).toBeChecked() - radio is selected
*/
test('showcase: toBeChecked (roles: switch, radio)', async () => {
await render(
<View>
<View accessible role="switch" testID="checked" aria-checked />
<View accessible role="radio" testID="not-checked" />
</View>,
);
// Test checked switch
expect(screen.getByTestId('checked')).toBeChecked();
// Test unchecked radio button
expect(screen.getByTestId('not-checked')).not.toBeChecked();
});
/**
* MATCHER 11: toBeBusy()
*
* Checks if an element is in a busy state based on the aria-busy attribute.
* This is useful for testing loading states and async operations.
*
* Usage:
* - expect(element).toBeBusy() - element is in loading/busy state
* - expect(element).not.toBeBusy() - element is not busy
*/
test('showcase: toBeBusy', async () => {
await render(
<View>
<View testID="busy" aria-busy />
<View testID="not-busy" />
</View>,
);
// Test busy element (loading state)
expect(screen.getByTestId('busy')).toBeBusy();
// Test non-busy element
expect(screen.getByTestId('not-busy')).not.toBeBusy();
});
/**
* MATCHER 12: toBeExpanded() / toBeCollapsed()
*
* Checks the expanded/collapsed state of elements based on the aria-expanded attribute.
* This is useful for testing accordions, dropdown menus, and other expandable components.
*
* States:
* - aria-expanded={true} - expanded
* - aria-expanded={false} - collapsed
* - no aria-expanded - neither expanded nor collapsed
*
* Usage:
* - expect(element).toBeExpanded() - element is expanded
* - expect(element).toBeCollapsed() - element is collapsed
* - expect(element).not.toBeExpanded() - element is not expanded
* - expect(element).not.toBeCollapsed() - element is not collapsed
*/
test('showcase: toBeExpanded/toBeCollapsed', async () => {
await render(
<View>
<View testID="expanded" aria-expanded />
<View testID="collapsed" aria-expanded={false} />
<View testID="default" />
</View>,
);
// Test expanded element
expect(screen.getByTestId('expanded')).toBeExpanded();
expect(screen.getByTestId('expanded')).not.toBeCollapsed();
// Test collapsed element
expect(screen.getByTestId('collapsed')).toBeCollapsed();
expect(screen.getByTestId('collapsed')).not.toBeExpanded();
// Test element with no aria-expanded (neither expanded nor collapsed)
expect(screen.getByTestId('default')).not.toBeCollapsed();
expect(screen.getByTestId('default')).not.toBeExpanded();
});
/**
* MATCHER 13: toBeVisible()
*
* Checks if an element is visible to the user. An element is considered visible
* if it has opacity greater than 0 and is not hidden by other means.
*
* Note: This checks visual visibility, not whether the element exists in the DOM.
* An element can be in the DOM but not visible (e.g., opacity: 0).
*
* Usage:
* - expect(element).toBeVisible() - element is visible to user
* - expect(element).not.toBeVisible() - element is hidden from user
*/
test('showcase: toBeVisible', async () => {
await render(
<View>
<View testID="visible" />
<View testID="not-visible" style={{ opacity: 0 }} />
</View>,
);
// Test visible element
expect(screen.getByTestId('visible')).toBeVisible();
// Test hidden element (opacity: 0)
expect(screen.getByTestId('not-visible')).not.toBeVisible();
});
/**
* MATCHER 14: toHaveStyle()
*
* Checks if an element has specific style properties. This matcher is perfect
* for testing CSS-in-JS styles, conditional styling, and theme applications.
*
* You can test:
* - Single style property: { backgroundColor: 'red' }
* - Multiple style properties: { backgroundColor: 'red', color: 'white' }
* - Nested style objects (for some style systems)
*
* Usage:
* - expect(element).toHaveStyle({ property: 'value' })
* - expect(element).not.toHaveStyle({ property: 'wrongValue' })
*/
test('showcase: toHaveStyle', async () => {
await render(
<View>
<View testID="view" style={{ backgroundColor: 'red' }} />
</View>,
);
// Test correct style property
expect(screen.getByTestId('view')).toHaveStyle({ backgroundColor: 'red' });
// Test incorrect style property
expect(screen.getByTestId('view')).not.toHaveStyle({ backgroundColor: 'blue' });
});
/**
* MATCHER 15: toHaveProp()
*
* Checks if an element has specific props. This is useful for testing
* component props, React Native component properties, and custom attributes.
*
* You can test:
* - Prop existence: expect(element).toHaveProp('propName')
* - Prop value: expect(element).toHaveProp('propName', expectedValue)
* - Prop absence: expect(element).not.toHaveProp('propName')
*
* Usage:
* - expect(element).toHaveProp('propName') - prop exists
* - expect(element).toHaveProp('propName', value) - prop has specific value
* - expect(element).not.toHaveProp('propName') - prop doesn't exist
*/
test('showcase: toHaveProp', async () => {
await render(
<View>
<Text testID="text" numberOfLines={1} />
</View>,
);
// Test prop existence
expect(screen.getByTestId('text')).toHaveProp('numberOfLines');
expect(screen.getByTestId('text')).not.toHaveProp('adjustsFontSizeToFit');
// Test prop value
expect(screen.getByTestId('text')).toHaveProp('numberOfLines', 1);
expect(screen.getByTestId('text')).not.toHaveProp('numberOfLines', 5);
});
/**
* CHAPTER 3 SUMMARY
*
* Jest matchers are powerful tools that make your tests more readable and maintainable.
* They provide clear, descriptive assertions that express your testing intentions.
*
* Key takeaways:
* 1. Use descriptive matchers instead of complex boolean logic
* 2. All matchers can be negated with .not
* 3. Choose the right matcher for what you're testing:
* - toBeOnTheScreen() for element existence
* - toHaveTextContent() for text content
* - toBeEnabled()/toBeDisabled() for interaction states
* - toHaveStyle() for styling
* - toHaveProp() for component properties
* 4. Accessibility matchers help ensure your app is inclusive
* 5. Better matchers lead to better error messages when tests fail
*
* Next steps: Practice using these matchers in your own tests and explore
* combining them with queries and user interactions for comprehensive testing.
*/