-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix: Dropdown shows 2 selected after merging expenses #79943
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c51caf8
fix: Dropdown shows 2 selected after merging expenses
truph01 b0d7a91
fix: lint
truph01 6c72c41
fix: lint
truph01 4b30673
Merge branch 'Expensify:main' into fix/79177
truph01 17035cf
fix: apply suggestion
truph01 fd87b53
Merge branch 'Expensify:main' into fix/79177
truph01 ed15bc0
fix: add test for new hooks
truph01 a88b593
fix: lint
truph01 b8f5111
fix: lint
truph01 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {useEffect, useMemo} from 'react'; | ||
| import {useSearchContext} from '@components/Search/SearchContext'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
|
|
||
| /** | ||
| * Hook that filters selected transaction IDs to only include transactions that exist in the provided list. | ||
| * This is useful when transactions are deleted and we need to clean up the selection state. | ||
| * | ||
| * @param transactions - The current list of transactions | ||
| */ | ||
| function useFilterSelectedTransactions(transactions: Transaction[]) { | ||
| const {selectedTransactionIDs, setSelectedTransactions} = useSearchContext(); | ||
|
|
||
| const transactionIDs = useMemo(() => transactions.map((transaction) => transaction.transactionID), [transactions]); | ||
| const filteredSelectedTransactionIDs = useMemo(() => selectedTransactionIDs.filter((id) => transactionIDs.includes(id)), [selectedTransactionIDs, transactionIDs]); | ||
| useEffect(() => { | ||
| if (filteredSelectedTransactionIDs.length === selectedTransactionIDs.length) { | ||
| return; | ||
| } | ||
| setSelectedTransactions(filteredSelectedTransactionIDs); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [filteredSelectedTransactionIDs]); | ||
| } | ||
|
|
||
| export default useFilterSelectedTransactions; | ||
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,160 @@ | ||
| import {renderHook} from '@testing-library/react-native'; | ||
| import useFilterSelectedTransactions from '@hooks/useFilterSelectedTransactions'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
| import createRandomTransaction from '../../utils/collections/transaction'; | ||
|
|
||
| // Mock variables that can be modified in tests | ||
| let mockSelectedTransactionIDs: string[] = []; | ||
| const mockSetSelectedTransactions = jest.fn(); | ||
|
|
||
| jest.mock('@components/Search/SearchContext', () => ({ | ||
| useSearchContext: () => ({ | ||
| selectedTransactionIDs: mockSelectedTransactionIDs, | ||
| setSelectedTransactions: mockSetSelectedTransactions, | ||
| clearSelectedTransactions: jest.fn(), | ||
| selectedTransactions: {}, | ||
| currentSearchHash: 12345, | ||
| }), | ||
| })); | ||
|
|
||
| describe('useFilterSelectedTransactions', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockSelectedTransactionIDs = []; | ||
| }); | ||
|
|
||
| it('should not call setSelectedTransactions when no transactions are selected', () => { | ||
| const transactions = [createRandomTransaction(1), createRandomTransaction(2)]; | ||
| const trans0 = transactions.at(0); | ||
| const trans1 = transactions.at(1); | ||
| if (trans0) { | ||
| trans0.transactionID = 'trans1'; | ||
| } | ||
| if (trans1) { | ||
| trans1.transactionID = 'trans2'; | ||
| } | ||
|
|
||
| mockSelectedTransactionIDs = []; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| expect(mockSetSelectedTransactions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not call setSelectedTransactions when all selected transactions exist in the list', () => { | ||
| const transactions = [createRandomTransaction(1), createRandomTransaction(2)]; | ||
| const trans0 = transactions.at(0); | ||
| const trans1 = transactions.at(1); | ||
| if (trans0) { | ||
| trans0.transactionID = 'trans1'; | ||
| } | ||
| if (trans1) { | ||
| trans1.transactionID = 'trans2'; | ||
| } | ||
|
|
||
| mockSelectedTransactionIDs = ['trans1', 'trans2']; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| expect(mockSetSelectedTransactions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should filter out selected transactions that no longer exist in the transactions list', () => { | ||
| const transactions = [createRandomTransaction(1)]; | ||
| const trans0 = transactions.at(0); | ||
| if (trans0) { | ||
| trans0.transactionID = 'trans1'; | ||
| } | ||
|
|
||
| // trans2 and trans3 are selected but don't exist in transactions | ||
| mockSelectedTransactionIDs = ['trans1', 'trans2', 'trans3']; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| expect(mockSetSelectedTransactions).toHaveBeenCalledWith(['trans1']); | ||
| }); | ||
|
|
||
| it('should clear all selections when all selected transactions are removed', () => { | ||
| const transactions = [createRandomTransaction(1)]; | ||
| const trans0 = transactions.at(0); | ||
| if (trans0) { | ||
| trans0.transactionID = 'trans1'; | ||
| } | ||
|
|
||
| // All selected transactions don't exist in the current list | ||
| mockSelectedTransactionIDs = ['trans2', 'trans3']; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| expect(mockSetSelectedTransactions).toHaveBeenCalledWith([]); | ||
| }); | ||
|
|
||
| it('should update filtered transactions when transactions list changes', () => { | ||
| // Initial state: 3 transactions, 3 selected | ||
| const initialTransactions = [createRandomTransaction(1), createRandomTransaction(2), createRandomTransaction(3)]; | ||
| const initTrans0 = initialTransactions.at(0); | ||
| const initTrans1 = initialTransactions.at(1); | ||
| const initTrans2 = initialTransactions.at(2); | ||
| if (initTrans0) { | ||
| initTrans0.transactionID = 'trans1'; | ||
| } | ||
| if (initTrans1) { | ||
| initTrans1.transactionID = 'trans2'; | ||
| } | ||
| if (initTrans2) { | ||
| initTrans2.transactionID = 'trans3'; | ||
| } | ||
|
|
||
| mockSelectedTransactionIDs = ['trans1', 'trans2', 'trans3']; | ||
|
|
||
| const {rerender} = renderHook(({transactions}) => useFilterSelectedTransactions(transactions), { | ||
| initialProps: {transactions: initialTransactions}, | ||
| }); | ||
|
|
||
| // No filtering needed initially | ||
| expect(mockSetSelectedTransactions).not.toHaveBeenCalled(); | ||
|
|
||
| // Now remove trans2 from the list (simulating deletion) | ||
| const updatedTransactions = [initialTransactions.at(0), initialTransactions.at(2)].filter((t): t is Transaction => !!t); | ||
|
|
||
| rerender({transactions: updatedTransactions}); | ||
|
|
||
| // Should filter out trans2 from selection | ||
| expect(mockSetSelectedTransactions).toHaveBeenCalledWith(['trans1', 'trans3']); | ||
| }); | ||
|
|
||
| it('should handle empty transactions list', () => { | ||
| const transactions: Transaction[] = []; | ||
|
|
||
| mockSelectedTransactionIDs = ['trans1', 'trans2']; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| // All selections should be cleared since no transactions exist | ||
| expect(mockSetSelectedTransactions).toHaveBeenCalledWith([]); | ||
| }); | ||
|
|
||
| it('should preserve order of selected transactions after filtering', () => { | ||
| const transactions = [createRandomTransaction(1), createRandomTransaction(2), createRandomTransaction(3)]; | ||
| const trans0 = transactions.at(0); | ||
| const trans1 = transactions.at(1); | ||
| const trans2 = transactions.at(2); | ||
| if (trans0) { | ||
| trans0.transactionID = 'trans1'; | ||
| } | ||
| if (trans1) { | ||
| trans1.transactionID = 'trans2'; | ||
| } | ||
| if (trans2) { | ||
| trans2.transactionID = 'trans3'; | ||
| } | ||
|
|
||
| // Selected in specific order, with some non-existent IDs interspersed | ||
| mockSelectedTransactionIDs = ['trans3', 'nonexistent1', 'trans1', 'nonexistent2', 'trans2']; | ||
|
|
||
| renderHook(() => useFilterSelectedTransactions(transactions)); | ||
|
|
||
| // Should maintain the original order of valid selections | ||
| expect(mockSetSelectedTransactions).toHaveBeenCalledWith(['trans3', 'trans1', 'trans2']); | ||
| }); | ||
| }); |
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.
selectedTransactionIDsis global, buttransactionsis local, so they are only comparable when from the same report screen. Implemented in #85510.