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
|
import { domHref, parseHref } from '../../helpers/links'
global.OC = {
Util: {
History: {
parseUrlQuery: () => ({
dir: '/Wiki',
}),
},
},
}
describe('Preparing href attributes for the DOM', () => {
test('leave empty hrefs alone', () => {
expect(domHref({attrs: {href: ''}})).toBe('')
})
test('leave undefined hrefs alone', () => {
expect(domHref({attrs: {}})).toBe(undefined)
})
test('full url', () => {
expect(domHref({attrs: {href: 'https://otherdomain.tld'}}))
.toBe('https://otherdomain.tld')
})
test('other protocol', () => {
expect(domHref({attrs: {href: 'mailTo:test@mail.example'}}))
.toBe('mailTo:test@mail.example')
})
test('relative link with fileid', () => {
expect(domHref({attrs: {href: 'otherfile?fileId=123'}}))
.toBe('?dir=/Wiki&openfile=123&relPath=otherfile')
})
test('relative path with ../', () => {
expect(domHref({attrs: {href: '../other/otherfile?fileId=123'}}))
.toBe('?dir=/other&openfile=123&relPath=../other/otherfile')
})
test('absolute path', () => {
expect(domHref({attrs: {href: '/other/otherfile?fileId=123'}}))
.toBe('?dir=/other&openfile=123&relPath=/other/otherfile')
})
test('absolute path', () => {
expect(domHref({attrs: {href: '/otherfile?fileId=123'}}))
.toBe('?dir=/&openfile=123&relPath=/otherfile')
})
})
describe('Extracting short urls from the DOM', () => {
const domStub = (href) => ({
getAttribute() {
return href
},
})
test('leave empty hrefs alone', () => {
expect(parseHref(domStub(''))).toBe('')
})
test('leave undefined hrefs alone', () => {
expect(parseHref(domStub())).toBe(undefined)
})
test('relative link with fileid', () => {
expect(parseHref(domStub('?dir=/other&openfile=123&relPath=../other/otherfile')))
.toBe('../other/otherfile?fileId=123')
})
})
describe('Inserting hrefs into the dom and extracting them again', () => {
function insertAndExtract(attrs) {
const node = {attrs}
const dom = {
getAttribute() {
return domHref(node)
},
}
return parseHref(dom)
}
test('leave empty hrefs alone', () => {
expect(insertAndExtract({href: ''})).toBe('')
})
test('leave undefined hrefs alone', () => {
expect(insertAndExtract({})).toBe(undefined)
})
test('default relative link format is unchanged', () => {
expect(insertAndExtract({href: 'otherfile?fileId=123'}))
.toBe('otherfile?fileId=123')
})
})
|