]>
Commit | Line | Data |
---|---|---|
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 | import { LinkMatcher } from './Types'; | |
9 | ||
10 | class TestLinkifier extends Linkifier { | |
11 | constructor(document: Document, rows: HTMLElement[]) { | |
12 | Linkifier.TIME_BEFORE_LINKIFY = 0; | |
13 | super(document, rows); | |
14 | } | |
15 | ||
16 | public get linkMatchers(): LinkMatcher[] { return this._linkMatchers; } | |
17 | } | |
18 | ||
19 | describe('Linkifier', () => { | |
20 | let window: Window; | |
21 | let document: Document; | |
22 | ||
23 | let container: HTMLElement; | |
24 | let rows: HTMLElement[]; | |
25 | let linkifier: TestLinkifier; | |
26 | ||
27 | beforeEach(done => { | |
28 | rows = []; | |
29 | jsdom.env('', (err, w) => { | |
30 | window = w; | |
31 | document = window.document; | |
32 | linkifier = new TestLinkifier(document, rows); | |
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 | function assertLinkifiesEntireRow(uri: string, done: MochaDone) { | |
53 | addRow(uri); | |
54 | linkifier.linkifyRow(0); | |
55 | setTimeout(() => { | |
56 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); | |
57 | assert.equal((<HTMLElement>rows[0].firstChild).textContent, uri); | |
58 | done(); | |
59 | }, 0); | |
60 | } | |
61 | ||
62 | describe('http links', () => { | |
63 | it('should allow ~ character in URI path', done => assertLinkifiesEntireRow('http://foo.com/a~b#c~d?e~f', done)); | |
64 | }); | |
65 | ||
66 | describe('validationCallback', () => { | |
67 | it('should enable link if true', done => { | |
68 | addRow('test'); | |
69 | linkifier.registerLinkMatcher(/test/, () => done(), { | |
70 | validationCallback: (url, cb) => { | |
71 | cb(true); | |
72 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); | |
73 | setTimeout(() => clickElement(rows[0].firstChild), 0); | |
74 | } | |
75 | }); | |
76 | linkifier.linkifyRow(0); | |
77 | }); | |
78 | ||
79 | it('should disable link if false', done => { | |
80 | addRow('test'); | |
81 | linkifier.registerLinkMatcher(/test/, () => assert.fail(), { | |
82 | validationCallback: (url, cb) => { | |
83 | cb(false); | |
84 | assert.equal((<HTMLElement>rows[0].firstChild).tagName, 'A'); | |
85 | setTimeout(() => clickElement(rows[0].firstChild), 0); | |
86 | } | |
87 | }); | |
88 | linkifier.linkifyRow(0); | |
89 | // Allow time for the click to be performed | |
90 | setTimeout(() => done(), 10); | |
91 | }); | |
92 | ||
93 | it('should trigger for multiple link matches on one row', done => { | |
94 | addRow('test test'); | |
95 | let count = 0; | |
96 | linkifier.registerLinkMatcher(/test/, () => assert.fail(), { | |
97 | validationCallback: (url, cb) => { | |
98 | count += 1; | |
99 | if (count === 2) { | |
100 | done(); | |
101 | } | |
102 | cb(false); | |
103 | } | |
104 | }); | |
105 | linkifier.linkifyRow(0); | |
106 | }); | |
107 | }); | |
108 | ||
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]); | |
114 | }); | |
115 | ||
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]); | |
120 | }); | |
121 | ||
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]); | |
126 | }); | |
127 | }); | |
128 | }); |