-
Notifications
You must be signed in to change notification settings - Fork 4k
[No QA] IOURequestStepScan clean-up, Phase 1: Add comprehensive test coverage for multi-scan receipt handling
#80628
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
roryabraham
merged 3 commits into
Expensify:main
from
samranahm:79929/clean-up-IOURequestStepScan
Jan 27, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import {shouldReuseInitialTransaction} from '@libs/TransactionUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
| import createRandomTransaction from '../utils/collections/transaction'; | ||
| import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
|
||
| describe('IOURequestStepScan', () => { | ||
| beforeAll(() => { | ||
| Onyx.init({ | ||
| keys: ONYXKEYS, | ||
| }); | ||
| }); | ||
|
samranahm marked this conversation as resolved.
|
||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| await Onyx.clear(); | ||
| await waitForBatchedUpdates(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await Onyx.clear(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should preserve multiple draft transactions when processing new receipts', async () => { | ||
| const transaction1 = createRandomTransaction(1); | ||
| transaction1.receipt = {source: 'file://receipt1.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| const transaction2 = createRandomTransaction(2); | ||
| transaction2.receipt = {source: 'file://receipt2.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, transaction1); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, transaction2); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const tx1Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const tx2Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| expect(tx1Data?.transactionID).toBe(transaction1.transactionID); | ||
| expect(tx1Data?.receipt?.source).toBe('file://receipt1.png'); | ||
| expect(tx2Data?.transactionID).toBe(transaction2.transactionID); | ||
| expect(tx2Data?.receipt?.source).toBe('file://receipt2.png'); | ||
| }); | ||
|
|
||
| it('should not lose draft transactions when updating one receipt', async () => { | ||
| const transaction1 = createRandomTransaction(1); | ||
| transaction1.receipt = {source: 'file://receipt1.png'}; | ||
|
|
||
| const transaction2 = createRandomTransaction(2); | ||
| transaction2.receipt = {source: 'file://receipt2.png'}; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, transaction1); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, transaction2); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const updatedTransaction1 = {...transaction1, receipt: {source: 'file://receipt1-updated.png'}}; | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, updatedTransaction1); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const tx1Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const tx2Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| expect(tx2Data?.transactionID).toBe(transaction2.transactionID); | ||
| expect(tx2Data?.receipt?.source).toBe('file://receipt2.png'); | ||
| expect(tx1Data?.transactionID).toBe(transaction1.transactionID); | ||
| expect(tx1Data?.receipt?.source).toBe('file://receipt1-updated.png'); | ||
| }); | ||
|
|
||
| it('replacing receipt in multi-scan does not clear other drafts', async () => { | ||
| const transaction1 = createRandomTransaction(1); | ||
| transaction1.receipt = {source: 'file://receipt1.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| const transaction2 = createRandomTransaction(2); | ||
| transaction2.receipt = {source: 'file://receipt2.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction1.transactionID}`, transaction1); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, transaction2); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const transactions = [transaction1, transaction2]; | ||
| const shouldReuse = shouldReuseInitialTransaction(transaction1, true, 0, true, transactions); | ||
|
|
||
| expect(shouldReuse).toBe(false); | ||
|
|
||
| const tx2Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction2.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| expect(tx2Data?.transactionID).toBe(transaction2.transactionID); | ||
| expect(tx2Data?.receipt?.source).toBe('file://receipt2.png'); | ||
| }); | ||
|
|
||
| it('multi-scan mode preserves receipts when adding new ones', async () => { | ||
| const initialTx = createRandomTransaction(0); | ||
| initialTx.receipt = {source: 'file://initial-receipt.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${initialTx.transactionID}`, initialTx); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const secondTx = createRandomTransaction(1); | ||
| secondTx.receipt = {source: 'file://second-receipt.png', state: CONST.IOU.RECEIPT_STATE.OPEN}; | ||
|
|
||
| const transactions = [initialTx]; | ||
| const shouldReuseForSecondFile = shouldReuseInitialTransaction(initialTx, true, 1, true, transactions); | ||
|
|
||
| expect(shouldReuseForSecondFile).toBe(false); | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${secondTx.transactionID}`, secondTx); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const tx1Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${initialTx.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const tx2Data = await new Promise<Transaction | undefined>((resolve) => { | ||
| const connection = Onyx.connect({ | ||
| key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${secondTx.transactionID}`, | ||
| callback: (val) => { | ||
| resolve(val); | ||
| Onyx.disconnect(connection); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| expect(tx1Data?.receipt?.source).toBe('file://initial-receipt.png'); | ||
| expect(tx2Data?.receipt?.source).toBe('file://second-receipt.png'); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.