forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-event-helper.js
247 lines (214 loc) · 6.54 KB
/
test-event-helper.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
createCustomEvent,
isLoaded,
listen,
listenOnce,
listenOncePromise,
loadPromise,
} from '../../src/event-helper';
import {Observable} from '../../src/observable';
import * as sinon from 'sinon';
describe('EventHelper', () => {
function getEvent(name, target) {
const event = new Event(name, {bubbles: true, cancelable: true});
event.testTarget = target;
return event;
}
let sandbox;
let element;
let loadObservable;
let errorObservable;
beforeEach(() => {
sandbox = sinon.sandbox.create();
loadObservable = new Observable();
errorObservable = new Observable();
element = {
tagName: 'TEST',
complete: false,
readyState: '',
addEventListener(type, callback) {
if (type == 'load') {
loadObservable.add(callback);
} else if (type == 'error') {
errorObservable.add(callback);
} else {
expect(type).to.equal('load or error');
}
},
removeEventListener(type, callback) {
if (type == 'load') {
loadObservable.remove(callback);
} else if (type == 'error') {
errorObservable.remove(callback);
} else {
expect(type).to.equal('load or error');
}
},
};
});
afterEach(() => {
// Very important that all listeners are removed.
expect(loadObservable.getHandlerCount()).to.equal(0);
expect(errorObservable.getHandlerCount()).to.equal(0);
sandbox.restore();
});
it('listen', () => {
const event = getEvent('load', element);
let c = 0;
const handler = e => {
c++;
expect(e).to.equal(event);
};
const unlisten = listen(element, 'load', handler);
// Not fired yet.
expect(c).to.equal(0);
// Fired once.
loadObservable.fire(event);
expect(c).to.equal(1);
// Fired second time.
loadObservable.fire(event);
expect(c).to.equal(2);
unlisten();
loadObservable.fire(event);
expect(c).to.equal(2);
});
it('listenOnce', () => {
const event = getEvent('load', element);
let c = 0;
const handler = e => {
c++;
expect(e).to.equal(event);
};
listenOnce(element, 'load', handler);
// Not fired yet.
expect(c).to.equal(0);
// Fired once.
loadObservable.fire(event);
expect(c).to.equal(1);
// Fired second time: no longer listening.
loadObservable.fire(event);
expect(c).to.equal(1);
});
it('listenOnce - cancel', () => {
const event = getEvent('load', element);
let c = 0;
const handler = e => {
c++;
expect(e).to.equal(event);
};
const unlisten = listenOnce(element, 'load', handler);
// Not fired yet.
expect(c).to.equal(0);
// Cancel.
unlisten();
// Fired once: no longer listening.
loadObservable.fire(event);
expect(c).to.equal(0);
});
it('listenOncePromise - load event', () => {
const event = getEvent('load', element);
const promise = listenOncePromise(element, 'load').then(result => {
expect(result).to.equal(event);
});
loadObservable.fire(event);
return promise;
});
it('isLoaded for complete property', () => {
expect(isLoaded(element)).to.equal(false);
element.complete = true;
expect(isLoaded(element)).to.equal(true);
});
it('isLoaded for readyState property', () => {
expect(isLoaded(element)).to.equal(false);
element.readyState = 'complete';
expect(isLoaded(element)).to.equal(true);
});
it('isLoaded for Window', () => {
expect(isLoaded(window)).to.equal(true);
const win = {
document: {
readyState: 'interactive',
},
};
expect(isLoaded(win)).to.equal(false);
win.document.readyState = 'complete';
expect(isLoaded(win)).to.equal(true);
});
it('loadPromise - already complete', () => {
element.complete = true;
return loadPromise(element).then(result => {
expect(result).to.equal(element);
});
});
it('loadPromise - already readyState == complete', () => {
element.readyState = 'complete';
return loadPromise(element).then(result => {
expect(result).to.equal(element);
});
});
it('loadPromise - load event', () => {
const promise = loadPromise(element).then(result => {
expect(result).to.equal(element);
});
loadObservable.fire(getEvent('load', element));
return promise;
});
it('loadPromise - error event', () => {
const promise = loadPromise(element).then(result => {
assert.fail('must never be here: ' + result);
}).then(() => {
throw new Error('Should not be reached.');
}, reason => {
expect(reason.message).to.include('Failed to load');
});
errorObservable.fire(getEvent('error', element));
return promise;
});
it('should polyfill CustomEvent constructor', () => {
const native = createCustomEvent(window, 'foo', {bar: 123},
{bubbles: true, cancelable: true});
expect(native.type).to.equal('foo');
expect(native.detail).to.deep.equal({bar: 123});
expect(native.bubbles).to.be.true;
expect(native.cancelable).to.be.true;
const polyfilled = createCustomEvent({document}, 'foo', {bar: 123},
{bubbles: true, cancelable: true});
expect(polyfilled.type).to.equal('foo');
expect(polyfilled.detail).to.deep.equal({bar: 123});
expect(polyfilled.bubbles).to.be.true;
expect(polyfilled.cancelable).to.be.true;
});
it('should create the correct custom event for IE11', () => {
const native = createCustomEvent(window, 'foo', {bar: 123});
expect(native.type).to.equal('foo');
expect(native.detail).to.deep.equal({bar: 123});
const initCustomEventSpy = sandbox.spy();
const win = {};
win.CustomEvent = {};
win.document = {};
win.document.createEvent = function() {
return {
initCustomEvent() {
initCustomEventSpy();
},
};
};
createCustomEvent(win, 'foo', {bar: 123});
expect(initCustomEventSpy).to.be.calledOnce;
});
});