From 060f48eb4cd517269d78ebf4c285f4d63b9dbd6c Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 07:41:57 +0500 Subject: [PATCH 1/7] Add selector for single report IDs in todos and navigate directly when only one report exists --- src/CONST/index.ts | 7 ++++++ src/pages/home/ForYouSection/index.tsx | 30 ++++++++++++++++++++------ src/selectors/Todos.ts | 23 ++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/CONST/index.ts b/src/CONST/index.ts index 541eebbbd1bd..f3096b62c233 100755 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -28,6 +28,12 @@ const EMPTY_TODOS_REPORT_COUNTS = Object.freeze({ pay: 0, export: 0, }); +const EMPTY_TODOS_SINGLE_REPORT_IDS = Object.freeze({ + submit: undefined, + approve: undefined, + pay: undefined, + export: undefined, +}); // Using 28 days to align with OldDot and because all months are guaranteed to be at least 28 days. const MONTH_DAYS = Object.freeze([...Array(28).keys()].map((i) => i + 1)); @@ -1018,6 +1024,7 @@ const CONST = { EMPTY_OBJECT, EMPTY_SET, EMPTY_TODOS_REPORT_COUNTS, + EMPTY_TODOS_SINGLE_REPORT_IDS, DEFAULT_NUMBER_ID, DEFAULT_MISSING_ID, DEFAULT_COUNTRY_CODE, diff --git a/src/pages/home/ForYouSection/index.tsx b/src/pages/home/ForYouSection/index.tsx index b7ae1564afad..4f7a7b74120d 100644 --- a/src/pages/home/ForYouSection/index.tsx +++ b/src/pages/home/ForYouSection/index.tsx @@ -15,7 +15,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import {accountIDSelector} from '@src/selectors/Session'; -import todosReportCountsSelector from '@src/selectors/Todos'; +import todosReportCountsSelector, {todosSingleReportIDsSelector} from '@src/selectors/Todos'; import EmptyState from './EmptyState'; function ForYouSection() { @@ -26,6 +26,7 @@ function ForYouSection() { const [accountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: accountIDSelector}); const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true}); const [reportCounts = CONST.EMPTY_TODOS_REPORT_COUNTS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosReportCountsSelector}); + const [singleReportIDs] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosSingleReportIDsSelector}); const icons = useMemoizedLazyExpensifyIcons(['MoneyBag', 'Send', 'ThumbsUp', 'Export']); @@ -36,7 +37,16 @@ function ForYouSection() { const hasAnyTodos = submitCount > 0 || approveCount > 0 || payCount > 0 || exportCount > 0; - const createNavigationHandler = (action: string, queryParams: Record) => () => { + const createNavigationHandler = (action: string, queryParams: Record, reportID?: string) => () => { + if (reportID) { + if (shouldUseNarrowLayout) { + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID)); + } else { + Navigation.navigate(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + } + return; + } + Navigation.navigate( ROUTES.SEARCH_ROOT.getRoute({ query: buildQueryStringFromFilterFormValues({ @@ -54,28 +64,36 @@ function ForYouSection() { count: submitCount, icon: icons.Send, translationKey: 'homePage.forYouSection.submit' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.SUBMIT, {from: [`${accountID}`]}), + handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.SUBMIT, {from: [`${accountID}`]}, singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.SUBMIT]), }, { key: 'approve', count: approveCount, icon: icons.ThumbsUp, translationKey: 'homePage.forYouSection.approve' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.APPROVE, {to: [`${accountID}`]}), + handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.APPROVE, {to: [`${accountID}`]}, singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.APPROVE]), }, { key: 'pay', count: payCount, icon: icons.MoneyBag, translationKey: 'homePage.forYouSection.pay' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.PAY, {reimbursable: CONST.SEARCH.BOOLEAN.YES, payer: accountID?.toString()}), + handler: createNavigationHandler( + CONST.SEARCH.ACTION_FILTERS.PAY, + {reimbursable: CONST.SEARCH.BOOLEAN.YES, payer: accountID?.toString()}, + singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.PAY], + ), }, { key: 'export', count: exportCount, icon: icons.Export, translationKey: 'homePage.forYouSection.export' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.EXPORT, {exporter: [`${accountID}`], exportedOn: CONST.SEARCH.DATE_PRESETS.NEVER}), + handler: createNavigationHandler( + CONST.SEARCH.ACTION_FILTERS.EXPORT, + {exporter: [`${accountID}`], exportedOn: CONST.SEARCH.DATE_PRESETS.NEVER}, + singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.EXPORT], + ), }, ].filter((item) => item.count > 0); diff --git a/src/selectors/Todos.ts b/src/selectors/Todos.ts index 78b9c0f93a47..6c09f4e36edb 100644 --- a/src/selectors/Todos.ts +++ b/src/selectors/Todos.ts @@ -15,4 +15,27 @@ const todosReportCountsSelector = (todos: OnyxEntry) => { }; }; +const todosSingleReportIDsSelector = (todos: OnyxEntry) => { + if (!todos) { + return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; + } + + const submitReportID = todos.reportsToSubmit.length === 1 ? todos.reportsToSubmit.at(0)?.reportID : undefined; + const approveReportID = todos.reportsToApprove.length === 1 ? todos.reportsToApprove.at(0)?.reportID : undefined; + const payReportID = todos.reportsToPay.length === 1 ? todos.reportsToPay.at(0)?.reportID : undefined; + const exportReportID = todos.reportsToExport.length === 1 ? todos.reportsToExport.at(0)?.reportID : undefined; + + if (!submitReportID && !approveReportID && !payReportID && !exportReportID) { + return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; + } + + return { + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: submitReportID, + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: approveReportID, + [CONST.SEARCH.SEARCH_KEYS.PAY]: payReportID, + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: exportReportID, + }; +}; + export default todosReportCountsSelector; +export {todosSingleReportIDsSelector}; From 41ba6400ccb22405c9b80f9de520e1c14d628e3c Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 07:51:07 +0500 Subject: [PATCH 2/7] Optimize ForYouSection performance with useCallback, useMemo, and selector memoization --- src/pages/home/ForYouSection/index.tsx | 121 +++++++++++++------------ src/selectors/Todos.ts | 20 +++- 2 files changed, 83 insertions(+), 58 deletions(-) diff --git a/src/pages/home/ForYouSection/index.tsx b/src/pages/home/ForYouSection/index.tsx index 4f7a7b74120d..b3ff01a916ba 100644 --- a/src/pages/home/ForYouSection/index.tsx +++ b/src/pages/home/ForYouSection/index.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, {useCallback, useMemo} from 'react'; import {View} from 'react-native'; import ActivityIndicator from '@components/ActivityIndicator'; import BaseWidgetItem from '@components/BaseWidgetItem'; @@ -26,7 +26,7 @@ function ForYouSection() { const [accountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: accountIDSelector}); const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true}); const [reportCounts = CONST.EMPTY_TODOS_REPORT_COUNTS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosReportCountsSelector}); - const [singleReportIDs] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosSingleReportIDsSelector}); + const [singleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosSingleReportIDsSelector}); const icons = useMemoizedLazyExpensifyIcons(['MoneyBag', 'Send', 'ThumbsUp', 'Export']); @@ -37,65 +37,72 @@ function ForYouSection() { const hasAnyTodos = submitCount > 0 || approveCount > 0 || payCount > 0 || exportCount > 0; - const createNavigationHandler = (action: string, queryParams: Record, reportID?: string) => () => { - if (reportID) { - if (shouldUseNarrowLayout) { - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID)); - } else { - Navigation.navigate(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + const createNavigationHandler = useCallback( + (action: string, queryParams: Record, reportID?: string) => () => { + if (reportID) { + if (shouldUseNarrowLayout) { + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID)); + } else { + Navigation.navigate(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + } + return; } - return; - } - Navigation.navigate( - ROUTES.SEARCH_ROOT.getRoute({ - query: buildQueryStringFromFilterFormValues({ - type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, - action, - ...queryParams, + Navigation.navigate( + ROUTES.SEARCH_ROOT.getRoute({ + query: buildQueryStringFromFilterFormValues({ + type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, + action, + ...queryParams, + }), }), - }), - ); - }; - - const todoItems = [ - { - key: 'submit', - count: submitCount, - icon: icons.Send, - translationKey: 'homePage.forYouSection.submit' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.SUBMIT, {from: [`${accountID}`]}, singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.SUBMIT]), - }, - { - key: 'approve', - count: approveCount, - icon: icons.ThumbsUp, - translationKey: 'homePage.forYouSection.approve' as const, - handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.APPROVE, {to: [`${accountID}`]}, singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.APPROVE]), - }, - { - key: 'pay', - count: payCount, - icon: icons.MoneyBag, - translationKey: 'homePage.forYouSection.pay' as const, - handler: createNavigationHandler( - CONST.SEARCH.ACTION_FILTERS.PAY, - {reimbursable: CONST.SEARCH.BOOLEAN.YES, payer: accountID?.toString()}, - singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.PAY], - ), - }, - { - key: 'export', - count: exportCount, - icon: icons.Export, - translationKey: 'homePage.forYouSection.export' as const, - handler: createNavigationHandler( - CONST.SEARCH.ACTION_FILTERS.EXPORT, - {exporter: [`${accountID}`], exportedOn: CONST.SEARCH.DATE_PRESETS.NEVER}, - singleReportIDs?.[CONST.SEARCH.SEARCH_KEYS.EXPORT], - ), + ); }, - ].filter((item) => item.count > 0); + [shouldUseNarrowLayout], + ); + + const todoItems = useMemo( + () => + [ + { + key: 'submit', + count: submitCount, + icon: icons.Send, + translationKey: 'homePage.forYouSection.submit' as const, + handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.SUBMIT, {from: [`${accountID}`]}, singleReportIDs[CONST.SEARCH.SEARCH_KEYS.SUBMIT]), + }, + { + key: 'approve', + count: approveCount, + icon: icons.ThumbsUp, + translationKey: 'homePage.forYouSection.approve' as const, + handler: createNavigationHandler(CONST.SEARCH.ACTION_FILTERS.APPROVE, {to: [`${accountID}`]}, singleReportIDs[CONST.SEARCH.SEARCH_KEYS.APPROVE]), + }, + { + key: 'pay', + count: payCount, + icon: icons.MoneyBag, + translationKey: 'homePage.forYouSection.pay' as const, + handler: createNavigationHandler( + CONST.SEARCH.ACTION_FILTERS.PAY, + {reimbursable: CONST.SEARCH.BOOLEAN.YES, payer: accountID?.toString()}, + singleReportIDs[CONST.SEARCH.SEARCH_KEYS.PAY], + ), + }, + { + key: 'export', + count: exportCount, + icon: icons.Export, + translationKey: 'homePage.forYouSection.export' as const, + handler: createNavigationHandler( + CONST.SEARCH.ACTION_FILTERS.EXPORT, + {exporter: [`${accountID}`], exportedOn: CONST.SEARCH.DATE_PRESETS.NEVER}, + singleReportIDs[CONST.SEARCH.SEARCH_KEYS.EXPORT], + ), + }, + ].filter((item) => item.count > 0), + [accountID, approveCount, createNavigationHandler, exportCount, icons.Export, icons.MoneyBag, icons.Send, icons.ThumbsUp, payCount, singleReportIDs, submitCount], + ); const renderTodoItems = () => ( diff --git a/src/selectors/Todos.ts b/src/selectors/Todos.ts index 6c09f4e36edb..ccc29abb72bc 100644 --- a/src/selectors/Todos.ts +++ b/src/selectors/Todos.ts @@ -1,4 +1,5 @@ import type {OnyxEntry} from 'react-native-onyx'; +import {shallowEqual} from 'fast-equals'; import CONST from '@src/CONST'; import type {TodosDerivedValue} from '@src/types/onyx'; @@ -15,6 +16,15 @@ const todosReportCountsSelector = (todos: OnyxEntry) => { }; }; +type SingleReportIDs = { + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: string | undefined; + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: string | undefined; + [CONST.SEARCH.SEARCH_KEYS.PAY]: string | undefined; + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: string | undefined; +}; + +let previousSingleReportIDs: SingleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS as SingleReportIDs; + const todosSingleReportIDsSelector = (todos: OnyxEntry) => { if (!todos) { return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; @@ -26,15 +36,23 @@ const todosSingleReportIDsSelector = (todos: OnyxEntry) => { const exportReportID = todos.reportsToExport.length === 1 ? todos.reportsToExport.at(0)?.reportID : undefined; if (!submitReportID && !approveReportID && !payReportID && !exportReportID) { + previousSingleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; } - return { + const newValue = { [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: submitReportID, [CONST.SEARCH.SEARCH_KEYS.APPROVE]: approveReportID, [CONST.SEARCH.SEARCH_KEYS.PAY]: payReportID, [CONST.SEARCH.SEARCH_KEYS.EXPORT]: exportReportID, }; + + if (shallowEqual(previousSingleReportIDs, newValue)) { + return previousSingleReportIDs; + } + + previousSingleReportIDs = newValue; + return newValue; }; export default todosReportCountsSelector; From 83652241b6f893dad247131897b9dce8d7878ff9 Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 08:17:35 +0500 Subject: [PATCH 3/7] Add unit tests for todosSingleReportIDsSelector --- tests/unit/selectors/TodosTest.ts | 42 ++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/unit/selectors/TodosTest.ts b/tests/unit/selectors/TodosTest.ts index 2b14df403133..a9ead929fc18 100644 --- a/tests/unit/selectors/TodosTest.ts +++ b/tests/unit/selectors/TodosTest.ts @@ -1,4 +1,4 @@ -import todosReportCountsSelector from '@selectors/Todos'; +import todosReportCountsSelector, {todosSingleReportIDsSelector} from '@selectors/Todos'; import CONST from '@src/CONST'; import type {TodosDerivedValue} from '@src/types/onyx'; @@ -93,3 +93,43 @@ describe('todosReportCountsSelector', () => { }); }); }); + +describe('todosSingleReportIDsSelector', () => { + it('returns report ID when exactly one report exists in each array', () => { + const todos: TodosDerivedValue = { + reportsToSubmit: [{reportID: '1'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [{reportID: '2'}] as TodosDerivedValue['reportsToApprove'], + reportsToPay: [{reportID: '3'}] as TodosDerivedValue['reportsToPay'], + reportsToExport: [{reportID: '4'}] as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const result = todosSingleReportIDsSelector(todos); + + expect(result).toEqual({ + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: '1', + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: '2', + [CONST.SEARCH.SEARCH_KEYS.PAY]: '3', + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: '4', + }); + }); + + it('returns undefined for arrays with more than one report', () => { + const todos: TodosDerivedValue = { + reportsToSubmit: [{reportID: '1'}, {reportID: '2'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [{reportID: '3'}, {reportID: '4'}, {reportID: '5'}] as TodosDerivedValue['reportsToApprove'], + reportsToPay: [{reportID: '6'}] as TodosDerivedValue['reportsToPay'], + reportsToExport: [{reportID: '7'}, {reportID: '8'}] as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const result = todosSingleReportIDsSelector(todos); + + expect(result).toEqual({ + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: undefined, + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: undefined, + [CONST.SEARCH.SEARCH_KEYS.PAY]: '6', + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: undefined, + }); + }); +}); From 48ea77c137a1af688b8a951fb382c80d6a064b3f Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 16:01:52 +0500 Subject: [PATCH 4/7] Move EMPTY_TODOS_SINGLE_REPORT_IDS constant from CONST to Todos selector and export it --- src/CONST/index.ts | 7 ------- src/pages/home/ForYouSection/index.tsx | 4 ++-- src/selectors/Todos.ts | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/CONST/index.ts b/src/CONST/index.ts index f3096b62c233..541eebbbd1bd 100755 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -28,12 +28,6 @@ const EMPTY_TODOS_REPORT_COUNTS = Object.freeze({ pay: 0, export: 0, }); -const EMPTY_TODOS_SINGLE_REPORT_IDS = Object.freeze({ - submit: undefined, - approve: undefined, - pay: undefined, - export: undefined, -}); // Using 28 days to align with OldDot and because all months are guaranteed to be at least 28 days. const MONTH_DAYS = Object.freeze([...Array(28).keys()].map((i) => i + 1)); @@ -1024,7 +1018,6 @@ const CONST = { EMPTY_OBJECT, EMPTY_SET, EMPTY_TODOS_REPORT_COUNTS, - EMPTY_TODOS_SINGLE_REPORT_IDS, DEFAULT_NUMBER_ID, DEFAULT_MISSING_ID, DEFAULT_COUNTRY_CODE, diff --git a/src/pages/home/ForYouSection/index.tsx b/src/pages/home/ForYouSection/index.tsx index b3ff01a916ba..9d229b6a527d 100644 --- a/src/pages/home/ForYouSection/index.tsx +++ b/src/pages/home/ForYouSection/index.tsx @@ -15,7 +15,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import {accountIDSelector} from '@src/selectors/Session'; -import todosReportCountsSelector, {todosSingleReportIDsSelector} from '@src/selectors/Todos'; +import todosReportCountsSelector, {EMPTY_TODOS_SINGLE_REPORT_IDS, todosSingleReportIDsSelector} from '@src/selectors/Todos'; import EmptyState from './EmptyState'; function ForYouSection() { @@ -26,7 +26,7 @@ function ForYouSection() { const [accountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: accountIDSelector}); const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true}); const [reportCounts = CONST.EMPTY_TODOS_REPORT_COUNTS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosReportCountsSelector}); - const [singleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosSingleReportIDsSelector}); + const [singleReportIDs = EMPTY_TODOS_SINGLE_REPORT_IDS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {canBeMissing: true, selector: todosSingleReportIDsSelector}); const icons = useMemoizedLazyExpensifyIcons(['MoneyBag', 'Send', 'ThumbsUp', 'Export']); diff --git a/src/selectors/Todos.ts b/src/selectors/Todos.ts index ccc29abb72bc..57f60a89b274 100644 --- a/src/selectors/Todos.ts +++ b/src/selectors/Todos.ts @@ -3,6 +3,13 @@ import {shallowEqual} from 'fast-equals'; import CONST from '@src/CONST'; import type {TodosDerivedValue} from '@src/types/onyx'; +const EMPTY_TODOS_SINGLE_REPORT_IDS = Object.freeze({ + submit: undefined, + approve: undefined, + pay: undefined, + export: undefined, +}); + const todosReportCountsSelector = (todos: OnyxEntry) => { if (!todos) { return CONST.EMPTY_TODOS_REPORT_COUNTS; @@ -23,11 +30,11 @@ type SingleReportIDs = { [CONST.SEARCH.SEARCH_KEYS.EXPORT]: string | undefined; }; -let previousSingleReportIDs: SingleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS as SingleReportIDs; +let previousSingleReportIDs: SingleReportIDs = EMPTY_TODOS_SINGLE_REPORT_IDS as SingleReportIDs; const todosSingleReportIDsSelector = (todos: OnyxEntry) => { if (!todos) { - return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; + return EMPTY_TODOS_SINGLE_REPORT_IDS; } const submitReportID = todos.reportsToSubmit.length === 1 ? todos.reportsToSubmit.at(0)?.reportID : undefined; @@ -36,8 +43,8 @@ const todosSingleReportIDsSelector = (todos: OnyxEntry) => { const exportReportID = todos.reportsToExport.length === 1 ? todos.reportsToExport.at(0)?.reportID : undefined; if (!submitReportID && !approveReportID && !payReportID && !exportReportID) { - previousSingleReportIDs = CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; - return CONST.EMPTY_TODOS_SINGLE_REPORT_IDS; + previousSingleReportIDs = EMPTY_TODOS_SINGLE_REPORT_IDS; + return EMPTY_TODOS_SINGLE_REPORT_IDS; } const newValue = { @@ -56,4 +63,4 @@ const todosSingleReportIDsSelector = (todos: OnyxEntry) => { }; export default todosReportCountsSelector; -export {todosSingleReportIDsSelector}; +export {todosSingleReportIDsSelector, EMPTY_TODOS_SINGLE_REPORT_IDS}; From 83383a7fda13bb0ae93b6e9919786ae71f0f8f78 Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 16:21:11 +0500 Subject: [PATCH 5/7] Add comprehensive unit and UI tests for ForYouSection and todosSingleReportIDsSelector --- tests/ui/ForYouSectionTest.tsx | 330 ++++++++++++++++++++++++++++++ tests/unit/selectors/TodosTest.ts | 79 +++++++ 2 files changed, 409 insertions(+) create mode 100644 tests/ui/ForYouSectionTest.tsx diff --git a/tests/ui/ForYouSectionTest.tsx b/tests/ui/ForYouSectionTest.tsx new file mode 100644 index 000000000000..699b08d034ee --- /dev/null +++ b/tests/ui/ForYouSectionTest.tsx @@ -0,0 +1,330 @@ +import {act, fireEvent, render, screen} from '@testing-library/react-native'; +import React from 'react'; +import Onyx from 'react-native-onyx'; +import useResponsiveLayout from '@hooks/useResponsiveLayout'; +import Navigation from '@libs/Navigation/Navigation'; +import ForYouSection from '@pages/home/ForYouSection'; +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; +import type {TodosDerivedValue} from '@src/types/onyx'; +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; + +jest.mock('@libs/Navigation/Navigation', () => ({ + navigate: jest.fn(), +})); + +jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); + +jest.mock('@hooks/useLocalize', () => + jest.fn(() => ({ + translate: jest.fn((key: string, params?: Record) => { + if (key === 'homePage.forYouSection.begin') { + return 'Begin'; + } + return params ? `${key}:${JSON.stringify(params)}` : key; + }), + numberFormat: jest.fn((num: number) => num.toString()), + localeCompare: jest.fn((a: string, b: string) => a.localeCompare(b)), + })), +); + +jest.mock('@hooks/useThemeStyles', () => + jest.fn( + () => + new Proxy( + {}, + { + get: () => jest.fn(() => ({})), + }, + ), + ), +); + +jest.mock('@hooks/useTheme', () => jest.fn(() => ({}))); + +jest.mock('@hooks/useLazyAsset', () => ({ + useMemoizedLazyExpensifyIcons: jest.fn(() => ({ + MoneyBag: null, + Send: null, + ThumbsUp: null, + Export: null, + })), + useMemoizedLazyIllustrations: jest.fn(() => ({ + ThumbsUpStars: null, + Fireworks: null, + })), +})); + +jest.mock('react-native-reanimated', () => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return require('react-native-reanimated/mock'); +}); + +const mockNavigate = jest.mocked(Navigation.navigate); +const mockUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction; + +const ACCOUNT_ID = 12345; + +const BASE_TODOS: TodosDerivedValue = { + reportsToSubmit: [], + reportsToApprove: [], + reportsToPay: [], + reportsToExport: [], + transactionsByReportID: {}, +}; + +function renderForYouSection() { + return render(); +} + +function pressFirstBeginButton() { + const [firstButton] = screen.getAllByText('Begin'); + fireEvent.press(firstButton); +} + +describe('ForYouSection', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(async () => { + mockUseResponsiveLayout.mockReturnValue({ + shouldUseNarrowLayout: false, + isSmallScreenWidth: false, + isInNarrowPaneModal: false, + isExtraSmallScreenHeight: false, + isMediumScreenWidth: false, + isLargeScreenWidth: true, + isExtraLargeScreenWidth: false, + isExtraSmallScreenWidth: false, + isSmallScreen: false, + onboardingIsMediumOrLargerScreenWidth: true, + }); + + await act(async () => { + await Onyx.multiSet({ + [ONYXKEYS.SESSION]: {accountID: ACCOUNT_ID, email: 'test@example.com'}, + [ONYXKEYS.IS_LOADING_APP]: false, + }); + }); + await waitForBatchedUpdatesWithAct(); + }); + + afterEach(async () => { + jest.clearAllMocks(); + await act(async () => { + await Onyx.clear(); + }); + await waitForBatchedUpdatesWithAct(); + }); + + describe('EmptyState', () => { + it('renders EmptyState when there are no todos', async () => { + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, BASE_TODOS); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText('Begin')).not.toBeOnTheScreen(); + }); + }); + + describe('navigation with multiple reports (search route)', () => { + it('navigates to SEARCH_ROOT when submit has multiple reports', async () => { + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToSubmit: [ + {reportID: '1'} as TodosDerivedValue['reportsToSubmit'][number], + {reportID: '2'} as TodosDerivedValue['reportsToSubmit'][number], + ], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + const calledRoute = mockNavigate.mock.calls.at(0)?.at(0) as string; + expect(calledRoute).toContain(ROUTES.SEARCH_ROOT.route); + }); + + it('navigates to SEARCH_ROOT when approve has multiple reports', async () => { + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToApprove: [ + {reportID: '3'} as TodosDerivedValue['reportsToApprove'][number], + {reportID: '4'} as TodosDerivedValue['reportsToApprove'][number], + ], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + const calledRoute = mockNavigate.mock.calls.at(0)?.at(0) as string; + expect(calledRoute).toContain(ROUTES.SEARCH_ROOT.route); + }); + }); + + describe('navigation with a single report (direct report route)', () => { + describe('wide layout', () => { + beforeEach(() => { + mockUseResponsiveLayout.mockReturnValue({ + shouldUseNarrowLayout: false, + isSmallScreenWidth: false, + isInNarrowPaneModal: false, + isExtraSmallScreenHeight: false, + isMediumScreenWidth: false, + isLargeScreenWidth: true, + isExtraLargeScreenWidth: false, + isExtraSmallScreenWidth: false, + isSmallScreen: false, + onboardingIsMediumOrLargerScreenWidth: true, + }); + }); + + it('navigates to EXPENSE_REPORT_RHP when submit has exactly one report on wide layout', async () => { + const reportID = '42'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToSubmit: [{reportID} as TodosDerivedValue['reportsToSubmit'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + }); + + it('navigates to EXPENSE_REPORT_RHP when approve has exactly one report on wide layout', async () => { + const reportID = '55'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToApprove: [{reportID} as TodosDerivedValue['reportsToApprove'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + }); + + it('navigates to EXPENSE_REPORT_RHP when pay has exactly one report on wide layout', async () => { + const reportID = '66'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToPay: [{reportID} as TodosDerivedValue['reportsToPay'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + }); + + it('navigates to EXPENSE_REPORT_RHP when export has exactly one report on wide layout', async () => { + const reportID = '77'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToExport: [{reportID} as TodosDerivedValue['reportsToExport'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.EXPENSE_REPORT_RHP.getRoute({reportID})); + }); + }); + + describe('narrow layout', () => { + beforeEach(() => { + mockUseResponsiveLayout.mockReturnValue({ + shouldUseNarrowLayout: true, + isSmallScreenWidth: true, + isInNarrowPaneModal: false, + isExtraSmallScreenHeight: false, + isMediumScreenWidth: false, + isLargeScreenWidth: false, + isExtraLargeScreenWidth: false, + isExtraSmallScreenWidth: false, + isSmallScreen: true, + onboardingIsMediumOrLargerScreenWidth: false, + }); + }); + + it('navigates to REPORT_WITH_ID when submit has exactly one report on narrow layout', async () => { + const reportID = '99'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToSubmit: [{reportID} as TodosDerivedValue['reportsToSubmit'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(reportID)); + }); + + it('navigates to REPORT_WITH_ID when approve has exactly one report on narrow layout', async () => { + const reportID = '100'; + await act(async () => { + await Onyx.set(ONYXKEYS.DERIVED.TODOS, { + ...BASE_TODOS, + reportsToApprove: [{reportID} as TodosDerivedValue['reportsToApprove'][number]], + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderForYouSection(); + await waitForBatchedUpdatesWithAct(); + + pressFirstBeginButton(); + + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(reportID)); + }); + }); + }); +}); diff --git a/tests/unit/selectors/TodosTest.ts b/tests/unit/selectors/TodosTest.ts index a9ead929fc18..7bb2bbeca7e2 100644 --- a/tests/unit/selectors/TodosTest.ts +++ b/tests/unit/selectors/TodosTest.ts @@ -95,6 +95,36 @@ describe('todosReportCountsSelector', () => { }); describe('todosSingleReportIDsSelector', () => { + it('returns EMPTY_TODOS_SINGLE_REPORT_IDS when todos is undefined', () => { + const result = todosSingleReportIDsSelector(undefined); + + expect(result).toEqual({ + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: undefined, + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: undefined, + [CONST.SEARCH.SEARCH_KEYS.PAY]: undefined, + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: undefined, + }); + }); + + it('returns EMPTY_TODOS_SINGLE_REPORT_IDS when all arrays are empty', () => { + const todos: TodosDerivedValue = { + reportsToSubmit: [], + reportsToApprove: [], + reportsToPay: [], + reportsToExport: [], + transactionsByReportID: {}, + }; + + const result = todosSingleReportIDsSelector(todos); + + expect(result).toEqual({ + [CONST.SEARCH.SEARCH_KEYS.SUBMIT]: undefined, + [CONST.SEARCH.SEARCH_KEYS.APPROVE]: undefined, + [CONST.SEARCH.SEARCH_KEYS.PAY]: undefined, + [CONST.SEARCH.SEARCH_KEYS.EXPORT]: undefined, + }); + }); + it('returns report ID when exactly one report exists in each array', () => { const todos: TodosDerivedValue = { reportsToSubmit: [{reportID: '1'}] as TodosDerivedValue['reportsToSubmit'], @@ -132,4 +162,53 @@ describe('todosSingleReportIDsSelector', () => { [CONST.SEARCH.SEARCH_KEYS.EXPORT]: undefined, }); }); + + it('returns the same object reference when called twice with shallowly equal values (memoization)', () => { + const todos: TodosDerivedValue = { + reportsToSubmit: [{reportID: '10'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [] as unknown as TodosDerivedValue['reportsToApprove'], + reportsToPay: [] as unknown as TodosDerivedValue['reportsToPay'], + reportsToExport: [] as unknown as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const first = todosSingleReportIDsSelector(todos); + + const todosClone: TodosDerivedValue = { + reportsToSubmit: [{reportID: '10'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [] as unknown as TodosDerivedValue['reportsToApprove'], + reportsToPay: [] as unknown as TodosDerivedValue['reportsToPay'], + reportsToExport: [] as unknown as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const second = todosSingleReportIDsSelector(todosClone); + + expect(second).toBe(first); + }); + + it('returns a new object reference when values change (memoization invalidation)', () => { + const todos: TodosDerivedValue = { + reportsToSubmit: [{reportID: '20'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [] as unknown as TodosDerivedValue['reportsToApprove'], + reportsToPay: [] as unknown as TodosDerivedValue['reportsToPay'], + reportsToExport: [] as unknown as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const first = todosSingleReportIDsSelector(todos); + + const todosChanged: TodosDerivedValue = { + reportsToSubmit: [{reportID: '21'}] as TodosDerivedValue['reportsToSubmit'], + reportsToApprove: [] as unknown as TodosDerivedValue['reportsToApprove'], + reportsToPay: [] as unknown as TodosDerivedValue['reportsToPay'], + reportsToExport: [] as unknown as TodosDerivedValue['reportsToExport'], + transactionsByReportID: {}, + }; + + const second = todosSingleReportIDsSelector(todosChanged); + + expect(second).not.toBe(first); + expect(second[CONST.SEARCH.SEARCH_KEYS.SUBMIT]).toBe('21'); + }); }); From 654c7e35e174cf5c281e0c1786fddc6e55f8e819 Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Fri, 20 Feb 2026 16:22:49 +0500 Subject: [PATCH 6/7] prettier fix --- tests/ui/ForYouSectionTest.tsx | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/ui/ForYouSectionTest.tsx b/tests/ui/ForYouSectionTest.tsx index 699b08d034ee..f2f358bac0e7 100644 --- a/tests/ui/ForYouSectionTest.tsx +++ b/tests/ui/ForYouSectionTest.tsx @@ -137,10 +137,7 @@ describe('ForYouSection', () => { await act(async () => { await Onyx.set(ONYXKEYS.DERIVED.TODOS, { ...BASE_TODOS, - reportsToSubmit: [ - {reportID: '1'} as TodosDerivedValue['reportsToSubmit'][number], - {reportID: '2'} as TodosDerivedValue['reportsToSubmit'][number], - ], + reportsToSubmit: [{reportID: '1'} as TodosDerivedValue['reportsToSubmit'][number], {reportID: '2'} as TodosDerivedValue['reportsToSubmit'][number]], }); }); await waitForBatchedUpdatesWithAct(); @@ -159,10 +156,7 @@ describe('ForYouSection', () => { await act(async () => { await Onyx.set(ONYXKEYS.DERIVED.TODOS, { ...BASE_TODOS, - reportsToApprove: [ - {reportID: '3'} as TodosDerivedValue['reportsToApprove'][number], - {reportID: '4'} as TodosDerivedValue['reportsToApprove'][number], - ], + reportsToApprove: [{reportID: '3'} as TodosDerivedValue['reportsToApprove'][number], {reportID: '4'} as TodosDerivedValue['reportsToApprove'][number]], }); }); await waitForBatchedUpdatesWithAct(); From 6bb87ea3ad38332381f430a48e6f959ecdd5e6d2 Mon Sep 17 00:00:00 2001 From: Mukhriddin Shakhriyorov Date: Wed, 25 Feb 2026 21:53:30 +0500 Subject: [PATCH 7/7] Remove unnecessary canBeMissing flags from useOnyx hooks in ForYouSection --- src/pages/home/ForYouSection/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/home/ForYouSection/index.tsx b/src/pages/home/ForYouSection/index.tsx index d2e6182739a7..cf86ac528b90 100644 --- a/src/pages/home/ForYouSection/index.tsx +++ b/src/pages/home/ForYouSection/index.tsx @@ -23,8 +23,8 @@ function ForYouSection() { const theme = useTheme(); const {translate} = useLocalize(); const {shouldUseNarrowLayout} = useResponsiveLayout(); - const [accountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: accountIDSelector}); - const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true}); + const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector}); + const [isLoadingApp = true] = useOnyx(ONYXKEYS.IS_LOADING_APP); const [reportCounts = CONST.EMPTY_TODOS_REPORT_COUNTS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {selector: todosReportCountsSelector}); const [singleReportIDs = EMPTY_TODOS_SINGLE_REPORT_IDS] = useOnyx(ONYXKEYS.DERIVED.TODOS, {selector: todosSingleReportIDsSelector});