-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathAttachmentSelector.test.js
More file actions
508 lines (475 loc) Β· 15.7 KB
/
AttachmentSelector.test.js
File metadata and controls
508 lines (475 loc) Β· 15.7 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import React from 'react';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { MessageInput } from '../MessageInput';
import { Chat } from '../../Chat';
import {
ChannelActionProvider,
ChannelStateProvider,
ComponentProvider,
TranslationProvider,
TypingProvider,
} from '../../../context';
import { generateMessage, initClientWithChannels } from '../../../mock-builders';
import { CHANNEL_CONTAINER_ID } from '../../Channel/constants';
import { AttachmentSelector } from '../AttachmentSelector';
import { LegacyThreadContext } from '../../Thread/LegacyThreadContext';
const ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID = 'attachment-selector-actions-menu';
const POLL_CREATION_DIALOG_TEST_ID = 'poll-creation-dialog';
const ATTACHMENT_SELECTOR_CLASS = 'str-chat__attachment-selector';
const UPLOAD_FILE_BUTTON_CLASS =
'str-chat__attachment-selector-actions-menu__upload-file-button';
const CREATE_POLL_BUTTON_CLASS =
'str-chat__attachment-selector-actions-menu__create-poll-button';
const SHARE_LOCATION_BUTTON_CLASS =
'str-chat__attachment-selector-actions-menu__add-location-button';
const translationContext = {
t: (v) => v,
tDateTimeParser: (v) => v.toString(),
};
const defaultChannelData = {
own_capabilities: ['upload-file'],
};
const defaultConfig = {
config: { shared_locations: true },
};
const defaultChannelStateContext = {
channelCapabilities: { 'send-poll': true, 'upload-file': true },
notifications: [],
};
const invokeMenu = async () => {
await act(async () => {
await fireEvent.click(screen.getByTestId('invoke-attachment-selector-button'));
});
};
const renderComponent = async ({
channelData = {},
channelStateContext,
componentContext,
customChannel,
customClient,
messageInputProps,
} = {}) => {
let channel, client;
if (customChannel && customClient) {
channel = customChannel;
client = customClient;
} else {
const res = await initClientWithChannels({
channelsData: [
{ channel: { ...defaultChannelData, config: defaultConfig, ...channelData } },
],
});
channel = res.channels[0];
client = res.client;
}
jest.spyOn(channel, 'getDraft').mockImplementation();
let result;
await act(() => {
result = render(
<Chat client={client}>
<ComponentProvider value={{ ...componentContext }}>
<TranslationProvider value={translationContext}>
<TypingProvider value={{}}>
<ChannelActionProvider value={{ addNotification: jest.fn() }}>
<ChannelStateProvider
value={{
...defaultChannelStateContext,
channel,
...channelStateContext,
}}
>
<div id={CHANNEL_CONTAINER_ID}>
{channelStateContext?.thread ? (
<LegacyThreadContext.Provider
value={{
legacyThread: channelStateContext.thread ?? undefined,
}}
>
<MessageInput {...messageInputProps} />
</LegacyThreadContext.Provider>
) : (
<MessageInput {...messageInputProps} />
)}
</div>
</ChannelStateProvider>
</ChannelActionProvider>
</TypingProvider>
</TranslationProvider>
</ComponentProvider>
</Chat>,
);
});
return result;
};
describe('AttachmentSelector', () => {
it('renders with all the buttons if all the permissions are granted', async () => {
await renderComponent();
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).toHaveTextContent('File');
expect(menu).toHaveTextContent('Poll');
expect(menu).toHaveTextContent('Location');
});
it('renders with poll only if only polls are enabled', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: true,
shared_locations: false,
uploads: false,
},
id: 'id',
type: 'type',
},
},
],
});
await renderComponent({
channelStateContext: { channelCapabilities: { 'send-poll': true } },
customChannel,
customClient,
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).not.toHaveTextContent('File');
expect(menu).toHaveTextContent('Poll');
expect(menu).not.toHaveTextContent('Location');
});
it('does not render with poll only if polls are not enabled and send-poll permission is granted', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: false,
shared_locations: false,
uploads: false,
},
id: 'id',
type: 'type',
},
},
],
});
await renderComponent({
channelStateContext: { channelCapabilities: { 'send-poll': true } },
customChannel,
customClient,
});
expect(
screen.queryByTestId('invoke-attachment-selector-button'),
).not.toBeInTheDocument();
});
it('renders with location only if only shared_locations are enabled', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: false,
shared_locations: true,
uploads: false,
},
id: 'id',
type: 'type',
},
},
],
});
await renderComponent({
channelStateContext: { channelCapabilities: {} },
customChannel,
customClient,
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).not.toHaveTextContent('File');
expect(menu).not.toHaveTextContent('Poll');
expect(menu).toHaveTextContent('Location');
});
it('falls back to SimpleAttachmentSelector if only file uploads are enabled', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: false,
shared_locations: false,
uploads: true,
},
id: 'id',
type: 'type',
},
},
],
});
const { container } = await renderComponent({
channelStateContext: { channelCapabilities: { 'upload-file': true } },
customChannel,
customClient,
});
expect(
container.querySelector(`.${ATTACHMENT_SELECTOR_CLASS}`),
).not.toBeInTheDocument();
expect(screen.getByTestId('file-upload-button')).toBeInTheDocument();
});
it('does not render SimpleAttachmentSelector neither AttachmentSelector menu if upload permission is granted but file upload disabled', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: false,
shared_locations: false,
uploads: false,
},
id: 'id',
type: 'type',
},
},
],
});
await renderComponent({
channelStateContext: { channelCapabilities: { 'upload-file': true } },
customChannel,
customClient,
});
expect(
screen.queryByTestId('invoke-attachment-selector-button'),
).not.toBeInTheDocument();
expect(screen.queryByTestId('file-upload-button')).not.toBeInTheDocument();
});
it('renders SimpleAttachmentSelector if rendered in a thread', async () => {
const {
channels: [customChannel],
client: customClient,
} = await initClientWithChannels({
channelsData: [
{
channel: {
...defaultChannelData,
cid: 'type:id',
config: {
polls: false,
shared_locations: false,
uploads: true,
},
id: 'id',
type: 'type',
},
},
],
});
const { container } = await renderComponent({
channelStateContext: {
channelCapabilities: { 'upload-file': true },
thread: generateMessage({ cid: customChannel.cid }),
},
customChannel,
customClient,
});
expect(
container.querySelector(`.${ATTACHMENT_SELECTOR_CLASS}`),
).not.toBeInTheDocument();
expect(screen.getByTestId('file-upload-button')).toBeInTheDocument();
});
it('renders AttachmentSelector if upload-file permission is not granted', async () => {
await renderComponent({
channelStateContext: { channelCapabilities: { 'send-poll': true } },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).not.toHaveTextContent('File');
expect(menu).toHaveTextContent('Poll');
expect(menu).toHaveTextContent('Location');
});
it('renders AttachmentSelector if only location sharing is enabled', async () => {
await renderComponent({
channelData: {
config: { shared_locations: true },
},
channelStateContext: { channelCapabilities: {} },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).not.toHaveTextContent('File');
expect(menu).not.toHaveTextContent('Poll');
expect(menu).toHaveTextContent('Location');
});
it('does not render the invoke button if no permissions are not granted', async () => {
await renderComponent({
channelStateContext: { channelCapabilities: {} },
});
expect(
screen.queryByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID),
).not.toBeInTheDocument();
});
it('opens poll creation dialog if Poll option is selected and closes the attachment selector menu', async () => {
await renderComponent();
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
const createPollButton = menu.querySelector(`.${CREATE_POLL_BUTTON_CLASS}`);
expect(createPollButton).toBeInTheDocument();
fireEvent.click(createPollButton);
await waitFor(() => {
expect(menu).not.toBeInTheDocument();
expect(screen.queryByTestId(POLL_CREATION_DIALOG_TEST_ID)).toBeInTheDocument();
});
});
it('is closed if File menu button is clicked', async () => {
await renderComponent();
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
const uploadFileMenuBtn = menu.querySelector(`.${UPLOAD_FILE_BUTTON_CLASS}`);
expect(uploadFileMenuBtn).toBeInTheDocument();
await act(async () => {
await fireEvent.click(uploadFileMenuBtn);
});
await waitFor(() => {
expect(menu).not.toBeInTheDocument();
});
});
it('renders custom menu actions if provided', async () => {
const customText = 'Custom text';
const ActionButton = () => <div>{customText}</div>;
const CustomAttachmentSelector = () => (
<AttachmentSelector
attachmentSelectorActionSet={[{ ActionButton, type: 'custom' }]}
/>
);
await renderComponent({
componentContext: { AttachmentSelector: CustomAttachmentSelector },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
expect(menu).toBeInTheDocument();
expect(menu).toHaveTextContent(customText);
expect(menu).not.toHaveTextContent('File');
expect(menu).not.toHaveTextContent('Poll');
expect(menu).not.toHaveTextContent('Location');
});
it('renders custom modal content if provided', async () => {
const buttonText = 'Custom text';
const modalText = 'Modal text';
const ActionButton = ({ closeMenu, openModalForAction }) => (
<div
onClick={() => {
openModalForAction('custom');
closeMenu();
}}
>
{buttonText}
</div>
);
const ModalContent = ({ close }) => <div onClick={close}>{modalText}</div>;
const CustomAttachmentSelector = () => (
<AttachmentSelector
attachmentSelectorActionSet={[{ ActionButton, ModalContent, type: 'custom' }]}
/>
);
await renderComponent({
componentContext: { AttachmentSelector: CustomAttachmentSelector },
});
await invokeMenu();
act(() => {
fireEvent.click(screen.getByText(buttonText));
});
await waitFor(() => {
expect(screen.getByText(modalText)).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(modalText));
});
await waitFor(() => {
expect(screen.queryByText(modalText)).not.toBeInTheDocument();
expect(
screen.queryByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID),
).not.toBeInTheDocument();
});
});
it('allows to customize the portal destination', async () => {
const getModalPortalDestination = jest.fn();
const CustomAttachmentSelector = () => (
<AttachmentSelector getModalPortalDestination={getModalPortalDestination} />
);
await renderComponent({
componentContext: { AttachmentSelector: CustomAttachmentSelector },
});
await invokeMenu();
act(() => {
fireEvent.click(screen.getByText('Poll'));
});
await waitFor(() => {
expect(getModalPortalDestination).toHaveBeenCalledWith();
});
});
it('allows to override PollCreationDialog', async () => {
const testId = 'custom-poll-creation-dialog';
const CustomPollCreationDialog = () => <div data-testid={testId} />;
await renderComponent({
componentContext: { PollCreationDialog: CustomPollCreationDialog },
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
const createPollButton = menu.querySelector(`.${CREATE_POLL_BUTTON_CLASS}`);
act(() => {
fireEvent.click(createPollButton);
});
await waitFor(() => {
expect(screen.getByTestId(testId)).toBeInTheDocument();
});
});
it('allows to override ShareLocationDialog', async () => {
const SHARE_LOCATION_DIALOG_TEST_ID = 'custom-share-location-dialog';
const CustomShareLocationDialog = () => (
<div data-testid={SHARE_LOCATION_DIALOG_TEST_ID} />
);
await renderComponent({
componentContext: {
ShareLocationDialog: CustomShareLocationDialog,
},
});
await invokeMenu();
const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID);
const locationButton = menu.querySelector(`.${SHARE_LOCATION_BUTTON_CLASS}`);
act(() => {
fireEvent.click(locationButton);
});
await waitFor(() => {
expect(screen.getByTestId(SHARE_LOCATION_DIALOG_TEST_ID)).toBeInTheDocument();
});
});
});