-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
83 lines (67 loc) · 2.56 KB
/
index.spec.js
File metadata and controls
83 lines (67 loc) · 2.56 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
import { createElement } from 'lwc';
import { getHooks, setHooks } from 'test-utils';
import XInnerHtml from 'x/innerHtml';
let originalSanitizeHtmlContent;
beforeAll(() => {
originalSanitizeHtmlContent = getHooks().sanitizeHtmlContent;
setHooks({ sanitizeHtmlContent: (content) => content });
});
afterAll(() => {
setHooks({ sanitizeHtmlContent: originalSanitizeHtmlContent });
});
afterEach(() => {
window.__lwcResetGlobalStylesheets();
});
it('renders the content as HTML', async () => {
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = 'Hello <b>World</b>';
document.body.appendChild(elm);
const div = elm.shadowRoot.querySelector('div');
expect(div.childNodes.length).toBe(2);
expect(div.childNodes[0].nodeType).toBe(Node.TEXT_NODE);
expect(div.childNodes[0].textContent).toBe('Hello ');
expect(div.childNodes[1].nodeType).toBe(Node.ELEMENT_NODE);
expect(div.childNodes[1].tagName).toBe('B');
expect(div.childNodes[1].textContent).toBe('World');
});
it('re-renders the content on update', () => {
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = 'Hello <b>World</b>';
document.body.appendChild(elm);
const b = elm.shadowRoot.querySelector('b');
expect(b.textContent).toBe('World');
elm.content = 'Hello <b>LWC</b>';
return Promise.resolve().then(() => {
const b = elm.shadowRoot.querySelector('b');
expect(b.textContent).toBe('LWC');
});
});
describe('type conversion', () => {
const cases = [
[null, ''],
[undefined, 'undefined'],
['string', 'string'],
[true, 'true'],
[42, '42'],
];
cases.forEach(([actual, expected]) => {
it(`renders properly when passing type ${typeof actual}`, () => {
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = actual;
document.body.appendChild(elm);
const div = elm.shadowRoot.querySelector('div');
expect(div.innerHTML).toBe(expected);
});
});
});
it('applies styles to injected content', async () => {
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = '<b>Test</b>';
document.body.appendChild(elm);
// When running with synthetic shadow a micro task is needed to for the MutationObserver to add
// the styling tokens.
await Promise.resolve();
const b = elm.shadowRoot.querySelector('b');
const styles = window.getComputedStyle(b);
expect(styles.borderBottomStyle).toContain('dashed');
});