@@ -9,12 +9,24 @@ test('toHaveAccessibleName() on view with accessibilityLabel prop', () => {
9
9
expect ( element ) . toHaveAccessibleName ( 'Test Label' ) ;
10
10
} ) ;
11
11
12
+ test ( 'not.toHaveAccessibleName() on view with accessibilityLabel prop' , ( ) => {
13
+ render ( < View testID = "accessibility-label" accessibilityLabel = "Test Label" /> ) ;
14
+ const element = screen . getByTestId ( 'accessibility-label' ) ;
15
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Test Label' ) ;
16
+ } ) ;
17
+
12
18
test ( 'toHaveAccessibleName() on view with aria-label prop' , ( ) => {
13
19
render ( < View testID = "aria-label" aria-label = "Aria Test Label" /> ) ;
14
20
const element = screen . getByTestId ( 'aria-label' ) ;
15
21
expect ( element ) . toHaveAccessibleName ( 'Aria Test Label' ) ;
16
22
} ) ;
17
23
24
+ test ( 'not.toHaveAccessibleName() on view with aria-label prop' , ( ) => {
25
+ render ( < View testID = "aria-label" aria-label = "Aria Test Label" /> ) ;
26
+ const element = screen . getByTestId ( 'aria-label' ) ;
27
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Aria Test Label' ) ;
28
+ } ) ;
29
+
18
30
test ( 'toHaveAccessibleName() on view with accessibilityLabel prop with no expectedName' , ( ) => {
19
31
render ( < View testID = "no-expectName-label" accessibilityLabel = "Test Label" /> ) ;
20
32
const element = screen . getByTestId ( 'no-expectName-label' ) ;
@@ -27,6 +39,12 @@ test('toHaveAccessibleName() on view with no accessibility props', () => {
27
39
expect ( element ) . toHaveAccessibleName ( 'Text' ) ;
28
40
} ) ;
29
41
42
+ test ( 'not.toHaveAccessibleName() on view with no accessibility props' , ( ) => {
43
+ render ( < Text testID = "accessibility-label" > Text</ Text > ) ;
44
+ const element = screen . getByTestId ( 'accessibility-label' ) ;
45
+ expect ( element ) . not . toHaveAccessibleName ( 'Not the expected Text' ) ;
46
+ } ) ;
47
+
30
48
test ( 'toHaveAccessibleName() on view with that does not have the expected accessible name' , ( ) => {
31
49
render ( < View testID = "wrong-label" accessibilityLabel = "The actual label" /> ) ;
32
50
const element = screen . getByTestId ( 'wrong-label' ) ;
@@ -56,6 +74,13 @@ test('toHaveAccessibleName() on view that doesnt have accessible name defined',
56
74
` ) ;
57
75
} ) ;
58
76
77
+ test ( 'not.toHaveAccessibleName() on view that doesnt have accessible name defined' , ( ) => {
78
+ render ( < View testID = "no-accessibile-name" /> ) ;
79
+ const element = screen . getByTestId ( 'no-accessibile-name' ) ;
80
+
81
+ expect ( element ) . not . toHaveAccessibleName ( ) ;
82
+ } ) ;
83
+
59
84
it ( 'toHaveAccessibleName() on a non-host element' , ( ) => {
60
85
const nonElement = 'This is not a ReactTestInstance' ;
61
86
expect ( ( ) => expect ( nonElement ) . toHaveAccessibleName ( ) )
@@ -68,6 +93,18 @@ it('toHaveAccessibleName() on a non-host element', () => {
68
93
` ) ;
69
94
} ) ;
70
95
96
+ it ( 'not.toHaveAccessibleName() on a non-host element' , ( ) => {
97
+ const nonElement = 'This is not a ReactTestInstance' ;
98
+ expect ( ( ) => expect ( nonElement ) . not . toHaveAccessibleName ( ) )
99
+ . toThrowErrorMatchingInlineSnapshot ( `
100
+ "expect(received).not.toHaveAccessibleName()
101
+
102
+ received value must be a host element.
103
+ Received has type: string
104
+ Received has value: "This is not a ReactTestInstance""
105
+ ` ) ;
106
+ } ) ;
107
+
71
108
test ( 'toHaveAccessibleName() on view with accessibilityLabelledBy prop' , async ( ) => {
72
109
render (
73
110
< View >
@@ -83,6 +120,63 @@ test('toHaveAccessibleName() on view with accessibilityLabelledBy prop', async (
83
120
expect ( element ) . toHaveAccessibleName ( 'Accessibility LabelledBy' ) ;
84
121
} ) ;
85
122
123
+ test ( 'not.toHaveAccessibleName() on view with accessibilityLabelledBy prop' , async ( ) => {
124
+ render (
125
+ < View >
126
+ < Text nativeID = "formLabel" > Accessibility LabelledBy</ Text >
127
+ < TextInput
128
+ testID = "accessibility-labelledby"
129
+ accessibilityLabelledBy = "formLabel"
130
+ />
131
+ </ View >
132
+ ) ;
133
+
134
+ const element = screen . getByTestId ( 'accessibility-labelledby' ) ;
135
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Accessibility LabelledBy' ) ;
136
+ } ) ;
137
+
138
+ test ( 'getByLabelText supports nested accessibilityLabelledBy' , async ( ) => {
139
+ render (
140
+ < >
141
+ < View nativeID = "label" >
142
+ < Text > Nested Accessibility LabelledBy</ Text >
143
+ </ View >
144
+ < TextInput testID = "text-input" accessibilityLabelledBy = "label" />
145
+ </ >
146
+ ) ;
147
+
148
+ const element = screen . getByTestId ( 'text-input' ) ;
149
+ expect ( element ) . toHaveAccessibleName ( 'Nested Accessibility LabelledBy' ) ;
150
+ } ) ;
151
+
152
+ test ( 'not.toHaveAccessibleName() on view with nested accessibilityLabelledBy' , async ( ) => {
153
+ render (
154
+ < >
155
+ < View nativeID = "label" >
156
+ < Text > Nested Aria LabelledBy</ Text >
157
+ </ View >
158
+ < TextInput testID = "text-input" accessibilityLabelledBy = "label" />
159
+ </ >
160
+ ) ;
161
+
162
+ const element = screen . getByTestId ( 'text-input' ) ;
163
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Nested Aria LabelledBy' ) ;
164
+ } ) ;
165
+
166
+ test ( 'not.toHaveAccessibleName() on view with nested accessibilityLabelledBy with no text' , async ( ) => {
167
+ render (
168
+ < >
169
+ < View nativeID = "label" >
170
+ < View />
171
+ </ View >
172
+ < TextInput testID = "text-input" accessibilityLabelledBy = "label" />
173
+ </ >
174
+ ) ;
175
+
176
+ const element = screen . getByTestId ( 'text-input' ) ;
177
+ expect ( element ) . not . toHaveAccessibleName ( ) ;
178
+ } ) ;
179
+
86
180
test ( 'toHaveAccessibleName() on view with ariaLabelledBy prop' , async ( ) => {
87
181
render (
88
182
< View >
@@ -95,9 +189,20 @@ test('toHaveAccessibleName() on view with ariaLabelledBy prop', async () => {
95
189
expect ( element ) . toHaveAccessibleName ( 'Aria LabelledBy' ) ;
96
190
} ) ;
97
191
98
- //TODO: This fails as expected as I am unsure on how to extract the Text from the nested Element
192
+ test ( 'not.toHaveAccessibleName() on view with ariaLabelledBy prop' , async ( ) => {
193
+ render (
194
+ < View >
195
+ < Text nativeID = "formLabel" > Aria LabelledBy</ Text >
196
+ < TextInput testID = "aria-labelledby" aria-labelledby = "formLabel" />
197
+ </ View >
198
+ ) ;
199
+
200
+ const element = screen . getByTestId ( 'aria-labelledby' ) ;
201
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Aria LabelledBy' ) ;
202
+ } ) ;
203
+
99
204
test ( 'getByLabelText supports nested aria-labelledby' , async ( ) => {
100
- const screen = render (
205
+ render (
101
206
< >
102
207
< View nativeID = "label" >
103
208
< Text > Nested Aria LabelledBy</ Text >
@@ -109,3 +214,31 @@ test('getByLabelText supports nested aria-labelledby', async () => {
109
214
const element = screen . getByTestId ( 'text-input' ) ;
110
215
expect ( element ) . toHaveAccessibleName ( 'Nested Aria LabelledBy' ) ;
111
216
} ) ;
217
+
218
+ test ( 'not.toHaveAccessibleName() on view with nested aria-labelledby' , async ( ) => {
219
+ render (
220
+ < >
221
+ < View nativeID = "label" >
222
+ < Text > Nested Aria LabelledBy</ Text >
223
+ </ View >
224
+ < TextInput testID = "text-input" aria-labelledby = "label" />
225
+ </ >
226
+ ) ;
227
+
228
+ const element = screen . getByTestId ( 'text-input' ) ;
229
+ expect ( element ) . not . toHaveAccessibleName ( 'Not Nested Aria LabelledBy' ) ;
230
+ } ) ;
231
+
232
+ test ( 'not.toHaveAccessibleName() on view with nested aria-labelledby with no text' , async ( ) => {
233
+ render (
234
+ < >
235
+ < View nativeID = "label" >
236
+ < View />
237
+ </ View >
238
+ < TextInput testID = "text-input" aria-labelledby = "label" />
239
+ </ >
240
+ ) ;
241
+
242
+ const element = screen . getByTestId ( 'text-input' ) ;
243
+ expect ( element ) . not . toHaveAccessibleName ( ) ;
244
+ } ) ;
0 commit comments