-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
element-events.spec.ts
140 lines (105 loc) · 3.81 KB
/
element-events.spec.ts
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
131
132
133
134
135
136
137
138
139
import { ElementEvents } from '../src/element-events';
describe('ElementEvents', () => {
let input: HTMLInputElement;
let elementEvents: ElementEvents;
beforeEach(() => {
input = document.createElement('input');
elementEvents = new ElementEvents(input);
})
it('should subscribe', () => {
let value;
let callCount = 0;
const eventHandler = elementEvents.subscribe('input', () => {
callCount++;
value = input.value;
});
const newValue = 1234;
input.value = newValue + '';
input.dispatchEvent(new CustomEvent('input'));
expect(value === newValue.toString()).toBe(true);
expect(callCount).toBe(1);
eventHandler.dispose();
input.dispatchEvent(new CustomEvent('input'));
expect(callCount).toBe(1);
});
it('should subscribe once', () => {
let value;
let callCount = 0;
elementEvents.subscribeOnce('input', () => {
callCount++;
value = input.value;
});
const newValue = 1234;
input.value = newValue + '';
input.dispatchEvent(new CustomEvent('input'));
expect(value === newValue.toString()).toBe(true);
expect(callCount).toBe(1);
input.dispatchEvent(new CustomEvent('input'));
expect(callCount).toBe(1);
});
it("should subscribe and take ElementEvent default listener options into account", () => {
let isCapture;
// we need to track event on a parent of the input, let's take body
const bodyElementEvents = new ElementEvents(document.body);
ElementEvents.defaultListenerOptions = { capture: false };
let eventHandler = bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
});
// input has to be attached for the event to bubble up
document.body.appendChild(input);
input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(false);
eventHandler.dispose();
// set capture back to true and check if it's being used
ElementEvents.defaultListenerOptions = { capture: true };
eventHandler = bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
});
input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(true);
eventHandler.dispose();
});
it("should subscribe and ignore ElementEvent default listener options when argument captureOrOptions is passed", () => {
let isCapture;
const bodyElementEvents = new ElementEvents(document.body);
ElementEvents.defaultListenerOptions = { capture: false };
bodyElementEvents.subscribe('input', event => {
isCapture = event.eventPhase === Event.CAPTURING_PHASE;
}, true);
document.body.appendChild(input);
input.dispatchEvent(new CustomEvent('input', {bubbles: true}));
expect(isCapture).toBe(true);
});
it('should dispose single event', () => {
let value;
let callCount = 0;
elementEvents.subscribe('input', () => {
callCount++;
value = input.value;
});
const newValue = 1234;
input.value = newValue + '';
input.dispatchEvent(new CustomEvent('input'));
expect(value === newValue.toString()).toBe(true);
expect(callCount).toBe(1);
elementEvents.dispose('input');
input.dispatchEvent(new CustomEvent('input'));
expect(callCount).toBe(1);
});
it('should dispose all events', () => {
let value;
let callCount = 0;
elementEvents.subscribe('input', () => {
callCount++;
value = input.value;
});
const newValue = 1234;
input.value = newValue + '';
input.dispatchEvent(new CustomEvent('input'));
expect(value === newValue.toString()).toBe(true);
expect(callCount).toBe(1);
elementEvents.disposeAll();
input.dispatchEvent(new CustomEvent('input'));
expect(callCount).toBe(1);
});
});