forked from foiseworth/external-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
163 lines (142 loc) · 4.81 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* eslint-env mocha */
'use strict';
var external = require('./index.js');
var jsdom = require('jsdom');
var extend = require('extend');
var util = require('util');
var assert = require('assert');
function makeLinkNode(options, cb) {
options = extend(false, {
url: 'http://aboutandrew.co.uk',
pageUrl: 'http://github.com',
rel: '',
target: ''
}, options);
jsdom.env({
url: options.pageUrl,
html: util.format('<a href="%s" target="%s" rel="%s"></a>', options.url, options.target, options.rel),
done: function(err, window) {
if (err) cb(err);
cb(null, window.document.querySelector('a'), window);
}
});
}
describe('Make link node', function() {
it('should make a link with target and rel set', function(done) {
makeLinkNode({url: 'http://aboutandrew.co.uk', target: '_blank', rel: 'external'}, function(err, link) {
assert.ifError(err);
assert.equal(link.getAttribute('href'), 'http://aboutandrew.co.uk');
assert.equal(link.getAttribute('target'), '_blank');
assert.equal(link.getAttribute('rel'), 'external');
done();
});
});
it('should make a link without target and rel set', function(done) {
makeLinkNode({url: 'http://aboutandrew.co.uk'}, function(err, link) {
assert.ifError(err);
assert.equal(link.getAttribute('href'), 'http://aboutandrew.co.uk');
assert.equal(link.getAttribute('rel'), '');
assert.equal(link.getAttribute('target'), '');
done();
});
});
it('should set the current domain', function(done) {
makeLinkNode({pageUrl: 'http://somewhere.co.uk/awesome'}, function(err, link, window) {
assert.ifError(err);
assert.equal(window.location.href, 'http://somewhere.co.uk/awesome');
done();
});
});
});
describe('External link', function() {
it('should know a absoloute relative link is not external', function(done) {
makeLinkNode({url: '/somewhere'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know a link without a href is not external', function(done) {
makeLinkNode({url: ''}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know a named anchor link on the current page is not external', function(done) {
makeLinkNode({url: '#awesome'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know a named anchor link on another page is not external', function(done) {
makeLinkNode({url: 'somedire/page#awesome'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know index.html is not external', function(done) {
makeLinkNode({url: 'index.html'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know ./index.html is not external', function(done) {
makeLinkNode({url: './index.html'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know somedir/page is not external', function(done) {
makeLinkNode({url: 'somedir/page'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know ../index.html is not external', function(done) {
makeLinkNode({url: '../index.html'}, function(err, link) {
assert.equal(external(link), false);
done();
});
});
it('should know a absoloute link is external', function(done) {
makeLinkNode({url: 'http://another-app.com'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a absoloute secure link is external', function(done) {
makeLinkNode({url: 'https://github.com/foiseworth'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a absoloute protocoless link is external', function(done) {
makeLinkNode({url: '//github.com/foiseworth'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a telephone link is external', function(done) {
makeLinkNode({url: 'tel:0123456789'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a mailto link is external', function(done) {
makeLinkNode({url: 'mailto:[email protected]'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a link with target="_blank" is external', function(done) {
makeLinkNode({url: 'http://github.com', target: '_blank'}, function(err, link) {
assert(external(link));
done();
});
});
it('should know a link with rel="external" is external', function(done) {
makeLinkNode({url: 'http://github.com', rel: 'external'}, function(err, link) {
assert(external(link));
done();
});
});
});