forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.spec.js
60 lines (49 loc) · 2.46 KB
/
clipboard.spec.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
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2022 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import { parseDataFromClipboard, prepareDataForHTMLClipboard } from 'docc-render/utils/clipboard';
const payload = { foo: 'foo' };
const xmlPayload = `<data id="copy-data">${JSON.stringify(payload)}</data>`;
describe('clipboard', () => {
describe('parseDataFromClipboard', () => {
it('parses data from an html clipboard', () => {
expect(parseDataFromClipboard(xmlPayload)).toEqual(payload);
});
it('returns null, if the data is not properly formatted', () => {
expect(parseDataFromClipboard('<div id="copy-data">{ "foo": "bar" }</div>')).toEqual(null);
expect(parseDataFromClipboard('<data id="copy-data">{ "foo": "bar" }')).toEqual(null);
expect(parseDataFromClipboard('<data>{ "foo": "bar" }</data>')).toEqual(null);
});
it('returns proper JSON if content has special tags in it', () => {
expect(parseDataFromClipboard('<data id="copy-data">{ "foo": "</data>" }</data>')).toEqual({ foo: '</data>' });
});
it('returns proper JSON if wrapper has extra attributes', () => {
expect(parseDataFromClipboard(`<data id="copy-data" style="color: pink">${JSON.stringify(payload)}</data>`)).toEqual(payload);
});
it('retrieves data from deeply nested xml data', () => {
// Different browsers wrap the data differently, so we need to make sure it works deeply
expect(parseDataFromClipboard(`<html><body>${xmlPayload}</body></html>`)).toEqual(payload);
});
it('does not error out, if the data is not properly formatted', () => {
expect(parseDataFromClipboard('<data id="copy-data">not JSON</data>')).toBe(null);
expect(parseDataFromClipboard('<data id="copy-data">{ "foo": bar }</data>')).toEqual(null);
});
});
describe('prepareDataForHTMLClipboard', () => {
it('accepts string data', () => {
expect(prepareDataForHTMLClipboard(JSON.stringify(payload))).toEqual(xmlPayload);
});
it('accepts none string data', () => {
expect(prepareDataForHTMLClipboard(payload)).toEqual(xmlPayload);
});
it('does not sanitize data', () => {
expect(prepareDataForHTMLClipboard('foo')).toEqual('<data id="copy-data">foo</data>');
});
});
});