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
4 changes: 1 addition & 3 deletions src/pages/home/InsightsSection/useInsightData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function useInsightData(config: SearchTypeMenuItem | undefined, isConfigResolved
const [searchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${queryJSON?.hash}`);

const {isOffline} = useNetwork();
// The chart is built from a snapshot that no update patches, so an expense change has to move the key.
const [spendDataSignature] = useOnyx(ONYXKEYS.DERIVED.SPEND_DATA_SIGNATURE);
const [policyIDs] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policyIDsSelector});

const retry = () => {
Expand All @@ -61,7 +59,7 @@ function useInsightData(config: SearchTypeMenuItem | undefined, isConfigResolved
});
};

useTabFocusedRefresh(SCREENS.HOME, [queryJSON?.hash, isOffline, isConfigResolved, spendDataSignature?.expenses ?? 0, policyIDs ?? ''].join('|'), retry);
useTabFocusedRefresh(SCREENS.HOME, [queryJSON?.hash, isOffline, isConfigResolved, policyIDs ?? ''].join('|'), retry);

const sortedData = useGroupedItems(searchResults, queryJSON);

Expand Down
112 changes: 107 additions & 5 deletions tests/unit/HomePage/InsightsSection/useInsightDataRefreshTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* Contracts asserted:
* - the Insights chart fetches once when Home is the active tab
* - closing an RHP over Home does not refetch it
* - an expense change moves the derived spend counter and does refetch it
* - expense changes do not refetch it, including after closing an RHP
* - returning to Home, changing charts, and reconnecting still refetch it
* - joining a workspace fetches it again
*/
import {renderHook} from '@testing-library/react-native';

import type {SearchQueryJSON} from '@components/Search/types';

import useIsTabFocused from '@hooks/useIsTabFocused';
import useNetwork from '@hooks/useNetwork';

import {search} from '@libs/actions/Search';
import type {SearchTypeMenuItem} from '@libs/SearchUIUtils';
Expand Down Expand Up @@ -96,6 +98,7 @@ jest.mock('@hooks/useOnyx', () => ({
const mockedSearch = jest.mocked(search);
const mockedUseIsFocused = jest.mocked(useIsFocused);
const mockedUseIsTabFocused = jest.mocked(useIsTabFocused);
const mockedUseNetwork = jest.mocked(useNetwork);

beforeEach(() => {
for (const key of Object.keys(onyxData)) {
Expand All @@ -104,6 +107,7 @@ beforeEach(() => {
mockedSearch.mockClear();
mockedUseIsFocused.mockReturnValue(true);
mockedUseIsTabFocused.mockReturnValue(true);
mockedUseNetwork.mockReturnValue({isOffline: false});
});

describe('useInsightData — refresh gating', () => {
Expand Down Expand Up @@ -132,18 +136,116 @@ describe('useInsightData — refresh gating', () => {
expect(mockedSearch).toHaveBeenCalledTimes(1);
});

it('fetches again when an expense changes', () => {
it('does not refetch when expenses are added, edited, or deleted', () => {
// Given the chart has already fetched
const {rerender} = renderHook(() => useInsightData(config));

// When an expense changes, which moves the derived counter but no query
onyxData[ONYXKEYS.DERIVED.SPEND_DATA_SIGNATURE] = {expenses: 1, cardExpenses: 0};
// When successive expense updates move the derived counter without changing the query
for (const expenses of [1, 2, 3]) {
onyxData[ONYXKEYS.DERIVED.SPEND_DATA_SIGNATURE] = {expenses, cardExpenses: 0};
rerender(undefined);
}

// Then the chart keeps its snapshot instead of live-updating
expect(mockedSearch).toHaveBeenCalledTimes(1);
});

it('does not queue an expense-driven refresh behind an RHP', () => {
// Given an RHP covers Home after the chart has loaded
const {rerender} = renderHook(() => useInsightData(config));
mockedUseIsFocused.mockReturnValue(false);
rerender(undefined);

// Then the chart fetches again instead of showing a snapshot it knows nothing about
// When expenses change behind the RHP and it is then closed
for (const expenses of [1, 2, 3]) {
onyxData[ONYXKEYS.DERIVED.SPEND_DATA_SIGNATURE] = {expenses, cardExpenses: 0};
rerender(undefined);
}
expect(mockedSearch).toHaveBeenCalledTimes(1);
mockedUseIsFocused.mockReturnValue(true);
rerender(undefined);

// Then closing the RHP does not replay an expense-driven search
expect(mockedSearch).toHaveBeenCalledTimes(1);
});

it('fetches again when returning to the Home tab', () => {
// Given the chart loaded before navigating to another tab
const {rerender} = renderHook(() => useInsightData(config));
mockedUseIsTabFocused.mockReturnValue(false);
mockedUseIsFocused.mockReturnValue(false);
rerender(undefined);
expect(mockedSearch).toHaveBeenCalledTimes(1);

// When Home becomes the active, visible tab again
mockedUseIsTabFocused.mockReturnValue(true);
mockedUseIsFocused.mockReturnValue(true);
rerender(undefined);

// Then the chart gets fresh data even though its query is unchanged
expect(mockedSearch).toHaveBeenCalledTimes(2);
});

it('fetches the newly selected chart', () => {
// Given the initial chart has loaded
const {rerender} = renderHook((selectedConfig: SearchTypeMenuItem) => useInsightData(selectedConfig), {initialProps: config});
const nextQuery = {...queryJSON, hash: SNAPSHOT_HASH + 1, groupBy: CONST.SEARCH.GROUP_BY.CATEGORY};
const nextConfig = {...config, searchQueryJSON: nextQuery, key: CONST.SEARCH.SEARCH_KEYS.TOP_CATEGORIES};

// When selecting a chart with a different query
rerender(nextConfig);

// Then the new chart is fetched with the existing Home search options
expect(mockedSearch).toHaveBeenCalledTimes(2);
expect(mockedSearch).toHaveBeenLastCalledWith({
queryJSON: nextQuery,
searchKey: nextConfig.key,
offset: 0,
shouldCalculateTotals: true,
isLoading: false,
shouldUpdateLastSearchParams: false,
skipWaitForWrites: true,
});
});

it('does not refetch for an equivalent chart configuration', () => {
// Given the chart has already loaded
const {rerender} = renderHook((selectedConfig: SearchTypeMenuItem) => useInsightData(selectedConfig), {initialProps: config});

// When a render rebuilds the configuration without changing its query hash
rerender({...config, searchQueryJSON: {...queryJSON}});

// Then object identity alone does not trigger another search
expect(mockedSearch).toHaveBeenCalledTimes(1);
});

it('fetches again after reconnecting', () => {
// Given the chart loaded before the connection was lost
const {rerender} = renderHook(() => useInsightData(config));
mockedUseNetwork.mockReturnValue({isOffline: true});
rerender(undefined);
expect(mockedSearch).toHaveBeenCalledTimes(1);

// When the connection is restored
mockedUseNetwork.mockReturnValue({isOffline: false});
rerender(undefined);

// Then the chart refreshes without needing an expense update
expect(mockedSearch).toHaveBeenCalledTimes(2);
});

it('waits for configuration resolution before fetching', () => {
// Given the persisted chart selection has not loaded yet
const {rerender} = renderHook((isConfigResolved: boolean) => useInsightData(config, isConfigResolved), {initialProps: false});
expect(mockedSearch).not.toHaveBeenCalled();

// When the chart configuration finishes loading
rerender(true);

// Then the resolved chart is fetched once, not a temporary fallback
expect(mockedSearch).toHaveBeenCalledTimes(1);
});

it('fetches again when the user joins a workspace', () => {
// Given the chart has already fetched
const {rerender} = renderHook(() => useInsightData(config));
Expand Down
Loading