Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import FormHelpMessage from '@components/FormHelpMessage';
import Icon from '@components/Icon';
import MenuItem from '@components/MenuItem';
import {MENU_ITEM_ACCESSIBILITY_ANNOUNCEMENT, useMenuItemAccessibilityAnnouncement} from '@components/MenuItem/MenuItemAccessibilityContext';

import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';

Expand Down Expand Up @@ -82,6 +84,16 @@ type ExpenseFieldRowProps = {
testID?: string;
};

/**
* Tells screen readers that the row needs a review, the way `MenuItem.BrickRoadIndicator` does on a push row.
* The error message sits outside the focusable row and this layout draws no dot, so nothing else announces it.
*/
function ExpenseFieldRowReviewRequiredAnnouncement() {
const {translate} = useLocalize();
useMenuItemAccessibilityAnnouncement(MENU_ITEM_ACCESSIBILITY_ANNOUNCEMENT.REVIEW_REQUIRED, translate('common.yourReviewIsRequired'));
return null;
}

/**
* A selectable row of the expense form, rendered as one of the form's bordered fields rather than as a push row:
* the field name stands in for the value while the field is empty, and moves above the value once one is picked.
Expand Down Expand Up @@ -191,6 +203,7 @@ function ExpenseFieldRow({
sentryLabel={sentryLabel}
testID={testID}
>
{!!errorText && <ExpenseFieldRowReviewRequiredAnnouncement />}
{row}
</MenuItem.Root>
</Animated.View>
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/components/ExpenseFieldRow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {fireEvent, render, renderHook, screen} from '@testing-library/react-native';

import {LocaleContextProvider} from '@components/LocaleContextProvider';
import ExpenseFieldRow from '@components/MoneyRequestConfirmationList/sections/ExpenseFieldRow';

import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -15,6 +16,9 @@ import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';
const FIELD_NAME = 'Category';
const FIELD_VALUE = 'Travel';
const RIGHT_LABEL = 'Automatic';
const ACCESSIBILITY_LABEL = 'Attendees, John Doe';
const ERROR_TEXT = 'Multiple attendees required for this category';
const REVIEW_REQUIRED = 'Your review is required';

// `MenuItem.Root` drops a press it was handed no event for, so every press here carries one.
const pressRow = () => fireEvent.press(screen.getByTestId('category-row'), {nativeEvent: {}});
Expand Down Expand Up @@ -202,6 +206,45 @@ describe('ExpenseFieldRow', () => {
});
});

describe('error announcement', () => {
it('announces that the row needs a review after the label', async () => {
// Given a row in an error state that names itself, the way the Attendees row does
render(
<LocaleContextProvider>
<ExpenseFieldRow
name={FIELD_NAME}
value={FIELD_VALUE}
accessibilityLabel={ACCESSIBILITY_LABEL}
errorText={ERROR_TEXT}
onPress={jest.fn()}
/>
</LocaleContextProvider>,
);

// When the row is read out
// Then the review prompt follows the label, since the error message sits outside the focusable row
expect(await screen.findByLabelText(`${ACCESSIBILITY_LABEL}. ${REVIEW_REQUIRED}`)).toBeOnTheScreen();
});

it('says nothing about a review while the field has no error', async () => {
// Given a row with no error
render(
<LocaleContextProvider>
<ExpenseFieldRow
name={FIELD_NAME}
value={FIELD_VALUE}
accessibilityLabel={ACCESSIBILITY_LABEL}
onPress={jest.fn()}
/>
</LocaleContextProvider>,
);

// When the row is read out
// Then only its label is announced
expect(await screen.findByLabelText(ACCESSIBILITY_LABEL)).toBeOnTheScreen();
});
});

describe('read-only presentation', () => {
it('renders a field the user cannot change as a disabled input', () => {
const {result} = renderHook(() => useThemeStyles());
Expand Down
Loading