forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_formatting.test.jsx
224 lines (207 loc) · 8.62 KB
/
text_formatting.test.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import emojiRegex from 'emoji-regex';
import {formatText, autolinkAtMentions, highlightSearchTerms, handleUnicodeEmoji, parseSearchTerms} from 'utils/text_formatting.jsx';
import {getEmojiMap} from 'selectors/emojis';
import store from 'stores/redux_store.jsx';
import LinkOnlyRenderer from 'utils/markdown/link_only_renderer';
describe('formatText', () => {
test('jumbo emoji should be able to handle up to 3 spaces before the emoji character', () => {
const emoji = ':)';
let spaces = '';
for (let i = 0; i < 3; i++) {
spaces += ' ';
const output = formatText(`${spaces}${emoji}`, {});
expect(output).toBe(`<span class="all-emoji"><p>${spaces}<span data-emoticon="slightly_smiling_face">${emoji}</span></p></span>`);
}
});
});
describe('autolinkAtMentions', () => {
// testing to make sure @channel, @all & @here are setup properly to get highlighted correctly
const mentionTestCases = [
'channel',
'all',
'here',
];
function runSuccessfulAtMentionTests(leadingText = '', trailingText = '') {
mentionTestCases.forEach((testCase) => {
const mention = `@${testCase}`;
const text = `${leadingText}${mention}${trailingText}`;
const tokens = new Map();
const output = autolinkAtMentions(text, tokens);
expect(output).toBe(`${leadingText}$MM_ATMENTION0$${trailingText}`);
expect(tokens.get('$MM_ATMENTION0$').value).toBe(`<span data-mention="${testCase}">${mention}</span>`);
});
}
function runUnsuccessfulAtMentionTests(leadingText = '', trailingText = '') {
mentionTestCases.forEach((testCase) => {
const mention = `@${testCase}`;
const text = `${leadingText}${mention}${trailingText}`;
const tokens = new Map();
const output = autolinkAtMentions(text, tokens);
expect(output).toBe(text);
expect(tokens.get('$MM_ATMENTION0$')).toBeUndefined();
});
}
// cases where highlights should be successful
test('@channel, @all, @here should highlight properly with no leading or trailing content', () => {
runSuccessfulAtMentionTests();
});
test('@channel, @all, @here should highlight properly with a leading space', () => {
runSuccessfulAtMentionTests(' ', '');
});
test('@channel, @all, @here should highlight properly with a trailing space', () => {
runSuccessfulAtMentionTests('', ' ');
});
test('@channel, @all, @here should highlight properly with a leading period', () => {
runSuccessfulAtMentionTests('.', '');
});
test('@channel, @all, @here should highlight properly with a trailing period', () => {
runSuccessfulAtMentionTests('', '.');
});
test('@channel, @all, @here should highlight properly with a leading dash', () => {
runSuccessfulAtMentionTests('-', '');
});
test('@channel, @all, @here should highlight properly with a trailing dash', () => {
runSuccessfulAtMentionTests('', '-');
});
test('@channel, @all, @here should highlight properly within a typical sentance', () => {
runSuccessfulAtMentionTests('This is a typical sentance, ', ' check out this sentance!');
});
// cases where highlights should be unseccessful
test('@channel, @all, @here should not highlight with a leading underscore', () => {
runUnsuccessfulAtMentionTests('_');
});
test('@channel, @all, @here should not highlight when the last part of a word', () => {
runUnsuccessfulAtMentionTests('testing');
});
test('@channel, @all, @here should not highlight when in the middle of a word', () => {
runUnsuccessfulAtMentionTests('test', 'ing');
});
});
describe('highlightSearchTerms', () => {
test('hashtags should highlight case-insensitively', () => {
const text = '$MM_HASHTAG0$';
const tokens = new Map(
[['$MM_HASHTAG0$', {
hashtag: 'Test',
originalText: '#Test',
value: '<a class="mention-link" href="#" data-hashtag="#Test">#Test</a>',
}]],
);
const searchPatterns = [
{
pattern: /(\W|^)(#test)\b/gi,
term: '#test',
},
];
const output = highlightSearchTerms(text, tokens, searchPatterns);
expect(output).toBe('$MM_SEARCHTERM1$');
expect(tokens.get('$MM_SEARCHTERM1$').value).toBe('<span class="search-highlight">$MM_HASHTAG0$</span>');
});
});
describe('handleUnicodeEmoji', () => {
test('unicode emoji with image support should get replaced with an image', () => {
const text = '👍';
const emojiMap = getEmojiMap(store.getState());
const UNICODE_EMOJI_REGEX = emojiRegex();
const output = handleUnicodeEmoji(text, emojiMap, UNICODE_EMOJI_REGEX);
expect(output).toBe('<span data-emoticon="+1">👍</span>');
});
test('unicode emoji without image support should get wrapped in a span tag', () => {
const text = '🤟'; // note, this test will fail as soon as this emoji gets a corresponding image
const emojiMap = getEmojiMap(store.getState());
const UNICODE_EMOJI_REGEX = emojiRegex();
const output = handleUnicodeEmoji(text, emojiMap, UNICODE_EMOJI_REGEX);
expect(output).toBe('<span class="emoticon emoticon--unicode">🤟</span>');
});
});
describe('linkOnlyMarkdown', () => {
const options = {markdown: false, renderer: new LinkOnlyRenderer()};
test('link without a title', () => {
const text = 'Do you like https://www.mattermost.com?';
const output = formatText(text, options);
expect(output).toBe(
'Do you like <a class="theme markdown__link" href="https://www.mattermost.com" target="_blank">' +
'https://www.mattermost.com</a>?');
});
test('link with a title', () => {
const text = 'Do you like [Mattermost](https://www.mattermost.com)?';
const output = formatText(text, options);
expect(output).toBe(
'Do you like <a class="theme markdown__link" href="https://www.mattermost.com" target="_blank">' +
'Mattermost</a>?');
});
test('link with header signs to skip', () => {
const text = '#### Do you like [Mattermost](https://www.mattermost.com)?';
const output = formatText(text, options);
expect(output).toBe(
'Do you like <a class="theme markdown__link" href="https://www.mattermost.com" target="_blank">' +
'Mattermost</a>?');
});
});
describe('parseSearchTerms', () => {
const tests = [
{
description: 'no input',
input: undefined,
expected: [],
},
{
description: 'empty input',
input: '',
expected: [],
},
{
description: 'simple word',
input: 'someword',
expected: ['someword'],
},
{
description: 'simple phrase',
input: '"some phrase"',
expected: ['some phrase'],
},
{
description: 'empty phrase',
input: '""',
expected: [],
},
{
description: 'phrase before word',
input: '"some phrase" someword',
expected: ['some phrase', 'someword'],
},
{
description: 'word before phrase',
input: 'someword "some phrase"',
expected: ['someword', 'some phrase'],
},
{
description: 'words and phrases',
input: 'someword "some phrase" otherword "other phrase"',
expected: ['someword', 'some phrase', 'otherword', 'other phrase'],
},
{
description: 'with search flags after',
input: 'someword "some phrase" from:someone in:somechannel',
expected: ['someword', 'some phrase'],
},
{
description: 'with search flags before',
input: 'from:someone in: channel someword "some phrase"',
expected: ['someword', 'some phrase'],
},
{
description: 'with search flags before and after',
input: 'from:someone someword "some phrase" in:somechannel',
expected: ['someword', 'some phrase'],
},
];
for (const t of tests) {
test(t.description, () => {
const output = parseSearchTerms(t.input);
expect(output).toStrictEqual(t.expected);
});
}
});