-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathChannelHeader.test.js
More file actions
433 lines (391 loc) Β· 15 KB
/
ChannelHeader.test.js
File metadata and controls
433 lines (391 loc) Β· 15 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
import React from 'react';
import { act, cleanup, render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ChannelHeader } from '../ChannelHeader';
import { ChannelStateProvider } from '../../../context/ChannelStateContext';
import { ChatProvider } from '../../../context/ChatContext';
import { TranslationProvider } from '../../../context/TranslationContext';
import {
dispatchUserUpdatedEvent,
generateChannel,
generateMember,
generateMessage,
generateUser,
getOrCreateChannelApi,
getTestClientWithUser,
initClientWithChannels,
useMockedApis,
} from '../../../mock-builders';
import { toHaveNoViolations } from 'jest-axe';
import { axe } from '../../../../axe-helper';
import { ChannelAvatar } from '../../Avatar';
expect.extend(toHaveNoViolations);
const AVATAR_IMG_TEST_ID = 'avatar-img';
const user1 = generateUser();
const user2 = generateUser({ image: null });
let testChannel1;
let client;
const CustomMenuIcon = () => <div id='custom-icon'>Custom Menu Icon</div>;
const defaultChannelState = {
members: [generateMember({ user: user1 }), generateMember({ user: user2 })],
};
const t = jest.fn((key) => key);
const renderComponentBase = ({ channel, client, props }) =>
render(
<ChatProvider value={{ channel, client }}>
<ChannelStateProvider value={{ channel }}>
<TranslationProvider value={{ t }}>
<ChannelHeader {...props} />
</TranslationProvider>
</ChannelStateProvider>
</ChatProvider>,
);
async function renderComponent({ channelData, channelType = 'messaging', props } = {}) {
client = await getTestClientWithUser(user1);
testChannel1 = generateChannel({ ...defaultChannelState, channel: channelData });
/* eslint-disable-next-line react-hooks/rules-of-hooks */
useMockedApis(client, [getOrCreateChannelApi(testChannel1)]);
const channel = client.channel(channelType, testChannel1.id, channelData);
await channel.query();
return renderComponentBase({ channel, client, props });
}
afterEach(cleanup);
describe('ChannelHeader', () => {
it('should display live label when prop live is true', async () => {
const { container } = await renderComponent({
channelData: { image: 'image.jpg', name: 'test-channel-1' },
props: { live: true },
});
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(
container.querySelector('.str-chat__header-livestream-livelabel'),
).toBeInTheDocument();
});
it("should display avatar with fallback image only if other user's name is available", async () => {
await renderComponent({ channelData: { image: null } });
await waitFor(() => {
expect(screen.queryByTestId('avatar-img')).not.toBeInTheDocument();
expect(screen.queryByTestId('avatar-fallback')).toBeInTheDocument();
});
});
it('should display avatar when channel has an image', async () => {
const { container, getByTestId } = await renderComponent({
channelData: { image: 'image.jpg', name: 'test-channel-1' },
props: { live: false },
});
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(getByTestId('avatar-img')).toBeInTheDocument();
expect(getByTestId('avatar-img')).toHaveAttribute('src', 'image.jpg');
});
it('should display custom title', async () => {
const { container, getByText } = await renderComponent({
channelData: { image: 'image.jpg', name: 'test-channel-1' },
props: { title: 'Custom Title' },
});
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(getByText('Custom Title')).toBeInTheDocument();
});
it('should display subtitle if present in channel data', async () => {
const { container, getByText } = await renderComponent({
channelData: {
image: 'image.jpg',
name: 'test-channel-1',
subtitle: 'test subtitle',
},
});
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(getByText('test subtitle')).toBeInTheDocument();
});
it('should display watcher_count', async () => {
const { container, getByText } = await renderComponent({
channelData: {
image: 'image.jpg',
name: 'test-channel-1',
subtitle: 'test subtitle',
watcher_count: 34,
},
});
const results = await axe(container);
expect(results).toHaveNoViolations();
waitFor(() => {
expect(getByText('34 online')).toBeInTheDocument();
});
});
it('should display correct member_count', async () => {
const { container, getByText } = await renderComponent({
channelData: {
image: 'image.jpg',
member_count: 34,
name: 'test-channel-1',
subtitle: 'test subtitle',
},
});
const results = await axe(container);
expect(results).toHaveNoViolations();
waitFor(() => {
expect(getByText('34 members')).toBeInTheDocument();
});
});
it('should display default menu icon if none provided', async () => {
const { getByTestId } = await renderComponent();
expect(getByTestId('menu-icon')).toMatchInlineSnapshot(`
<svg
data-testid="menu-icon"
viewBox="0 0 448 512"
xmlns="http://www.w3.org/2000/svg"
>
<title>
Menu
</title>
<path
d="M0 88C0 74.75 10.75 64 24 64H424C437.3 64 448 74.75 448 88C448 101.3 437.3 112 424 112H24C10.75 112 0 101.3 0 88zM0 248C0 234.7 10.75 224 24 224H424C437.3 224 448 234.7 448 248C448 261.3 437.3 272 424 272H24C10.75 272 0 261.3 0 248zM424 432H24C10.75 432 0 421.3 0 408C0 394.7 10.75 384 24 384H424C437.3 384 448 394.7 448 408C448 421.3 437.3 432 424 432z"
fill="currentColor"
/>
</svg>
`);
});
it('should display custom menu icon', async () => {
const { container } = await renderComponent({
props: {
MenuIcon: CustomMenuIcon,
},
});
expect(container.querySelector('div#custom-icon')).toBeInTheDocument();
});
it("DM channel should reflect change of other user's name", async () => {
const updatedAttribute = { name: 'new-name' };
await renderComponent();
await waitFor(() =>
expect(screen.queryByText(updatedAttribute.name)).not.toBeInTheDocument(),
);
act(() => {
dispatchUserUpdatedEvent(client, { ...user2, ...updatedAttribute });
});
await waitFor(() =>
expect(screen.queryAllByText(updatedAttribute.name).length).toBeGreaterThan(0),
);
});
it("DM channel should reflect change of other user's image", async () => {
const updatedAttribute = { image: 'new-image' };
await renderComponent();
await waitFor(() => {
expect(screen.queryByTestId('avatar-img')).not.toBeInTheDocument();
expect(screen.queryByTestId('avatar-fallback')).toBeInTheDocument();
});
act(() => {
dispatchUserUpdatedEvent(client, { ...user2, ...updatedAttribute });
});
await waitFor(() =>
expect(screen.getByTestId('avatar-img')).toHaveAttribute(
'src',
updatedAttribute.image,
),
);
});
describe('group channel', () => {
const props = {
Avatar: ChannelAvatar,
watchers: { limit: 10 },
};
const getChannelState = (memberCount, channelData) => {
const users = Array.from({ length: memberCount }, generateUser);
const members = users.map((user) => generateMember({ user }));
return generateChannel({
members,
messages: users.map((user) => generateMessage({ user })),
...channelData,
});
};
const channelName = 'channel-name';
const channelState = getChannelState(3, { channel: { name: channelName } });
it('renders max 4 avatars in channel avatar', async () => {
const channelState = getChannelState(5);
const ownUser = channelState.members[0].user;
const {
channels: [channel],
client,
} = await initClientWithChannels({
channelsData: [channelState],
customUser: ownUser,
});
await renderComponentBase({ channel, client, props });
await waitFor(() => {
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
expect(avatarImages).toHaveLength(4);
avatarImages.slice(0, 4).forEach((img, i) => {
expect(img).toHaveAttribute('src', channelState.members[i].user.image);
});
});
});
it.each([
['own user', channelState.members[0].user],
['other user', channelState.members[2].user],
])(
"should not update the direct messaging channel's preview title if %s's name has changed",
async (_, user) => {
const {
channels: [channel],
client,
} = await initClientWithChannels({ channelsData: [channelState] });
const updatedAttribute = { name: 'new-name' };
await renderComponentBase({ channel, client, props });
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.name)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
});
act(() => {
dispatchUserUpdatedEvent(client, { ...user, ...updatedAttribute });
});
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.name)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
});
},
);
it("should update the direct messaging channel's preview image if own user's image has changed", async () => {
const ownUser = channelState.members[0].user;
const {
channels: [channel],
client,
} = await initClientWithChannels({
channelsData: [channelState],
customUser: ownUser,
});
const updatedAttribute = { image: 'new-image' };
await renderComponentBase({ channel, client, props });
await waitFor(() => {
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
expect(avatarImages).toHaveLength(3);
expect(avatarImages[0]).toHaveAttribute('src', ownUser.image);
expect(avatarImages[1]).toHaveAttribute(
'src',
channelState.members[1].user.image,
);
expect(avatarImages[2]).toHaveAttribute(
'src',
channelState.members[2].user.image,
);
});
act(() => {
dispatchUserUpdatedEvent(client, { ...ownUser, ...updatedAttribute });
});
await waitFor(() => {
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
expect(avatarImages[0]).toHaveAttribute('src', updatedAttribute.image);
expect(avatarImages[1]).toHaveAttribute(
'src',
channelState.members[1].user.image,
);
expect(avatarImages[2]).toHaveAttribute(
'src',
channelState.members[2].user.image,
);
});
});
it("should update the direct messaging channel's preview image if other user's image has changed", async () => {
const ownUser = channelState.members[0].user;
const otherUser = channelState.members[2].user;
const {
channels: [channel],
client,
} = await initClientWithChannels({
channelsData: [channelState],
customUser: ownUser,
});
const updatedAttribute = { image: 'new-image' };
await renderComponentBase({ channel, client, props });
await waitFor(() => {
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
expect(avatarImages).toHaveLength(3);
expect(avatarImages[0]).toHaveAttribute('src', ownUser.image);
expect(avatarImages[1]).toHaveAttribute(
'src',
channelState.members[1].user.image,
);
expect(avatarImages[2]).toHaveAttribute(
'src',
channelState.members[2].user.image,
);
});
act(() => {
dispatchUserUpdatedEvent(client, { ...otherUser, ...updatedAttribute });
});
await waitFor(() => {
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
expect(avatarImages[0]).toHaveAttribute('src', ownUser.image);
expect(avatarImages[1]).toHaveAttribute(
'src',
channelState.members[1].user.image,
);
expect(avatarImages[2]).toHaveAttribute('src', updatedAttribute.image);
});
});
it("should not update the direct messaging channel's preview if other user's attribute than name or image has changed", async () => {
const ownUser = channelState.members[0].user;
const otherUser = channelState.members[2].user;
const {
channels: [channel],
client,
} = await initClientWithChannels({
channelsData: [channelState],
customUser: ownUser,
});
const updatedAttribute = { custom: 'new-custom' };
await renderComponentBase({ channel, client, props });
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
avatarImages.forEach((img, i) => {
expect(img).toHaveAttribute('src', channelState.members[i].userimage);
});
});
act(() => {
dispatchUserUpdatedEvent(client, { ...otherUser, ...updatedAttribute });
});
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
avatarImages.forEach((img, i) => {
expect(img).toHaveAttribute('src', channelState.members[i].userimage);
});
});
});
it("should not update the direct messaging channel's preview if own user's attribute than name or image has changed", async () => {
const ownUser = channelState.members[0].user;
const {
channels: [channel],
client,
} = await initClientWithChannels({
channelsData: [channelState],
customUser: ownUser,
});
const updatedAttribute = { custom: 'new-custom' };
await renderComponentBase({ channel, client, props });
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
avatarImages.forEach((img, i) => {
expect(img).toHaveAttribute('src', channelState.members[i].userimage);
});
});
act(() => {
dispatchUserUpdatedEvent(client, { ...ownUser, ...updatedAttribute });
});
await waitFor(() => {
expect(screen.queryByText(updatedAttribute.custom)).not.toBeInTheDocument();
expect(screen.getByText(channelName)).toBeInTheDocument();
const avatarImages = screen.getAllByTestId(AVATAR_IMG_TEST_ID);
avatarImages.forEach((img, i) => {
expect(img).toHaveAttribute('src', channelState.members[i].userimage);
});
});
});
});
});