Skip to content

Commit 1a073c1

Browse files
committed
refactor: update tests
1 parent d4de509 commit 1a073c1

File tree

6 files changed

+135
-163
lines changed

6 files changed

+135
-163
lines changed

src/helpers/accessibility.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
} from 'react-native';
88
import { ReactTestInstance } from 'react-test-renderer';
99
import { getHostSiblings, getUnsafeRootElement } from './component-tree';
10-
import { getHostComponentNames, isHostSwitch, isHostText } from './host-component-names';
10+
import {
11+
getHostComponentNames,
12+
isHostSwitch,
13+
isHostText,
14+
isHostTextInput,
15+
} from './host-component-names';
1116
import { getTextContent } from './text-content';
1217
import { isTextInputEditable } from './text-input';
1318

@@ -169,8 +174,8 @@ export function computeAriaChecked(element: ReactTestInstance): AccessibilitySta
169174

170175
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#disabled-state
171176
export function computeAriaDisabled(element: ReactTestInstance): boolean {
172-
if (isHostSwitch(element)) {
173-
return !isTextInputEditable(element);
177+
if (isHostTextInput(element) && !isTextInputEditable(element)) {
178+
return true;
174179
}
175180

176181
const { props } = element;

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

Lines changed: 0 additions & 96 deletions
This file was deleted.

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,95 @@ test('toBeExpanded() error messages', () => {
9494
/>"
9595
`);
9696
});
97+
98+
test('toBeCollapsed() basic case', () => {
99+
render(
100+
<>
101+
<View testID="expanded" accessibilityState={{ expanded: true }} />
102+
<View testID="expanded-aria" aria-expanded />
103+
<View testID="not-expanded" accessibilityState={{ expanded: false }} />
104+
<View testID="not-expanded-aria" aria-expanded={false} />
105+
<View testID="default" />
106+
</>,
107+
);
108+
109+
expect(screen.getByTestId('expanded')).not.toBeCollapsed();
110+
expect(screen.getByTestId('expanded-aria')).not.toBeCollapsed();
111+
expect(screen.getByTestId('not-expanded')).toBeCollapsed();
112+
expect(screen.getByTestId('not-expanded-aria')).toBeCollapsed();
113+
expect(screen.getByTestId('default')).not.toBeCollapsed();
114+
});
115+
116+
test('toBeCollapsed() error messages', () => {
117+
render(
118+
<>
119+
<View testID="expanded" accessibilityState={{ expanded: true }} />
120+
<View testID="expanded-aria" aria-expanded />
121+
<View testID="not-expanded" accessibilityState={{ expanded: false }} />
122+
<View testID="not-expanded-aria" aria-expanded={false} />
123+
<View testID="default" />
124+
</>,
125+
);
126+
127+
expect(() => expect(screen.getByTestId('expanded')).toBeCollapsed())
128+
.toThrowErrorMatchingInlineSnapshot(`
129+
"expect(element).toBeCollapsed()
130+
131+
Received element is not collapsed:
132+
<View
133+
accessibilityState={
134+
{
135+
"expanded": true,
136+
}
137+
}
138+
testID="expanded"
139+
/>"
140+
`);
141+
142+
expect(() => expect(screen.getByTestId('expanded-aria')).toBeCollapsed())
143+
.toThrowErrorMatchingInlineSnapshot(`
144+
"expect(element).toBeCollapsed()
145+
146+
Received element is not collapsed:
147+
<View
148+
aria-expanded={true}
149+
testID="expanded-aria"
150+
/>"
151+
`);
152+
153+
expect(() => expect(screen.getByTestId('not-expanded')).not.toBeCollapsed())
154+
.toThrowErrorMatchingInlineSnapshot(`
155+
"expect(element).not.toBeCollapsed()
156+
157+
Received element is collapsed:
158+
<View
159+
accessibilityState={
160+
{
161+
"expanded": false,
162+
}
163+
}
164+
testID="not-expanded"
165+
/>"
166+
`);
167+
168+
expect(() => expect(screen.getByTestId('not-expanded-aria')).not.toBeCollapsed())
169+
.toThrowErrorMatchingInlineSnapshot(`
170+
"expect(element).not.toBeCollapsed()
171+
172+
Received element is collapsed:
173+
<View
174+
aria-expanded={false}
175+
testID="not-expanded-aria"
176+
/>"
177+
`);
178+
179+
expect(() => expect(screen.getByTestId('default')).toBeCollapsed())
180+
.toThrowErrorMatchingInlineSnapshot(`
181+
"expect(element).toBeCollapsed()
182+
183+
Received element is not collapsed:
184+
<View
185+
testID="default"
186+
/>"
187+
`);
188+
});

src/matchers/extend-expect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { toBeOnTheScreen } from './to-be-on-the-screen';
22
import { toBeChecked } from './to-be-checked';
3-
import { toBeCollapsed } from './to-be-collapsed';
43
import { toBeDisabled, toBeEnabled } from './to-be-disabled';
54
import { toBeBusy } from './to-be-busy';
65
import { toBeEmptyElement } from './to-be-empty-element';
7-
import { toBeExpanded } from './to-be-expanded';
6+
import { toBeExpanded, toBeCollapsed } from './to-be-expanded';
87
import { toBePartiallyChecked } from './to-be-partially-checked';
98
import { toBeSelected } from './to-be-selected';
109
import { toBeVisible } from './to-be-visible';

src/queries/__tests__/accessibility-state.test.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ test('getAllByA11yState, queryAllByA11yState, findAllByA11yState', async () => {
8888

8989
describe('checked state matching', () => {
9090
it('handles true', () => {
91-
render(<View accessibilityState={{ checked: true }} />);
91+
render(<View role="checkbox" accessibilityState={{ checked: true }} />);
9292

9393
expect(screen.getByA11yState({ checked: true })).toBeTruthy();
9494
expect(screen.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
9595
expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
9696
});
9797

9898
it('handles mixed', () => {
99-
render(<View accessibilityState={{ checked: 'mixed' }} />);
99+
render(<View role="checkbox" accessibilityState={{ checked: 'mixed' }} />);
100100

101101
expect(screen.getByA11yState({ checked: 'mixed' })).toBeTruthy();
102102
expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
103103
expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
104104
});
105105

106106
it('handles false', () => {
107-
render(<View accessibilityState={{ checked: false }} />);
107+
render(<View role="checkbox" accessibilityState={{ checked: false }} />);
108108

109109
expect(screen.getByA11yState({ checked: false })).toBeTruthy();
110110
expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
111111
expect(screen.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
112112
});
113113

114-
it('handles default', () => {
115-
render(<View accessibilityState={{}} />);
114+
it('handles default', () => {
115+
render(<View role="checkbox" accessibilityState={{}} />);
116116

117117
expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
118118
expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
@@ -135,7 +135,7 @@ describe('expanded state matching', () => {
135135
expect(screen.queryByA11yState({ expanded: true })).toBeFalsy();
136136
});
137137

138-
it('handles default', () => {
138+
it('handles default', () => {
139139
render(<View accessibilityState={{}} />);
140140

141141
expect(screen.queryByA11yState({ expanded: false })).toBeFalsy();
@@ -158,7 +158,7 @@ describe('disabled state matching', () => {
158158
expect(screen.queryByA11yState({ disabled: true })).toBeFalsy();
159159
});
160160

161-
it('handles default', () => {
161+
it('handles default', () => {
162162
render(<View accessibilityState={{}} />);
163163

164164
expect(screen.getByA11yState({ disabled: false })).toBeTruthy();
@@ -181,7 +181,7 @@ describe('busy state matching', () => {
181181
expect(screen.queryByA11yState({ busy: true })).toBeFalsy();
182182
});
183183

184-
it('handles default', () => {
184+
it('handles default', () => {
185185
render(<View accessibilityState={{}} />);
186186

187187
expect(screen.getByA11yState({ busy: false })).toBeTruthy();
@@ -204,7 +204,7 @@ describe('selected state matching', () => {
204204
expect(screen.queryByA11yState({ selected: true })).toBeFalsy();
205205
});
206206

207-
it('handles default', () => {
207+
it('handles default', () => {
208208
render(<View accessibilityState={{}} />);
209209

210210
expect(screen.getByA11yState({ selected: false })).toBeTruthy();
@@ -463,28 +463,28 @@ describe('aria-selected prop', () => {
463463

464464
describe('aria-checked prop', () => {
465465
test('supports aria-checked={true} prop', () => {
466-
render(<View accessible accessibilityRole="button" aria-checked={true} />);
466+
render(<View accessible accessibilityRole="checkbox" aria-checked={true} />);
467467
expect(screen.getByAccessibilityState({ checked: true })).toBeTruthy();
468468
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();
469469
expect(screen.queryByAccessibilityState({ checked: 'mixed' })).toBeNull();
470470
});
471471

472472
test('supports aria-checked={false} prop', () => {
473-
render(<View accessible accessibilityRole="button" aria-checked={false} />);
473+
render(<View accessible accessibilityRole="checkbox" aria-checked={false} />);
474474
expect(screen.getByAccessibilityState({ checked: false })).toBeTruthy();
475475
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
476476
expect(screen.queryByAccessibilityState({ checked: 'mixed' })).toBeNull();
477477
});
478478

479-
test('supports aria-checked="mixed prop', () => {
480-
render(<View accessible accessibilityRole="button" aria-checked="mixed" />);
479+
test('supports aria-checked="mixed" prop', () => {
480+
render(<View accessible accessibilityRole="checkbox" aria-checked="mixed" />);
481481
expect(screen.getByAccessibilityState({ checked: 'mixed' })).toBeTruthy();
482482
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
483483
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();
484484
});
485485

486-
test('supports default aria-selected prop', () => {
487-
render(<View accessible accessibilityRole="button" />);
486+
test('supports default aria-checked prop', () => {
487+
render(<View accessible accessibilityRole="checkbox" />);
488488
expect(screen.getByAccessibilityState({})).toBeTruthy();
489489
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
490490
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();

0 commit comments

Comments
 (0)