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 describe('validationCallback', () => {
53 it('should enable link if true', done => {
55 linkifier.registerLinkMatcher(/test/, () => done(), {
56 validationCallback: (url, element, cb) => {
58 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
59 setTimeout(() => clickElement(rows[0].firstChild), 0);
62 linkifier.linkifyRow(0);
65 it('should disable link if false', done => {
67 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
68 validationCallback: (url, element, cb) => {
70 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
71 setTimeout(() => clickElement(rows[0].firstChild), 0);
74 linkifier.linkifyRow(0);
75 // Allow time for the click to be performed
76 setTimeout(() => done(), 10);
80 describe('priority', () => {
81 it('should order the list from highest priority to lowest #1', () => {
82 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 1 });
83 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: -1 });
84 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [aId, 0, bId]);
87 it('should order the list from highest priority to lowest #2', () => {
88 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: -1 });
89 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 1 });
90 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [bId, 0, aId]);
93 it('should order items of equal priority in the order they are added', () => {
94 const aId = linkifier.registerLinkMatcher(/a/, () => {}, { priority: 0 });
95 const bId = linkifier.registerLinkMatcher(/b/, () => {}, { priority: 0 });
96 assert.deepEqual(linkifier.linkMatchers.map(lm => lm.id), [0, aId, bId]);