-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathuseUserMediaCallAction.spec.tsx
More file actions
128 lines (102 loc) · 4.06 KB
/
useUserMediaCallAction.spec.tsx
File metadata and controls
128 lines (102 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { act, renderHook } from '@testing-library/react';
import { useUserMediaCallAction } from './useUserMediaCallAction';
import { createFakeRoom, createFakeSubscription, createFakeUser } from '../../../../../../tests/mocks/data';
const usePeekMediaSessionStateMock = jest.fn().mockReturnValue('available');
const toggleWidgetMock = jest.fn();
jest.mock('@rocket.chat/ui-contexts', () => ({
...jest.requireActual('@rocket.chat/ui-contexts'),
useUserAvatarPath: jest.fn().mockReturnValue((_args: any) => 'avatar-url'),
useUserCard: jest.fn().mockReturnValue({ closeUserCard: jest.fn() }),
}));
jest.mock('@rocket.chat/ui-voip', () => ({
...jest.requireActual('@rocket.chat/ui-voip'),
useWidgetExternalControls: jest.fn().mockReturnValue({ toggleWidget: (...args: any[]) => toggleWidgetMock(...args) }),
usePeekMediaSessionState: () => usePeekMediaSessionStateMock(),
}));
describe('useUserMediaCallAction', () => {
const fakeUser = createFakeUser({ _id: 'own-uid' });
const mockRid = 'room-id';
afterEach(() => {
jest.clearAllMocks();
});
beforeEach(() => {
usePeekMediaSessionStateMock.mockReturnValue('available');
});
it('should return undefined if room is federated', () => {
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), {
wrapper: mockAppRoot()
.withJohnDoe()
.withRoom(createFakeRoom({ federated: true }))
.build(),
});
expect(result.current).toBeUndefined();
});
it('should return undefined if state is unauthorized', () => {
usePeekMediaSessionStateMock.mockReturnValueOnce('unavailable');
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), { wrapper: mockAppRoot().build() });
expect(result.current).toBeUndefined();
});
it('should return undefined if subscription is blocked', () => {
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), {
wrapper: mockAppRoot()
.withJohnDoe()
.withRoom(createFakeRoom())
.withSubscription(createFakeSubscription({ blocker: false, blocked: true }))
.build(),
});
expect(result.current).toBeUndefined();
});
it('should return undefined if subscription is blocker', () => {
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), {
wrapper: mockAppRoot()
.withJohnDoe()
.withRoom(createFakeRoom())
.withSubscription(createFakeSubscription({ blocker: true, blocked: false }))
.build(),
});
expect(result.current).toBeUndefined();
});
it('should return undefined if user is own user', () => {
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), {
wrapper: mockAppRoot().withUser(fakeUser).withRoom(createFakeRoom()).withSubscription(createFakeSubscription()).build(),
});
expect(result.current).toBeUndefined();
});
it('should return action if conditions are met', () => {
const fakeUser = createFakeUser();
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid), {
wrapper: mockAppRoot()
.withJohnDoe()
.withRoom(createFakeRoom())
.withSubscription(createFakeSubscription())
.withTranslations('en', 'core', {
Voice_call__user_: 'Voice call {{user}}',
})
.build(),
});
expect(result.current).toEqual(
expect.objectContaining({
type: 'communication',
icon: 'phone',
title: `Voice call ${fakeUser.name}`,
disabled: false,
}),
);
});
it('should call onClick handler correctly', () => {
usePeekMediaSessionStateMock.mockReturnValueOnce('available');
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid));
act(() => result.current?.onClick());
expect(toggleWidgetMock).toHaveBeenCalledWith({
userId: fakeUser._id,
displayName: fakeUser.name,
avatarUrl: 'avatar-url',
});
});
it('should be disabled if state is not closed, new, or unlicensed', () => {
usePeekMediaSessionStateMock.mockReturnValueOnce('calling');
const { result } = renderHook(() => useUserMediaCallAction(fakeUser, mockRid));
expect(result.current?.disabled).toBe(true);
});
});