-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-tests.js
130 lines (103 loc) · 3.42 KB
/
main-tests.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
const { throws, deepEqual } = require('assert');
const mod = require('./main.js');
import { JSDOM } from 'jsdom';
describe('OLSKDOMMetadata', function test_OLSKDOMMetadata () {
const _OLSKDOMMetadata = function (inputData) {
return mod.OLSKDOMMetadata(inputData, {
JSDOM: JSDOM.fragment,
});
};
it('throws if not string', function () {
throws(function () {
mod.OLSKDOMMetadata(null);
}, /OLSKErrorInputNotValid/);
});
it('returns object', function () {
deepEqual(_OLSKDOMMetadata(''), {});
});
it('extracts title', function () {
const title = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<title>${ title }</title>`), {
title,
});
});
it('extracts meta:name', function () {
const name = Math.random().toString();
const content = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<meta name="${ name }" content="${ content }" />`), {
[name]: content,
});
});
it('extracts meta:property', function () {
const property = Math.random().toString();
const content = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<meta property="${ property }" content="${ content }" />`), {
[property]: content,
});
});
it('extracts meta:itemprop', function () {
const itemprop = Math.random().toString();
const content = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<meta itemprop="${ itemprop }" content="${ content }" />`), {
[itemprop]: content,
});
});
it('extracts link:itemprop', function () {
const itemprop = Math.random().toString();
const href = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<link itemprop="${ itemprop }" href="${ href }" />`), {
[itemprop]: href,
});
});
it('extracts *:itemprop', function () {
const tag = uRandomElement('meta', 'link');
const itemprop = Math.random().toString();
const content = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<${ tag } itemprop="${ itemprop }" content="${ content }" />`), {
[itemprop]: content,
});
});
it('extracts json-ld', function () {
const key = Math.random().toString();
const value = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<script type="application/ld+json">[{"${ key }":"${ value }"}]</script>`), {
[key]: value,
});
});
it('extracts link:rel', function () {
const rel = Math.random().toString();
const href = Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<link rel="${ rel }" href="${ href }" />`), {
[rel]: href,
});
});
context('_OLSKDOMMetadataFunding', function () {
mod.OLSKDOMMetadataFundingDomains().forEach(function (domain) {
context(domain, function () {
const link = `${ uRandomElement('http', 'https') }://${ domain }/`;
it('excludes if no info', function () {
deepEqual(_OLSKDOMMetadata(`<a href="${ link }"></a>`), {});
});
it('includes if info', function () {
const item = link + Math.random().toString();
deepEqual(_OLSKDOMMetadata(`<a href="${ item }"></a>`), {
_OLSKDOMMetadataFunding: [item],
});
});
});
});
it('excludes if relative', function () {
deepEqual(_OLSKDOMMetadata(`<a href="/${ Math.random().toString() }"></a>`), {});
});
});
});
describe('OLSKDOMMetadataFundingDomains', function test_OLSKDOMMetadataFundingDomains () {
it('returns array', function () {
deepEqual(mod.OLSKDOMMetadataFundingDomains(), [
'opencollective.com',
'github.com/sponsors',
'patreon.com',
'liberapay.com',
]);
});
});