Skip to content
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

fix: support TextInput editable={false} prop check in isEnabled/isDisabled matchers #135

7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ toBeDisabled();

Check whether or not an element is disabled from a user perspective.

This matcher will check if the element or its parent has a `disabled` prop, or if it has
`accessibilityState={{disabled: true}}.
This matcher will check if the element or its parent has any of those props :

- `disabled={true}`
- `accessibilityState={{disabled: true}}`
- `editable={false}` (for `TextInput` only)
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved

It also works with `accessibilityStates={['disabled']}` for now. However, this prop is deprecated in
React Native [0.62](https://reactnative.dev/blog/2020/03/26/version-0.62#breaking-changes)
Expand Down
34 changes: 31 additions & 3 deletions src/__tests__/to-be-disabled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ describe('.toBeDisabled', () => {
expect(() => expect(queryByTestId(name)).not.toBeDisabled()).toThrow();
});
});

test('handle editable prop for TextInput', () => {
const { getByTestId } = render(
<View>
<TextInput testID="disabled" editable={false} />
HugoChollet marked this conversation as resolved.
Show resolved Hide resolved
<TextInput testID="enabled-by-default" />
<TextInput testID="enabled" editable />
</View>,
);

expect(getByTestId('disabled')).toBeDisabled();
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
expect(getByTestId('enabled-by-default')).not.toBeDisabled();
expect(getByTestId('enabled')).not.toBeDisabled();
});
});

describe('.toBeEnabled', () => {
Expand Down Expand Up @@ -79,12 +93,26 @@ describe('.toBeEnabled', () => {
expect(() => expect(queryByTestId(name)).not.toBeEnabled()).toThrow();
});
});

test('handle editable prop for TextInput', () => {
const { getByTestId } = render(
<View>
<TextInput testID="enabled-by-default" />
<TextInput testID="enabled" editable />
<TextInput testID="disabled" editable={false} />
</View>,
);

expect(getByTestId('enabled-by-default')).toBeEnabled();
expect(getByTestId('enabled')).toBeEnabled();
expect(getByTestId('disabled')).not.toBeEnabled();
});
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
});

describe('for .toBeEnabled/Disabled Button', () => {
test('handles disabled prop for button', () => {
const { queryByTestId } = render(
<View testID="view">
<View>
<Button testID="enabled" title="enabled" />
<Button disabled testID="disabled" title="disabled" />
</View>,
Expand All @@ -96,7 +124,7 @@ describe('for .toBeEnabled/Disabled Button', () => {

test('handles button a11y state', () => {
const { queryByTestId } = render(
<View testID="view">
<View>
<Button accessibilityState={{ disabled: false }} testID="enabled" title="enabled" />
<Button accessibilityState={{ disabled: true }} testID="disabled" title="disabled" />
</View>,
Expand All @@ -108,7 +136,7 @@ describe('for .toBeEnabled/Disabled Button', () => {

test('Errors when matcher misses', () => {
const { queryByTestId, queryByText } = render(
<View testID="view">
<View>
<Button testID="enabled" title="enabled" />
<Button disabled testID="disabled" title="disabled" />
</View>,
Expand Down
3 changes: 3 additions & 0 deletions src/to-be-disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const DISABLE_TYPES = [
];

function isElementDisabled(element: ReactTestInstance) {
if (getType(element) === 'TextInput' && element?.props?.editable === false) {
HugoChollet marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
if (!DISABLE_TYPES.includes(getType(element))) return false;
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved

return (
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
Expand Down