From 5e17c7e9e00ca1f6ea94be22fc54fcd20bada35f Mon Sep 17 00:00:00 2001 From: Gion Kunz Date: Fri, 13 Nov 2015 00:48:49 +0100 Subject: [PATCH] feat(indexdb): Added property patches and event target methods as well as tests for Indexed DB --- lib/patch/event-target.js | 8 ++- lib/patch/property-descriptor.js | 6 ++ test/patch/IndexedDB.spec.js | 114 +++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 test/patch/IndexedDB.spec.js diff --git a/lib/patch/event-target.js b/lib/patch/event-target.js index c419587a4..c563ab02d 100644 --- a/lib/patch/event-target.js +++ b/lib/patch/event-target.js @@ -29,7 +29,13 @@ function apply() { 'WorkerGlobalScope', 'XMLHttpRequest', 'XMLHttpRequestEventTarget', - 'XMLHttpRequestUpload' + 'XMLHttpRequestUpload', + 'IDBRequest', + 'IDBOpenDBRequest', + 'IDBDatabase', + 'IDBTransaction', + 'IDBCursor', + 'IDBIndex' ]; apis.forEach(function(api) { diff --git a/lib/patch/property-descriptor.js b/lib/patch/property-descriptor.js index 17ab7011c..b8ea7acfb 100644 --- a/lib/patch/property-descriptor.js +++ b/lib/patch/property-descriptor.js @@ -20,6 +20,12 @@ function apply() { }); utils.patchProperties(HTMLElement.prototype, onEventNames); utils.patchProperties(XMLHttpRequest.prototype); + utils.patchProperties(IDBIndex.prototype); + utils.patchProperties(IDBRequest.prototype); + utils.patchProperties(IDBOpenDBRequest.prototype); + utils.patchProperties(IDBDatabase.prototype); + utils.patchProperties(IDBTransaction.prototype); + utils.patchProperties(IDBCursor.prototype); if (supportsWebSocket) { utils.patchProperties(WebSocket.prototype); } diff --git a/test/patch/IndexedDB.spec.js b/test/patch/IndexedDB.spec.js new file mode 100644 index 000000000..937474510 --- /dev/null +++ b/test/patch/IndexedDB.spec.js @@ -0,0 +1,114 @@ +'use strict'; + +describe('IndexedDB', ifEnvSupports('IDBDatabase', function () { + var testZone = zone.fork(); + var db; + + beforeEach(function (done) { + var openRequest = indexedDB.open('_zone_testdb'); + openRequest.onupgradeneeded = function(event) { + db = event.target.result; + var objectStore = db.createObjectStore('test-object-store', { + keyPath: 'key' + }); + objectStore.createIndex('key', 'key', { unique: true }); + objectStore.createIndex('data', 'data', { unique: false }); + + objectStore.transaction.oncomplete = function() { + var testStore = db.transaction('test-object-store', 'readwrite').objectStore('test-object-store'); + testStore.add({ + key: 1, + data: 'Test data' + }); + testStore.transaction.oncomplete = function() { + done(); + } + }; + }; + }); + + afterEach(function(done) { + db.close(); + + var openRequest = indexedDB.deleteDatabase('_zone_testdb'); + openRequest.onsuccess = function(event) { + done(); + }; + }); + + describe('IDBRequest', function() { + it('should bind EventTarget.addEventListener', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').get(1).addEventListener('success', function(event) { + expect(zone).toBeDirectChildOf(testZone); + expect(event.target.result.data).toBe('Test data'); + done(); + }); + }); + }); + + it('should bind onEventType listeners', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').get(1).onsuccess = function(event) { + expect(zone).toBeDirectChildOf(testZone); + expect(event.target.result.data).toBe('Test data'); + done(); + }; + }); + }); + }); + + describe('IDBCursor', function() { + it('should bind EventTarget.addEventListener', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').openCursor().addEventListener('success', function(event) { + var cursor = event.target.result; + if (cursor) { + expect(zone).toBeDirectChildOf(testZone); + expect(cursor.value.data).toBe('Test data'); + done(); + } else { + throw 'Error while reading cursor!'; + } + }); + }); + }); + + it('should bind onEventType listeners', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').openCursor().onsuccess = function(event) { + var cursor = event.target.result; + if (cursor) { + expect(zone).toBeDirectChildOf(testZone); + expect(cursor.value.data).toBe('Test data'); + done(); + } else { + throw 'Error while reading cursor!'; + } + }; + }); + }); + }); + + describe('IDBIndex', function() { + it('should bind EventTarget.addEventListener', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').index('data').get('Test data').addEventListener('success', function(event) { + expect(zone).toBeDirectChildOf(testZone); + expect(event.target.result.key).toBe(1); + done(); + }); + }); + }); + + it('should bind onEventType listeners', function (done) { + testZone.run(function () { + db.transaction('test-object-store').objectStore('test-object-store').index('data').get('Test data').onsuccess = function(event) { + expect(zone).toBeDirectChildOf(testZone); + expect(event.target.result.key).toBe(1); + done(); + }; + }); + }); + }); +})); \ No newline at end of file