Skip to content

Commit 8ea75b8

Browse files
author
mokimo
committed
test(clipboard): add tests and improve setup
1 parent ee18b34 commit 8ea75b8

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

spec/clipboard.spec.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { parseContent, updateConfig } from '../src/clipboard'
2+
import cloneDeep from 'lodash.clonedeep'
23
import * as config from '../src/config'
34

45
describe('Clipboard', () => {
56
describe('parseContent()', () => {
7+
8+
afterEach(() => {
9+
updateConfig(config)
10+
})
11+
612
function extract (str) {
713
const div = document.createElement('div')
814
div.innerHTML = str
@@ -24,10 +30,38 @@ describe('Clipboard', () => {
2430
expect(extractSingleBlock(' a ')).toEqual('a')
2531
})
2632

27-
it('keeps a <a> element with an href attribute', () => {
33+
it('keeps a <a> element with an href attribute with an absolute link', () => {
2834
expect(extractSingleBlock('<a href="http://link.com">a</a>')).toEqual('<a href="http://link.com">a</a>')
2935
})
3036

37+
it('keeps a <a> element with an href attribute with an relative link', () => {
38+
expect(extractSingleBlock('<a href="/link/1337">a</a>')).toEqual('<a href="/link/1337">a</a>')
39+
})
40+
41+
it('keeps a <a> element with an a list of whitelisted-attributes', () => {
42+
const updatedConfig = cloneDeep(config)
43+
console.log(updatedConfig)
44+
updatedConfig.pastedHtmlRules.allowedElements = { a: { href: true, rel: true, target: true } }
45+
46+
updateConfig(updatedConfig)
47+
expect(
48+
extractSingleBlock(
49+
'<a target="_blank" rel="nofollow" href="/link/1337">a</a>'
50+
)
51+
).toEqual('<a target="_blank" rel="nofollow" href="/link/1337">a</a>')
52+
})
53+
54+
it('removes attributes that arent whitelisted for an <a> element ', () => {
55+
const updatedConfig = cloneDeep(config)
56+
updatedConfig.pastedHtmlRules.allowedElements = { a: { href: true } }
57+
updateConfig(updatedConfig)
58+
expect(
59+
extractSingleBlock(
60+
'<a target="_blank" rel="nofollow" href="/link/1337">a</a>'
61+
)
62+
).toEqual('<a href="/link/1337">a</a>')
63+
})
64+
3165
it('keeps a <strong> element', () => {
3266
expect(extractSingleBlock('<strong>a</strong>')).toEqual('<strong>a</strong>')
3367
})
@@ -93,6 +127,11 @@ describe('Clipboard', () => {
93127
expect(extractSingleBlock('<a>a</a>')).toEqual('a')
94128
})
95129

130+
131+
it('removes an <a> element with an empty href attribute', () => {
132+
expect(extractSingleBlock('<a href>a</a>')).toEqual('a')
133+
})
134+
96135
it('removes an <a> element with an empty href attribute', () => {
97136
expect(extractSingleBlock('<a href="">a</a>')).toEqual('a')
98137
})
@@ -120,6 +159,14 @@ describe('Clipboard', () => {
120159
expect(extractSingleBlock('<b>a</b>')).toEqual('<strong>a</strong>')
121160
})
122161

162+
it('changes absolute links to relative ones with the keepInternalRelativeLinks flag set to true', () => {
163+
const updatedConfig = cloneDeep(config)
164+
updatedConfig.pastedHtmlRules.keepInternalRelativeLinks = true
165+
console.log(updatedConfig)
166+
updateConfig(updatedConfig)
167+
expect(extractSingleBlock(`<a href="${window.location.origin}/test123">a</a>`)).toEqual('<a href="/test123">a</a>')
168+
})
169+
123170
// Escape Content
124171
// --------------
125172

@@ -143,12 +190,5 @@ describe('Clipboard', () => {
143190

144191
expect(parseContent(div)[0]).toEqual('bar')
145192
})
146-
147-
it('keeps internal links of a anchorTag with an href attribute', () => {
148-
expect(extractSingleBlock('<a href="http://localhost:9876/test123">a</a>')).toEqual('<a href="http://localhost:9876/test123">a</a>')
149-
config.pastedHtmlRules.keepInternalRelativeLinks = true
150-
updateConfig(config)
151-
expect(extractSingleBlock('<a href="http://localhost:9876/test123">a</a>')).toEqual('<a href="/test123">a</a>')
152-
})
153193
})
154194
})

0 commit comments

Comments
 (0)