-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathutils.test.js
More file actions
150 lines (133 loc) · 4.87 KB
/
utils.test.js
File metadata and controls
150 lines (133 loc) · 4.87 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
import React from 'react';
import ReactMarkdown from 'react-markdown';
import '@testing-library/jest-dom';
import { nanoid } from 'nanoid';
import {
generateChannel,
generateImageAttachment,
generateMember,
generateMessage,
generateUser,
getOrCreateChannelApi,
getTestClientWithUser,
useMockedApis,
} from 'mock-builders';
import { getDisplayImage, getDisplayTitle, getLatestMessagePreview } from '../utils';
import { generateStaticLocationResponse } from '../../../mock-builders';
import { render } from '@testing-library/react';
describe('ChannelPreview utils', () => {
const clientUser = generateUser();
let chatClient;
const getQueriedChannelInstance = async (c) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
useMockedApis(chatClient, [getOrCreateChannelApi(c)]);
const channel = chatClient.channel('messaging');
await channel.watch();
return channel;
};
beforeEach(async () => {
chatClient = await getTestClientWithUser(clientUser);
});
describe('getLatestMessagePreview', () => {
const channelWithEmptyMessage = generateChannel();
const channelWithDeletedMessage = generateChannel({
messages: [generateMessage({ deleted_at: new Date() })],
});
const channelWithLocationMessage = generateChannel({
messages: [
generateMessage({
attachments: [],
shared_location: generateStaticLocationResponse(),
text: '',
}),
],
});
const channelWithAttachmentMessage = generateChannel({
messages: [
generateMessage({
attachments: [generateImageAttachment()],
text: undefined,
}),
],
});
const channelWithHTMLInMessage = generateChannel({
messages: [
generateMessage({
attachments: [generateImageAttachment()],
text:
'<h1>Hello, world!</h1> \n' +
'<p>This is my first web page.</p> \n' +
'<p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>',
}),
],
});
const expectedTextWithHTMLRendering =
'<h1>Hello, world!</h1> <p>This is my first web page.</p> <p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>';
function isReactMarkdownElement(x) {
return React.isValidElement(x) && x.type === ReactMarkdown;
}
it.each([
['Nothing yet...', 'channelWithEmptyMessage', channelWithEmptyMessage],
['Message deleted', 'channelWithDeletedMessage', channelWithDeletedMessage],
['🏙 Attachment...', 'channelWithAttachmentMessage', channelWithAttachmentMessage],
['📍Shared location', 'channelWithLocationMessage', channelWithLocationMessage],
[
expectedTextWithHTMLRendering,
'channelWithHTMLInMessage',
channelWithHTMLInMessage,
],
])('should return %s for %s', async (expectedValue, testCaseName, c) => {
const t = (text) => text;
const channel = await getQueriedChannelInstance(c);
const preview = getLatestMessagePreview(channel, t);
if (isReactMarkdownElement(preview)) {
const { container } = render(preview);
expect(container).toHaveTextContent(expectedValue);
} else {
expect(getLatestMessagePreview(channel, t)).toBe(expectedValue);
}
});
});
describe('getDisplayTitle', () => {
it('should return channel name, if it exists', async () => {
const name = nanoid();
const channel = await getQueriedChannelInstance(
generateChannel({ channel: { name } }),
);
expect(getDisplayTitle(channel, chatClient.user)).toBe(name);
});
it('should return name of other member of conversation if only 2 members and channel name doesnot exist', async () => {
const otherUser = generateUser();
const channel = await getQueriedChannelInstance(
generateChannel({
members: [
generateMember({ user: otherUser }),
generateMember({ user: clientUser }),
],
}),
);
expect(getDisplayTitle(channel, chatClient.user)).toBe(otherUser.name);
});
});
describe('getDisplayImage', () => {
it('should return channel image, if it exists', async () => {
const image = nanoid();
const channel = await getQueriedChannelInstance(
generateChannel({ channel: { image } }),
);
expect(getDisplayImage(channel, chatClient.user)).toBe(image);
});
it('should return picture of other member of conversation if only 2 members and channel name doesnot exist', async () => {
const otherUser = generateUser();
const channel = await getQueriedChannelInstance(
generateChannel({
members: [
generateMember({ user: otherUser }),
generateMember({ user: clientUser }),
],
}),
);
expect(getDisplayImage(channel, chatClient.user)).toBe(otherUser.image);
});
});
});