-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
71 lines (54 loc) · 2.32 KB
/
index.spec.js
File metadata and controls
71 lines (54 loc) · 2.32 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
import { createElement } from 'lwc';
import XInnerHtml from 'x/innerHtml';
import { jasmine } from '../../../helpers/jasmine.js';
import { getHooks, setHooks } from '../../../helpers/hooks.js';
const ACTUAL_CONTENT = 'Hello <b>World</b>';
const ALTERNATIVE_CONTENT = 'Hello <b>LWC</b>';
const override = (content) => content;
it('throws when not overridden', () => {
expect(() => {
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = ACTUAL_CONTENT;
document.body.appendChild(elm);
}).toThrowCallbackReactionError(Error, /sanitizeHtmlContent hook must be implemented/);
});
function setSanitizeHtmlContentHookForTest(impl) {
const { sanitizeHtmlContent } = getHooks();
setHooks({
sanitizeHtmlContent: impl,
});
return sanitizeHtmlContent;
}
it('receives the right parameters', () => {
const spy = jasmine.createSpy('sanitizeHook', override);
const original = setSanitizeHtmlContentHookForTest(spy);
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = ACTUAL_CONTENT;
document.body.appendChild(elm);
expect(spy).toHaveBeenCalledWith(ACTUAL_CONTENT);
setSanitizeHtmlContentHookForTest(original);
});
it('does not call sanitizeHtmlContent when raw value does not change', () => {
const spy = jasmine.createSpy('sanitizeHook', override);
const original = setSanitizeHtmlContentHookForTest(spy);
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.message = 'initial';
elm.content = ACTUAL_CONTENT;
document.body.appendChild(elm);
expect(spy).toHaveBeenCalledTimes(1);
elm.message = 'modified';
return Promise.resolve().then(() => {
expect(spy).toHaveBeenCalledTimes(1);
expect(elm.shadowRoot.querySelector('p').innerText).toBe('modified');
setSanitizeHtmlContentHookForTest(original);
});
});
it('replace the original attribute value with the returned value', () => {
const original = setSanitizeHtmlContentHookForTest(() => ALTERNATIVE_CONTENT);
const elm = createElement('x-inner-html', { is: XInnerHtml });
elm.content = ACTUAL_CONTENT;
document.body.appendChild(elm);
const div = elm.shadowRoot.querySelector('div');
expect(div.innerHTML).toBe(ALTERNATIVE_CONTENT);
setSanitizeHtmlContentHookForTest(original);
});