forked from indexeddbshim/IndexedDBShim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
events-spec.js
300 lines (267 loc) · 14.7 KB
/
events-spec.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* eslint-env mocha */
/* globals expect, sinon, util, env */
/* eslint-disable no-var */
describe('IDBTransaction events', function () {
'use strict';
it('should fire the oncomplete event if a transaction completes successfully', function (done) {
this.timeout(5000);
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
var tx = db.transaction('inline', 'readwrite');
var store = tx.objectStore('inline');
var add = store.add({id: 1});
add.onsuccess = sinon.spy();
tx.oncomplete = function () {
sinon.assert.calledOnce(add.onsuccess);
db.close();
done();
};
});
});
it('should fire the oncomplete event if a transaction does nothing', function (done) {
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
var tx = db.transaction('inline', 'readwrite');
tx.oncomplete = function () {
db.close();
done();
};
});
});
it('should fire the oncomplete event if a synchronous error occurs', function (done) {
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
var tx = db.transaction('inline', 'readonly'); // <-- read-only
db.onerror = sinon.spy();
tx.onerror = sinon.spy();
var windowOnerrorState = {};
util.stubWindowOnerror(windowOnerrorState);
// if (!env.browser.isFirefox) sinon.spy(window, 'onerror').returns(true);
var store = tx.objectStore('inline');
var errored = false;
try {
store.add({id: 1}); // <-- This causes a synchronous error
} catch (e) {
errored = true;
}
tx.oncomplete = function () {
expect(errored).equal(true);
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
expect(windowOnerrorState.erred).equal(false); // <-- window.onerror NEVER fires
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror);
};
setTimeout(function () {
try {
expect(errored).equal(true);
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror NEVER fires
expect(windowOnerrorState.erred).equal(false); // <-- window.onerror NEVER fires
db.close();
done();
} finally {
// if (!env.browser.isFirefox) window.onerror.restore();
util.restoreWindowOnerror();
}
}, env.transactionDuration);
});
});
it('should fire the onerror event if an asynchronous error occurs', function (done) {
this.timeout(5000);
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
var tx = db.transaction('inline', 'readwrite');
var windowOnerrorState = {};
util.stubWindowOnerror(windowOnerrorState);
var store = tx.objectStore('inline');
store.add({id: 1});
var add = store.add({id: 1}); // <-- This causes an asynchronous error (duplicate id)
// IDBRequest events
add.onsuccess = sinon.spy();
add.onerror = sinon.spy(function () {
sinon.assert.notCalled(add.onsuccess); // <-- request.onsuccess NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror HAS NOT fired YET
sinon.assert.notCalled(db.onerror); // <-- database.onerror HAS NOT fired YET
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror HAS NOT fired YET
expect(windowOnerrorState.erred).equal(false);
});
// IDBTransaction events
tx.onerror = sinon.spy(function (e) {
sinon.assert.notCalled(add.onsuccess); // <-- request.onsuccess NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
sinon.assert.calledOnce(add.onerror); // <-- request.onerror HAS fired
sinon.assert.notCalled(db.onerror); // <-- database.onerror HAS NOT fired YET
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror HAS NOT fired YET
if (env.browser.isFirefox) e.preventDefault(); // Firefox reaches here unless preventDefault() is called (which will also cause our tests to fail)
else expect(windowOnerrorState.erred).equal(false);
});
tx.oncomplete = sinon.spy(function (e) {
db.close();
if (!env.browser.isFirefox) done(new Error('IDBObjectStore.add() should have thrown an error when two records were added with the same primary key'));
});
// IDBDatabase events
db.onerror = sinon.spy(function () {
sinon.assert.notCalled(add.onsuccess); // <-- request.onsuccess NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
sinon.assert.calledOnce(add.onerror); // <-- request.onerror HAS fired
sinon.assert.calledOnce(tx.onerror); // <-- transaction.onerror HAS fired
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror HAS NOT fired YET
expect(windowOnerrorState.erred).equal(false);
});
// Window events
// if (!env.browser.isFirefox) sinon.stub(window, 'onerror').returns(true);
setTimeout(function () {
try {
sinon.assert.notCalled(add.onsuccess); // <-- request.onsuccess NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
sinon.assert.calledOnce(add.onerror); // <-- request.onerror HAS fired
sinon.assert.calledOnce(tx.onerror); // <-- transaction.onerror HAS fired
sinon.assert.calledOnce(db.onerror); // <-- database.onerror HAS fired
// Firefox fires window.onerror. No other browser does.
// sinon.assert.notCalled(window.onerror);
expect(windowOnerrorState.erred).equal(false);
db.close();
done();
} finally {
// if (!env.browser.isFirefox) window.onerror.restore();
util.restoreWindowOnerror();
}
}, env.transactionDuration);
});
});
it('should not fire the onerror event if an error occurs during oncomplete', function (done) {
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
if (typeof process !== 'undefined') { // Node
const originalException = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', originalException);
process.once('uncaughtException', function (error) {
process.addListener('uncaughtException', originalException);
window.onerror(error);
});
}
var tx = db.transaction('inline', 'readwrite');
tx.onabort = sinon.spy();
var windowOnerrorState = {};
util.stubWindowOnerror(windowOnerrorState);
db.onerror = tx.onerror = sinon.spy(function () {
db.close();
done(new Error('The onerror event fired when it should not have'));
});
tx.oncomplete = sinon.spy(function () {
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.onabort); // <-- transaction.abort NEVER fires
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror HAS NOT fired YET
expect(windowOnerrorState.erred).equal(false);
throw new Error('Test Error');
});
util.stubWindowOnerror(windowOnerrorState, function () {
sinon.assert.calledOnce(tx.oncomplete); // <-- transaction.oncomplete HAS fired
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.onabort); // <-- transaction.abort NEVER fires
return true;
});
window.addEventListener('cordovacallbackerror', function handler () {
window.removeEventListener('cordovacallbackerror', handler);
db.close();
done(new Error('The WebSQL plugin suppressed an error event. (window.onerror should have fired)'));
});
setTimeout(function () {
try {
sinon.assert.calledOnce(tx.oncomplete); // <-- transaction.oncomplete HAS fired
// if (!env.browser.isFirefox) sinon.assert.calledOnce(window.onerror); // <-- window.onerror HAS fired
expect(windowOnerrorState.erred).equal(true);
windowOnerrorState.erred = false;
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.onabort); // <-- transaction.abort NEVER fires
db.close();
done();
} finally {
// if (!env.browser.isFirefox) window.onerror.restore();
util.restoreWindowOnerror();
}
}, env.transactionDuration);
});
});
it('should not fire the onerror event if an error occurs during onabort', function (done) {
this.timeout(5000);
util.createDatabase('inline', function (err, db) {
if (err) {
expect(function () { throw err; }).to.not.throw(Error);
done();
return;
}
if (typeof process !== 'undefined') { // Node
const originalException = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', originalException);
process.once('uncaughtException', function (error) {
process.addListener('uncaughtException', originalException);
window.onerror(error);
});
}
var tx = db.transaction('inline', 'readwrite');
db.onerror = sinon.spy();
tx.onerror = sinon.spy();
tx.oncomplete = sinon.spy();
var windowOnerrorState = {};
util.stubWindowOnerror(windowOnerrorState);
tx.onabort = sinon.spy(function () {
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror HAS NOT fired YET
expect(windowOnerrorState.erred).equal(false);
throw new Error('Test Error');
});
util.stubWindowOnerror({}, function () {
sinon.assert.calledOnce(tx.onabort); // <-- transaction.onabort HAS fired
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
return true;
});
tx.abort();
setTimeout(function () {
try {
sinon.assert.calledOnce(tx.onabort); // <-- transaction.onabort HAS fired
// if (!env.browser.isFirefox) sinon.assert.calledOnce(window.onerror); // <-- window.onerror HAS fired
sinon.assert.notCalled(db.onerror); // <-- database.onerror NEVER fires
sinon.assert.notCalled(tx.onerror); // <-- transaction.onerror NEVER fires
sinon.assert.notCalled(tx.oncomplete); // <-- transaction.oncomplete NEVER fires
expect(windowOnerrorState.erred).equal(false); // <-- window.onerror NEVER fires
// if (!env.browser.isFirefox) sinon.assert.notCalled(window.onerror); // <-- window.onerror NEVER fires
db.close();
done();
} finally {
// if (!env.browser.isFirefox) window.onerror.restore();
util.restoreWindowOnerror();
}
}, env.transactionDuration);
});
});
});