|
| 1 | +import {fireEvent, render, screen} from '@testing-library/react-native'; |
| 2 | +import React from 'react'; |
| 3 | +import Onyx from 'react-native-onyx'; |
| 4 | +import ComposeProviders from '@components/ComposeProviders'; |
| 5 | +import {LocaleContextProvider} from '@components/LocaleContextProvider'; |
| 6 | +import OnboardingHelpDropdownButton from '@components/OnboardingHelpDropdownButton'; |
| 7 | +import OnyxProvider from '@components/OnyxProvider'; |
| 8 | +import {openExternalLink} from '@libs/actions/Link'; |
| 9 | +import {cancelBooking, clearBookingDraft, rescheduleBooking} from '@libs/actions/ScheduleCall'; |
| 10 | +import Navigation from '@libs/Navigation/Navigation'; |
| 11 | +import CONST from '@src/CONST'; |
| 12 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 13 | +import ROUTES from '@src/ROUTES'; |
| 14 | +import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; |
| 15 | +import waitForBatchedUpdatesWithAct from '../../utils/waitForBatchedUpdatesWithAct'; |
| 16 | + |
| 17 | +// Mock the dependencies |
| 18 | +jest.mock('@libs/actions/Link', () => ({ |
| 19 | + openExternalLink: jest.fn(), |
| 20 | +})); |
| 21 | +jest.mock('@libs/Navigation/Navigation', () => ({ |
| 22 | + navigate: jest.fn(), |
| 23 | +})); |
| 24 | +jest.mock('@libs/actions/ScheduleCall', () => ({ |
| 25 | + clearBookingDraft: jest.fn(), |
| 26 | + rescheduleBooking: jest.fn(), |
| 27 | + cancelBooking: jest.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +const mockOpenExternalLink = jest.mocked(openExternalLink); |
| 31 | +const mockNavigate = jest.mocked(Navigation.navigate); |
| 32 | +const mockClearBookingDraft = jest.mocked(clearBookingDraft); |
| 33 | +const mockRescheduleBooking = jest.mocked(rescheduleBooking); |
| 34 | +const mockCancelBooking = jest.mocked(cancelBooking); |
| 35 | + |
| 36 | +// Helper function to create mock events for PopoverMenuItem fireEvent.press |
| 37 | +function createMockPressEvent(target: unknown) { |
| 38 | + return { |
| 39 | + nativeEvent: {}, |
| 40 | + type: 'press', |
| 41 | + target, |
| 42 | + currentTarget: target, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +// Helper function to render OnboardingHelpDropdownButton with props |
| 47 | +function renderOnboardingHelpDropdownButton(props: { |
| 48 | + reportID: string; |
| 49 | + shouldUseNarrowLayout: boolean; |
| 50 | + shouldShowRegisterForWebinar: boolean; |
| 51 | + shouldShowGuideBooking: boolean; |
| 52 | + hasActiveScheduledCall: boolean; |
| 53 | +}) { |
| 54 | + return render( |
| 55 | + <ComposeProviders components={[OnyxProvider, LocaleContextProvider]}> |
| 56 | + <OnboardingHelpDropdownButton |
| 57 | + reportID={props.reportID} |
| 58 | + shouldUseNarrowLayout={props.shouldUseNarrowLayout} |
| 59 | + shouldShowRegisterForWebinar={props.shouldShowRegisterForWebinar} |
| 60 | + shouldShowGuideBooking={props.shouldShowGuideBooking} |
| 61 | + hasActiveScheduledCall={props.hasActiveScheduledCall} |
| 62 | + /> |
| 63 | + </ComposeProviders>, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +const mockScheduledCall = { |
| 68 | + eventTime: '2025-07-05 10:00:00', |
| 69 | + id: 'call-id-123', |
| 70 | + status: CONST.SCHEDULE_CALL_STATUS.CREATED, |
| 71 | + host: 123, |
| 72 | + eventURI: 'test-uri', |
| 73 | + inserted: '2025-07-04 09:00:00', |
| 74 | +}; |
| 75 | +const currentUserAccountID = 1; |
| 76 | + |
| 77 | +describe('OnboardingHelpDropdownButton', () => { |
| 78 | + beforeAll(() => { |
| 79 | + Onyx.init({ |
| 80 | + keys: ONYXKEYS, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + beforeEach(() => { |
| 85 | + Onyx.merge(ONYXKEYS.SESSION, {accountID: currentUserAccountID}); |
| 86 | + return waitForBatchedUpdates(); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + jest.clearAllMocks(); |
| 91 | + Onyx.clear(); |
| 92 | + return waitForBatchedUpdates(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should display the schedule call option when guide booking is enabled', async () => { |
| 96 | + // Given component configured to show schedule call option only |
| 97 | + const props = { |
| 98 | + reportID: '1', |
| 99 | + shouldUseNarrowLayout: false, |
| 100 | + shouldShowRegisterForWebinar: false, |
| 101 | + shouldShowGuideBooking: true, |
| 102 | + hasActiveScheduledCall: false, |
| 103 | + }; |
| 104 | + |
| 105 | + // When component is rendered |
| 106 | + renderOnboardingHelpDropdownButton(props); |
| 107 | + await waitForBatchedUpdatesWithAct(); |
| 108 | + |
| 109 | + // Then only schedule call option is visible |
| 110 | + const scheduleCallOption = screen.getByText('getAssistancePage.scheduleACall'); |
| 111 | + expect(scheduleCallOption).toBeOnTheScreen(); |
| 112 | + expect(screen.queryByText('getAssistancePage.registerForWebinar')).not.toBeOnTheScreen(); |
| 113 | + expect(screen.queryByText('common.reschedule')).not.toBeOnTheScreen(); |
| 114 | + expect(screen.queryByText('common.cancel')).not.toBeOnTheScreen(); |
| 115 | + |
| 116 | + // When schedule call option is pressed |
| 117 | + fireEvent.press(scheduleCallOption); |
| 118 | + |
| 119 | + // Then booking draft is cleared and navigation occurs |
| 120 | + expect(mockClearBookingDraft).toHaveBeenCalledTimes(1); |
| 121 | + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.SCHEDULE_CALL_BOOK.getRoute(props.reportID)); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should only display the registerForWebinar option when webinar is enabled', async () => { |
| 125 | + // Given component configured to display the registerForWebinar |
| 126 | + const props = { |
| 127 | + reportID: '1', |
| 128 | + shouldUseNarrowLayout: false, |
| 129 | + shouldShowRegisterForWebinar: true, |
| 130 | + shouldShowGuideBooking: false, |
| 131 | + hasActiveScheduledCall: false, |
| 132 | + }; |
| 133 | + |
| 134 | + // When component is rendered |
| 135 | + renderOnboardingHelpDropdownButton(props); |
| 136 | + await waitForBatchedUpdatesWithAct(); |
| 137 | + |
| 138 | + // Then only webinar registration option is visible |
| 139 | + const registerOption = screen.getByText('getAssistancePage.registerForWebinar'); |
| 140 | + expect(registerOption).toBeOnTheScreen(); |
| 141 | + expect(screen.queryByText('getAssistancePage.scheduleACall')).not.toBeOnTheScreen(); |
| 142 | + expect(screen.queryByText('common.reschedule')).not.toBeOnTheScreen(); |
| 143 | + expect(screen.queryByText('common.cancel')).not.toBeOnTheScreen(); |
| 144 | + |
| 145 | + // When webinar registration option is pressed |
| 146 | + fireEvent.press(registerOption); |
| 147 | + |
| 148 | + // Then webinar registration URL is opened |
| 149 | + expect(mockOpenExternalLink).toHaveBeenCalledTimes(1); |
| 150 | + expect(mockOpenExternalLink).toHaveBeenCalledWith(CONST.REGISTER_FOR_WEBINAR_URL); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should display dropdown menu with all options when user has active scheduled call', async () => { |
| 154 | + // Given component configured with active scheduled call and webinar registration |
| 155 | + const props = { |
| 156 | + reportID: '1', |
| 157 | + shouldUseNarrowLayout: false, |
| 158 | + shouldShowRegisterForWebinar: true, |
| 159 | + shouldShowGuideBooking: false, |
| 160 | + hasActiveScheduledCall: true, |
| 161 | + }; |
| 162 | + // Given scheduled call data exists in Onyx |
| 163 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${props.reportID}`, { |
| 164 | + calendlyCalls: [mockScheduledCall], |
| 165 | + }); |
| 166 | + |
| 167 | + // When component is rendered |
| 168 | + renderOnboardingHelpDropdownButton(props); |
| 169 | + await waitForBatchedUpdatesWithAct(); |
| 170 | + |
| 171 | + // Then dropdown button displays "Call scheduled" text |
| 172 | + const dropdownButton = screen.getByText('scheduledCall.callScheduled'); |
| 173 | + expect(dropdownButton).toBeOnTheScreen(); |
| 174 | + |
| 175 | + // When dropdown menu is opened |
| 176 | + fireEvent.press(dropdownButton); |
| 177 | + await waitForBatchedUpdatesWithAct(); |
| 178 | + |
| 179 | + // Then all expected menu options are present |
| 180 | + expect(screen.getByText('common.reschedule')).toBeOnTheScreen(); |
| 181 | + expect(screen.getByText('common.cancel')).toBeOnTheScreen(); |
| 182 | + expect(screen.getByText('getAssistancePage.registerForWebinar')).toBeOnTheScreen(); |
| 183 | + expect(screen.queryByText('getAssistancePage.scheduleACall')).not.toBeOnTheScreen(); |
| 184 | + }); |
| 185 | + |
| 186 | + describe('dropdown actions with active scheduled call', () => { |
| 187 | + // Given component configured with active scheduled call and webinar registration enabled |
| 188 | + const props = { |
| 189 | + reportID: '1', |
| 190 | + shouldUseNarrowLayout: false, |
| 191 | + shouldShowRegisterForWebinar: true, |
| 192 | + shouldShowGuideBooking: false, |
| 193 | + hasActiveScheduledCall: true, |
| 194 | + }; |
| 195 | + beforeEach(() => { |
| 196 | + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${props.reportID}`, { |
| 197 | + calendlyCalls: [mockScheduledCall], |
| 198 | + }); |
| 199 | + return waitForBatchedUpdates(); |
| 200 | + }); |
| 201 | + it('should open webinar registration URL when webinar option is pressed', async () => { |
| 202 | + // Given scheduled call data exists in Onyx |
| 203 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${props.reportID}`, { |
| 204 | + calendlyCalls: [mockScheduledCall], |
| 205 | + }); |
| 206 | + |
| 207 | + // When component is rendered and dropdown is opened |
| 208 | + renderOnboardingHelpDropdownButton(props); |
| 209 | + await waitForBatchedUpdatesWithAct(); |
| 210 | + |
| 211 | + const dropdownButton = screen.getByText('scheduledCall.callScheduled'); |
| 212 | + fireEvent.press(dropdownButton); |
| 213 | + await waitForBatchedUpdatesWithAct(); |
| 214 | + |
| 215 | + // When webinar menu item is pressed |
| 216 | + const webinarMenuItem = screen.getByText('getAssistancePage.registerForWebinar'); |
| 217 | + fireEvent.press(webinarMenuItem, createMockPressEvent(webinarMenuItem)); |
| 218 | + await waitForBatchedUpdatesWithAct(); |
| 219 | + |
| 220 | + // Then webinar registration URL is opened |
| 221 | + expect(mockOpenExternalLink).toHaveBeenCalledTimes(1); |
| 222 | + expect(mockOpenExternalLink).toHaveBeenCalledWith(CONST.REGISTER_FOR_WEBINAR_URL); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should call reschedule booking when reschedule option is pressed', async () => { |
| 226 | + // When component is rendered and dropdown is opened |
| 227 | + renderOnboardingHelpDropdownButton(props); |
| 228 | + await waitForBatchedUpdatesWithAct(); |
| 229 | + |
| 230 | + const dropdownButton = screen.getByText('scheduledCall.callScheduled'); |
| 231 | + fireEvent.press(dropdownButton); |
| 232 | + await waitForBatchedUpdatesWithAct(); |
| 233 | + |
| 234 | + // When reschedule option is pressed |
| 235 | + const rescheduleMenuItem = screen.getByText('common.reschedule'); |
| 236 | + fireEvent.press(rescheduleMenuItem, createMockPressEvent(rescheduleMenuItem)); |
| 237 | + await waitForBatchedUpdatesWithAct(); |
| 238 | + |
| 239 | + // Then reschedule booking action is called with scheduled call data |
| 240 | + expect(mockRescheduleBooking).toHaveBeenCalledTimes(1); |
| 241 | + expect(mockRescheduleBooking).toHaveBeenCalledWith(mockScheduledCall); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should call cancel booking when cancel option is pressed', async () => { |
| 245 | + // When component is rendered and dropdown is opened |
| 246 | + renderOnboardingHelpDropdownButton(props); |
| 247 | + await waitForBatchedUpdatesWithAct(); |
| 248 | + |
| 249 | + const dropdownButton = screen.getByText('scheduledCall.callScheduled'); |
| 250 | + fireEvent.press(dropdownButton); |
| 251 | + await waitForBatchedUpdatesWithAct(); |
| 252 | + |
| 253 | + // When cancel option is pressed |
| 254 | + const cancelMenuItem = screen.getByText('common.cancel'); |
| 255 | + fireEvent.press(cancelMenuItem, createMockPressEvent(cancelMenuItem)); |
| 256 | + await waitForBatchedUpdatesWithAct(); |
| 257 | + |
| 258 | + // Then cancel booking action is called with scheduled call data |
| 259 | + expect(mockCancelBooking).toHaveBeenCalledTimes(1); |
| 260 | + expect(mockCancelBooking).toHaveBeenCalledWith(mockScheduledCall); |
| 261 | + }); |
| 262 | + }); |
| 263 | +}); |
0 commit comments