|
| 1 | +import Onyx from 'react-native-onyx'; |
| 2 | +import {getIsP2PForAmount, getReportOrReportDraftForAmount} from '@pages/iou/request/step/AmountSubmission'; |
| 3 | +import CONST from '@src/CONST'; |
| 4 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 5 | +import type {Report} from '@src/types/onyx'; |
| 6 | +import {createRandomReport} from '../utils/collections/reports'; |
| 7 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 8 | + |
| 9 | +const CURRENT_USER_ACCOUNT_ID = 5; |
| 10 | +const OTHER_USER_ACCOUNT_ID = 10; |
| 11 | + |
| 12 | +jest.mock('@src/libs/Navigation/Navigation', () => ({ |
| 13 | + navigate: jest.fn(), |
| 14 | + goBack: jest.fn(), |
| 15 | + navigationRef: { |
| 16 | + getCurrentRoute: jest.fn(() => undefined), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('AmountSubmission', () => { |
| 21 | + beforeAll(() => { |
| 22 | + Onyx.init({keys: ONYXKEYS}); |
| 23 | + return waitForBatchedUpdates(); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + await Onyx.clear(); |
| 28 | + await waitForBatchedUpdates(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('getReportOrReportDraftForAmount', () => { |
| 32 | + it('returns undefined when reportID is undefined', () => { |
| 33 | + expect(getReportOrReportDraftForAmount(undefined)).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns undefined when reportID is an empty string', () => { |
| 37 | + expect(getReportOrReportDraftForAmount('')).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns the report from COLLECTION.REPORT when it exists', async () => { |
| 41 | + const reportID = 'report-1'; |
| 42 | + const testReport: Report = {...createRandomReport(1, undefined), reportID}; |
| 43 | + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, testReport); |
| 44 | + await waitForBatchedUpdates(); |
| 45 | + |
| 46 | + const result = getReportOrReportDraftForAmount(reportID); |
| 47 | + expect(result?.reportID).toBe(reportID); |
| 48 | + }); |
| 49 | + |
| 50 | + it('falls back to COLLECTION.REPORT_DRAFT when not in REPORT', async () => { |
| 51 | + const reportID = 'draft-1'; |
| 52 | + const draftReport: Report = {...createRandomReport(2, undefined), reportID}; |
| 53 | + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`, draftReport); |
| 54 | + await waitForBatchedUpdates(); |
| 55 | + |
| 56 | + const result = getReportOrReportDraftForAmount(reportID); |
| 57 | + expect(result?.reportID).toBe(reportID); |
| 58 | + }); |
| 59 | + |
| 60 | + it('prefers COLLECTION.REPORT over COLLECTION.REPORT_DRAFT when both have the reportID', async () => { |
| 61 | + const reportID = 'both-1'; |
| 62 | + const realReport: Report = {...createRandomReport(3, undefined), reportID, reportName: 'real'}; |
| 63 | + const draftReport: Report = {...createRandomReport(4, undefined), reportID, reportName: 'draft'}; |
| 64 | + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, realReport); |
| 65 | + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`, draftReport); |
| 66 | + await waitForBatchedUpdates(); |
| 67 | + |
| 68 | + const result = getReportOrReportDraftForAmount(reportID); |
| 69 | + expect(result?.reportName).toBe('real'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns undefined when neither collection has the reportID', () => { |
| 73 | + expect(getReportOrReportDraftForAmount('nonexistent')).toBeUndefined(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('getIsP2PForAmount', () => { |
| 78 | + it('returns true for a P2P chat with another participant', () => { |
| 79 | + const p2pChat: Report = { |
| 80 | + ...createRandomReport(11, undefined), |
| 81 | + participants: { |
| 82 | + [CURRENT_USER_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, |
| 83 | + [OTHER_USER_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + const result = getIsP2PForAmount({chatReportForP2P: p2pChat, currentUserAccountID: CURRENT_USER_ACCOUNT_ID}); |
| 88 | + expect(result).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns false for a self-DM', () => { |
| 92 | + const selfDMChat: Report = createRandomReport(12, CONST.REPORT.CHAT_TYPE.SELF_DM); |
| 93 | + |
| 94 | + const result = getIsP2PForAmount({chatReportForP2P: selfDMChat, currentUserAccountID: CURRENT_USER_ACCOUNT_ID}); |
| 95 | + expect(result).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns false for a policy expense chat', () => { |
| 99 | + const policyExpenseChat: Report = createRandomReport(13, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); |
| 100 | + |
| 101 | + const result = getIsP2PForAmount({chatReportForP2P: policyExpenseChat, currentUserAccountID: CURRENT_USER_ACCOUNT_ID}); |
| 102 | + expect(result).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns false when chatReportForP2P is undefined', () => { |
| 106 | + const result = getIsP2PForAmount({chatReportForP2P: undefined, currentUserAccountID: CURRENT_USER_ACCOUNT_ID}); |
| 107 | + expect(result).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns false when the chat report only contains the current user as participant', () => { |
| 111 | + const soloReport: Report = { |
| 112 | + ...createRandomReport(14, undefined), |
| 113 | + participants: { |
| 114 | + [CURRENT_USER_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + const result = getIsP2PForAmount({chatReportForP2P: soloReport, currentUserAccountID: CURRENT_USER_ACCOUNT_ID}); |
| 119 | + expect(result).toBe(false); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments