diff --git a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx index c835a625f528..a8220e6a1bda 100644 --- a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx +++ b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx @@ -600,7 +600,10 @@ function MoneyRequestReportPreviewContent({ }, [carouselTransactions.length]); return ( - + (0); - const [currentWrapperWidth, setCurrentWrapperWidth] = useState(0); + + const widthsRef = useRef<{currentWidth: number | null; currentWrapperWidth: number | null}>({currentWidth: null, currentWrapperWidth: null}); + const [widths, setWidths] = useState({currentWidth: 0, currentWrapperWidth: 0}); + + const updateWidths = useCallback(() => { + const {currentWidth, currentWrapperWidth} = widthsRef.current; + + if (currentWidth && currentWrapperWidth) { + setWidths({currentWidth, currentWrapperWidth}); + } + }, []); + + const onCarouselLayout = useCallback( + (e: LayoutChangeEvent) => { + const newWidth = e.nativeEvent.layout.width; + if (widthsRef.current.currentWidth !== newWidth) { + widthsRef.current.currentWidth = newWidth; + updateWidths(); + } + }, + [updateWidths], + ); + const onWrapperLayout = useCallback( + (e: LayoutChangeEvent) => { + const newWrapperWidth = e.nativeEvent.layout.width; + if (widthsRef.current.currentWrapperWidth !== newWrapperWidth) { + widthsRef.current.currentWrapperWidth = newWrapperWidth; + updateWidths(); + } + }, + [updateWidths], + ); + const reportPreviewStyles = useMemo( - () => StyleUtils.getMoneyRequestReportPreviewStyle(shouldUseNarrowLayout, transactions.length, currentWidth, currentWrapperWidth), - [StyleUtils, currentWidth, currentWrapperWidth, shouldUseNarrowLayout, transactions.length], + () => StyleUtils.getMoneyRequestReportPreviewStyle(shouldUseNarrowLayout, transactions.length, widths.currentWidth, widths.currentWrapperWidth), + [StyleUtils, widths, shouldUseNarrowLayout, transactions.length], ); const shouldShowPayerAndReceiver = useMemo(() => { if (!isIOUReport(iouReport) && action.childType !== CONST.REPORT.TYPE.IOU) { @@ -121,13 +152,9 @@ function MoneyRequestReportPreview({ invoiceReceiverPolicy={invoiceReceiverPolicy} lastTransactionViolations={lastTransactionViolations} renderTransactionItem={renderItem} - onCarouselLayout={(e: LayoutChangeEvent) => { - setCurrentWidth(e.nativeEvent.layout.width); - }} - onWrapperLayout={(e: LayoutChangeEvent) => { - setCurrentWrapperWidth(e.nativeEvent.layout.width); - }} - currentWidth={currentWidth} + onCarouselLayout={onCarouselLayout} + onWrapperLayout={onWrapperLayout} + currentWidth={widths.currentWidth} reportPreviewStyles={reportPreviewStyles} shouldDisplayContextMenu={shouldDisplayContextMenu} isInvoice={isInvoice} diff --git a/tests/ui/MoneyRequestReportPreview.test.tsx b/tests/ui/MoneyRequestReportPreview.test.tsx index 8ade6e785ef1..47f7e7cf5844 100644 --- a/tests/ui/MoneyRequestReportPreview.test.tsx +++ b/tests/ui/MoneyRequestReportPreview.test.tsx @@ -1,7 +1,7 @@ import {PortalProvider} from '@gorhom/portal'; import * as NativeNavigation from '@react-navigation/native'; import {fireEvent, render, screen} from '@testing-library/react-native'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry, OnyxMergeInput} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import ComposeProviders from '@components/ComposeProviders'; import {LocaleContextProvider} from '@components/LocaleContextProvider'; @@ -10,7 +10,6 @@ import OptionsListContextProvider from '@components/OptionListContextProvider'; import MoneyRequestReportPreview from '@components/ReportActionItem/MoneyRequestReportPreview'; import type {MoneyRequestReportPreviewProps} from '@components/ReportActionItem/MoneyRequestReportPreview/types'; import ScreenWrapper from '@components/ScreenWrapper'; -import TransactionPreviewSkeletonView from '@components/TransactionPreviewSkeletonView'; import {convertToDisplayString} from '@libs/CurrencyUtils'; import DateUtils from '@libs/DateUtils'; import {translateLocal} from '@libs/Localize'; @@ -100,6 +99,9 @@ const getTransactionDisplayAmountAndHeaderText = (transaction: Transaction) => { }; const setCurrentWidth = () => { + fireEvent(screen.getByTestId('MoneyRequestReportPreviewContent-wrapper'), 'layout', { + nativeEvent: {layout: {width: 600}}, + }); fireEvent(screen.getByTestId('carouselWidthSetter'), 'layout', { nativeEvent: {layout: {width: 500}}, }); @@ -147,6 +149,7 @@ describe('MoneyRequestReportPreview', () => { await waitForBatchedUpdatesWithAct(); setCurrentWidth(); await Onyx.mergeCollection(ONYXKEYS.COLLECTION.TRANSACTION, mockOnyxTransactions).then(waitForBatchedUpdates); + await waitForBatchedUpdatesWithAct(); const {reportName: moneyRequestReportPreviewName = ''} = mockChatReport; for (const transaction of arrayOfTransactions) { const {transactionDisplayAmount, transactionHeaderText} = getTransactionDisplayAmountAndHeaderText(transaction); @@ -163,6 +166,7 @@ describe('MoneyRequestReportPreview', () => { await waitForBatchedUpdatesWithAct(); setCurrentWidth(); await Onyx.multiSet({...mockOnyxTransactions, ...mockOnyxViolations}); + await waitForBatchedUpdatesWithAct(); expect(screen.getAllByText(translateLocal('violations.reviewRequired'))).toHaveLength(2); }); @@ -170,6 +174,11 @@ describe('MoneyRequestReportPreview', () => { renderPage({}); await waitForBatchedUpdatesWithAct(); setCurrentWidth(); - expect(screen.getAllByTestId(TransactionPreviewSkeletonView.displayName)).toHaveLength(2); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${mockTransaction.transactionID}`, {} as OnyxMergeInput<`transactions_${string}`>); + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${mockSecondTransactionID}`, {} as OnyxMergeInput<`transactions_${string}`>); + await waitForBatchedUpdatesWithAct(); + + expect(screen.getAllByTestId('TransactionPreviewSkeletonView')).toHaveLength(2); }); });