-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[NoQA] Add useNewTransactions tests #64938
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
mountiny
merged 4 commits into
Expensify:main
from
software-mansion-labs:@szymczak/add-useNewTransaction-tests
Jun 30, 2025
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,299 @@ | ||
| import {renderHook} from '@testing-library/react-native'; | ||
| import useNewTransactions from '@hooks/useNewTransactions'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
|
|
||
| // We need to mock requestAnimationFrame to mimic long Onyx merge overhead | ||
| jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback) => { | ||
| setTimeout(() => { | ||
| callback(performance.now()); | ||
| }, 30); | ||
| return 0; | ||
| }); | ||
|
|
||
| const delay = (ms: number) => | ||
| new Promise((resolve) => { | ||
| setTimeout(resolve, ms); | ||
| }); | ||
|
|
||
| describe('useNewTransactions with empty cache', () => { | ||
| const transactionsAlreadyInReport = [ | ||
| {transactionID: '2', amount: 200, created: '2023-10-02', currency: 'USD', reportID: 'report1', merchant: ''}, | ||
| {transactionID: '3', amount: 300, created: '2023-10-03', currency: 'USD', reportID: 'report1', merchant: ''}, | ||
| ]; | ||
| const newTransaction = {transactionID: '1', amount: 100, created: '2023-10-01T00:00:00Z', currency: 'USD', reportID: 'report1', merchant: ''}; | ||
|
|
||
| it("doesn't return new transactions when no transactions are added", () => { | ||
| // 1. Report and transactions data is not loaded yet | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: false, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| // 2. Report is loaded and it has no transactions so there are no further rerenders | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns no new transactions when transactions come from initial Report load', () => { | ||
| // 1. Report and transactions data is not loaded yet | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: false, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. Report is loaded and transactions data is not loaded yet | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. Report is loaded and transactions data is loaded | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }); | ||
| // there is no new transactions, because the transactions that were already in the report are not considered new | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns new transactions when transactions are added after initial load', () => { | ||
| // 1. Report and transactions data is not loaded yet | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: false, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. Report is loaded and transactions data is not loaded yet | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. Report is loaded and transactions data is loaded | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 4. User added new transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [...transactionsAlreadyInReport, newTransaction], | ||
| }); | ||
| expect(result.current).toEqual([newTransaction]); | ||
| }); | ||
|
|
||
| it('returns new transactions when adding transactions to empty report', async () => { | ||
| // 1. Report and transactions data is not loaded yet | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: false, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| // 2. Report is loaded and it has no transactions so there are no further rerenders | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| await delay(1000); // We need to wait to ensure that the skipFirstTransactionsChange is set to false by the useEffect | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. User added new transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [newTransaction], | ||
| }); | ||
| expect(result.current).toEqual([newTransaction]); | ||
| }); | ||
|
|
||
| it('returns no new transactions when transactions are removed', () => { | ||
| // 1. Report and transactions data is not loaded yet | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: false, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. Report is loaded and transactions data is not loaded yet | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. Report is loaded and transactions data is loaded | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 4. User removes a transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport.slice(1), | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('useNewTransactions with transactions in cache', () => { | ||
| const transactionsAlreadyInReport = [ | ||
| {transactionID: '2', amount: 200, created: '2023-10-02', currency: 'USD', reportID: 'report1', merchant: ''}, | ||
| {transactionID: '3', amount: 300, created: '2023-10-03', currency: 'USD', reportID: 'report1', merchant: ''}, | ||
| ]; | ||
| const newTransaction = {transactionID: '1', amount: 100, created: '2023-10-01T00:00:00Z', currency: 'USD', reportID: 'report1', merchant: ''}; | ||
|
|
||
| it("doesn't return new transactions when no transactions are added", () => { | ||
| // 1. Report and transactions data is loaded from Onyx | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| // 2. Report is loaded and transactions data is loaded, but there were no new transactions | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns new transactions when newly added transactions come from initial Report load', () => { | ||
| // 1. Report and transactions data is loaded from Onyx | ||
| const {rerender, result} = renderHook((props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. New transaction comes in when report is loaded | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [...transactionsAlreadyInReport, newTransaction], | ||
| }); | ||
| expect(result.current).toEqual([newTransaction]); | ||
| }); | ||
|
|
||
| it('returns new transactions when transactions are added after initial load', () => { | ||
| // 1. Report and transactions data is loaded from Onyx | ||
| const {rerender, result} = renderHook((props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. Report is loaded and transactions data is loaded, but there were no new transactions | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. User added new transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [...transactionsAlreadyInReport, newTransaction], | ||
| }); | ||
| expect(result.current).toEqual([newTransaction]); | ||
| }); | ||
|
|
||
| it('returns new transactions when adding transactions to empty report', async () => { | ||
| // 1. Report and transactions data is loaded from Onyx | ||
| const {rerender, result} = renderHook<Transaction[], {transactions: Transaction[]; hasOnceLoadedReportActions: boolean}>( | ||
| (props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), | ||
| { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| // 2. Report is loaded and it has no transactions, so there are no further rerenders | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [], | ||
| }); | ||
| await delay(1000); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. User added new transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: [newTransaction], | ||
| }); | ||
| expect(result.current).toEqual([newTransaction]); | ||
| }); | ||
|
|
||
| it('returns no new transactions when transactions are removed', () => { | ||
| // 1. Report and transactions data is loaded from Onyx | ||
| const {rerender, result} = renderHook((props) => useNewTransactions(props.hasOnceLoadedReportActions, props.transactions), { | ||
| initialProps: { | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 2. Report is loaded and transactions data is loaded, but there were no new transactions | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport, | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
|
|
||
| // 3. User removes a transaction | ||
| rerender({ | ||
| hasOnceLoadedReportActions: true, | ||
| transactions: transactionsAlreadyInReport.slice(1), | ||
| }); | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
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.