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
15 changes: 12 additions & 3 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,24 @@ function getPayActionCallback(
goToItem();
}

function getOnyxLoadingData(hash: number, queryJSON?: SearchQueryJSON, offset?: number, isOffline?: boolean, isSearchAPI = false): OnyxData<typeof ONYXKEYS.COLLECTION.SNAPSHOT> {
function getOnyxLoadingData(
hash: number,
queryJSON?: SearchQueryJSON,
offset?: number,
isOffline?: boolean,
isSearchAPI = false,
shouldCalculateTotals?: boolean,
): OnyxData<typeof ONYXKEYS.COLLECTION.SNAPSHOT> {
const shouldClearTotals = isSearchAPI && shouldCalculateTotals === false && offset === 0;
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.SNAPSHOT>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`,
value: {
search: {
...(isSearchAPI && {isLoading: true}),
...(offset ? {offset} : {}),
...(offset !== undefined ? {offset} : {}),
...(shouldClearTotals ? {count: null, total: null, currency: null} : {}),
},
},
},
Expand Down Expand Up @@ -434,7 +443,7 @@ function search({
return;
}

const {optimisticData, finallyData, failureData} = getOnyxLoadingData(queryJSON.hash, queryJSON, offset, isOffline, true);
const {optimisticData, finallyData, failureData} = getOnyxLoadingData(queryJSON.hash, queryJSON, offset, isOffline, true, shouldCalculateTotals);
const {flatFilters, limit, ...queryJSONWithoutFlatFilters} = queryJSON;
const query = {
...queryJSONWithoutFlatFilters,
Expand Down
137 changes: 137 additions & 0 deletions tests/unit/Search/searchTotalsLoadingDataTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* eslint-disable rulesdir/no-multiple-api-calls */
import {search} from '@libs/actions/Search';
import {makeRequestWithSideEffects, waitForWrites} from '@libs/API';
import {READ_COMMANDS} from '@libs/API/types';
import {buildSearchQueryJSON} from '@libs/SearchQueryUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

jest.mock('@libs/API', () => ({
makeRequestWithSideEffects: jest.fn(),
waitForWrites: jest.fn(),
write: jest.fn(),
read: jest.fn(),
}));

function getQueryJSON() {
const queryJSON = buildSearchQueryJSON('');
if (!queryJSON) {
throw new Error('Query JSON should be defined for test setup');
}

return queryJSON;
}

type SearchLoadingState = {
isLoading?: boolean;
offset?: number;
count?: number | null;
total?: number | null;
currency?: string | null;
};

type SearchRequestData = {
optimisticData?: Array<{
key: string;
value?: {
search?: SearchLoadingState;
};
}>;
};

function getMakeRequestWithSideEffectsMock() {
return makeRequestWithSideEffects as unknown as {
mock: {
calls: Array<[unknown, unknown, SearchRequestData?]>;
};
mockResolvedValue: (value: {onyxData: Array<{value: {search: {offset: number; hasMoreResults: boolean}; data: Record<string, unknown>}}>; jsonCode: number}) => void;
};
}

function getWaitForWritesMock() {
return waitForWrites as unknown as {
mockResolvedValue: (value: void) => void;
};
}

function getSearchLoadingUpdateForHash(hash: number) {
const makeRequestWithSideEffectsMock = getMakeRequestWithSideEffectsMock();
const [, , requestData] = makeRequestWithSideEffectsMock.mock.calls.at(-1) ?? [];
const optimisticData = requestData?.optimisticData ?? [];
return optimisticData.find((update) => update.key === `${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}` && !!update.value?.search?.isLoading)?.value?.search;
}

describe('search loading totals handling', () => {
beforeEach(() => {
jest.clearAllMocks();
getWaitForWritesMock().mockResolvedValue(undefined);
getMakeRequestWithSideEffectsMock().mockResolvedValue({
onyxData: [{value: {search: {offset: 0, hasMoreResults: false}, data: {}}}],
jsonCode: CONST.JSON_CODE.SUCCESS,
});
});

it('clears stale totals optimistically for initial load when totals are not requested', async () => {
const queryJSON = getQueryJSON();

await search({
queryJSON,
searchKey: CONST.SEARCH.SEARCH_KEYS.EXPENSES,
offset: 0,
shouldCalculateTotals: false,
isLoading: false,
});

const loadingSearchData = getSearchLoadingUpdateForHash(queryJSON.hash);
expect(loadingSearchData).toMatchObject({
isLoading: true,
offset: 0,
count: null,
total: null,
currency: null,
});
expect(waitForWrites).toHaveBeenCalledWith(READ_COMMANDS.SEARCH);
});

it('does not clear totals for initial load when totals are requested', async () => {
const queryJSON = getQueryJSON();

await search({
queryJSON,
searchKey: CONST.SEARCH.SEARCH_KEYS.EXPENSES,
offset: 0,
shouldCalculateTotals: true,
isLoading: false,
});

const loadingSearchData = getSearchLoadingUpdateForHash(queryJSON.hash);
expect(loadingSearchData).toMatchObject({
isLoading: true,
offset: 0,
});
expect(loadingSearchData?.count).toBeUndefined();
expect(loadingSearchData?.total).toBeUndefined();
expect(loadingSearchData?.currency).toBeUndefined();
});

it('does not clear totals for paginated loads even when totals are not requested', async () => {
const queryJSON = getQueryJSON();

await search({
queryJSON,
searchKey: CONST.SEARCH.SEARCH_KEYS.EXPENSES,
offset: 20,
shouldCalculateTotals: false,
isLoading: false,
});

const loadingSearchData = getSearchLoadingUpdateForHash(queryJSON.hash);
expect(loadingSearchData).toMatchObject({
isLoading: true,
offset: 20,
});
expect(loadingSearchData?.count).toBeUndefined();
expect(loadingSearchData?.total).toBeUndefined();
expect(loadingSearchData?.currency).toBeUndefined();
});
});
Loading