@@ -6,7 +6,7 @@ Please see LICENSE in the repository root for full details.
66*/
77
88import { beforeEach , expect , type MockedFunction , test , vitest } from "vitest" ;
9- import { render } from "@testing-library/react" ;
9+ import { render , waitFor } from "@testing-library/react" ;
1010import { type MatrixClient } from "matrix-js-sdk/src/client" ;
1111import { type MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc" ;
1212import { of } from "rxjs" ;
@@ -20,6 +20,7 @@ import { prefetchSounds } from "../soundUtils";
2020import { useAudioContext } from "../useAudioContext" ;
2121import { ActiveCall } from "./InCallView" ;
2222import {
23+ flushPromises ,
2324 mockMatrixRoom ,
2425 mockMatrixRoomMember ,
2526 mockRtcMembership ,
@@ -51,13 +52,13 @@ const carol = mockMatrixRoomMember(localRtcMember);
5152const roomMembers = new Map ( [ carol ] . map ( ( p ) => [ p . userId , p ] ) ) ;
5253
5354const roomId = "!foo:bar" ;
54- const soundPromise = Promise . resolve ( true ) ;
5555
5656beforeEach ( ( ) => {
57+ vitest . clearAllMocks ( ) ;
5758 ( prefetchSounds as MockedFunction < typeof prefetchSounds > ) . mockResolvedValue ( {
5859 sound : new ArrayBuffer ( 0 ) ,
5960 } ) ;
60- playSound = vitest . fn ( ) . mockReturnValue ( soundPromise ) ;
61+ playSound = vitest . fn ( ) ;
6162 ( useAudioContext as MockedFunction < typeof useAudioContext > ) . mockReturnValue ( {
6263 playSound,
6364 } ) ;
@@ -136,8 +137,15 @@ test("will play a leave sound asynchronously in SPA mode", async () => {
136137 const leaveButton = getByText ( "Leave" ) ;
137138 await user . click ( leaveButton ) ;
138139 expect ( playSound ) . toHaveBeenCalledWith ( "left" ) ;
139- expect ( leaveRTCSession ) . toHaveBeenCalledWith ( rtcSession , undefined ) ;
140+ expect ( leaveRTCSession ) . toHaveBeenCalledWith (
141+ rtcSession ,
142+ "user" ,
143+ expect . any ( Promise ) ,
144+ ) ;
140145 expect ( rtcSession . leaveRoomSession ) . toHaveBeenCalledOnce ( ) ;
146+ // Ensure that the playSound promise resolves within this test to avoid
147+ // impacting the results of other tests
148+ await waitFor ( ( ) => expect ( leaveRTCSession ) . toHaveResolved ( ) ) ;
141149} ) ;
142150
143151test ( "will play a leave sound synchronously in widget mode" , async ( ) => {
@@ -148,12 +156,31 @@ test("will play a leave sound synchronously in widget mode", async () => {
148156 } as Partial < WidgetHelpers [ "api" ] > ,
149157 lazyActions : new LazyEventEmitter ( ) ,
150158 } ;
159+ let resolvePlaySound : ( ) => void ;
160+ playSound = vitest
161+ . fn ( )
162+ . mockReturnValue (
163+ new Promise < void > ( ( resolve ) => ( resolvePlaySound = resolve ) ) ,
164+ ) ;
165+ ( useAudioContext as MockedFunction < typeof useAudioContext > ) . mockReturnValue ( {
166+ playSound,
167+ } ) ;
168+
151169 const { getByText, rtcSession } = createGroupCallView (
152170 widget as WidgetHelpers ,
153171 ) ;
154172 const leaveButton = getByText ( "Leave" ) ;
155173 await user . click ( leaveButton ) ;
174+ await flushPromises ( ) ;
175+ expect ( leaveRTCSession ) . not . toHaveResolved ( ) ;
176+ resolvePlaySound ! ( ) ;
177+ await flushPromises ( ) ;
178+
156179 expect ( playSound ) . toHaveBeenCalledWith ( "left" ) ;
157- expect ( leaveRTCSession ) . toHaveBeenCalledWith ( rtcSession , soundPromise ) ;
180+ expect ( leaveRTCSession ) . toHaveBeenCalledWith (
181+ rtcSession ,
182+ "user" ,
183+ expect . any ( Promise ) ,
184+ ) ;
158185 expect ( rtcSession . leaveRoomSession ) . toHaveBeenCalledOnce ( ) ;
159186} ) ;
0 commit comments