|
| 1 | +import {renderHook} from '@testing-library/react-native'; |
| 2 | +import useLoadReportActions from '@hooks/useLoadReportActions'; |
| 3 | +import type Navigation from '@libs/Navigation/Navigation'; |
| 4 | + |
| 5 | +jest.mock('@hooks/useNetwork', () => jest.fn(() => ({isOffline: false}))); |
| 6 | + |
| 7 | +jest.mock('@react-navigation/native', () => { |
| 8 | + const actualNav = jest.requireActual<typeof Navigation>('@react-navigation/native'); |
| 9 | + return { |
| 10 | + ...actualNav, |
| 11 | + useNavigationState: () => true, |
| 12 | + useRoute: jest.fn(), |
| 13 | + useFocusEffect: jest.fn(), |
| 14 | + useIsFocused: () => true, |
| 15 | + useNavigation: () => ({ |
| 16 | + navigate: jest.fn(), |
| 17 | + addListener: jest.fn(), |
| 18 | + }), |
| 19 | + createNavigationContainerRef: jest.fn(), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe('useLoadReportActions', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.clearAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + // Base test data from your example |
| 29 | + const baseProps = { |
| 30 | + reportID: '6549335221793525', |
| 31 | + reportActions: [ |
| 32 | + /* your 4 reportActions array here */ |
| 33 | + ], |
| 34 | + allReportActionIDs: ['8759152536123291182', '2034215190990675144', '186758379215594799'], |
| 35 | + transactionThreadReport: undefined, |
| 36 | + hasOlderActions: true, |
| 37 | + hasNewerActions: false, |
| 38 | + }; |
| 39 | + |
| 40 | + describe('Base cases', () => { |
| 41 | + test('correctly identifies current report actions', () => { |
| 42 | + jest.doMock('@userActions/Report', () => ({ |
| 43 | + getNewerActions: (_: string | undefined, reportActionID: string | undefined) => { |
| 44 | + expect(reportActionID).toBe('186758379215594799'); |
| 45 | + }, |
| 46 | + getOlderActions: (_: string | undefined, reportActionID: string | undefined) => { |
| 47 | + expect(reportActionID).toBe('8759152536123291182'); |
| 48 | + }, |
| 49 | + })); |
| 50 | + const {result} = renderHook(() => useLoadReportActions(baseProps)); |
| 51 | + |
| 52 | + result.current.loadOlderChats(); |
| 53 | + result.current.loadNewerChats(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('handles transaction thread report actions', () => { |
| 57 | + jest.doMock('@userActions/Report', () => ({ |
| 58 | + getNewerActions: (_: string | undefined, reportActionID: string | undefined) => { |
| 59 | + expect(reportActionID).toBe('186758379215594799'); |
| 60 | + }, |
| 61 | + getOlderActions: (_: string | undefined, reportActionID: string | undefined) => { |
| 62 | + expect(reportActionID).toBe('2034215190990675144'); |
| 63 | + }, |
| 64 | + })); |
| 65 | + |
| 66 | + const propsWithTransaction = { |
| 67 | + ...baseProps, |
| 68 | + transactionThreadReport: {reportID: '186758379215594798'}, |
| 69 | + allReportActionIDs: ['8759152536123291182'], // Only first action belongs to main report |
| 70 | + }; |
| 71 | + |
| 72 | + const {result} = renderHook(() => useLoadReportActions(propsWithTransaction)); |
| 73 | + |
| 74 | + result.current.loadOlderChats(); |
| 75 | + result.current.loadNewerChats(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('loadOlderChats behavior', () => { |
| 80 | + test('loads older actions for current report', () => { |
| 81 | + jest.doMock('@userActions/Report', () => ({ |
| 82 | + getNewerActions: jest.fn(), |
| 83 | + getOlderActions: (reportID: string | undefined, reportActionID: string | undefined) => { |
| 84 | + expect(reportID).toBe('6549335221793525'); |
| 85 | + expect(reportActionID).toBe('186758379215594799'); |
| 86 | + }, |
| 87 | + })); |
| 88 | + const {result} = renderHook(() => useLoadReportActions(baseProps)); |
| 89 | + result.current.loadOlderChats(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('loads actions for both reports when transaction thread exists', () => { |
| 93 | + jest.doMock('@userActions/Report', () => ({ |
| 94 | + getNewerActions: jest.fn(), |
| 95 | + getOlderActions: (reportID: string | undefined, reportActionID: string | undefined) => { |
| 96 | + if (reportID !== 'TRANSACTION_THREAD_REPORT') { |
| 97 | + expect(reportID).toBe('8759152536123291182'); |
| 98 | + expect(reportActionID).toBe('6549335221793525'); |
| 99 | + } else { |
| 100 | + expect(reportID).toBe('TRANSACTION_THREAD_REPORT'); |
| 101 | + expect(reportActionID).toBe('186758379215594799'); |
| 102 | + } |
| 103 | + }, |
| 104 | + })); |
| 105 | + const props = { |
| 106 | + ...baseProps, |
| 107 | + transactionThreadReport: {reportID: 'TRANSACTION_THREAD_REPORT'}, |
| 108 | + }; |
| 109 | + |
| 110 | + const {result} = renderHook(() => useLoadReportActions(props)); |
| 111 | + result.current.loadOlderChats(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('loadNewerChats behavior', () => { |
| 116 | + test('loads newer actions when conditions met', () => { |
| 117 | + jest.doMock('@userActions/Report', () => ({ |
| 118 | + getNewerActions: (reportID: string | undefined, reportActionID: string | undefined) => { |
| 119 | + expect(reportID).toBe('6549335221793525'); |
| 120 | + expect(reportActionID).toBe('8759152536123291182'); |
| 121 | + }, |
| 122 | + getOlderActions: jest.fn(), |
| 123 | + })); |
| 124 | + const props = { |
| 125 | + ...baseProps, |
| 126 | + hasNewerActions: true, |
| 127 | + reportActionID: 'EXISTING_ACTION_ID', |
| 128 | + }; |
| 129 | + |
| 130 | + const {result} = renderHook(() => useLoadReportActions(props)); |
| 131 | + result.current.loadNewerChats(); |
| 132 | + }); |
| 133 | + |
| 134 | + test('handles transaction thread newer actions', () => { |
| 135 | + jest.doMock('@userActions/Report', () => ({ |
| 136 | + getNewerActions: (reportID: string | undefined, reportActionID: string | undefined) => { |
| 137 | + if (reportID !== 'TRANSACTION_THREAD_REPORT') { |
| 138 | + expect(reportID).toBe('6549335221793525'); |
| 139 | + expect(reportActionID).toBe('8759152536123291182'); |
| 140 | + } else { |
| 141 | + expect(reportID).toBe('TRANSACTION_THREAD_REPORT'); |
| 142 | + expect(reportActionID).toBe('2034215190990675144'); |
| 143 | + } |
| 144 | + }, |
| 145 | + getOlderActions: jest.fn(), |
| 146 | + })); |
| 147 | + const props = { |
| 148 | + ...baseProps, |
| 149 | + transactionThreadReport: {reportID: 'TRANSACTION_THREAD_REPORT'}, |
| 150 | + hasNewerActions: true, |
| 151 | + }; |
| 152 | + |
| 153 | + const {result} = renderHook(() => useLoadReportActions(props)); |
| 154 | + result.current.loadNewerChats(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('Edge cases', () => { |
| 159 | + test('handles empty reportActions', () => { |
| 160 | + const mockGetOlderActions = jest.fn(); |
| 161 | + const mockGetNewerActions = jest.fn(); |
| 162 | + jest.doMock('@userActions/Report', () => ({ |
| 163 | + getNewerActions: mockGetNewerActions, |
| 164 | + getOlderActions: mockGetOlderActions, |
| 165 | + })); |
| 166 | + const props = { |
| 167 | + ...baseProps, |
| 168 | + reportActions: [], |
| 169 | + }; |
| 170 | + |
| 171 | + const {result} = renderHook(() => useLoadReportActions(props)); |
| 172 | + result.current.loadOlderChats(); |
| 173 | + result.current.loadNewerChats(); |
| 174 | + |
| 175 | + expect(mockGetOlderActions).not.toHaveBeenCalled(); |
| 176 | + expect(mockGetNewerActions).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments