-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathEvent.target.spec.js
More file actions
130 lines (103 loc) · 4.94 KB
/
Event.target.spec.js
File metadata and controls
130 lines (103 loc) · 4.94 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { createElement } from 'lwc';
import Container from 'x/container';
import { jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
describe('Event.target', () => {
let globalListener = () => {};
afterEach(() => {
document.removeEventListener('test', globalListener);
});
it('should retarget', async () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
const child = container.shadowRoot.querySelector('x-child');
const target = await new Promise((resolve) => {
child.addEventListener('test', (event) => {
resolve(event.target);
});
const div = child.shadowRoot.querySelector('div');
div.dispatchEvent(new CustomEvent('test', { bubbles: true, composed: true }));
});
expect(target).toBe(child);
});
it('should patch the prototype instead of the instance', () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
function dispatchEventWithAssertions(target, event) {
const hasOwnProperty = Object.prototype.hasOwnProperty;
for (var node = target; node; node = node.parentNode || node.host) {
node.addEventListener(event.type, function (event) {
expect(hasOwnProperty.call(event, 'target')).toBeFalse();
});
}
target.dispatchEvent(event);
}
dispatchEventWithAssertions(
container.shadowRoot.querySelector('x-child'),
new CustomEvent('test', { bubbles: true, composed: true })
);
expect(hasOwnProperty.call(Event.prototype, 'target')).toBeTrue();
});
it('should retarget to the root component when accessed asynchronously', () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
let event;
const child = container.shadowRoot.querySelector('x-child');
child.addEventListener('test', (e) => {
event = e;
});
const div = child.shadowRoot.querySelector('div');
div.dispatchEvent(new CustomEvent('test', { bubbles: true, composed: true }));
expect(event.target).toBe(container);
});
it('should retarget when accessed in a document event listener', async () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
const target = await new Promise((resolve) => {
globalListener = (event) => {
resolve(event.target);
};
document.addEventListener('test', globalListener);
const child = container.shadowRoot.querySelector('x-child');
const div = child.shadowRoot.querySelector('div');
div.dispatchEvent(new CustomEvent('test', { bubbles: true, composed: true }));
});
expect(target).toBe(container);
});
describe.skipIf(process.env.NATIVE_SHADOW)('legacy behavior', () => {
beforeAll(() => {
// Suppress error logging
spyOn(console, 'warn');
});
it('should not retarget when the target was manually added without lwc:dom="manual" and accessed asynchronously [W-6626752]', async () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
const child = container.shadowRoot.querySelector('x-child');
const span = child.appendSpanAndReturn();
const [first, second] = await new Promise((resolve) => {
container.addEventListener('test', (event) => {
const first = event.target;
setTimeout(() => {
resolve([first, event.target]);
});
});
span.dispatchEvent(new CustomEvent('test', { bubbles: true, composed: true }));
});
expect(first).toBe(container);
expect(second).toBe(span);
});
it('should not retarget when the target was manually added without lwc:dom="manual" and accessed in a document event listener [W-6626752]', async () => {
const container = createElement('x-container', { is: Container });
document.body.appendChild(container);
const child = container.shadowRoot.querySelector('x-child');
const span = child.appendSpanAndReturn();
const target = await new Promise((resolve) => {
globalListener = (event) => {
resolve(event.target);
};
document.addEventListener('test', globalListener);
span.dispatchEvent(new CustomEvent('test', { bubbles: true, composed: true }));
});
expect(target).toBe(span);
});
});
});