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
2 changes: 2 additions & 0 deletions src/components/Search/SearchResultsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type SearchResultsProviderProps = {
const defaultSearchInfo: SearchResultsInfo = {
offset: 0,
hash: 0,
sortBy: CONST.SEARCH.TABLE_COLUMNS.DATE,
sortOrder: CONST.SEARCH.SORT_ORDER.DESC,
type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT,
hasMoreResults: false,
hasResults: true,
Expand Down
1 change: 1 addition & 0 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,7 @@ export {
getDateRangeDisplayValueFromFormValue,
getRangeBoundariesFromFormValue,
getRangeQueryValue,
getQueryHashes,
isSearchDatePreset,
getDateRangeForPreset,
isFilterSupported,
Expand Down
14 changes: 11 additions & 3 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ import {
getDateRangeDisplayValueFromFormValue,
getDateRangeForPreset,
getFilterFromQuery,
getQueryHashes,
isFilterNegatable,
isFilterSupported,
isSearchDatePreset,
Expand Down Expand Up @@ -4807,9 +4808,16 @@ function shouldShowEmptyState(isDataLoaded: boolean, dataLength: number, type: S
}

function isSearchDataLoaded(searchResults: SearchResults | undefined, queryJSON: Readonly<SearchQueryJSON> | undefined) {
const isDataLoaded = (searchResults?.data != null || searchResults?.errors != null) && searchResults?.search?.type === queryJSON?.type && searchResults.search.hash === queryJSON?.hash;

return isDataLoaded;
return (
(searchResults?.data != null || searchResults?.errors != null) &&
searchResults.search.type === queryJSON?.type &&
searchResults.search.hash ===
getQueryHashes({
...queryJSON,
sortBy: searchResults.search.sortBy,
sortOrder: searchResults.search.sortOrder,
Comment thread
bernhardoj marked this conversation as resolved.
Resolved
}).primaryHash
);
}

function getValidGroupBy(groupBy: string | undefined): ValueOf<typeof CONST.SEARCH.GROUP_BY> | undefined {
Expand Down
8 changes: 7 additions & 1 deletion src/types/onyx/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
TransactionListItemType,
TransactionReportGroupListItemType,
} from '@components/Search/SearchList/ListItem/types';
import type {SearchGroupBy} from '@components/Search/types';
import type {SearchGroupBy, SearchSortBy, SortOrder} from '@components/Search/types';

import type CONST from '@src/CONST';
import type ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -59,6 +59,12 @@ type SearchResultsInfo = {
/** Whether the search results are currently loading */
isLoading: boolean;

/** The sort by of the current search */
sortBy: SearchSortBy;

/** The sort order of the current search */
sortOrder: SortOrder;

/** Explicit terminal lifecycle state of the most recent search request for this snapshot.
* Optional because snapshots persisted before this field existed (and snapshots written by
* non-search actions) may not carry it.
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/SearchPageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ describe('SearchPageNarrow', () => {
type: CONST.SEARCH.DATA_TYPES.CHAT,
offset: 0,
hash: failedQueryJSON?.hash,
sortBy: failedQueryJSON?.sortBy,
sortOrder: failedQueryJSON?.sortOrder,
isLoading: false,
hasMoreResults: false,
},
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/HomePage/YourSpendSection/rowStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const makeSearchResults = (overrides: Partial<SearchResults> = {}): SearchResult
search: {
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
type: 'expense',
hasMoreResults: false,
hasResults: true,
Expand All @@ -25,6 +27,8 @@ const makeSearchResultsWithCount = (count: number): SearchResults =>
search: {
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
type: 'expense',
hasMoreResults: false,
hasResults: count > 0,
Expand Down
58 changes: 54 additions & 4 deletions tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ function makeSearchResultsWithCount(count: number): SearchResults {
type: 'expense',
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: count > 0,
isLoading: false,
Expand Down Expand Up @@ -591,15 +593,39 @@ describe('useYourSpendData — third-party cardRows', () => {
mockedGetDisplayableThirdPartyCards.mockReturnValue(makeThirdPartyCards([{cardID: THIRD_PARTY_CARD_ID_1, lastFourPAN: THIRD_PARTY_LAST_FOUR_1}]));
// First render: READY snapshot with count > 0 → row produced and total cached.
setupCardSnapshot(THIRD_PARTY_CARD_ID_1, {
search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 3, total: 1234, currency: 'USD'},
search: {
type: 'expense',
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
count: 3,
total: 1234,
currency: 'USD',
},
data: {},
});
const {result, rerender} = renderHook(() => useYourSpendData());
expect(result.current.cardRows.at(0)?.total).toBe(1234);

// Search screen wipes count/total/currency on the shared snapshot.
setupCardSnapshot(THIRD_PARTY_CARD_ID_1, {
search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: undefined, total: undefined, currency: undefined},
search: {
type: 'expense',
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
count: undefined,
total: undefined,
currency: undefined,
},
data: {},
});
rerender(undefined);
Expand Down Expand Up @@ -631,11 +657,35 @@ describe('useYourSpendData — third-party cardRows', () => {
]),
);
setupCardSnapshot(THIRD_PARTY_CARD_ID_1, {
search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 2, total: 500, currency: 'USD'},
search: {
type: 'expense',
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
count: 2,
total: 500,
currency: 'USD',
},
data: {},
});
setupCardSnapshot(THIRD_PARTY_CARD_ID_2, {
search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 3, total: 2200, currency: 'EUR'},
search: {
type: 'expense',
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
count: 3,
total: 2200,
currency: 'EUR',
},
data: {},
});
const {result} = renderHook(() => useYourSpendData());
Expand Down
62 changes: 59 additions & 3 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import IntlStore from '@src/languages/IntlStore';
import type {CardFeedForDisplay} from '@src/libs/CardFeedUtils';
import {getCardDescriptionForSearchTable} from '@src/libs/CardUtils';
import DateUtils from '@src/libs/DateUtils';
import {buildSearchQueryJSON, getDateRangeForPreset, getUserFriendlyValue} from '@src/libs/SearchQueryUtils';
import {buildSearchQueryJSON, getDateRangeForPreset, getQueryHashes, getUserFriendlyValue} from '@src/libs/SearchQueryUtils';
import * as SearchUIUtils from '@src/libs/SearchUIUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -550,6 +550,8 @@ const searchResults: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
isLoading: false,
type: 'expense',
},
Expand Down Expand Up @@ -607,6 +609,8 @@ const searchResultsGroupByFrom: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 100,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -657,6 +661,8 @@ const searchResultsGroupByCard: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 60,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -694,6 +700,8 @@ const searchResultsGroupByWithdrawalID: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 60,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -726,6 +734,8 @@ const searchResultsGroupByCategory: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -1955,6 +1965,8 @@ const searchResultsGroupByMerchant: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 470,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -2012,6 +2024,8 @@ const searchResultsGroupByTag: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -2093,6 +2107,8 @@ const searchResultsGroupByMonth: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -2122,6 +2138,8 @@ const searchResultsGroupByYear: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -2153,6 +2171,8 @@ const searchResultsGroupByQuarter: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -2182,6 +2202,8 @@ const searchResultsGroupByWeek: OnyxTypes.SearchResults = {
hasResults: true,
offset: 0,
hash: 0,
sortBy: 'date',
sortOrder: 'desc',
total: 325,
isLoading: false,
type: 'expense',
Expand Down Expand Up @@ -8338,6 +8360,8 @@ describe('SearchUIUtils', () => {
hash: queryJSON?.hash ?? 0,
isLoading: false,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
sortBy: queryJSON?.sortBy ?? 'date',
sortOrder: queryJSON?.sortOrder ?? 'desc',
},
...overrides,
};
Expand All @@ -8359,18 +8383,46 @@ describe('SearchUIUtils', () => {

it('should return false when the search type does not match the query type', () => {
const results = makeSearchResults({
search: {hasMoreResults: false, hasResults: true, offset: 0, hash: queryJSON?.hash ?? 0, isLoading: false, type: CONST.SEARCH.DATA_TYPES.CHAT},
search: {
hasMoreResults: false,
hasResults: true,
offset: 0,
hash: queryJSON?.hash ?? 0,
sortBy: 'date',
sortOrder: 'desc',
isLoading: false,
type: CONST.SEARCH.DATA_TYPES.CHAT,
},
});
expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(false);
});

it('should return false when the search hash does not match the query hash', () => {
const results = makeSearchResults({
search: {hasMoreResults: false, hasResults: true, offset: 0, hash: (queryJSON?.hash ?? 0) + 1, isLoading: false, type: CONST.SEARCH.DATA_TYPES.EXPENSE},
search: {
hasMoreResults: false,
hasResults: true,
offset: 0,
hash: (queryJSON?.hash ?? 0) + 1,
sortBy: 'date',
sortOrder: 'desc',
isLoading: false,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
},
});
expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(false);
});

it('should return true even when the search sortBy/sortOrder is different from the query', () => {
const sortBy = CONST.SEARCH.TABLE_COLUMNS.MERCHANT;
const sortOrder = CONST.SEARCH.SORT_ORDER.ASC;
const expectedHash = queryJSON ? getQueryHashes({...queryJSON, sortBy, sortOrder}).primaryHash : 0;
const results = makeSearchResults({
search: {hasMoreResults: false, hasResults: true, offset: 0, hash: expectedHash, isLoading: false, type: CONST.SEARCH.DATA_TYPES.EXPENSE, sortBy, sortOrder},
});
expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(true);
});

it('should return false when searchResults is undefined', () => {
expect(SearchUIUtils.isSearchDataLoaded(undefined, queryJSON)).toBe(false);
});
Expand Down Expand Up @@ -8417,6 +8469,8 @@ describe('SearchUIUtils', () => {
type: 'expense',
hash: 0,
offset: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
Expand Down Expand Up @@ -8549,6 +8603,8 @@ describe('SearchUIUtils', () => {
type: 'expense',
hash: 0,
offset: 0,
sortBy: 'date',
sortOrder: 'desc',
hasMoreResults: false,
hasResults: true,
isLoading: false,
Expand Down
17 changes: 13 additions & 4 deletions tests/unit/Search/getSpendOverTimeStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type SearchResults from '@src/types/onyx/SearchResults';

const queryJSON: SearchQueryJSON = {
inputQuery: '',
hash: 0,
recentSearchHash: 0,
similarSearchHash: 0,
hash: 1261888645,
recentSearchHash: 1239358332,
similarSearchHash: 1141398512,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
groupBy: CONST.SEARCH.GROUP_BY.MONTH,
view: CONST.SEARCH.VIEW.LINE,
Expand All @@ -20,7 +20,16 @@ const queryJSON: SearchQueryJSON = {
};

const defaultSearchResults: SearchResults = {
search: {offset: 0, hash: 0, type: queryJSON.type, hasMoreResults: false, hasResults: true, isLoading: false},
search: {
offset: 0,
hash: 1261888645,
sortBy: CONST.SEARCH.TABLE_COLUMNS.GROUP_MONTH,
sortOrder: CONST.SEARCH.SORT_ORDER.ASC,
type: queryJSON.type,
hasMoreResults: false,
hasResults: true,
isLoading: false,
},
data: {},
};

Expand Down
Loading
Loading