]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/Linkifier.test.ts
Merge pull request #600 from sourcelair/Tyriar-patch-1
[mirror_xterm.js.git] / src / Linkifier.test.ts
CommitLineData
26ebc3d9
DI
1/**
2 * @license MIT
3 */
4import jsdom = require('jsdom');
5import { assert } from 'chai';
6import { ITerminal, ILinkifier } from './Interfaces';
7import { Linkifier } from './Linkifier';
7ac4f1a9 8import { LinkMatcher } from './Types';
26ebc3d9 9
15d79143
DI
10class TestLinkifier extends Linkifier {
11 constructor(document: Document, rows: HTMLElement[]) {
12 Linkifier.TIME_BEFORE_LINKIFY = 0;
13 super(document, rows);
14 }
7ac4f1a9
DI
15
16 public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; }
15d79143 17}
26ebc3d9
DI
18
19describe('Linkifier', () => {
20 let window: Window;
21 let document: Document;
22
23 let container: HTMLElement;
24 let rows: HTMLElement[];
7ac4f1a9 25 let linkifier: TestLinkifier;
26ebc3d9
DI
26
27 beforeEach(done => {
28 rows = [];
29 jsdom.env('', (err, w) => {
30 window = w;
31 document = window.document;
7ac4f1a9 32 linkifier = new TestLinkifier(document, rows);
26ebc3d9
DI
33 container = document.createElement('div');
34 document.body.appendChild(container);
35 done();
36 });
37 });
38
39 function addRow(text: string) {
40 const element = document.createElement('div');
41 element.textContent = text;
42 container.appendChild(element);
43 rows.push(element);
44 }
45
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);
50 }
51
52 describe('validationCallback', () => {
53 it('should enable link if true', done => {
54 addRow('test');
55 linkifier.registerLinkMatcher(/test/, () => done(), {
56 validationCallback: (url, cb) => {
57 cb(true);
58 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
59 setTimeout(() => clickElement(rows[0].firstChild), 0);
60 }
61 });
62 linkifier.linkifyRow(0);
63 });
64
65 it('should disable link if false', done => {
66 addRow('test');
67 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
68 validationCallback: (url, cb) => {
69 cb(false);
70 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
71 setTimeout(() => clickElement(rows[0].firstChild), 0);
72 }
73 });
74 linkifier.linkifyRow(0);
75 // Allow time for the click to be performed
76 setTimeout(() => done(), 10);
77 });
78 });
7ac4f1a9
DI
79
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]);
85 });
86
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]);
91 });
92
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]);
97 });
98 });
26ebc3d9 99});