forked from GetStream/stream-chat-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageOptions.test.js
More file actions
412 lines (366 loc) Β· 14 KB
/
MessageOptions.test.js
File metadata and controls
412 lines (366 loc) Β· 14 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
import React from 'react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { Message } from '../Message';
import { MessageOptions } from '../MessageOptions';
import { MessageSimple } from '../MessageSimple';
import { ACTIONS_NOT_WORKING_IN_THREAD, MESSAGE_ACTIONS } from '../utils';
import { Attachment } from '../../Attachment';
import { defaultReactionOptions } from '../../Reactions';
import {
ChannelActionProvider,
ChannelStateProvider,
ChatProvider,
ComponentProvider,
DialogManagerProvider,
} from '../../../context';
import {
generateChannel,
generateMessage,
generateUser,
getTestClientWithUser,
} from '../../../mock-builders';
const MESSAGE_ACTIONS_TEST_ID = 'message-actions';
const minimumCapabilitiesToRenderMessageActions = { 'delete-any-message': true };
const alice = generateUser({ name: 'alice' });
const defaultMessageProps = {
initialMessage: false,
message: generateMessage(),
messageActions: Object.keys(MESSAGE_ACTIONS),
threadList: false,
};
const defaultOptionsProps = {};
function generateAliceMessage(messageOptions) {
return generateMessage({
user: alice,
...messageOptions,
});
}
async function renderMessageOptions({
channelConfig,
channelStateOpts = {},
customMessageProps = {},
customOptionsProps = {},
}) {
const client = await getTestClientWithUser(alice);
const channel = generateChannel({
getConfig: () => channelConfig,
state: { membership: {} },
});
return render(
<ChatProvider value={{ client }}>
<DialogManagerProvider id='message-options-dialog-provider'>
<ChannelStateProvider value={{ channel, ...channelStateOpts }}>
<ChannelActionProvider
value={{
openThread: jest.fn(),
removeMessage: jest.fn(),
updateMessage: jest.fn(),
}}
>
<ComponentProvider
value={{
Attachment,
Message: () => <MessageSimple channelConfig={channelConfig} />,
reactionOptions: defaultReactionOptions,
}}
>
<Message {...defaultMessageProps} {...customMessageProps}>
<MessageOptions {...defaultOptionsProps} {...customOptionsProps} />
</Message>
</ComponentProvider>
</ChannelActionProvider>
</ChannelStateProvider>
</DialogManagerProvider>
</ChatProvider>,
);
}
const threadActionTestId = 'thread-action';
const reactionActionTestId = 'message-reaction-action';
describe('<MessageOptions />', () => {
beforeEach(jest.clearAllMocks);
it('should not render message options when there is no message set', async () => {
const { queryByTestId } = await renderMessageOptions({
customMessageProps: {
message: {},
},
});
expect(queryByTestId(/message-options/)).not.toBeInTheDocument();
});
it.each([
['type', 'error'],
['type', 'system'],
['type', 'ephemeral'],
['status', 'failed'],
['status', 'sending'],
])(
'should not render message options when message is of %s %s and is from current user.',
async (key, value) => {
const message = generateAliceMessage({ [key]: value });
const { queryByTestId } = await renderMessageOptions({
customMessageProps: { message },
});
expect(queryByTestId(/message-options/)).not.toBeInTheDocument();
},
);
it('should not render message options when it is parent message in a thread', async () => {
const { queryByTestId } = await renderMessageOptions({
customMessageProps: {
initialMessage: true,
},
});
expect(queryByTestId(/message-options/)).not.toBeInTheDocument();
});
it('should display thread actions when message is not displayed in a thread list and channel has replies configured', async () => {
const { getByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reply': true },
channelConfig: { replies: true },
},
customMessageProps: defaultMessageProps,
});
expect(getByTestId(threadActionTestId)).toBeInTheDocument();
});
it('should not display thread actions when message is in a thread list', async () => {
const { queryByTestId } = await renderMessageOptions({
channelConfig: { replies: true },
customMessageProps: { threadList: true },
});
expect(queryByTestId(threadActionTestId)).not.toBeInTheDocument();
});
it('should not display thread actions when channel does not have replies enabled', async () => {
const { queryByTestId } = await renderMessageOptions({
channelConfig: { replies: false },
});
expect(queryByTestId(threadActionTestId)).not.toBeInTheDocument();
});
it('should trigger open thread handler when custom thread action is set and thread action is clicked', async () => {
const handleOpenThread = jest.fn(() => {});
const message = generateMessage();
const { getByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reply': true },
channelConfig: { replies: true },
},
customMessageProps: { message, openThread: handleOpenThread, threadList: false },
});
expect(handleOpenThread).not.toHaveBeenCalled();
fireEvent.click(getByTestId(threadActionTestId));
expect(handleOpenThread).toHaveBeenCalled();
});
it('should display reactions action when channel has reactions enabled', async () => {
const { getByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
expect(getByTestId(reactionActionTestId)).toBeInTheDocument();
});
it('should not display reactions action when channel has reactions disabled', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': false },
},
});
expect(queryByTestId(reactionActionTestId)).not.toBeInTheDocument();
});
it('should not render ReactionsSelector until open', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
expect(screen.queryByTestId('reaction-selector')).not.toBeInTheDocument();
await act(async () => {
await fireEvent.click(queryByTestId(reactionActionTestId));
});
expect(screen.getByTestId('reaction-selector')).toBeInTheDocument();
});
it('should unmount ReactionsSelector when closed by click on dialog overlay', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await act(async () => {
await fireEvent.click(queryByTestId(reactionActionTestId));
});
await act(async () => {
await fireEvent.click(screen.getByTestId('str-chat__dialog-overlay'));
});
expect(screen.queryByTestId('reaction-selector')).not.toBeInTheDocument();
});
it('should unmount ReactionsSelector when closed pressed Esc button', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
});
await act(async () => {
await fireEvent.click(queryByTestId(reactionActionTestId));
});
await act(async () => {
await fireEvent.keyUp(document, { charCode: 27, code: 'Escape', key: 'Escape' });
});
expect(screen.queryByTestId('reaction-selector')).not.toBeInTheDocument();
});
it('should unmount ReactionsSelector when closed on reaction selection and closeReactionSelectorOnClick enabled', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
customMessageProps: {
closeReactionSelectorOnClick: true,
},
});
await act(async () => {
await fireEvent.click(queryByTestId(reactionActionTestId));
});
await act(async () => {
await fireEvent.click(screen.queryAllByTestId('select-reaction-button')[0]);
});
expect(screen.queryByTestId('reaction-selector')).not.toBeInTheDocument();
});
it('should not unmount ReactionsSelector when closed on reaction selection and closeReactionSelectorOnClick enabled', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: { 'send-reaction': true },
},
customMessageProps: {
closeReactionSelectorOnClick: false,
},
});
await act(async () => {
await fireEvent.click(queryByTestId(reactionActionTestId));
});
await act(async () => {
await fireEvent.click(screen.queryAllByTestId('select-reaction-button')[0]);
});
expect(screen.queryByTestId('reaction-selector')).toBeInTheDocument();
});
it('should render message actions', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
it('should not show message actions button if actions are disabled', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: { messageActions: [] },
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).not.toBeInTheDocument();
});
it('should not show actions box for message in thread if only non-thread actions are available', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
messageActions: ACTIONS_NOT_WORKING_IN_THREAD,
threadList: true,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).not.toBeInTheDocument();
});
it('should show actions box for message in thread if not only non-thread actions are available', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
messageActions: [...ACTIONS_NOT_WORKING_IN_THREAD, MESSAGE_ACTIONS.delete],
threadList: true,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
it('should show actions box for a message in thread if custom actions provided are non-thread', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
customMessageActions: ACTIONS_NOT_WORKING_IN_THREAD,
messageActions: ACTIONS_NOT_WORKING_IN_THREAD,
threadList: true,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
it('should not show actions box for message outside thread with single action "react"', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
messageActions: [MESSAGE_ACTIONS.react],
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).not.toBeInTheDocument();
});
it('should show actions box for message outside thread with single action "react" if custom actions available', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
customMessageActions: [MESSAGE_ACTIONS.react],
messageActions: [MESSAGE_ACTIONS.react],
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
it('should not show actions box for message outside thread with single action "reply"', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
messageActions: [MESSAGE_ACTIONS.reply],
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).not.toBeInTheDocument();
});
it('should show actions box for message outside thread with single action "reply" if custom actions available', async () => {
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
customMessageActions: [MESSAGE_ACTIONS.reply],
messageActions: [MESSAGE_ACTIONS.reply],
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
it('should not show actions box for message outside thread with two actions "react" & "reply"', async () => {
const actions = [MESSAGE_ACTIONS.react, MESSAGE_ACTIONS.reply];
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
messageActions: actions,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).not.toBeInTheDocument();
});
it('should show actions box for message outside thread with single actions "react" & "reply" if custom actions available', async () => {
const actions = [MESSAGE_ACTIONS.react, MESSAGE_ACTIONS.reply];
const { queryByTestId } = await renderMessageOptions({
channelStateOpts: {
channelCapabilities: minimumCapabilitiesToRenderMessageActions,
},
customMessageProps: {
customMessageActions: actions,
messageActions: actions,
},
});
expect(queryByTestId(MESSAGE_ACTIONS_TEST_ID)).toBeInTheDocument();
});
});