-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.js
176 lines (162 loc) · 4.68 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
164
165
166
167
168
169
170
171
172
173
174
175
176
import assert from 'node:assert/strict'
import {URL} from 'node:url'
import test from 'node:test'
import {h} from 'hastscript'
import {rehype} from 'rehype'
import min from 'rehype-minify-url'
test('rehype-minify-url', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('rehype-minify-url')).sort(), [
'default'
])
})
await t.test('should throw w/o options', async function () {
assert.throws(function () {
rehype().use(min).processSync('')
}, /^Error: Missing absolute `from` in options$/)
})
await t.test('should throw with relative `from`', async function () {
assert.throws(function () {
rehype().use(min, {from: '/'}).processSync('')
}, /^Error: Missing absolute `from` in options$/)
})
const options = {from: 'http://example.com/one/alpha/'}
await t.test(
'should work w/ an absolute URL -> relative path',
async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [
h('a', {href: 'http://example.com/one/bravo/index.html'})
])
),
h(undefined, [h('a', {href: '../bravo/'})])
)
}
)
await t.test(
'should work w/ an absolute URL -> absolute path',
async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [
h('a', {href: 'http://example.com/two/charlie/index.html'})
])
),
h(undefined, [h('a', {href: '/two/charlie/'})])
)
}
)
await t.test(
'should work w/ an absolute URL -> empty string, if it’s equal to `from`',
async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(h(undefined, [h('a', {href: options.from})])),
h(undefined, [h('a', {href: ''})])
)
}
)
await t.test('should work w/ https', async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [h('a', {href: options.from.replace(/http/, 'https')})])
),
h(undefined, [h('a', {href: options.from.replace(/http/, 'https')})])
)
})
await t.test('should drop port `80`', async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(h(undefined, [h('a', {href: 'http://google.com:80/alpha'})])),
h(undefined, [h('a', {href: '//google.com/alpha'})])
)
})
await t.test('should understand when the top is reached', async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [h('a', {href: '../../../../../../../../#anchor'})])
),
h(undefined, [h('a', {href: '/#anchor'})])
)
})
await t.test('should support lists', async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [
h('a', {href: false, ping: ['../../foo', '../../bar']})
])
),
h(undefined, [h('a', {href: false, ping: ['/foo', '/bar']})])
)
})
await t.test('should ignore non-strings', async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(h(undefined, [h('a', {href: true})])),
h(undefined, [h('a', {href: true})])
)
})
await t.test(
'should keep `href` on `link[rel=canonical]`',
async function () {
assert.deepEqual(
rehype()
.use(min, options)
.runSync(
h(undefined, [
{
type: 'element',
tagName: 'link',
properties: {href: options.from, rel: ['canonical']},
children: []
}
])
),
h(undefined, [
{
type: 'element',
tagName: 'link',
properties: {href: options.from, rel: ['canonical']},
children: []
}
])
)
}
)
await t.test(
'should support `data.meta.{origin,pathname}`',
async function () {
assert.deepEqual(
rehype()
.use(function () {
return function (_, file) {
const url = new URL(options.from)
// From: <https://github.com/rehypejs/rehype-meta#configorigin>
file.data.meta = {origin: url.origin, pathname: url.pathname}
}
})
.use(min)
.runSync(
h(undefined, [
h('a', {href: 'http://example.com/one/bravo/index.html'})
])
),
h(undefined, [h('a', {href: '../bravo/'})])
)
}
)
})