Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Add tests and update link regex #9

Merged
merged 4 commits into from
May 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rich-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ function makeRichMessage (message, username) {
: 'https://github.com/' + message.username + '.png'
message.timeago = util.timeago(message.timestamp)

message.text = message.text.replace(/(^|\s)(#[a-zA-Z0-9]+)(?=$|\s)/g,
'$1[$2]($2)')
message.html = md.render(message.text)
message.html = message.html.replace(/\n/g, '<p></p>')
message.html = ghlink(message.html, { format: 'html' })
message.html = message.html.replace(/([> ])(#[a-zA-Z0-9]+)([ <])/g, '$1<a href="$2">$2</a>$3')

var highlight = (message.text.indexOf(username) !== -1)
var classStr = highlight ? ' class="highlight"' : ''
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"standard": "^3.7.2",
"tap-spec": "^3.0.0",
"tap-dot": "^1.0.0",
"tape": "^4.0.0"
},
"homepage": "https://github.com/moose-team/rich-message",
Expand All @@ -30,7 +30,7 @@
"url": "https://github.com/moose-team/rich-message.git"
},
"scripts": {
"test": "standard && tape test/*.js | tap-spec"
"test": "standard && tape test/*.js | tap-dot"
},
"dependencies": {
"ghlink": "^0.1.2",
Expand Down
141 changes: 141 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,142 @@
var test = require('tape')
var richMessage = require('../')
var mergeMessages = richMessage.mergeMessages

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)
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, 'static/cat.png')
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()
})

test('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()
})