|
| 1 | +import * as React from 'react'; |
| 2 | +import type { AccessibilityActionEvent } from 'react-native'; |
| 3 | +import { View } from 'react-native'; |
| 4 | + |
| 5 | +import { render, screen, userEvent } from '../../..'; |
| 6 | +import { createEventLogger, lastEventPayload } from '../../../test-utils/events'; |
| 7 | + |
| 8 | +async function renderViewWithActions(props: React.ComponentProps<typeof View> = {}) { |
| 9 | + const { events, logEvent } = createEventLogger(); |
| 10 | + |
| 11 | + await render( |
| 12 | + <View |
| 13 | + testID="view" |
| 14 | + accessible |
| 15 | + accessibilityActions={[{ name: 'increment' }, { name: 'activate', label: 'Activate' }]} |
| 16 | + onAccessibilityAction={(event: AccessibilityActionEvent) => |
| 17 | + logEvent('accessibilityAction')(event) |
| 18 | + } |
| 19 | + {...props} |
| 20 | + />, |
| 21 | + ); |
| 22 | + |
| 23 | + return { events }; |
| 24 | +} |
| 25 | + |
| 26 | +describe('userEvent.accessibilityAction', () => { |
| 27 | + test('triggers the onAccessibilityAction handler with the given action name', async () => { |
| 28 | + const user = userEvent.setup(); |
| 29 | + const { events } = await renderViewWithActions(); |
| 30 | + |
| 31 | + await user.accessibilityAction(screen.getByTestId('view'), 'increment'); |
| 32 | + |
| 33 | + expect(events).toHaveLength(1); |
| 34 | + expect(events[0].name).toBe('accessibilityAction'); |
| 35 | + expect(lastEventPayload(events, 'accessibilityAction').nativeEvent).toEqual({ |
| 36 | + actionName: 'increment', |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test('supports the direct (setup-less) call form', async () => { |
| 41 | + const { events } = await renderViewWithActions(); |
| 42 | + |
| 43 | + await userEvent.accessibilityAction(screen.getByTestId('view'), 'activate'); |
| 44 | + |
| 45 | + expect(events).toHaveLength(1); |
| 46 | + expect(lastEventPayload(events, 'accessibilityAction').nativeEvent.actionName).toBe('activate'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('throws when passed a non-host instance', async () => { |
| 50 | + const user = userEvent.setup(); |
| 51 | + await renderViewWithActions(); |
| 52 | + |
| 53 | + // @ts-expect-error intentionally passing a non-host instance |
| 54 | + await expect(user.accessibilityAction('not a host instance', 'increment')).rejects.toThrow( |
| 55 | + /works only with host instances/, |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + test('throws when the action is not declared in accessibilityActions', async () => { |
| 60 | + const user = userEvent.setup(); |
| 61 | + await renderViewWithActions(); |
| 62 | + |
| 63 | + await expect(user.accessibilityAction(screen.getByTestId('view'), 'decrement')).rejects.toThrow( |
| 64 | + /has no "decrement" accessibility action.*"increment", "activate"/s, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + test('throws when the element declares no accessibility actions', async () => { |
| 69 | + const user = userEvent.setup(); |
| 70 | + await renderViewWithActions({ accessibilityActions: undefined }); |
| 71 | + |
| 72 | + await expect(user.accessibilityAction(screen.getByTestId('view'), 'increment')).rejects.toThrow( |
| 73 | + /has no accessibility actions/, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + test('throws when the element is disabled', async () => { |
| 78 | + const user = userEvent.setup(); |
| 79 | + await renderViewWithActions({ 'aria-disabled': true }); |
| 80 | + |
| 81 | + await expect(user.accessibilityAction(screen.getByTestId('view'), 'increment')).rejects.toThrow( |
| 82 | + /on a disabled element/, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test('throws when the element is disabled via accessibilityState', async () => { |
| 87 | + const user = userEvent.setup(); |
| 88 | + await renderViewWithActions({ accessibilityState: { disabled: true } }); |
| 89 | + |
| 90 | + await expect(user.accessibilityAction(screen.getByTestId('view'), 'increment')).rejects.toThrow( |
| 91 | + /on a disabled element/, |
| 92 | + ); |
| 93 | + }); |
| 94 | +}); |
0 commit comments