Repository navigation
Guard search snapshot data reads that crash while a search is loading #103937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import {renderHook, waitFor} from '@testing-library/react-native'; | ||
|
|
||
| import useMergeTransactions from '@hooks/useMergeTransactions'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {MergeTransaction, SearchResults} from '@src/types/onyx'; | ||
|
|
||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import createRandomTransaction from '../../utils/collections/transaction'; | ||
| import createMock from '../../utils/createMock'; | ||
|
|
||
| let mockCurrentSearchResults: SearchResults | undefined; | ||
|
|
||
| jest.mock('@components/Search/SearchContext', () => ({ | ||
| useSearchQueryContext: () => ({ | ||
| currentSearchHash: 12345, | ||
| }), | ||
| useSearchResultsContext: () => ({ | ||
| currentSearchResults: mockCurrentSearchResults, | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('@hooks/usePolicyForMovingExpenses', () => ({ | ||
| __esModule: true, | ||
| default: () => ({policyForMovingExpensesID: undefined}), | ||
| })); | ||
|
|
||
| describe('useMergeTransactions', () => { | ||
| beforeAll(() => { | ||
| Onyx.init({keys: ONYXKEYS}); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await Onyx.clear(); | ||
| mockCurrentSearchResults = undefined; | ||
| }); | ||
|
|
||
| it('falls back to the Onyx transactions while the search snapshot has no data yet', async () => { | ||
| // Given a merge started from search while the new search's snapshot only has `search` (optimistic loading state), | ||
| // with transactions that exist in Onyx but not in the merge draft, so the hook looks them up in the snapshot first | ||
| mockCurrentSearchResults = createMock<SearchResults>({search: {type: CONST.SEARCH.DATA_TYPES.EXPENSE}}); | ||
| const onyxTransaction1 = createRandomTransaction(1); | ||
| const onyxTransaction2 = createRandomTransaction(2); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}1`, onyxTransaction1); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}2`, onyxTransaction2); | ||
| const mergeTransaction = createMock<MergeTransaction>({targetTransactionID: '1', sourceTransactionID: '2'}); | ||
|
|
||
| // When the hook resolves the target and source transactions | ||
| const {result} = renderHook(() => useMergeTransactions({mergeTransaction})); | ||
|
|
||
| // Then it falls back to the Onyx transactions instead of throwing on the missing snapshot data | ||
| await waitFor(() => { | ||
| expect(result.current.targetTransaction).toEqual(onyxTransaction1); | ||
| expect(result.current.sourceTransaction).toEqual(onyxTransaction2); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This renders only
useSearchBulkActions, whoseselectedMergeReportsaccess already usedcurrentSearchResults?.data?.[...]in the parent commit; it never executesMoneyRequestView,useMergeTransactions, orSearchMergeReports, which are the three production readers changed here. Consequently, this test passes with all three fixes reverted and cannot prevent this crash from regressing; add coverage that supplies a data-less snapshot to the changed call paths.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MelvinBot 🟡 Agree with Codex here. This test protects the
selectedMergeReportsline that #100674 already fixed, and that's still useful since #100674 shipped without a test.But none of the 3 lines this PR changes run here, so reverting the fixes in
MoneyRequestView,useMergeTransactionsandSearchReportsMergeReportsstill passes. Can we add one case that hits a changed line?useMergeTransactionsis the cheapest one:renderHookit withuseSearchResultsContextmocked to return a snapshot that has onlysearch, plus amergeTransactionwhose target ID isn't in the merge draft, sogetTransactionreaches the snapshot read:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the case in c30a062, as a new file: tests/unit/hooks/useMergeTransactionsTest.ts.
It renders
useMergeTransactionswith a snapshot that has onlysearch. The merge draft has noeligibleTransactions, sogetTransactionreaches the snapshot read changed at useMergeTransactions.ts:59. It asserts that both target and source fall back to the Onyx transactions. If you revert thedata?.fix, the read throws during render and the test fails.It passed in CI (
test (job 7)), along with typecheck, ESLint and Oxfmt.view run