Skip to content

Commit 80b0bb8

Browse files
committed
refactor: tweaks
1 parent ad6c5b1 commit 80b0bb8

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

src/helpers/accessiblity.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ export function getAccessibilityCheckedState(
176176
return ariaChecked ?? accessibilityState?.checked;
177177
}
178178

179+
export function getAccessibilitySelectedState(
180+
element: ReactTestInstance
181+
): NonNullable<AccessibilityState['selected']> {
182+
const { accessibilityState, 'aria-selected': ariaSelected } = element.props;
183+
return ariaSelected ?? accessibilityState?.selected ?? false;
184+
}
185+
179186
export function getAccessibilityValue(
180187
element: ReactTestInstance
181188
): AccessibilityValue | undefined {

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ test('.toBeSelected() error messages', () => {
3030
.toThrowErrorMatchingInlineSnapshot(`
3131
"expect(element).toBeSelected()
3232
33-
Expected the element to have accessibility state selected:
34-
35-
Received element is not selected:
33+
Received element is not selected
3634
<View
3735
accessibilityState={
3836
{
@@ -46,9 +44,7 @@ test('.toBeSelected() error messages', () => {
4644
.toThrowErrorMatchingInlineSnapshot(`
4745
"expect(element).not.toBeSelected()
4846
49-
Expected the element not to have accessibility state selected:
50-
51-
Received element is selected:
47+
Received element is selected
5248
<View
5349
accessibilityState={
5450
{
@@ -63,9 +59,7 @@ test('.toBeSelected() error messages', () => {
6359
).toThrowErrorMatchingInlineSnapshot(`
6460
"expect(element).toBeSelected()
6561
66-
Expected the element to have accessibility state selected:
67-
68-
Received element is not selected:
62+
Received element is not selected
6963
<View
7064
testID="no-accessibilityState"
7165
/>"

src/matchers/to-be-selected.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { matcherHint } from 'jest-matcher-utils';
3-
import { getAccessibilityState } from '../helpers/accessiblity';
4-
import { checkHostElement, formatElement, formatMessage } from './utils';
3+
import { getAccessibilitySelectedState } from '../helpers/accessiblity';
4+
import { checkHostElement, formatElement } from './utils';
55

66
export function toBeSelected(
77
this: jest.MatcherContext,
@@ -10,23 +10,15 @@ export function toBeSelected(
1010
checkHostElement(element, toBeSelected, this);
1111

1212
return {
13-
pass: getAccessibilityState(element)?.selected === true,
13+
pass: getAccessibilitySelectedState(element),
1414
message: () => {
1515
const is = this.isNot ? 'is' : 'is not';
16-
const matcher = matcherHint(
17-
`${this.isNot ? '.not' : ''}.toBeSelected`,
18-
'element',
19-
''
20-
);
21-
return formatMessage(
22-
matcher,
23-
`Expected the element ${
24-
this.isNot ? 'not to' : 'to'
25-
} have accessibility state selected`,
16+
return [
17+
matcherHint(`${this.isNot ? '.not' : ''}.toBeSelected`, 'element', ''),
2618
'',
2719
`Received element ${is} selected`,
28-
formatElement(element)
29-
);
20+
formatElement(element),
21+
].join('\n');
3022
},
3123
};
3224
}

0 commit comments

Comments
 (0)