Skip to content

Commit 0ecb9c9

Browse files
fix: various disabled calculation edge cases (#1955)
1 parent 5678d04 commit 0ecb9c9

16 files changed

Lines changed: 234 additions & 41 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
All notable changes to React Native Testing Library will be documented in this file, starting
44
with v14.
55

6+
## Unreleased
7+
8+
### Bug fixes
9+
10+
- `toBeDisabled()` and `toBeEnabled()` incorrectly reported an element as disabled when one of
11+
its ancestors was disabled. This is not how React Native works: it doesn't pass the disabled
12+
state down to children, so a `Pressable` inside a disabled `View` or `Pressable` still responds
13+
to presses and isn't reported as disabled to assistive technologies. The matchers now check
14+
only the element's own props, as the `disabled` option of `*ByRole` queries already did.
15+
Assert on the element that has the state, e.g. the button instead of the text inside it.
16+
- A `TextInput` with `readOnly` is treated as non-editable, as React Native does: `fireEvent`
17+
and `userEvent` don't change its text, and `toBeDisabled()` and `*ByRole` queries with
18+
`disabled: true` match it. `readOnly` wins over `editable`.
19+
- `toBeDisabled()` and `*ByRole` queries with `disabled: true` match a `Switch` with `disabled` on
20+
iOS.
21+
- `toBeDisabled()` and `*ByRole` queries prefer `disabled` over `aria-disabled` and
22+
`accessibilityState.disabled` on `Text`, as React Native does. A `Text` with
23+
`disabled={false}` and `aria-disabled` is enabled.
24+
625
## 14.1.0
726

827
### Features

‎docs/api/jest-matchers.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,17 @@ expect(element).toBeEnabled();
9292
expect(element).toBeDisabled();
9393
```
9494

95-
Checks if an element is enabled or disabled from `aria-disabled` or `accessibilityState.disabled` props. An element is disabled if it or any ancestor is disabled.
95+
Checks if an element is enabled or disabled from `aria-disabled` or `accessibilityState.disabled` props. These host elements are also disabled:
96+
97+
- `TextInput` with `editable={false}` or `readOnly`
98+
- `Text` with `disabled`
99+
- `Switch` with `disabled`
100+
101+
As in React Native, an element doesn't inherit the disabled state from its ancestors. Check the element that has the state, e.g. the button, not the text inside it:
102+
103+
```tsx
104+
expect(screen.getByRole('button', { name: 'Submit' })).toBeDisabled();
105+
```
96106

97107
> [!NOTE]
98108
> These matchers are opposites. Both are available so you can avoid double negations like `expect(element).not.toBeDisabled()`.

‎docs/api/user-event.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ This function supports only host `TextInput` elements. Passing other element typ
111111

112112
The sequence of events depends on the `multiline` prop and the passed options.
113113

114-
Events will not be emitted if the `editable` prop is set to `false`.
114+
Events will not be emitted if the `editable` prop is set to `false` or the `readOnly` prop is set to `true`.
115115

116116
**Entering the element**:
117117

@@ -159,7 +159,7 @@ This function supports only host `TextInput` elements. Passing other element typ
159159

160160
### Sequence of events
161161

162-
Events will not be emitted if the `editable` prop is set to `false`.
162+
Events will not be emitted if the `editable` prop is set to `false` or the `readOnly` prop is set to `true`.
163163

164164
**Entering the element**:
165165

@@ -203,7 +203,7 @@ This function supports only host `TextInput` elements. Passing other element typ
203203

204204
### Sequence of events
205205

206-
Events will not be emitted if the `editable` prop is set to `false`.
206+
Events will not be emitted if the `editable` prop is set to `false` or the `readOnly` prop is set to `true`.
207207

208208
**Entering the element**:
209209

‎docs/guides/llm-guidelines.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ const { result, rerender, unmount } = await renderHook(() => useMyHook(), option
7373
| `toBeVisible()` | Element is visible (checks style, `aria-hidden`, `accessibilityElementsHidden`, ancestors) |
7474
| `toBeEmptyElement()` | Element has no children or text content |
7575
| `toContainElement(element)` | Element contains another element |
76-
| `toBeEnabled()` | Element is not disabled (checks `aria-disabled`, `accessibilityState`, ancestors) |
77-
| `toBeDisabled()` | Element has `aria-disabled` or `accessibilityState={{ disabled: true }}` (checks ancestors) |
76+
| `toBeEnabled()` | Element is not disabled (checks `aria-disabled`, `accessibilityState`) |
77+
| `toBeDisabled()` | Element has `aria-disabled` or `accessibilityState={{ disabled: true }}` (not from parents) |
7878
| `toBeBusy()` | Element has `aria-busy` or `accessibilityState={{ busy: true }}` |
7979
| `toBeChecked()` | Element has `aria-checked` or `accessibilityState={{ checked: true }}` |
8080
| `toBePartiallyChecked()` | Element has `aria-checked="mixed"` or `accessibilityState={{ checked: 'mixed' }}` |

‎src/events/__tests__/fire-event.test.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ describe('fireEvent.changeText', () => {
231231
expect(onChangeText).not.toHaveBeenCalled();
232232
expect(nativeState.valueForInstance.get(input)).toBeUndefined();
233233
});
234+
235+
test('does not fire on read-only TextInput', async () => {
236+
const onChangeText = jest.fn();
237+
await render(<TextInput testID="input" readOnly onChangeText={onChangeText} />);
238+
const input = screen.getByTestId('input');
239+
await fireEvent.changeText(input, 'new text');
240+
expect(onChangeText).not.toHaveBeenCalled();
241+
expect(nativeState.valueForInstance.get(input)).toBeUndefined();
242+
});
234243
});
235244

236245
describe('fireEvent.scroll', () => {

‎src/helpers/__tests__/accessibility.test.tsx‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,34 @@ describe('computeAriaDisabled', () => {
543543
expect(computeAriaDisabled(screen.getByTestId('editable-false'))).toBe(true);
544544
});
545545

546+
test('supports TextInput with "readOnly" prop', async () => {
547+
await render(
548+
<View>
549+
<TextInput testID="read-only" readOnly />
550+
<TextInput testID="read-only-false" readOnly={false} />
551+
<TextInput testID="read-only-false-non-editable" readOnly={false} editable={false} />
552+
</View>,
553+
);
554+
555+
expect(computeAriaDisabled(screen.getByTestId('read-only'))).toBe(true);
556+
expect(computeAriaDisabled(screen.getByTestId('read-only-false'))).toBe(false);
557+
expect(computeAriaDisabled(screen.getByTestId('read-only-false-non-editable'))).toBe(false);
558+
});
559+
560+
test('supports Switch', async () => {
561+
await render(
562+
<View>
563+
<Switch testID="default" />
564+
<Switch testID="disabled" disabled />
565+
<Switch testID="disabled-false" disabled={false} />
566+
</View>,
567+
);
568+
569+
expect(computeAriaDisabled(screen.getByTestId('default'))).toBe(false);
570+
expect(computeAriaDisabled(screen.getByTestId('disabled'))).toBe(true);
571+
expect(computeAriaDisabled(screen.getByTestId('disabled-false'))).toBe(false);
572+
});
573+
546574
test('supports Button', async () => {
547575
await render(
548576
<View>
@@ -569,12 +597,23 @@ describe('computeAriaDisabled', () => {
569597
<Text>Default Text</Text>
570598
<Text disabled>Disabled Text</Text>
571599
<Text aria-disabled>ARIA Disabled Text</Text>
600+
<Text accessibilityState={{ disabled: true }}>State Disabled Text</Text>
601+
<Text disabled={false} aria-disabled>
602+
Disabled False ARIA Disabled Text
603+
</Text>
604+
<Text disabled={false} accessibilityState={{ disabled: true }}>
605+
Disabled False State Disabled Text
606+
</Text>
572607
</View>,
573608
);
574609

575610
expect(computeAriaDisabled(screen.getByText('Default Text'))).toBe(false);
576611
expect(computeAriaDisabled(screen.getByText('Disabled Text'))).toBe(true);
577612
expect(computeAriaDisabled(screen.getByText('ARIA Disabled Text'))).toBe(true);
613+
expect(computeAriaDisabled(screen.getByText('State Disabled Text'))).toBe(true);
614+
// React Native's `Text` prefers `disabled` over `aria-disabled` and `accessibilityState`.
615+
expect(computeAriaDisabled(screen.getByText('Disabled False ARIA Disabled Text'))).toBe(false);
616+
expect(computeAriaDisabled(screen.getByText('Disabled False State Disabled Text'))).toBe(false);
578617
});
579618
});
580619

‎src/helpers/__tests__/text-input.test.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ test('isEditableTextInput basic test', async () => {
3737
expect(isEditableTextInput(screen.getByTestId('non-editable'))).toBe(false);
3838
expect(isEditableTextInput(screen.getByTestId('view'))).toBe(false);
3939
});
40+
41+
test('isEditableTextInput supports readOnly prop', async () => {
42+
await render(
43+
<View>
44+
<TextInput testID="read-only" readOnly />
45+
<TextInput testID="read-only-false" readOnly={false} />
46+
<TextInput testID="read-only-false-non-editable" readOnly={false} editable={false} />
47+
<TextInput testID="read-only-editable" readOnly editable />
48+
</View>,
49+
);
50+
51+
expect(isEditableTextInput(screen.getByTestId('read-only'))).toBe(false);
52+
expect(isEditableTextInput(screen.getByTestId('read-only-false'))).toBe(true);
53+
// React Native's `TextInput` prefers `readOnly` over `editable`.
54+
expect(isEditableTextInput(screen.getByTestId('read-only-false-non-editable'))).toBe(true);
55+
expect(isEditableTextInput(screen.getByTestId('read-only-editable'))).toBe(false);
56+
});

‎src/helpers/accessibility.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,16 @@ export function computeAriaDisabled(instance: TestInstance): boolean {
230230

231231
const { props } = instance;
232232

233-
if (isHostText(instance) && props.disabled) {
233+
// React Native's `Text` prefers `disabled` over `aria-disabled` and `accessibilityState`. The Jest
234+
// preset mocks `Text`, so its host element gets these props unchanged.
235+
if (isHostText(instance)) {
236+
return (
237+
(props.disabled ?? props['aria-disabled'] ?? props.accessibilityState?.disabled) === true
238+
);
239+
}
240+
241+
// On iOS, `Switch` passes `disabled` to its host element instead of `accessibilityState`.
242+
if (isHostSwitch(instance) && props.disabled === true) {
234243
return true;
235244
}
236245

‎src/helpers/text-input.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import type { TestInstance } from 'test-renderer';
33
import { nativeState } from '../events/native-state';
44
import { isHostTextInput } from './host-component-names';
55

6+
/**
7+
* Whether the element is a host `TextInput` the user can edit. As in React Native's `TextInput`,
8+
* `readOnly` wins over `editable`. The Jest preset mocks `TextInput`, so its host element gets
9+
* both props unchanged.
10+
*/
611
export function isEditableTextInput(instance: TestInstance) {
7-
return isHostTextInput(instance) && instance.props.editable !== false;
12+
if (!isHostTextInput(instance)) {
13+
return false;
14+
}
15+
16+
const { readOnly, editable } = instance.props;
17+
return readOnly !== undefined ? !readOnly : editable !== false;
818
}
919

1020
export function getTextInputValue(instance: TestInstance) {

‎src/matchers/__tests__/to-be-disabled.test.tsx‎

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import {
33
Button,
44
Pressable,
5+
Switch,
56
Text,
67
TextInput,
78
TouchableHighlight,
@@ -18,7 +19,7 @@ test('toBeDisabled()/toBeEnabled() supports basic case', async () => {
1819
<View>
1920
<View testID="disabled-parent" aria-disabled>
2021
<View>
21-
<View testID="disabled-child" />
22+
<View testID="child-of-disabled" />
2223
</View>
2324
</View>
2425
<View>
@@ -31,7 +32,7 @@ test('toBeDisabled()/toBeEnabled() supports basic case', async () => {
3132
);
3233

3334
expect(screen.getByTestId('disabled-parent')).toBeDisabled();
34-
expect(screen.getByTestId('disabled-child')).toBeDisabled();
35+
expect(screen.getByTestId('child-of-disabled')).not.toBeDisabled();
3536
expect(screen.getByTestId('enabled-view')).not.toBeDisabled();
3637
expect(screen.getByTestId('enabled-text')).not.toBeDisabled();
3738
expect(screen.getByTestId('enabled-text-input')).not.toBeDisabled();
@@ -59,7 +60,7 @@ test('toBeDisabled()/toBeEnabled() supports basic case', async () => {
5960
`);
6061

