1515 */
1616
1717import React from 'react' ;
18- import { fireEvent , render , screen } from '@testing-library/react' ;
18+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
1919import { PhoneNumberStep } from '../PhoneNumberStep' ;
2020import { E164Number } from 'libphonenumber-js/types' ;
2121import { Country } from 'react-phone-number-input' ;
2222import { parsePhoneNumber } from 'libphonenumber-js/min' ;
2323import { PhoneNumber } from 'libphonenumber-js' ;
24+ import * as eddlUtils from '../../../../utils/eddl-utils' ;
25+
26+ // Mock the useTrackAnalytics hook
27+ jest . mock ( '../../../../utils/eddl-utils' , ( ) => ( {
28+ ...jest . requireActual ( '../../../../utils/eddl-utils' ) ,
29+ useTrackAnalytics : jest . fn ( ) ,
30+ } ) ) ;
2431
2532// Mock the props interface if it's not exported from the component file
2633declare module '../PhoneNumberStep' {
@@ -43,10 +50,16 @@ describe('PhoneNumberStep', () => {
4350 const mockHandleClose = jest . fn ( ) ;
4451 const mockHandlePhoneNumberSubmit = jest . fn ( ) ;
4552 const mockSetCountry = jest . fn ( ) ;
53+ const mockTrackAnalytics = jest . fn ( ) ;
4654 const phoneNumber = parsePhoneNumber ( ' 8 (800) 555-35-35 ' , 'RU' ) ;
4755
4856 beforeEach ( ( ) => {
4957 jest . clearAllMocks ( ) ;
58+ // Mock the useTrackAnalytics hook to return a mock function
59+ mockTrackAnalytics . mockResolvedValue ( undefined ) ; // Make it async
60+ ( eddlUtils . useTrackAnalytics as jest . Mock ) . mockReturnValue (
61+ mockTrackAnalytics ,
62+ ) ;
5063 } ) ;
5164
5265 function renderComponent ( inputPhoneNumber : PhoneNumber , error ?: string ) {
@@ -86,37 +99,49 @@ describe('PhoneNumberStep', () => {
8699 expect ( closeButton ) . toHaveLength ( 1 ) ;
87100 } ) ;
88101
89- test ( 'should submit phone number when clicking send code button' , ( ) => {
102+ test ( 'should submit phone number when clicking send code button' , async ( ) => {
90103 renderComponent ( phoneNumber ) ;
91104 // Find and click send code button
92105 const submitPhoneNumberButton = screen . getByRole ( 'button' , {
93106 name : / S e n d c o d e / i,
94107 } ) ;
95108 fireEvent . click ( submitPhoneNumberButton ) ;
96- expect ( mockHandlePhoneNumberSubmit ) . toHaveBeenCalled ( ) ;
109+
110+ // Wait for the async tracking call to complete
111+ await waitFor ( ( ) => {
112+ expect ( mockHandlePhoneNumberSubmit ) . toHaveBeenCalled ( ) ;
113+ } ) ;
97114 } ) ;
98115
99- test ( 'should show an error when phone number is invalid' , ( ) => {
116+ test ( 'should show an error when phone number is invalid' , async ( ) => {
100117 const invalidPhoneNumber = parsePhoneNumber ( ' 8 (800) xxxx ' , 'RU' ) ;
101118 renderComponent ( invalidPhoneNumber , 'invalid phone number error' ) ;
102119 // Find and click send code
103120 const submitPhoneNumberButton = screen . getByRole ( 'button' , {
104121 name : / S e n d c o d e / i,
105122 } ) ;
106123 fireEvent . click ( submitPhoneNumberButton ) ;
107- // submit the phone number to backend
108- expect ( mockHandlePhoneNumberSubmit ) . toHaveBeenCalled ( ) ;
124+
125+ // Wait for the async tracking call to complete
126+ await waitFor ( ( ) => {
127+ expect ( mockHandlePhoneNumberSubmit ) . toHaveBeenCalled ( ) ;
128+ } ) ;
129+
109130 // expect mock error from backend to be displayed
110131 expect ( screen . getByText ( 'invalid phone number error' ) ) . toBeInTheDocument ( ) ;
111132 // submit button should be enabled so user can retry with new number
112133 expect ( screen . getByText ( / S e n d c o d e / i) . closest ( 'button' ) ) . toBeEnabled ( ) ;
113134 } ) ;
114135
115- test ( 'closes the modal when the close button is clicked' , ( ) => {
136+ test ( 'closes the modal when the close button is clicked' , async ( ) => {
116137 renderComponent ( phoneNumber ) ;
117138 const closeButton = screen . getByRole ( 'button' , { name : / C a n c e l / i } ) ;
118139 fireEvent . click ( closeButton ) ;
119- expect ( mockHandleClose ) . toHaveBeenCalled ( ) ;
140+
141+ // Wait for the async tracking call to complete
142+ await waitFor ( ( ) => {
143+ expect ( mockHandleClose ) . toHaveBeenCalled ( ) ;
144+ } ) ;
120145 } ) ;
121146
122147 test ( 'should not have a default country when no country is provided' , ( ) => {
0 commit comments