Skip to content

Commit e36e9cb

Browse files
author
timmydoza
authored
Update useSelectedParticipant so that it listens to the room.participantDisconnected event (#339)
1 parent 7207826 commit e36e9cb

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
1-
import { act, renderHook } from '@testing-library/react-hooks';
1+
import { act, HookResult, renderHook, RenderHookResult } from '@testing-library/react-hooks';
22
import { EventEmitter } from 'events';
33
import React from 'react';
4-
import { Room } from 'twilio-video';
4+
import { Participant, Room } from 'twilio-video';
55
import useSelectedParticipant, { SelectedParticipantProvider } from './useSelectedParticipant';
66

77
describe('the useSelectedParticipant hook', () => {
88
let mockRoom: Room;
9+
let result: HookResult<readonly [Participant | null, (participant: Participant) => void]>;
10+
911
beforeEach(() => (mockRoom = new EventEmitter() as Room));
1012

11-
it('should return null as the default value', () => {
12-
const { result } = renderHook(useSelectedParticipant, {
13+
beforeEach(() => {
14+
({ result } = renderHook(useSelectedParticipant, {
1315
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
14-
});
16+
}));
17+
});
18+
19+
it('should return null as the default value', () => {
1520
expect(result.current[0]).toBe(null);
1621
});
1722

1823
it('should set a selected participant', () => {
19-
const { result } = renderHook(useSelectedParticipant, {
20-
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
21-
});
22-
2324
act(() => result.current[1]('mockParticipant' as any));
24-
2525
expect(result.current[0]).toBe('mockParticipant');
2626
});
2727

2828
it('should set "null" as the selected participant when the user selects the currently selected participant', () => {
29-
const { result } = renderHook(useSelectedParticipant, {
30-
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
31-
});
32-
3329
act(() => result.current[1]('mockParticipant' as any));
3430
act(() => result.current[1]('mockParticipant' as any));
3531

3632
expect(result.current[0]).toBe(null);
3733
});
3834

3935
it('should set "null" as the selected participant on room disconnect', () => {
40-
const { result } = renderHook(useSelectedParticipant, {
41-
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
42-
});
43-
4436
act(() => result.current[1]('mockParticipant' as any));
4537
expect(result.current[0]).toBe('mockParticipant');
4638
act(() => {
4739
mockRoom.emit('disconnected');
4840
});
4941
expect(result.current[0]).toBe(null);
5042
});
43+
44+
it('should set "null" as the selected participant when the participant disconnects from the room', () => {
45+
act(() => result.current[1]('mockParticipant' as any));
46+
act(() => {
47+
mockRoom.emit('participantDisconnected', 'mockParticipant');
48+
});
49+
expect(result.current[0]).toBe(null);
50+
});
51+
52+
it('should not set "null" as the selected participant when a non-selected participant disconnects from the room', () => {
53+
act(() => result.current[1]('mockParticipant' as any));
54+
act(() => {
55+
mockRoom.emit('participantDisconnected', 'otherMockParticipant');
56+
});
57+
expect(result.current[0]).toBe('mockParticipant');
58+
});
5159
});

src/components/VideoProvider/useSelectedParticipant/useSelectedParticipant.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ export function SelectedParticipantProvider({ room, children }: SelectedParticip
2222

2323
useEffect(() => {
2424
const onDisconnect = () => _setSelectedParticipant(null);
25+
const handleParticipantDisconnected = (participant: Participant) =>
26+
_setSelectedParticipant(prevParticipant => (prevParticipant === participant ? null : prevParticipant));
27+
2528
room.on('disconnected', onDisconnect);
29+
room.on('participantDisconnected', handleParticipantDisconnected);
2630
return () => {
2731
room.off('disconnected', onDisconnect);
32+
room.off('participantDisconnected', handleParticipantDisconnected);
2833
};
2934
}, [room]);
3035

0 commit comments

Comments
 (0)