|
1 | | -import { act, renderHook } from '@testing-library/react-hooks'; |
| 1 | +import { act, HookResult, renderHook, RenderHookResult } from '@testing-library/react-hooks'; |
2 | 2 | import { EventEmitter } from 'events'; |
3 | 3 | import React from 'react'; |
4 | | -import { Room } from 'twilio-video'; |
| 4 | +import { Participant, Room } from 'twilio-video'; |
5 | 5 | import useSelectedParticipant, { SelectedParticipantProvider } from './useSelectedParticipant'; |
6 | 6 |
|
7 | 7 | describe('the useSelectedParticipant hook', () => { |
8 | 8 | let mockRoom: Room; |
| 9 | + let result: HookResult<readonly [Participant | null, (participant: Participant) => void]>; |
| 10 | + |
9 | 11 | beforeEach(() => (mockRoom = new EventEmitter() as Room)); |
10 | 12 |
|
11 | | - it('should return null as the default value', () => { |
12 | | - const { result } = renderHook(useSelectedParticipant, { |
| 13 | + beforeEach(() => { |
| 14 | + ({ result } = renderHook(useSelectedParticipant, { |
13 | 15 | wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>, |
14 | | - }); |
| 16 | + })); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return null as the default value', () => { |
15 | 20 | expect(result.current[0]).toBe(null); |
16 | 21 | }); |
17 | 22 |
|
18 | 23 | it('should set a selected participant', () => { |
19 | | - const { result } = renderHook(useSelectedParticipant, { |
20 | | - wrapper: ({ children }) => <SelectedParticipantProvider room={mockRoom}>{children}</SelectedParticipantProvider>, |
21 | | - }); |
22 | | - |
23 | 24 | act(() => result.current[1]('mockParticipant' as any)); |
24 | | - |
25 | 25 | expect(result.current[0]).toBe('mockParticipant'); |
26 | 26 | }); |
27 | 27 |
|
28 | 28 | 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 | | - |
33 | 29 | act(() => result.current[1]('mockParticipant' as any)); |
34 | 30 | act(() => result.current[1]('mockParticipant' as any)); |
35 | 31 |
|
36 | 32 | expect(result.current[0]).toBe(null); |
37 | 33 | }); |
38 | 34 |
|
39 | 35 | 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 | | - |
44 | 36 | act(() => result.current[1]('mockParticipant' as any)); |
45 | 37 | expect(result.current[0]).toBe('mockParticipant'); |
46 | 38 | act(() => { |
47 | 39 | mockRoom.emit('disconnected'); |
48 | 40 | }); |
49 | 41 | expect(result.current[0]).toBe(null); |
50 | 42 | }); |
| 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 | + }); |
51 | 59 | }); |
0 commit comments