11/// <reference types="@types/jest" />
22
3- import DailyIframe , {
4- DailyCall ,
5- DailyEvent ,
6- DailyEventObject ,
7- } from '@daily-co/daily-js' ;
3+ import DailyIframe , { DailyCall } from '@daily-co/daily-js' ;
84import { act , renderHook } from '@testing-library/react-hooks' ;
95import React from 'react' ;
106
117import { DailyProvider } from '../../src/DailyProvider' ;
128import { useLocalParticipant } from '../../src/hooks/useLocalParticipant' ;
9+ import * as useParticipantModule from '../../src/hooks/useParticipant' ;
1310
1411const createWrapper =
1512 ( callObject : DailyCall = DailyIframe . createCallObject ( ) ) : React . FC =>
@@ -30,6 +27,7 @@ describe('useLocalParticipant', () => {
3027 const daily = DailyIframe . createCallObject ( ) ;
3128 ( daily . participants as jest . Mock ) . mockImplementation ( ( ) => ( {
3229 local : {
30+ local : true ,
3331 session_id : 'local' ,
3432 user_name : '' ,
3533 } ,
@@ -39,45 +37,33 @@ describe('useLocalParticipant', () => {
3937 } ) ;
4038 await waitFor ( ( ) => {
4139 expect ( result . current ) . toEqual ( {
40+ local : true ,
4241 session_id : 'local' ,
4342 user_name : '' ,
4443 } ) ;
4544 } ) ;
4645 } ) ;
47- it ( 'loaded event triggers a 1s timeout to init state' , async ( ) => {
48- jest . useFakeTimers ( ) ;
49- const setTimeoutSpy = jest . spyOn ( window , 'setTimeout' ) ;
46+ it ( 'participant-updated event inits state and calls useParticipant with session_id' , async ( ) => {
5047 const daily = DailyIframe . createCallObject ( ) ;
51- const { result, waitFor } = renderHook ( ( ) => useLocalParticipant ( ) , {
48+ const spy = jest . spyOn ( useParticipantModule , 'useParticipant' ) ;
49+ const { waitFor } = renderHook ( ( ) => useLocalParticipant ( ) , {
5250 wrapper : createWrapper ( daily ) ,
5351 } ) ;
54- const event : DailyEvent = 'loaded' ;
55- const payload : DailyEventObject = {
56- action : event ,
57- } ;
58- act ( ( ) => {
59- // @ts -ignore
60- daily . emit ( event , payload ) ;
61- } ) ;
62- await waitFor ( ( ) => {
63- expect ( setTimeoutSpy ) . toBeCalledWith ( expect . any ( Function ) , 1000 ) ;
64- expect ( result . current ) . toBeNull ( ) ;
65- } ) ;
66- ( daily . participants as jest . Mock ) . mockImplementation ( ( ) => ( {
67- local : {
52+ const action = 'participant-updated' ;
53+ const payload = {
54+ action,
55+ participant : {
56+ local : true ,
6857 session_id : 'local' ,
6958 user_name : '' ,
7059 } ,
71- } ) ) ;
60+ } ;
7261 act ( ( ) => {
73- jest . runAllTimers ( ) ;
62+ // @ts -ignore
63+ daily . emit ( 'participant-updated' , payload ) ;
7464 } ) ;
7565 await waitFor ( ( ) => {
76- expect ( result . current ) . toEqual ( {
77- session_id : 'local' ,
78- user_name : '' ,
79- } ) ;
66+ expect ( spy ) . toHaveBeenCalledWith ( 'local' ) ;
8067 } ) ;
81- jest . useRealTimers ( ) ;
8268 } ) ;
8369} ) ;
0 commit comments