1
1
import React from 'react' ;
2
- import { type AccessibilityRole , View } from 'react-native' ;
2
+ import { type AccessibilityRole , Switch , View } from 'react-native' ;
3
3
import render from '../../render' ;
4
4
import { screen } from '../../screen' ;
5
5
import '../extend-expect' ;
@@ -30,6 +30,55 @@ function renderViewsWithRole(role: AccessibilityRole) {
30
30
) ;
31
31
}
32
32
33
+ test ( 'toBeCheck() with Switch' , ( ) => {
34
+ render (
35
+ < >
36
+ < Switch testID = "checked" value = { true } />
37
+ < Switch testID = "unchecked" value = { false } />
38
+ < Switch testID = "default" />
39
+ </ > ,
40
+ ) ;
41
+
42
+ const checked = screen . getByTestId ( 'checked' ) ;
43
+ const unchecked = screen . getByTestId ( 'unchecked' ) ;
44
+ const defaultView = screen . getByTestId ( 'default' ) ;
45
+
46
+ expect ( checked ) . toBeChecked ( ) ;
47
+ expect ( unchecked ) . not . toBeChecked ( ) ;
48
+ expect ( defaultView ) . not . toBeChecked ( ) ;
49
+
50
+ expect ( ( ) => expect ( checked ) . not . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
51
+ "expect(element).not.toBeChecked()
52
+
53
+ Received element is checked:
54
+ <RCTSwitch
55
+ accessibilityRole="switch"
56
+ testID="checked"
57
+ value={true}
58
+ />"
59
+ ` ) ;
60
+ expect ( ( ) => expect ( unchecked ) . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
61
+ "expect(element).toBeChecked()
62
+
63
+ Received element is not checked:
64
+ <RCTSwitch
65
+ accessibilityRole="switch"
66
+ testID="unchecked"
67
+ value={false}
68
+ />"
69
+ ` ) ;
70
+ expect ( ( ) => expect ( defaultView ) . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
71
+ "expect(element).toBeChecked()
72
+
73
+ Received element is not checked:
74
+ <RCTSwitch
75
+ accessibilityRole="switch"
76
+ testID="default"
77
+ value={false}
78
+ />"
79
+ ` ) ;
80
+ } ) ;
81
+
33
82
test ( 'toBeCheck() with checkbox role' , ( ) => {
34
83
renderViewsWithRole ( 'checkbox' ) ;
35
84
@@ -160,10 +209,10 @@ test('throws error for invalid role', () => {
160
209
const unchecked = screen . getByTestId ( 'adjustable-unchecked' ) ;
161
210
162
211
expect ( ( ) => expect ( checked ) . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot (
163
- `"toBeChecked() works only on accessibility elements with "checkbox" or "radio" role."` ,
212
+ `"toBeChecked() works only on "Switch" elements or accessibility elements with "checkbox" or "radio" role."` ,
164
213
) ;
165
214
expect ( ( ) => expect ( unchecked ) . not . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot (
166
- `"toBeChecked() works only on accessibility elements with "checkbox" or "radio" role."` ,
215
+ `"toBeChecked() works only on "Switch" elements or accessibility elements with "checkbox" or "radio" role."` ,
167
216
) ;
168
217
} ) ;
169
218
@@ -172,6 +221,6 @@ test('throws error for non-accessibility element', () => {
172
221
173
222
const view = screen . getByTestId ( 'test' ) ;
174
223
expect ( ( ) => expect ( view ) . toBeChecked ( ) ) . toThrowErrorMatchingInlineSnapshot (
175
- `"toBeChecked() works only on accessibility elements with "checkbox" or "radio" role."` ,
224
+ `"toBeChecked() works only on "Switch" elements or accessibility elements with "checkbox" or "radio" role."` ,
176
225
) ;
177
226
} ) ;
0 commit comments