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
131 changes: 68 additions & 63 deletions src/components/ReceiptEmptyState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function ReceiptEmptyState({

const {validateFiles, PDFValidationComponent} = useFilesValidation(setReceiptFile);

const Wrapper = onPress ? PressableWithoutFeedback : View;
const isInteractive = !!onPress || isDisplayedInWideRHP;
const Wrapper = isInteractive ? PressableWithoutFeedback : View;
const containerStyle = isCompact
? [styles.alignItemsCenter, styles.justifyContentCenter, styles.receiptEmptyStateCompact, styles.moneyRequestAttachReceipt, style]
: [
Expand All @@ -115,72 +116,76 @@ function ReceiptEmptyState({
return (
<AttachmentPicker acceptedFileTypes={[...CONST.API_ATTACHMENT_VALIDATIONS.ALLOWED_RECEIPT_EXTENSIONS]}>
{({openPicker}) => (
<Wrapper
accessibilityRole="imagebutton"
accessibilityLabel={translate('receipt.upload')}
onPress={() => {
if (isDisplayedInWideRHP) {
openPicker({
onPicked: validateFiles,
});
return;
}
onPress?.();
}}
disabled={disabled}
disabledStyle={styles.cursorDefault}
hoverStyle={onPress && isCompact ? styles.hoveredComponentBG : undefined}
onHoverIn={onPress && isCompact ? () => setIsHovered(true) : undefined}
onHoverOut={onPress && isCompact ? () => setIsHovered(false) : undefined}
style={containerStyle}
>
{PDFValidationComponent}
{isCompact ? (
<View style={[styles.flexRow, styles.justifyContentCenter, styles.alignItemsCenter, styles.gap2]}>
<Icon
src={icons.ReceiptPlus}
fill={isHovered ? theme.success : theme.icon}
width={variables.iconSizeNormal}
height={variables.iconSizeNormal}
/>
<Text
numberOfLines={1}
style={[styles.textStrong, styles.flexShrink1]}
>
{translate('dropzone.addReceipt')}
</Text>
</View>
) : (
<View style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}>
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<View style={styles.pRelative}>
<Icon
fill={theme.border}
src={icons.Receipt}
width={variables.eReceiptEmptyIconWidth}
height={variables.eReceiptEmptyIconWidth}
/>
{!isThumbnail && (
<View style={[styles.moneyRequestAttachReceiptThumbnailIcon, StyleUtils.getWidthAndHeightStyle(StyleUtils.getAvatarSize(CONST.AVATAR_SIZE.SMALL))]}>
<ReceiptPlaceholderPlusIcon
circleFill={theme.success}
plusFill={theme.receiptPlaceholderPlus}
size={StyleUtils.getAvatarSize(CONST.AVATAR_SIZE.SMALL) - styles.moneyRequestAttachReceiptThumbnailIcon.borderWidth * 2}
/>
</View>
<View style={containerStyle}>
<Wrapper
accessibilityRole={isInteractive ? 'button' : undefined}
accessibilityLabel={translate('receipt.upload')}
onPress={() => {
if (isDisplayedInWideRHP) {
openPicker({
onPicked: validateFiles,
});
return;
}
onPress?.();
}}
disabled={disabled}
disabledStyle={styles.cursorDefault}
hoverStyle={onPress && isCompact ? styles.hoveredComponentBG : undefined}
onHoverIn={onPress && isCompact ? () => setIsHovered(true) : undefined}
onHoverOut={onPress && isCompact ? () => setIsHovered(false) : undefined}
style={[styles.flex1, styles.w100, styles.alignItemsCenter, styles.justifyContentCenter]}
>
{PDFValidationComponent}
{isCompact ? (
<View style={[styles.flexRow, styles.justifyContentCenter, styles.alignItemsCenter, styles.gap2]}>
<Icon
src={icons.ReceiptPlus}
fill={isHovered ? theme.success : theme.icon}
width={variables.iconSizeNormal}
height={variables.iconSizeNormal}
/>
<Text
numberOfLines={1}
style={[styles.textStrong, styles.flexShrink1]}
>
{translate('dropzone.addReceipt')}
</Text>
</View>
) : (
<View style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}>
<View style={[styles.alignItemsCenter, styles.justifyContentCenter]}>
<View style={styles.pRelative}>
<Icon
fill={theme.border}
src={icons.Receipt}
width={variables.eReceiptEmptyIconWidth}
height={variables.eReceiptEmptyIconWidth}
/>
{!isThumbnail && (
<View
style={[styles.moneyRequestAttachReceiptThumbnailIcon, StyleUtils.getWidthAndHeightStyle(StyleUtils.getAvatarSize(CONST.AVATAR_SIZE.SMALL))]}
>
<ReceiptPlaceholderPlusIcon
circleFill={theme.success}
plusFill={theme.receiptPlaceholderPlus}
size={StyleUtils.getAvatarSize(CONST.AVATAR_SIZE.SMALL) - styles.moneyRequestAttachReceiptThumbnailIcon.borderWidth * 2}
/>
</View>
)}
</View>
{!isThumbnail && isDisplayedInWideRHP && (
<>
<Text style={[styles.textHeadline, styles.mt4]}>{translate('receipt.addAReceipt.phrase1')}</Text>
<Text style={[styles.textSupporting, styles.textNormal]}>{translate('receipt.addAReceipt.phrase2')}</Text>
</>
)}
</View>
{!isThumbnail && isDisplayedInWideRHP && (
<>
<Text style={[styles.textHeadline, styles.mt4]}>{translate('receipt.addAReceipt.phrase1')}</Text>
<Text style={[styles.textSupporting, styles.textNormal]}>{translate('receipt.addAReceipt.phrase2')}</Text>
</>
)}
</View>
</View>
)}
)}
</Wrapper>
{isDisplayedInWideRHP && !disabled && <ReceiptAlternativeMethods />}
</Wrapper>
</View>
)}
</AttachmentPicker>
);
Expand Down
41 changes: 31 additions & 10 deletions tests/ui/components/ReceiptEmptyStateTest.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {fireEvent, render, screen} from '@testing-library/react-native';
import {fireEvent, render, screen, within} from '@testing-library/react-native';

import {LocaleContextProvider} from '@components/LocaleContextProvider';
import ReceiptEmptyState from '@components/ReceiptEmptyState';

import type {FileObject} from '@src/types/utils/Attachment';

import React from 'react';
import {View as MockView} from 'react-native';

import {translateLocal} from '../../utils/TestHelper';

Expand All @@ -24,13 +25,7 @@ jest.mock('@hooks/useFilesValidation', () => (onFilesValidated: (files: FileObje
}));

// ReceiptAlternativeMethods uses RenderHTML which requires TRenderEngineProvider (buildTTree). Mock to avoid the error.
jest.mock(
'@components/ReceiptAlternativeMethods',
() =>
function MockReceiptAlternativeMethods() {
return null;
},
);
jest.mock('@components/ReceiptAlternativeMethods', () => () => <MockView testID="receipt-alternative-methods" />);

function Wrapper({children}: {children: React.ReactNode}) {
return <LocaleContextProvider>{children}</LocaleContextProvider>;
Expand All @@ -42,7 +37,24 @@ describe('ReceiptEmptyState', () => {
});

describe('wide RHP attachment picker flow', () => {
it('keeps alternative receipt methods outside the upload button', () => {
// Given an editable wide receipt panel with alternative actions.
render(
<Wrapper>
<ReceiptEmptyState isDisplayedInWideRHP />
</Wrapper>,
);

// When the upload button and alternative methods are inspected.
const uploadButton = screen.getByRole('button', {name: translateLocal('receipt.upload')});

// Then alternative links cannot become nested interactive button content.
expect(screen.getByTestId('receipt-alternative-methods')).toBeOnTheScreen();
expect(within(uploadButton).queryByTestId('receipt-alternative-methods')).toBeNull();
});

it('calls openPicker when pressed and isDisplayedInWideRHP is true', () => {
// Given a wide receipt panel with its own picker action.
render(
<Wrapper>
<ReceiptEmptyState
Expand All @@ -52,15 +64,18 @@ describe('ReceiptEmptyState', () => {
</Wrapper>,
);

const uploadButton = screen.getByLabelText(translateLocal('receipt.upload'));
const uploadButton = screen.getByRole('button', {name: translateLocal('receipt.upload')});
// When its upload control is activated.
fireEvent.press(uploadButton);

// Then the picker, not a missing caller callback, handles the action.
expect(mockOpenPicker).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Jest matcher for callback
expect(mockOpenPicker).toHaveBeenCalledWith({onPicked: expect.any(Function)});
});

it('does not call openPicker when isDisplayedInWideRHP is false and calls onPress instead', () => {
// Given an upload control with a caller-owned navigation action.
const onPress = jest.fn();
render(
<Wrapper>
Expand All @@ -71,9 +86,11 @@ describe('ReceiptEmptyState', () => {
</Wrapper>,
);

const uploadButton = screen.getByLabelText(translateLocal('receipt.upload'));
const uploadButton = screen.getByRole('button', {name: translateLocal('receipt.upload')});
// When that control is activated.
fireEvent.press(uploadButton);

// Then its callback runs instead of the wide-panel picker.
expect(mockOpenPicker).not.toHaveBeenCalled();
expect(onPress).toHaveBeenCalledTimes(1);
});
Expand All @@ -94,6 +111,7 @@ describe('ReceiptEmptyState', () => {
});

it('does not show "Add a receipt" text when isDisplayedInWideRHP is false', () => {
// Given a receipt placeholder without an upload action.
render(
<Wrapper>
<ReceiptEmptyState
Expand All @@ -103,8 +121,11 @@ describe('ReceiptEmptyState', () => {
</Wrapper>,
);

// When its accessible output is inspected.
// Then it remains a static placeholder, not a misleading upload button.
expect(screen.queryByText(translateLocal('receipt.addAReceipt.phrase1'))).not.toBeOnTheScreen();
expect(screen.queryByText(translateLocal('receipt.addAReceipt.phrase2'))).not.toBeOnTheScreen();
expect(screen.getByLabelText(translateLocal('receipt.upload'))).not.toHaveProp('accessibilityRole', 'button');
});
});
});
Loading