summaryrefslogtreecommitdiffstats
path: root/webapp/tests/utils/formatting_hashtags.test.jsx
blob: e1b38260118366384239034c6ddf0ade5404f4bb (plain)
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
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import assert from 'assert';

import * as TextFormatting from 'utils/text_formatting.jsx';

describe('TextFormatting.Hashtags', function() {
    it('Not hashtags', function(done) {
        assert.equal(
            TextFormatting.formatText('# hashtag').trim(),
            '<h1 class="markdown__heading">hashtag</h1>'
        );

        assert.equal(
            TextFormatting.formatText('#ab').trim(),
            '<p>#ab</p>'
        );

        assert.equal(
            TextFormatting.formatText('#123test').trim(),
            '<p>#123test</p>'
        );

        done();
    });

    it('Hashtags', function(done) {
        assert.equal(
            TextFormatting.formatText('#test').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test'>#test</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test123').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test123'>#test123</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test-test').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test-test'>#test-test</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test_test').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test_test'>#test_test</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test.test').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test.test'>#test.test</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test1/#test2').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test1'>#test1</a>/<wbr /><a class='mention-link' href='#' data-hashtag='#test2'>#test2</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('(#test)').trim(),
            "<p>(<a class='mention-link' href='#' data-hashtag='#test'>#test</a>)</p>"
        );

        assert.equal(
            TextFormatting.formatText('#test-').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test'>#test</a>-</p>"
        );

        assert.equal(
            TextFormatting.formatText('#test.').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test'>#test</a>.</p>"
        );

        assert.equal(
            TextFormatting.formatText('This is a sentence #test containing a hashtag').trim(),
            "<p>This is a sentence <a class='mention-link' href='#' data-hashtag='#test'>#test</a> containing a hashtag</p>"
        );

        done();
    });

    it('Formatted hashtags', function(done) {
        assert.equal(
            TextFormatting.formatText('*#test*').trim(),
            "<p><em><a class='mention-link' href='#' data-hashtag='#test'>#test</a></em></p>"
        );

        assert.equal(
            TextFormatting.formatText('_#test_').trim(),
            "<p><em><a class='mention-link' href='#' data-hashtag='#test'>#test</a></em></p>"
        );

        assert.equal(
            TextFormatting.formatText('**#test**').trim(),
            "<p><strong><a class='mention-link' href='#' data-hashtag='#test'>#test</a></strong></p>"
        );

        assert.equal(
            TextFormatting.formatText('__#test__').trim(),
            "<p><strong><a class='mention-link' href='#' data-hashtag='#test'>#test</a></strong></p>"
        );

        assert.equal(
            TextFormatting.formatText('~~#test~~').trim(),
            "<p><del><a class='mention-link' href='#' data-hashtag='#test'>#test</a></del></p>"
        );

        assert.equal(
            TextFormatting.formatText('`#test`').trim(),
            '<p>' +
                '<span class="codespan__pre-wrap">' +
                    '<code>' +
                        '#test' +
                    '</code>' +
                '</span>' +
            '</p>'
        );

        assert.equal(
            TextFormatting.formatText('[this is a link #test](example.com)').trim(),
            '<p><a class="theme markdown__link" href="http://example.com" rel="noreferrer" target="_blank">this is a link #test</a></p>'
        );

        done();
    });

    it('Searching for hashtags', function(done) {
        assert.equal(
            TextFormatting.formatText('#test', {searchTerm: 'test'}).trim(),
            "<p><span class='search-highlight'><a class='mention-link' href='#' data-hashtag='#test'>#test</a></span></p>"
        );

        assert.equal(
            TextFormatting.formatText('#test', {searchTerm: '#test'}).trim(),
            "<p><span class='search-highlight'><a class='mention-link' href='#' data-hashtag='#test'>#test</a></span></p>"
        );

        assert.equal(
            TextFormatting.formatText('#foo/#bar', {searchTerm: '#foo'}).trim(),
            "<p><span class='search-highlight'><a class='mention-link' href='#' data-hashtag='#foo'>#foo</a></span>/<wbr /><a class='mention-link' href='#' data-hashtag='#bar'>#bar</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#foo/#bar', {searchTerm: 'bar'}).trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#foo'>#foo</a>/<wbr /><span class='search-highlight'><a class='mention-link' href='#' data-hashtag='#bar'>#bar</a></span></p>"
        );

        assert.equal(
            TextFormatting.formatText('not#test', {searchTerm: '#test'}).trim(),
            '<p>not#test</p>'
        );

        done();
    });

    it('Potential hashtags with other entities nested', function(done) {
        assert.equal(
            TextFormatting.formatText('#@test').trim(),
            '<p>#@test</p>'
        );

        let options = {
            usernameMap: {
                test: {id: '1234', username: 'test'}
            }
        };
        assert.equal(
            TextFormatting.formatText('#@test', options).trim(),
            "<p>#<a class='mention-link' href='#' data-mention='test'>@test</a></p>"
        );

        assert.equal(
            TextFormatting.formatText('#~test').trim(),
            '<p>#~test</p>'
        );

        options = {
            channelNamesMap: {
                test: {id: '1234', name: 'test', display_name: 'Test Channel'}
            },
            team: {id: 'abcd', name: 'abcd', display_name: 'Alphabet'}
        };
        assert.equal(
            TextFormatting.formatText('#~test', options).trim(),
            '<p>#~test</p>'
        );

        assert.equal(
            TextFormatting.formatText('#:taco:').trim(),
            '<p>#<span alt=":taco:" class="emoticon" title=":taco:" style="background-image:url(/static/emoji/taco.png)"></span></p>'
        );

        assert.equal(
            TextFormatting.formatText('#test@example.com').trim(),
            "<p><a class='mention-link' href='#' data-hashtag='#test'>#test</a>@example.com</p>"
        );

        done();
    });
});