Skip to content

Commit

Permalink
fix: support TextInput editable={false} prop check in isEnabled/isDis…
Browse files Browse the repository at this point in the history
…abled matchers (#135)

Co-authored-by: Maciej Jastrzebski <[email protected]>
  • Loading branch information
HugoChollet and mdjastrzebski authored Dec 9, 2022
1 parent d65e9f2 commit 4f74aa4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ 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 the following props :
- `disabled`
- `accessibilityState={{ disabled: true }}`
- `editable={false}` (for `TextInput` only)

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

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

// Check host TextInput
expect(getByTestId('disabled')).toBeDisabled();
expect(getByTestId('enabled-by-default')).not.toBeDisabled();
expect(getByTestId('enabled')).not.toBeDisabled();

// Check composite TextInput
expect(getByPlaceholderText('disabled')).toBeDisabled();
expect(getByPlaceholderText('enabled-by-default')).not.toBeDisabled();
expect(getByPlaceholderText('enabled')).not.toBeDisabled();
});
});

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

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

// Check host TextInput
expect(getByTestId('enabled-by-default')).toBeEnabled();
expect(getByTestId('enabled')).toBeEnabled();
expect(getByTestId('disabled')).not.toBeEnabled();

// Check composite TextInput
expect(getByPlaceholderText('enabled-by-default')).toBeEnabled();
expect(getByPlaceholderText('enabled')).toBeEnabled();
expect(getByPlaceholderText('disabled')).not.toBeEnabled();
});
});

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 +136,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 +148,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
8 changes: 7 additions & 1 deletion src/to-be-disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ const DISABLE_TYPES = [
];

function isElementDisabled(element: ReactTestInstance) {
if (!DISABLE_TYPES.includes(getType(element))) return false;
if (getType(element) === 'TextInput' && element?.props?.editable === false) {
return true;
}

if (!DISABLE_TYPES.includes(getType(element))) {
return false;
}

return (
!!element?.props?.disabled ||
Expand Down

0 comments on commit 4f74aa4

Please sign in to comment.