This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
169 lines (141 loc) · 3.83 KB
/
test.js
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
var test = require('tape')
var richMessage = require('./')
var mergeMessages = richMessage.mergeMessages
var CAT_URL = 'https://cdn.rawgit.com/moose-team/rich-message/master/cat.png'
test('usage example', function (t) {
var message = {
text: 'hi maxogden', // text entered by a user
username: 'mafintosh', // user's github name
timestamp: Date.now()
}
var username = 'maxogden' // current user's github name (used for highlighting)
var output = richMessage(message, username)
var expected = {
text: 'hi maxogden',
username: 'mafintosh',
anon: false,
avatar: 'https://github.com/mafintosh.png',
timeago: richMessage.timeago(message.timestamp),
html: '<div class="highlight"><p>hi maxogden</p><p></p></div>'
}
t.equal(output.html, expected.html, 'html is correctly formatted')
t.false(output.anon, 'user is not anon')
t.equal(output.timeago, expected.timeago, 'timeago works as expected')
t.end()
})
test('link replacement', function (t) {
var message = {
text: '#cats #cats #cats not#cat #cat!%$',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
var expected = '<div><p>' +
'<a href="#cats">#cats</a> <a href="#cats">#cats</a> ' +
'<a href="#cats">#cats</a> not#cat #cat!%$' +
'</p><p></p></div>'
t.equal(output.html, expected, 'links are replaced in output html')
t.end()
})
test('github avatars', function (t) {
var message = {
text: 'i like cats',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
t.equal(output.avatar, 'https://github.com/cat.png')
t.end()
})
test('anon avatars', function (t) {
var message = {
text: 'i like cats',
username: 'Anonymous cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
t.equal(output.avatar, CAT_URL)
t.end()
})
test('github links', function (t) {
var message = {
text: 'cats isaacs/npm#1234',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
var expected = '<div><p>' +
'cats <a href="https://github.com/isaacs/npm/issues/1234">' +
'isaacs/npm#1234</a>' +
'</p><p></p></div>'
t.equal(output.html, expected)
t.end()
})
test('newlines -> paragraphs', function (t) {
var message = {
text: 'cat\ncat',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
var expected = '<div><p>' +
'cat<p></p>cat' +
'</p><p></p></div>'
t.equal(output.html, expected)
t.end()
})
test('username highlight', function (t) {
var message = {
text: 'cat',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'cat')
var expected = '<div class="highlight"><p>' +
'cat' +
'</p><p></p></div>'
t.equal(output.html, expected)
t.end()
})
// bug: moment does not calculate time reliably in different environments
// on travis, 0 returns 1970, on mac os x, 1969
test.skip('timeago', function (t) {
var message = {
text: 'cat',
username: 'cat',
timestamp: 0
}
var output = richMessage(message, 'cat')
t.equal(output.timeago, '01/01/1970')
t.end()
})
test('markdown render', function (t) {
var message = {
text: '`cat` **cat**',
username: 'cat',
timestamp: Date.now()
}
var output = richMessage(message, 'other_cat')
var expected = '<div><p>' +
'<code>cat</code> <strong>cat</strong>' +
'</p><p></p></div>'
t.equal(output.html, expected)
t.end()
})
test('merge messages', function (t) {
var message1 = {
text: 'cat1',
html: '<p>cat1</p>'
}
var message2 = {
text: 'cat2',
html: '<p>cat2</p>'
}
var output = mergeMessages(message1, message2)
var expected = {
text: 'cat1\ncat2',
html: '<p>cat1</p><p></p><p>cat2</p>'
}
t.deepEqual(output, expected)
t.end()
})