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 {
11 constructor(document: Document, rows: HTMLElement[]) {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
13 super(document, rows);
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;
29 jsdom.env('', (err, w) => {
31 document = window.document;
32 linkifier = new TestLinkifier(document, rows);
33 container = document.createElement('div');
34 document.body.appendChild(container);
39 function addRow(text: string) {
40 const element = document.createElement('div');
41 element.textContent = text;
42 container.appendChild(element);
46 function clickElement(element: Node) {
47 const event = document.createEvent('MouseEvent');
48 event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
49 element.dispatchEvent(event);
52 function assertLinkifiesEntireRow(uri: string, done: MochaDone) {
54 linkifier.linkifyRow(0);
56 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
57 assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri);
62 describe('http links', () => {
63 it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done));
66 describe('validationCallback', () => {
67 it('should enable link if true', done => {
69 linkifier.registerLinkMatcher(/test/, () => done(), {
70 validationCallback: (url, cb) => {
72 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
73 setTimeout(() => clickElement(rows[0].firstChild), 0);
76 linkifier.linkifyRow(0);
79 it('should disable link if false', done => {
81 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
82 validationCallback: (url, cb) => {
84 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
85 setTimeout(() => clickElement(rows[0].firstChild), 0);
88 linkifier.linkifyRow(0);
89 // Allow time for the click to be performed
90 setTimeout(() => done(), 10);
94 describe('priority', () => {
95 it('should order the list from highest priority to lowest #1', () => {
96 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
97 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
98 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
101 it('should order the list from highest priority to lowest #2', () => {
102 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
103 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
104 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
107 it('should order items of equal priority in the order they are added', () => {
108 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
109 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
110 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);