Skip to content

Commit 9f61e47

Browse files
committed
fix tests
1 parent 92ea459 commit 9f61e47

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

src/DailyParticipants.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
207207
evts.forEach((ev) => {
208208
switch (ev.action) {
209209
case 'active-speaker-change': {
210-
const sessionId = ev.activeSpeaker.peerId;
211-
set(activeIdState, sessionId);
212-
set(participantState(sessionId), (prev) => {
210+
set(activeIdState, ev.activeSpeaker.peerId);
211+
set(participantState(ev.activeSpeaker.peerId), (prev) => {
213212
if (!prev) return null;
214213
return {
215214
...prev,

test/components/DailyAudio.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ describe('DailyAudio', () => {
352352
});
353353
});
354354
it('replaces least recent speaker slot', async () => {
355+
jest.useFakeTimers();
355356
/**
356357
* Scenario: 4 participants to trigger sorting by last_active dates.
357358
* - Remote participant 1 becomes active speaker (subscribed)
@@ -392,19 +393,24 @@ describe('DailyAudio', () => {
392393
);
393394
act(() => emitJoinedMeeting(callObject, participants));
394395
act(() => emitStartedCamera(callObject));
395-
act(() => emitActiveSpeakerChange(callObject, remoteParticipants[0]));
396+
act(() => {
397+
emitActiveSpeakerChange(callObject, remoteParticipants[0]);
398+
jest.advanceTimersByTime(500);
399+
});
396400
await waitFor(() => {
397401
expect(queryAudioById(remoteParticipants[0], container)).not.toBeNull();
398402
});
399403
act(() => {
400404
emitActiveSpeakerChange(callObject, remoteParticipants[1]);
405+
jest.advanceTimersByTime(500);
401406
});
402407
await waitFor(() => {
403408
expect(queryAudioById(remoteParticipants[0], container)).not.toBeNull();
404409
expect(queryAudioById(remoteParticipants[1], container)).not.toBeNull();
405410
});
406411
act(() => {
407412
emitActiveSpeakerChange(callObject, remoteParticipants[2]);
413+
jest.advanceTimersByTime(500);
408414
});
409415
await waitFor(() => {
410416
expect(queryAudioById(remoteParticipants[0], container)).not.toBeNull();
@@ -413,13 +419,15 @@ describe('DailyAudio', () => {
413419
});
414420
act(() => {
415421
emitActiveSpeakerChange(callObject, remoteParticipants[3]);
422+
jest.advanceTimersByTime(500);
416423
});
417424
await waitFor(() => {
418425
expect(queryAudioById(remoteParticipants[0], container)).toBeNull();
419426
expect(queryAudioById(remoteParticipants[1], container)).not.toBeNull();
420427
expect(queryAudioById(remoteParticipants[2], container)).not.toBeNull();
421428
expect(queryAudioById(remoteParticipants[3], container)).not.toBeNull();
422429
});
430+
jest.useRealTimers();
423431
});
424432
});
425433
});

test/hooks/useActiveSpeakerId.test.tsx

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import React from 'react';
1010

1111
import { DailyProvider } from '../../src/DailyProvider';
1212
import { useActiveSpeakerId } from '../../src/hooks/useActiveSpeakerId';
13+
import {
14+
emitActiveSpeakerChange,
15+
emitJoinedMeeting,
16+
} from '../.test-utils/event-emitter';
17+
import { mockParticipant } from '../.test-utils/mocks';
1318

1419
jest.mock('../../src/DailyLiveStreaming', () => ({
1520
...jest.requireActual('../../src/DailyLiveStreaming'),
@@ -82,15 +87,19 @@ describe('useActiveSpeakerId', () => {
8287
});
8388
it('does not change returned participant, when ignoreLocal is set', async () => {
8489
const daily = DailyIframe.createCallObject();
90+
const local = mockParticipant({
91+
local: true,
92+
session_id: 'local',
93+
user_name: '',
94+
});
95+
const a = mockParticipant({
96+
local: false,
97+
session_id: 'a',
98+
user_name: 'Alpha',
99+
});
85100
(daily.participants as jest.Mock).mockImplementation(() => ({
86-
local: {
87-
session_id: 'local',
88-
user_name: '',
89-
},
90-
a: {
91-
session_id: 'a',
92-
user_name: 'Alpha',
93-
},
101+
local,
102+
a,
94103
}));
95104
const { result, waitFor } = renderHook(
96105
() =>
@@ -101,16 +110,14 @@ describe('useActiveSpeakerId', () => {
101110
wrapper: createWrapper(daily),
102111
}
103112
);
104-
const event: DailyEvent = 'active-speaker-change';
105-
const payload: DailyEventObjectActiveSpeakerChange = {
106-
action: event,
107-
activeSpeaker: {
108-
peerId: 'local',
109-
},
110-
};
111113
act(() => {
112-
// @ts-ignore
113-
daily.emit(event, payload);
114+
emitJoinedMeeting(daily, {
115+
local,
116+
a,
117+
});
118+
});
119+
act(() => {
120+
emitActiveSpeakerChange(daily, local.session_id);
114121
});
115122
await waitFor(() => {
116123
expect(result.current).toEqual(null);

test/hooks/useThrottledDailyEvent.test.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ describe('useThrottledDailyEvent', () => {
7979
// And useThrottledDailyEvent registers call-instance-destroyed listener itself.
8080
expect(daily.on).toHaveBeenCalledTimes(2 + 2);
8181
});
82-
it('calls callback once in a given throttle timeframe', async () => {
82+
it('calls callback at most twice in a given throttle timeframe', async () => {
8383
jest.useFakeTimers();
8484
const daily = DailyIframe.createCallObject();
85-
const callback = jest.fn();
85+
const receivedEvents: DailyEventObjectAppMessage[] = [];
86+
const callback = jest.fn((evts: DailyEventObjectAppMessage[]) => {
87+
receivedEvents.push(...evts);
88+
});
8689
const delay = 100;
8790
const { waitFor } = renderHook(
8891
() => {
@@ -107,8 +110,8 @@ describe('useThrottledDailyEvent', () => {
107110
});
108111
jest.advanceTimersByTime(delay);
109112
await waitFor(() => {
110-
expect(callback).toHaveBeenCalledTimes(1);
111-
expect(callback).toHaveBeenCalledWith(expect.arrayContaining(evts));
113+
expect(callback.mock.calls.length).toBeLessThanOrEqual(2);
114+
expect(receivedEvents).toEqual(evts);
112115
});
113116
jest.useRealTimers();
114117
});

0 commit comments

Comments
 (0)