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 thoses props value:
HugoChollet marked this conversation as resolved.
Show resolved Hide resolved

- `disabled={true}`
- `accessibilityState={{disabled: true}}`
- `editable={false}`

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
26 changes: 26 additions & 0 deletions src/__tests__/to-be-disabled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ describe('.toBeDisabled', () => {
expect(() => expect(queryByTestId(name)).not.toBeDisabled()).toThrow();
});
});

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

expect(queryByTestId('disabled')).toBeDisabled();
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
expect(queryByTestId('enabled')).not.toBeDisabled();
});
});

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

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

expect(queryByTestId('enabled-by-default')).toBeEnabled();
expect(queryByTestId('enabled')).toBeEnabled();
expect(queryByTestId('disabled')).not.toBeEnabled();
});
});

describe('for .toBeEnabled/Disabled Button', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/to-be-disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function isElementDisabled(element: ReactTestInstance) {
return (
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
!!element?.props?.disabled ||
!!element?.props?.accessibilityState?.disabled ||
!!element?.props?.accessibilityStates?.includes('disabled')
!!element?.props?.accessibilityStates?.includes('disabled') ||
element?.props?.editable === false
);
}

Expand Down