Skip to content

Commit 3c29061

Browse files
committed
update logic and tests
1 parent 020bc00 commit 3c29061

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ import { render } from '../..';
1313
import '../extend-expect';
1414

1515
const ALLOWED_COMPONENTS = {
16-
View,
17-
TextInput,
1816
TouchableHighlight,
1917
TouchableOpacity,
2018
TouchableWithoutFeedback,
2119
TouchableNativeFeedback,
2220
Pressable,
2321
};
2422

23+
const ARIA_COMPONENTS = {
24+
View,
25+
TextInput,
26+
};
27+
28+
const ALL_COMPONENTS = { ...ALLOWED_COMPONENTS, ...ARIA_COMPONENTS };
29+
2530
describe('.toBeDisabled', () => {
2631
Object.entries(ALLOWED_COMPONENTS).forEach(([name, Component]) => {
2732
test(`handle disabled prop for element ${name}`, () => {
@@ -37,7 +42,20 @@ describe('.toBeDisabled', () => {
3742
});
3843
});
3944

40-
Object.entries(ALLOWED_COMPONENTS).forEach(([name, Component]) => {
45+
Object.entries(ARIA_COMPONENTS).forEach(([name, Component]) => {
46+
test(`handle aria-disabled prop for element ${name}`, () => {
47+
const { queryByTestId } = render(
48+
<Component aria-disabled testID={name}>
49+
<TextInput />
50+
</Component>
51+
);
52+
53+
expect(queryByTestId(name)).toBeDisabled();
54+
expect(() => expect(queryByTestId(name)).not.toBeDisabled()).toThrow();
55+
});
56+
});
57+
58+
Object.entries(ALL_COMPONENTS).forEach(([name, Component]) => {
4159
test(`handle disabled in accessibilityState for element ${name}`, () => {
4260
const { queryByTestId } = render(
4361
//@ts-expect-error JSX element type 'Component' does not have any construct or call signatures.ts(2604)
@@ -76,7 +94,7 @@ describe('.toBeDisabled', () => {
7694
});
7795

7896
describe('.toBeEnabled', () => {
79-
Object.entries(ALLOWED_COMPONENTS).forEach(([name, Component]) => {
97+
Object.entries(ALL_COMPONENTS).forEach(([name, Component]) => {
8098
test(`handle disabled prop for element ${name} when undefined`, () => {
8199
const { queryByTestId } = render(
82100
//@ts-expect-error JSX element type 'Component' does not have any construct or call signatures.ts(2604)
@@ -90,7 +108,7 @@ describe('.toBeEnabled', () => {
90108
});
91109
});
92110

93-
Object.entries(ALLOWED_COMPONENTS).forEach(([name, Component]) => {
111+
Object.entries(ALL_COMPONENTS).forEach(([name, Component]) => {
94112
test(`handle disabled in accessibilityState for element ${name} when false`, () => {
95113
const { queryByTestId } = render(
96114
//@ts-expect-error JSX element type 'Component' does not have any construct or call signatures.ts(2604)

src/matchers/to-be-disabled.tsx

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { matcherHint } from 'jest-matcher-utils';
3-
import { checkHostElement, formatMessage, getType } from './utils';
4-
5-
// Elements that support 'disabled'
6-
const DISABLE_TYPES = [
7-
'Button',
8-
'Slider',
9-
'Switch',
10-
'Text',
11-
'TouchableHighlight',
12-
'TouchableOpacity',
13-
'TouchableWithoutFeedback',
14-
'TouchableNativeFeedback',
15-
'View',
16-
'TextInput',
17-
'Pressable',
18-
];
3+
import { isHostTextInput } from '../helpers/host-component-names';
4+
import { checkHostElement, formatMessage } from './utils';
195

206
function isElementDisabled(element: ReactTestInstance) {
21-
if (getType(element) === 'TextInput' && element?.props?.editable === false) {
7+
if (isHostTextInput(element) && element?.props?.editable === false) {
228
return true;
239
}
2410

25-
if (!DISABLE_TYPES.includes(getType(element))) {
26-
return false;
27-
}
28-
2911
return (
30-
!!element?.props?.disabled ||
31-
!!element?.props?.accessibilityState?.disabled ||
32-
!!element?.props?.accessibilityStates?.includes('disabled')
12+
!!element?.props?.['aria-disabled'] ||
13+
!!element?.props?.accessibilityState?.disabled
3314
);
3415
}
3516

@@ -60,8 +41,8 @@ export function toBeDisabled(
6041
''
6142
),
6243
'',
44+
'',
6345
`Received element ${is} disabled:`,
64-
printElement(element),
6546
null
6647
),
6748
].join('\n');
@@ -82,10 +63,13 @@ export function toBeEnabled(
8263
message: () => {
8364
const is = isEnabled ? 'is' : 'is not';
8465
return [
85-
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''),
86-
'',
87-
`Received element ${is} enabled:`,
88-
printElement(element),
66+
formatMessage(
67+
matcherHint(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''),
68+
'',
69+
'',
70+
`Received element ${is} enabled:`,
71+
null
72+
),
8973
].join('\n');
9074
},
9175
};

src/matchers/utils.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,3 @@ export function formatMessage(
121121
function formatValue(value: unknown) {
122122
return typeof value === 'string' ? value : stringify(value);
123123
}
124-
125-
export function getType({ type }: ReactTestInstance) {
126-
// @ts-expect-error: ReactTestInstance contains too loose typing
127-
return type.displayName || type.name || type;
128-
}

0 commit comments

Comments
 (0)