-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
65 lines (53 loc) · 2.17 KB
/
index.spec.js
File metadata and controls
65 lines (53 loc) · 2.17 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
import { createElement } from 'lwc';
import Test from 'x/test';
import ConnectedCallbackThrow from 'x/connectedCallbackThrow';
import XSlottedParent from 'x/slottedParent';
import { customElementCallbackReactionErrorListener } from '../../../helpers/utils.js';
function testConnectSlot(name, fn) {
it(`should invoke the connectedCallback the root element is added in the DOM via ${name}`, () => {
let isConnected = false;
let thisValue;
const elm = createElement('x-test', { is: Test });
elm.connect = function (context) {
isConnected = true;
thisValue = context;
};
fn(elm);
expect(thisValue instanceof Test).toBe(true);
expect(isConnected).toBe(true);
});
}
testConnectSlot('Node.appendChild', (elm) => {
document.body.appendChild(elm);
});
testConnectSlot('Node.insertBefore', (elm) => {
const child = document.createElement('div');
document.body.appendChild(child);
document.body.insertBefore(elm, child);
});
testConnectSlot('Node.replaceChild', (elm) => {
const child = document.createElement('div');
document.body.appendChild(child);
document.body.replaceChild(elm, child);
});
it('should associate the component stack when the invocation throws', () => {
const elm = createElement('x-connected-callback-throw', { is: ConnectedCallbackThrow });
const error = customElementCallbackReactionErrorListener(() => {
document.body.appendChild(elm);
});
expect(error).not.toBe(undefined);
expect(error.message).toBe('throw in connected');
expect(error.wcStack).toBe('<x-connected-callback-throw>');
});
describe('addEventListner in `connectedCallback`', () => {
it('clicking force button should update value', function () {
const elm = createElement('x-slotted-parent', { is: XSlottedParent });
document.body.appendChild(elm);
const child = elm.shadowRoot.querySelector('x-child');
child.dispatchEventOnHost();
return Promise.resolve().then(() => {
expect(elm.eventHandled).toBe(true);
expect(elm.shadowRoot.querySelector('p').textContent).toBe('Was clicked: true');
});
});
});