Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpin participant when they disconnect from room #339

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { act, HookResult, renderHook, RenderHookResult } from '@testing-library/react-hooks';
import { EventEmitter } from 'events';
import React from 'react';
import { Room } from 'twilio-video';
import { Participant, Room } from 'twilio-video';
import useSelectedParticipant, { SelectedParticipantProvider } from './useSelectedParticipant';

describe('the useSelectedParticipant hook', () => {
let mockRoom: Room;
let result: HookResult<readonly [Participant | null, (participant: Participant) => void]>;

beforeEach(() => (mockRoom = new EventEmitter() as Room));

it('should return null as the default value', () => {
const { result } = renderHook(useSelectedParticipant, {
beforeEach(() => {
({ result } = renderHook(useSelectedParticipant, {
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
});
}));
});

it('should return null as the default value', () => {
expect(result.current[0]).toBe(null);
});

it('should set a selected participant', () => {
const { result } = renderHook(useSelectedParticipant, {
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
});

act(() => result.current[1]('mockParticipant' as any));

expect(result.current[0]).toBe('mockParticipant');
});

it('should set "null" as the selected participant when the user selects the currently selected participant', () => {
const { result } = renderHook(useSelectedParticipant, {
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
});

act(() => result.current[1]('mockParticipant' as any));
act(() => result.current[1]('mockParticipant' as any));

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

it('should set "null" as the selected participant on room disconnect', () => {
const { result } = renderHook(useSelectedParticipant, {
wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>,
});

act(() => result.current[1]('mockParticipant' as any));
expect(result.current[0]).toBe('mockParticipant');
act(() => {
mockRoom.emit('disconnected');
});
expect(result.current[0]).toBe(null);
});

it('should set "null" as the selected participant when the participant disconnects from the room', () => {
act(() => result.current[1]('mockParticipant' as any));
act(() => {
mockRoom.emit('participantDisconnected', 'mockParticipant');
});
expect(result.current[0]).toBe(null);
});

it('should not set "null" as the selected participant when a non-selected participant disconnects from the room', () => {
act(() => result.current[1]('mockParticipant' as any));
act(() => {
mockRoom.emit('participantDisconnected', 'otherMockParticipant');
});
expect(result.current[0]).toBe('mockParticipant');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ export function SelectedParticipantProvider({ room, children }: SelectedParticip

useEffect(() => {
const onDisconnect = () => _setSelectedParticipant(null);
const handleParticipantDisconnected = (participant: Participant) =>
_setSelectedParticipant(prevParticipant => (prevParticipant === participant ? null : prevParticipant));

room.on('disconnected', onDisconnect);
room.on('participantDisconnected', handleParticipantDisconnected);
return () => {
room.off('disconnected', onDisconnect);
room.off('participantDisconnected', handleParticipantDisconnected);
};
}, [room]);

Expand Down