6162
expect(screen.getByTestId('disabled-parent')).not.toBeEnabled();
62-
expect(screen.getByTestId('disabled-child')).not.toBeEnabled();
63+
expect(screen.getByTestId('child-of-disabled')).toBeEnabled();
6364
expect(screen.getByTestId('enabled-view')).toBeEnabled();
6465
expect(screen.getByTestId('enabled-text')).toBeEnabled();
6566
expect(screen.getByTestId('enabled-text-input')).toBeEnabled();
@@ -99,8 +100,8 @@ test('toBeDisabled()/toBeEnabled() supports Pressable with "disabled" prop', asy
99100
expect(pressable).not.toBeEnabled();
100101

101102
const title = screen.getByText('Button');
102-
expect(title).toBeDisabled();
103-
expect(title).not.toBeEnabled();
103+
expect(title).not.toBeDisabled();
104+
expect(title).toBeEnabled();
104105

105106
expect(() => expect(pressable).toBeEnabled()).toThrowErrorMatchingInlineSnapshot(`
106107
"expect(instance).toBeEnabled()
@@ -132,19 +133,19 @@ test('toBeDisabled()/toBeEnabled() supports Pressable with "disabled" prop', asy
132133
/>"
133134
`);
134135

135-
expect(() => expect(title).toBeEnabled()).toThrowErrorMatchingInlineSnapshot(`
136-
"expect(instance).toBeEnabled()
136+
expect(() => expect(title).toBeDisabled()).toThrowErrorMatchingInlineSnapshot(`
137+
"expect(instance).toBeDisabled()
137138
138-
Received instance is not enabled:
139+
Received instance is not disabled:
139140
<Text>
140141
Button
141142
</Text>"
142143
`);
143144

144-
expect(() => expect(title).not.toBeDisabled()).toThrowErrorMatchingInlineSnapshot(`
145-
"expect(instance).not.toBeDisabled()
145+
expect(() => expect(title).not.toBeEnabled()).toThrowErrorMatchingInlineSnapshot(`
146+
"expect(instance).not.toBeEnabled()
146147
147-
Received instance is disabled:
148+
Received instance is enabled:
148149
<Text>
149150
Button
150151
</Text>"
@@ -164,7 +165,10 @@ test.each([
164165
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
165166
// @ts-ignore - JSX element type 'Component' does not have any construct or call signatures.
166167
<Component disabled testID="subject">
167-
<Text>Button</Text>
168+
{/* `TouchableWithoutFeedback` and `TouchableNativeFeedback` render their child as the host. */}
169+
<View>
170+
<Text>Button</Text>
171+
</View>
168172
</Component>,
169173
);
170174

@@ -173,13 +177,13 @@ test.each([
173177
expect(touchable).not.toBeEnabled();
174178

175179
const title = screen.getByText('Button');
176-
expect(title).toBeDisabled();
177-
expect(title).not.toBeEnabled();
180+
expect(title).not.toBeDisabled();
181+
expect(title).toBeEnabled();
178182

179183
expect(() => expect(touchable).toBeEnabled()).toThrow();
180184
expect(() => expect(touchable).not.toBeDisabled()).toThrow();
181-
expect(() => expect(title).toBeEnabled()).toThrow();
182-
expect(() => expect(title).not.toBeDisabled()).toThrow();
185+
expect(() => expect(title).toBeDisabled()).toThrow();
186+
expect(() => expect(title).not.toBeEnabled()).toThrow();
183187
},
184188
);
185189

@@ -256,6 +260,48 @@ test('toBeDisabled()/toBeEnabled() supports "editable" prop on TextInput', async
256260
expect(screen.getByTestId('disabled')).not.toBeEnabled();
257261
});
258262

263+
test('toBeDisabled()/toBeEnabled() supports "readOnly" prop on TextInput', async () => {
264+
await render(
265+
<View>
266+
<TextInput testID="read-only" readOnly />
267+
<TextInput testID="read-only-false" readOnly={false} />
268+
</View>,
269+
);
270+
271+
expect(screen.getByTestId('read-only')).toBeDisabled();
272+
expect(screen.getByTestId('read-only-false')).toBeEnabled();
273+
});
274+
275+
test('toBeDisabled()/toBeEnabled() supports "disabled" prop on Switch', async () => {
276+
await render(
277+
<View>
278+
<Switch testID="enabled" />
279+
<Switch testID="disabled" disabled />
280+
</View>,
281+
);
282+
283+
expect(screen.getByTestId('enabled')).toBeEnabled();
284+
expect(screen.getByTestId('disabled')).toBeDisabled();
285+
});
286+
287+
test('toBeDisabled()/toBeEnabled() does not inherit disabled state from ancestors', async () => {
288+
await render(
289+
<View>
290+
<View aria-disabled>
291+
<Pressable testID="in-disabled-view" />
292+
</View>
293+
<Pressable disabled>
294+
<Pressable testID="in-disabled-pressable" />
295+
</Pressable>
296+
</View>,
297+
);
298+
299+
expect(screen.getByTestId('in-disabled-view')).toBeEnabled();
300+
expect(screen.getByTestId('in-disabled-view')).not.toBeDisabled();
301+
expect(screen.getByTestId('in-disabled-pressable')).toBeEnabled();
302+
expect(screen.getByTestId('in-disabled-pressable')).not.toBeDisabled();
303+
});
304+
259305
test('toBeDisabled()/toBeEnabled() supports "disabled" prop on Button', async () => {
260306
await render(
261307
<View>

0 commit comments

Comments
 (0)