4 import jsdom = require('jsdom');
5 import { assert } from 'chai';
6 import { ITerminal, ILinkifier } from './Interfaces';
7 import { Linkifier } from './Linkifier';
8 import { LinkMatcher } from './Types';
10 class TestLinkifier extends Linkifier {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
16 public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
19 describe('Linkifier', () => {
21 let document: Document;
23 let container: HTMLElement;
24 let rows: HTMLElement[];
25 let linkifier: TestLinkifier;
28 jsdom.env('', (err, w) => {
30 document = window.document;
31 linkifier = new TestLinkifier();
36 describe('before attachToDom', () => {
37 it('should allow link matcher registration', done => {
38 assert.doesNotThrow(() => {
39 const linkMatcherId = linkifier.registerLinkMatcher(/foo/, () => {});
40 assert.isTrue(linkifier.deregisterLinkMatcher(linkMatcherId));
46 describe('after attachToDom', () => {
49 linkifier.attachToDom(document, rows);
50 container = document.createElement('div');
51 document.body.appendChild(container);
54 function addRow(text: string) {
55 const element = document.createElement('div');
56 element.textContent = text;
57 container.appendChild(element);
61 function clickElement(element: Node) {
62 const event = document.createEvent('MouseEvent');
63 event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
64 element.dispatchEvent(event);
67 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
69 linkifier.linkifyRow(0);
71 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
72 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
77 describe('http links', () => {
78 it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
81 describe('validationCallback', () => {
82 it('should enable link if true', done => {
84 linkifier.registerLinkMatcher(/test/, () => done(), {
85 validationCallback: (url, cb) => {
87 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
88 setTimeout(() => clickElement(rows[0].firstChild), 0);
91 linkifier.linkifyRow(0);
94 it('should disable link if false', done => {
96 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
97 validationCallback: (url, cb) => {
99 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
100 setTimeout(() => clickElement(rows[0].firstChild), 0);
103 linkifier.linkifyRow(0);
104 // Allow time for the click to be performed
105 setTimeout(() => done(), 10);
109 describe('priority', () => {
110 it('should order the list from highest priority to lowest #1', () => {
111 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
112 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
113 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
116 it('should order the list from highest priority to lowest #2', () => {
117 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
118 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
119 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
122 it('should order items of equal priority in the order they are added', () => {
123 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
124 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
125 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);