diff --git a/src/hooks/useCollectionDelta.ts b/src/hooks/useCollectionDelta.ts index 2da818e6938d..fc64f759b547 100644 --- a/src/hooks/useCollectionDelta.ts +++ b/src/hooks/useCollectionDelta.ts @@ -1,3 +1,5 @@ +import {useIsOnSearch} from '@components/Search/SearchScopeProvider'; + import getCollectionDelta from '@libs/getCollectionDelta'; import type {OnyxCollection} from 'react-native-onyx'; @@ -10,6 +12,11 @@ import usePrevious from './usePrevious'; * structurally shares unchanged members, the underlying diff is a cheap reference-equality scan. */ function useCollectionDelta(value: OnyxCollection): OnyxCollection | undefined { + const isOnSearch = useIsOnSearch(); + // Under Search, useOnyx can return snapshot copies, which break the reference-equality diff. + if (__DEV__ && isOnSearch) { + throw new Error('useCollectionDelta cannot run under SearchScopeProvider'); + } const previous = usePrevious(value); return getCollectionDelta(value, previous); } diff --git a/tests/unit/useCollectionDeltaTest.ts b/tests/unit/useCollectionDeltaTest.tsx similarity index 54% rename from tests/unit/useCollectionDeltaTest.ts rename to tests/unit/useCollectionDeltaTest.tsx index 65afa9d5144f..eed03f44cbf8 100644 --- a/tests/unit/useCollectionDeltaTest.ts +++ b/tests/unit/useCollectionDeltaTest.tsx @@ -1,9 +1,13 @@ import {renderHook} from '@testing-library/react-native'; +import {SearchScopeProvider} from '@components/Search/SearchScopeProvider'; + import useCollectionDelta from '@hooks/useCollectionDelta'; import type {OnyxCollection} from 'react-native-onyx'; +import React from 'react'; + type Item = {v: number}; function renderUseCollectionDelta(initialProps: OnyxCollection) { @@ -59,4 +63,33 @@ describe('useCollectionDelta', () => { rerender({a}); expect(result.current).toBeUndefined(); }); + + it('should throw when rendered under Search scope', () => { + // Given a consumer mounted under SearchScopeProvider, where useOnyx may serve snapshot copies whose members are + // not the live objects, so a switch between sources would report every member as changed or removed + const wrapper = ({children}: {children: React.ReactNode}) => {children}; + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + // When the consumer renders + const renderUnderSearch = () => renderHook(() => useCollectionDelta({a: {v: 1}}), {wrapper}); + + // Then the hook refuses to run, so the misplaced consumer is caught in development and tests + expect(renderUnderSearch).toThrow('useCollectionDelta'); + consoleErrorSpy.mockRestore(); + }); + + it('should not throw when Search scope is turned off for the subtree', () => { + // Given a consumer under SearchScopeProvider with isOnSearch={false}, which is how Search opts a subtree back into live data + const wrapper = ({children}: {children: React.ReactNode}) => {children}; + const a: Item = {v: 1}; + const b: Item = {v: 2}; + const initialProps: OnyxCollection = {a}; + + // When the consumer renders and then receives a new member + const {result, rerender} = renderHook((value: OnyxCollection) => useCollectionDelta(value), {initialProps, wrapper}); + rerender({a, b}); + + // Then the guard stays silent and the delta works as it does outside Search + expect(result.current).toStrictEqual({b}); + }); });