-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
114 lines (97 loc) · 3.49 KB
/
index.spec.js
File metadata and controls
114 lines (97 loc) · 3.49 KB
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
import { createElement } from 'lwc';
import XlinkStatic from 'x/xlinkStatic';
import XlinkDynamic from 'x/xlinkDynamic';
import XlinkBooleanTrue from 'x/xlinkBooleanTrue';
import HrefStatic from 'x/hrefStatic';
import HrefDynamic from 'x/hrefDynamic';
import HrefBooleanTrue from 'x/hrefBooleanTrue';
const scenarios = [
{
type: 'static',
attrName: 'xlink:href',
tagName: 'x-xlink-static',
Ctor: XlinkStatic,
},
{
type: 'dynamic',
attrName: 'xlink:href',
tagName: 'x-xlink-dynamic',
Ctor: XlinkDynamic,
},
{
type: 'static',
attrName: 'href',
tagName: 'x-href-static',
Ctor: HrefStatic,
},
{
type: 'dynamic',
attrName: 'href',
tagName: 'x-href-dynamic',
Ctor: HrefDynamic,
},
];
scenarios.forEach(({ type, attrName, tagName, Ctor }) => {
describe(`${type} ${attrName}`, () => {
// Spy is created in a mock file and injected with the import map plugin
const sanitizeAttributeSpy = LWC.sanitizeAttribute;
afterEach(() => {
sanitizeAttributeSpy.mockReset();
});
it('uses the original passthrough sanitizer when not overridden', () => {
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
const use = elm.shadowRoot.querySelector('use');
expect(use.getAttribute(attrName)).toBe('/foo');
});
it('receives the right parameters', () => {
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
expect(LWC.sanitizeAttribute).toHaveBeenCalledWith(
'use',
'http://www.w3.org/2000/svg',
attrName,
'/foo'
);
});
it('replace the original attribute value with a string', () => {
sanitizeAttributeSpy.mockReturnValue('/bar');
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
const use = elm.shadowRoot.querySelector('use');
expect(use.getAttribute(attrName)).toBe('/bar');
});
it('replace the original attribute value with undefined', () => {
sanitizeAttributeSpy.mockReturnValue(undefined);
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
// sanity check to make sure returning `undefined` works correctly here
const use = elm.shadowRoot.querySelector('use');
expect(use.hasAttribute(attrName)).toBe(false);
});
});
});
const booleanTrueScenarios = [
{
attrName: 'xlink:href',
tagName: 'x-xlink-boolean-true',
Ctor: XlinkBooleanTrue,
},
{
attrName: 'href',
tagName: 'x-href-boolean-true',
Ctor: HrefBooleanTrue,
},
];
booleanTrueScenarios.forEach(({ attrName, tagName, Ctor }) => {
describe(attrName, () => {
// For boolean literals (e.g. `<use xlink:href>`), there is no reason to sanitize since it's empty
it('does not sanitize when used as a boolean-true attribute', () => {
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
const use = elm.shadowRoot.querySelector('use');
expect(use.getAttribute(attrName)).toBe('');
expect(LWC.sanitizeAttribute).not.toHaveBeenCalled();
});
});
});