]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/Linkifier.test.ts
Merge pull request #571 from Tyriar/570_link_validation
[mirror_xterm.js.git] / src / Linkifier.test.ts
1 /**
2 * @license MIT
3 */
4 import jsdom = require('jsdom');
5 import { assert } from 'chai';
6 import { ITerminal, ILinkifier } from './Interfaces';
7 import { Linkifier } from './Linkifier';
8
9 class TestLinkifier extends Linkifier {
10 constructor(document: Document, rows: HTMLElement[]) {
11 Linkifier.TIME_BEFORE_LINKIFY = 0;
12 super(document, rows);
13 }
14 }
15
16 describe('Linkifier', () => {
17 let window: Window;
18 let document: Document;
19
20 let container: HTMLElement;
21 let rows: HTMLElement[];
22 let linkifier: ILinkifier;
23
24 beforeEach(done => {
25 rows = [];
26 jsdom.env('', (err, w) => {
27 window = w;
28 document = window.document;
29 linkifier = new Linkifier(document, rows);
30 container = document.createElement('div');
31 document.body.appendChild(container);
32 done();
33 });
34 });
35
36 function addRow(text: string) {
37 const element = document.createElement('div');
38 element.textContent = text;
39 container.appendChild(element);
40 rows.push(element);
41 }
42
43 function clickElement(element: Node) {
44 const event = document.createEvent('MouseEvent');
45 event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
46 element.dispatchEvent(event);
47 }
48
49 describe('validationCallback', () => {
50 it('should enable link if true', done => {
51 addRow('test');
52 linkifier.registerLinkMatcher(/test/, () => done(), {
53 validationCallback: (url, cb) => {
54 cb(true);
55 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
56 setTimeout(() => clickElement(rows[0].firstChild), 0);
57 }
58 });
59 linkifier.linkifyRow(0);
60 });
61
62 it('should disable link if false', done => {
63 addRow('test');
64 linkifier.registerLinkMatcher(/test/, () => assert.fail(), {
65 validationCallback: (url, cb) => {
66 cb(false);
67 assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A');
68 setTimeout(() => clickElement(rows[0].firstChild), 0);
69 }
70 });
71 linkifier.linkifyRow(0);
72 // Allow time for the click to be performed
73 setTimeout(() => done(), 10);
74 });
75 });
76 });