-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure linkify.find respects validate option (#458)
- Loading branch information
Showing
4 changed files
with
103 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ if (!doc) { | |
} | ||
|
||
describe('linkify-jquery', function () { | ||
|
||
// Sometimes jQuery is slow to load | ||
this.timeout(10000); | ||
|
||
|
@@ -24,9 +23,7 @@ describe('linkify-jquery', function () { | |
This code allows testing on Node.js and on Browser environments | ||
*/ | ||
before(function (done) { | ||
|
||
function onDoc($, doc) { | ||
|
||
doc.body.innerHTML = htmlOptions.extra; | ||
|
||
// Add the linkify plugin to jQuery | ||
|
@@ -40,13 +37,18 @@ describe('linkify-jquery', function () { | |
done(); | ||
} | ||
|
||
if (doc) { return onDoc($, doc); } | ||
if (doc) { | ||
return onDoc($, doc); | ||
} | ||
// no document element, use a virtual dom to test | ||
|
||
let dom = new JSDOM('<html><head><title>Linkify Test</title></head><body><script src="https://code.jquery.com/jquery.js"></script></body></html>', { | ||
runScripts: 'dangerously', | ||
resources: 'usable' | ||
}); | ||
let dom = new JSDOM( | ||
'<html><head><title>Linkify Test</title></head><body><script src="https://code.jquery.com/jquery.js"></script></body></html>', | ||
{ | ||
runScripts: 'dangerously', | ||
resources: 'usable', | ||
}, | ||
); | ||
doc = dom.window.document; | ||
dom.window.onload = () => { | ||
$ = dom.window.jQuery; | ||
|
@@ -55,44 +57,42 @@ describe('linkify-jquery', function () { | |
}); | ||
|
||
// Make sure we start out with a fresh DOM every time | ||
beforeEach(() => testContainer.innerHTML = htmlOptions.original); | ||
beforeEach(() => (testContainer.innerHTML = htmlOptions.original)); | ||
|
||
it('Works with the DOM Data API', () => { | ||
expect($('header').first().html()).to.be.eql( | ||
'Have a link to:<br><a href="https://github.com">github.com</a>!' | ||
); | ||
expect($('header').first().html()).to.be.eql('Have a link to:<br><a href="https://github.com">github.com</a>!'); | ||
expect($('#linkify-test-div').html()).to.be.eql( | ||
'Another <i href="mailto:[email protected]" class="test-class" ' + | ||
'target="_parent">[email protected]</i> email as well as a <i '+ | ||
'href="http://t.co" class="test-class" target="_parent">' + | ||
'http://t.co</i> link.' | ||
'target="_parent">[email protected]</i> email as well as a <i ' + | ||
'href="http://t.co" class="test-class" target="_parent">' + | ||
'http://t.co</i> link.', | ||
); | ||
}); | ||
|
||
it('Works with default options', () => { | ||
var $container = $('#linkify-jquery-test-container'); | ||
expect(($container.length)).to.be.eql(1); | ||
expect($container.length).to.be.eql(1); | ||
var result = $container.linkify(); | ||
// `should` is not defined on jQuery objects | ||
expect((result === $container)).to.be.ok; // should return the same element | ||
expect(result === $container).to.be.ok; // should return the same element | ||
expect($container.html()).to.be.oneOf(htmlOptions.linkified); | ||
}); | ||
|
||
it('Works with overriden options (general)', () => { | ||
var $container = $('#linkify-jquery-test-container'); | ||
expect(($container.length)).to.be.eql(1); | ||
expect($container.length).to.be.eql(1); | ||
var result = $container.linkify(htmlOptions.altOptions); | ||
// `should` is not defined on jQuery objects | ||
expect((result === $container)).to.be.ok; // should return the same element | ||
expect(result === $container).to.be.ok; // should return the same element | ||
expect($container.html()).to.be.oneOf(htmlOptions.linkifiedAlt); | ||
}); | ||
|
||
it('Works with overriden options (validate)', () => { | ||
var $container = $('#linkify-jquery-test-container'); | ||
expect(($container.length)).to.be.eql(1); | ||
expect($container.length).to.be.eql(1); | ||
var result = $container.linkify(htmlOptions.validateOptions); | ||
// `should` is not defined on jQuery objects | ||
expect((result === $container)).to.be.ok; // should return the same element | ||
expect(result === $container).to.be.ok; // should return the same element | ||
expect($container.html()).to.be.oneOf(htmlOptions.linkifiedValidate); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,25 +94,29 @@ describe('linkifyjs', () => { | |
}); | ||
|
||
it('Find the link', () => { | ||
expect(linkify.find('hello.world!')).to.deep.eql([{ | ||
type: 'url', | ||
value: 'hello.world', | ||
href: 'http://hello.world', | ||
isLink: true, | ||
start: 0, | ||
end: 11 | ||
}]); | ||
expect(linkify.find('hello.world!')).to.deep.eql([ | ||
{ | ||
type: 'url', | ||
value: 'hello.world', | ||
href: 'http://hello.world', | ||
isLink: true, | ||
start: 0, | ||
end: 11, | ||
}, | ||
]); | ||
}); | ||
|
||
it('Find the link of the specific type', () => { | ||
expect(linkify.find('For help with github.com, please contact [email protected]', 'email')).to.deep.eql([{ | ||
type: 'email', | ||
value: '[email protected]', | ||
href: 'mailto:[email protected]', | ||
isLink: true, | ||
start: 41, | ||
end: 60 | ||
}]); | ||
expect(linkify.find('For help with github.com, please contact [email protected]', 'email')).to.deep.eql([ | ||
{ | ||
type: 'email', | ||
value: '[email protected]', | ||
href: 'mailto:[email protected]', | ||
isLink: true, | ||
start: 41, | ||
end: 60, | ||
}, | ||
]); | ||
}); | ||
|
||
it('Finds with opts', () => { | ||
|
@@ -123,27 +127,52 @@ describe('linkifyjs', () => { | |
isLink: true, | ||
href: 'http://www.truncate.com', | ||
start: 5, | ||
end: 21 | ||
} | ||
end: 21, | ||
}, | ||
]); | ||
}); | ||
|
||
it('Finds type and opts', () => { | ||
expect(linkify.find('Does www.truncate.com work with [email protected]?', 'email', { truncate: 10 })).to.deep.eql([ | ||
expect( | ||
linkify.find('Does www.truncate.com work with [email protected]?', 'email', { truncate: 10 }), | ||
).to.deep.eql([ | ||
{ | ||
type: 'email', | ||
value: 'example@tr…', | ||
isLink: true, | ||
href: 'mailto:[email protected]', | ||
start: 32, | ||
end: 52 | ||
} | ||
end: 52, | ||
}, | ||
]); | ||
}); | ||
|
||
it('Throws on ambiguous invocation', () => { | ||
expect(() => linkify.find('Hello.com', { type: 'email' }, { truncate: 10 })).to.throw(); | ||
}); | ||
|
||
it('Uses validation to ignore links', () => { | ||
expect( | ||
linkify.find('foo.com and bar.com and baz.com', { validate: (url) => url !== 'bar.com' }), | ||
).to.deep.eql([ | ||
{ | ||
type: 'url', | ||
value: 'foo.com', | ||
isLink: true, | ||
href: 'http://foo.com', | ||
start: 0, | ||
end: 7, | ||
}, | ||
{ | ||
end: 31, | ||
href: 'http://baz.com', | ||
isLink: true, | ||
start: 24, | ||
type: 'url', | ||
value: 'baz.com', | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('test', () => { | ||
|
@@ -167,7 +196,7 @@ describe('linkifyjs', () => { | |
['mailto:[email protected]', true, 'url'], | ||
['t.co', true], | ||
['t.co g.co', false], // can only be one | ||
['[email protected] t.co', false] // can only be one | ||
['[email protected] t.co', false], // can only be one | ||
]; | ||
|
||
it('is a function', () => { | ||
|