Skip to content

Commit cf185ed

Browse files
author
nfrasser
committed
Additional tests for linkify-html
1 parent 5dd70fd commit cf185ed

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

test/spec/linkify-html-test.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,82 @@ var htmlOptions = require('./html/options');
33

44
describe('linkify-html', function () {
55

6+
var options = { // test options
7+
tagName: 'span',
8+
target: '_parent',
9+
nl2br: true,
10+
linkClass: 'my-linkify-class',
11+
defaultProtocol: 'https',
12+
linkAttributes: {
13+
rel: 'nofollow',
14+
onclick: 'javascript:;'
15+
},
16+
format: function (val) {
17+
return val.truncate(40);
18+
},
19+
formatHref: function (href, type) {
20+
if (type === 'email') {
21+
href += '?subject=Hello%20from%20Linkify';
22+
}
23+
return href;
24+
}
25+
},
26+
27+
// For each element in this array
28+
// [0] - Original text
29+
// [1] - Linkified with default options
30+
// [2] - Linkified with new options
31+
tests = [
32+
[
33+
'Test with no links',
34+
'Test with no links',
35+
'Test with no links'
36+
], [
37+
'The URL is google.com and the email is <strong>[email protected]</strong>',
38+
'The URL is <a href="http://google.com" class="linkified" target="_blank">google.com</a> and the email is <strong><a href="mailto:[email protected]" class="linkified">[email protected]</a></strong>',
39+
'The URL is <span href="https://google.com" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">google.com</span> and the email is <strong><span href="mailto:[email protected]?subject=Hello%20from%20Linkify" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">[email protected]</span></strong>'
40+
], [
41+
'Super long maps URL https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en, a #hash-tag, and an email: test."wut"[email protected]!',
42+
'Super long maps URL <a href="https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en" class="linkified" target="_blank">https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en</a>, a #hash-tag, and an email: <a href="mailto:test.&quot;wut&quot;[email protected]" class="linkified">test."wut"[email protected]</a>!',
43+
'Super long maps URL <span href="https://www.google.ca/maps/@43.472082,-80.5426668,18z?hl=en" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">https://www.google.ca/maps/@43.472082,-8…</span>, a #hash-tag, and an email: <span href="mailto:test.&quot;wut&quot;[email protected]?subject=Hello%20from%20Linkify" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">test."wut"[email protected]</span>!',
44+
], [
45+
'This link is already in an anchor tag <a href="#bro">google.com</a> LOL and this one <h1>isnt http://github.com</h1>',
46+
'This link is already in an anchor tag <a href="#bro">google.com</a> LOL and this one <h1>isnt <a href="http://github.com" class="linkified" target="_blank">http://github.com</a></h1>',
47+
'This link is already in an anchor tag <a href="#bro">google.com</a> LOL and this one <h1>isnt <span href="http://github.com" class="my-linkify-class" target="_parent" rel="nofollow" onclick="javascript:;">http://github.com</span></h1>'
48+
], [
49+
'Unterminated anchor tag <a href="http://google.com"> This <em>is a link google.com</em> and this works!! https://reddit.com/r/photography/',
50+
'Unterminated anchor tag <a href="http://google.com"> This <em>is a link google.com</em> and this works!! https://reddit.com/r/photography/',
51+
'Unterminated anchor tag <a href="http://google.com"> This <em>is a link google.com</em> and this works!! https://reddit.com/r/photography/'
52+
]
53+
];
54+
655
it('Works with default options', function () {
56+
tests.map(function (test) {
57+
expect(linkifyHtml(test[0])).to.be.eql(test[1]);
58+
});
59+
});
60+
61+
it('Works with overriden options', function () {
62+
tests.map(function (test) {
63+
debugger;
64+
expect(linkifyHtml(test[0], options)).to.be.eql(test[2]);
65+
});
66+
});
67+
68+
it('Works with HTML and default options', function () {
769
var linkified = linkifyHtml(htmlOptions.original);
870
expect(htmlOptions.linkified).to.contain(linkified);
971
});
1072

11-
it('Works with overriden options', function () {
73+
it('Works with HTML and overriden options', function () {
1274
var linkified = linkifyHtml(
1375
htmlOptions.original,
1476
htmlOptions.altOptions
1577
);
1678
expect(htmlOptions.linkifiedAlt).to.contain(linkified);
1779
});
1880

81+
82+
83+
1984
});

0 commit comments

Comments
 (0)