Skip to content

fix: toBeDisabled now also evaluates disabled prop when element is Text #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 73 additions & 5 deletions src/helpers/__tests__/accessiblity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Pressable, Switch, Text, TextInput, TouchableOpacity, View } from 'react-native';

import { isHiddenFromAccessibility, isInaccessible, render, screen } from '../..';
import { computeAriaLabel, isAccessibilityElement } from '../accessibility';
import { computeAriaDisabled, computeAriaLabel, isAccessibilityElement } from '../accessibility';

describe('isHiddenFromAccessibility', () => {
test('returns false for accessible elements', () => {
Expand Down Expand Up @@ -278,11 +278,11 @@ describe('isHiddenFromAccessibility', () => {
test('has isInaccessible alias', () => {
expect(isInaccessible).toBe(isHiddenFromAccessibility);
});
});

test('is not triggered for element with "aria-modal" prop', () => {
render(<View aria-modal testID="subject" />);
expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
test('is not triggered for element with "aria-modal" prop', () => {
render(<View aria-modal testID="subject" />);
expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
});
});

describe('isAccessibilityElement', () => {
Expand Down Expand Up @@ -408,3 +408,71 @@ describe('computeAriaLabel', () => {
expect(computeAriaLabel(screen.getByTestId('subject'))).toEqual('External Label');
});
});

describe('computeAriaDisabled', () => {
test('supports basic usage', () => {
render(
<View>
<View testID="default" />
<View testID="disabled" aria-disabled />
<View testID="disabled-false" aria-disabled={false} />
<View testID="disabled-by-state" accessibilityState={{ disabled: true }} />
<View testID="disabled-false-by-state" accessibilityState={{ disabled: false }} />
</View>,
);

expect(computeAriaDisabled(screen.getByTestId('default'))).toBe(false);
expect(computeAriaDisabled(screen.getByTestId('disabled'))).toBe(true);
expect(computeAriaDisabled(screen.getByTestId('disabled-false'))).toBe(false);
expect(computeAriaDisabled(screen.getByTestId('disabled-by-state'))).toBe(true);
expect(computeAriaDisabled(screen.getByTestId('disabled-false-by-state'))).toBe(false);
});

test('supports TextInput', () => {
render(
<View>
<TextInput testID="default" />
<TextInput testID="editable" editable />
<TextInput testID="editable-false" editable={false} />
</View>,
);

expect(computeAriaDisabled(screen.getByTestId('default'))).toBe(false);
expect(computeAriaDisabled(screen.getByTestId('editable'))).toBe(false);
expect(computeAriaDisabled(screen.getByTestId('editable-false'))).toBe(true);
});

test('supports Button', () => {
render(
<View>
<Pressable testID="default" role="button">
<Text>Default Button</Text>
</Pressable>
<Pressable testID="disabled" role="button" disabled>
<Text>Disabled Button</Text>
</Pressable>
<Pressable testID="disabled-false" role="button" disabled={false}>
<Text>Disabled False Button</Text>
</Pressable>
</View>,
);

expect(computeAriaDisabled(screen.getByTestId('default'))).toBe(false);
expect(computeAriaDisabled(screen.getByTestId('disabled'))).toBe(true);
expect(computeAriaDisabled(screen.getByTestId('disabled-false'))).toBe(false);
});

test('supports Text', () => {
render(
<View>
<Text>Default Text</Text>
<Text disabled>Disabled Text</Text>
<Text aria-disabled>ARIA Disabled Text</Text>
</View>,
);

expect(computeAriaDisabled(screen.getByText('Default Text'))).toBe(false);
expect(computeAriaDisabled(screen.getByText('Disabled Text'))).toBe(true);
expect(computeAriaDisabled(screen.getByText('ARIA Disabled Text'))).toBe(true);
});
});
5 changes: 5 additions & 0 deletions src/helpers/accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ export function computeAriaDisabled(element: ReactTestInstance): boolean {
}

const { props } = element;

if (isHostText(element) && props.disabled) {
return true;
}

return props['aria-disabled'] ?? props.accessibilityState?.disabled ?? false;
}

Expand Down