Skip to content

Commit b586981

Browse files
committed
code review changes
1 parent 4b40f81 commit b586981

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/user-event/accessibility-action/__tests__/accessibility-action.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function renderViewWithActions(props: React.ComponentProps<typeof View> =
2424
}
2525

2626
describe('userEvent.accessibilityAction', () => {
27-
it('triggers the onAccessibilityAction handler with the given action name', async () => {
27+
test('triggers the onAccessibilityAction handler with the given action name', async () => {
2828
const user = userEvent.setup();
2929
const { events } = await renderViewWithActions();
3030

@@ -37,7 +37,7 @@ describe('userEvent.accessibilityAction', () => {
3737
});
3838
});
3939

40-
it('supports the direct (setup-less) call form', async () => {
40+
test('supports the direct (setup-less) call form', async () => {
4141
const { events } = await renderViewWithActions();
4242

4343
await userEvent.accessibilityAction(screen.getByTestId('view'), 'activate');
@@ -46,7 +46,7 @@ describe('userEvent.accessibilityAction', () => {
4646
expect(lastEventPayload(events, 'accessibilityAction').nativeEvent.actionName).toBe('activate');
4747
});
4848

49-
it('throws when the action is not declared in accessibilityActions', async () => {
49+
test('throws when the action is not declared in accessibilityActions', async () => {
5050
const user = userEvent.setup();
5151
await renderViewWithActions();
5252

@@ -55,7 +55,7 @@ describe('userEvent.accessibilityAction', () => {
5555
);
5656
});
5757

58-
it('throws when the element declares no accessibility actions', async () => {
58+
test('throws when the element declares no accessibility actions', async () => {
5959
const user = userEvent.setup();
6060
await renderViewWithActions({ accessibilityActions: undefined });
6161

@@ -64,7 +64,7 @@ describe('userEvent.accessibilityAction', () => {
6464
);
6565
});
6666

67-
it('throws when the element is disabled', async () => {
67+
test('throws when the element is disabled', async () => {
6868
const user = userEvent.setup();
6969
await renderViewWithActions({ 'aria-disabled': true });
7070

@@ -73,7 +73,7 @@ describe('userEvent.accessibilityAction', () => {
7373
);
7474
});
7575

76-
it('throws when the element is disabled via accessibilityState', async () => {
76+
test('throws when the element is disabled via accessibilityState', async () => {
7777
const user = userEvent.setup();
7878
await renderViewWithActions({ accessibilityState: { disabled: true } });
7979

src/user-event/accessibility-action/accessibility-action.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { TestInstance } from 'test-renderer';
66

77
import { buildAccessibilityActionEvent } from '../../event-builder';
88
import { computeAriaDisabled } from '../../helpers/accessibility';
9+
import { isTestInstance } from '../../helpers/component-tree';
910
import { ErrorWithStack } from '../../helpers/errors';
1011
import type { StringWithAutocomplete } from '../../types';
1112
import type { UserEventInstance } from '../setup';
@@ -39,18 +40,28 @@ export async function accessibilityAction(
3940
instance: TestInstance,
4041
actionName: AccessibilityActionName,
4142
): Promise<void> {
43+
if (!isTestInstance(instance)) {
44+
throw new ErrorWithStack(
45+
`accessibilityAction() works only with host instances.`,
46+
accessibilityAction,
47+
);
48+
}
49+
4250
const actions = instance.props.accessibilityActions as
4351
| ReadonlyArray<AccessibilityActionInfo>
4452
| undefined;
4553

46-
if (!actions?.some((action) => action.name === actionName)) {
47-
const declared = actions?.map((action) => action.name);
54+
if (!actions?.length) {
55+
throw new ErrorWithStack(
56+
`accessibilityAction() called with action "${actionName}", but the element declares no accessibility actions in the "accessibilityActions" prop.`,
57+
accessibilityAction,
58+
);
59+
}
60+
61+
if (!actions.some((action) => action.name === actionName)) {
62+
const declared = actions.map((action) => `"${action.name}"`).join(', ');
4863
throw new ErrorWithStack(
49-
`accessibilityAction() called with action "${actionName}", but the element does not declare it in the "accessibilityActions" prop. ${
50-
declared?.length
51-
? `Declared actions: ${declared.map((name) => `"${name}"`).join(', ')}.`
52-
: 'The element declares no accessibility actions.'
53-
}`,
64+
`accessibilityAction() called with action "${actionName}", but the element does not declare it in the "accessibilityActions" prop. Declared actions: ${declared}.`,
5465
accessibilityAction,
5566
);
5667
}

0 commit comments

Comments
 (0)