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
7 changes: 7 additions & 0 deletions src/hooks/useCollectionDelta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {useIsOnSearch} from '@components/Search/SearchScopeProvider';

import getCollectionDelta from '@libs/getCollectionDelta';

import type {OnyxCollection} from 'react-native-onyx';
Expand All @@ -10,6 +12,11 @@ import usePrevious from './usePrevious';
* structurally shares unchanged members, the underlying diff is a cheap reference-equality scan.
*/
function useCollectionDelta<TValue>(value: OnyxCollection<TValue>): OnyxCollection<TValue> | 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');
}
Comment thread
adhorodyski marked this conversation as resolved.
Resolved
const previous = usePrevious(value);
return getCollectionDelta(value, previous);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Item>) {
Expand Down Expand Up @@ -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}) => <SearchScopeProvider>{children}</SearchScopeProvider>;
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

// When the consumer renders
const renderUnderSearch = () => renderHook(() => useCollectionDelta<Item>({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}) => <SearchScopeProvider isOnSearch={false}>{children}</SearchScopeProvider>;
const a: Item = {v: 1};
const b: Item = {v: 2};
const initialProps: OnyxCollection<Item> = {a};

// When the consumer renders and then receives a new member
const {result, rerender} = renderHook((value: OnyxCollection<Item>) => 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});
});
});
Loading