Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(indexdb): Added property patches and event target methods as wel…
Browse files Browse the repository at this point in the history
…l as tests for Indexed DB

Closes #204
  • Loading branch information
gionkunz authored and mhevery committed Mar 21, 2016
1 parent 99940c3 commit 84a251f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/browser/event-target.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {patchEventTargetMethods} from './utils';

const WTF_ISSUE_555 = 'Anchor,Area,Audio,BR,Base,BaseFont,Body,Button,Canvas,Content,DList,Directory,Div,Embed,FieldSet,Font,Form,Frame,FrameSet,HR,Head,Heading,Html,IFrame,Image,Input,Keygen,LI,Label,Legend,Link,Map,Marquee,Media,Menu,Meta,Meter,Mod,OList,Object,OptGroup,Option,Output,Paragraph,Pre,Progress,Quote,Script,Select,Source,Span,Style,TableCaption,TableCell,TableCol,Table,TableRow,TableSection,TextArea,Title,Track,UList,Unknown,Video';
const NO_EVENT_TARGET = 'ApplicationCache,EventSource,FileReader,InputMethodContext,MediaController,MessagePort,Node,Performance,SVGElementInstance,SharedWorker,TextTrack,TextTrackCue,TextTrackList,WebKitNamedFlow,Worker,WorkerGlobalScope,XMLHttpRequest,XMLHttpRequestEventTarget,XMLHttpRequestUpload'.split(',');
const NO_EVENT_TARGET = 'ApplicationCache,EventSource,FileReader,InputMethodContext,MediaController,MessagePort,Node,Performance,SVGElementInstance,SharedWorker,TextTrack,TextTrackCue,TextTrackList,WebKitNamedFlow,Worker,WorkerGlobalScope,XMLHttpRequest,XMLHttpRequestEventTarget,XMLHttpRequestUpload,IDBRequest,IDBOpenDBRequest,IDBDatabase,IDBTransaction,IDBCursor,DBIndex'.split(',');
const EVENT_TARGET = 'EventTarget';

export function eventTargetPatch(_global) {
Expand Down
8 changes: 8 additions & 0 deletions lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export function propertyDescriptorPatch(_global) {
patchOnProperties(HTMLElement.prototype, eventNames);
}
patchOnProperties(XMLHttpRequest.prototype, null);
if (typeof IDBIndex !== 'undefined') {
patchOnProperties(IDBIndex.prototype, null);
patchOnProperties(IDBRequest.prototype, null);
patchOnProperties(IDBOpenDBRequest.prototype, null);
patchOnProperties(IDBDatabase.prototype, null);
patchOnProperties(IDBTransaction.prototype, null);
patchOnProperties(IDBCursor.prototype, null);
}
if (supportsWebSocket) {
patchOnProperties(WebSocket.prototype, null);
}
Expand Down
114 changes: 114 additions & 0 deletions test/patch/IndexedDB.spec.js
Original file line number Diff line number Diff line change
@@ -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();
};
});
});
});
}));

0 comments on commit 84a251f

Please sign in to comment.