Skip to content

Commit

Permalink
Some stupid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Aug 6, 2015
1 parent f113db4 commit c8cd1a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
4 changes: 3 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ gulp.task('clean', function (done) {

gulp.task('copy', function() {
return gulp.src([
'test/index.html'
'test/index.html',
'node_modules/mocha/mocha.css',
'node_modules/mocha/mocha.js'
]).pipe(gulp.dest('dist/test'))
.pipe(reload({stream: true}));
});
Expand Down
31 changes: 22 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ function promisifyRequest(request) {
}

function promisifyCursorRequest(request) {
return promisifyRequest.then(value => {
return promisifyRequest(request).then(value => {
if (!value) return;
return new Cursor(value, request);
})
});
}

function proxyProperties(ProxyClass, targetProp, properties) {
Expand Down Expand Up @@ -84,10 +84,14 @@ class Cursor {
proxyProperties(Cursor, '_cursor', [
'direction',
'key',
'primaryKey'
'primaryKey',
'value'
]);

proxyRequestMethods(Cursor, '_cursor', ['update', 'delete']);
proxyRequestMethods(Cursor, '_cursor', [
'update',
'delete'
]);

// proxy 'next' methods
for (let methodName of ['advance', 'continue', 'continuePrimaryKey']) {
Expand Down Expand Up @@ -134,7 +138,7 @@ proxyCursorRequestMethods(ObjectStore, '_store', [
'openKeyCursor'
]);

proxyMethod(ObjectStore, '_store', [
proxyMethods(ObjectStore, '_store', [
'deleteIndex'
]);

Expand All @@ -157,7 +161,7 @@ proxyProperties(Transaction, '_tx', [
'mode'
]);

proxyMethod(Transaction, '_tx', [
proxyMethods(Transaction, '_tx', [
'abort'
]);

Expand All @@ -179,7 +183,7 @@ proxyProperties(UpgradeDB, '_db', [
'objectStoreNames'
]);

proxyMethod(UpgradeDB, '_db', [
proxyMethods(UpgradeDB, '_db', [
'deleteObjectStore',
'close'
]);
Expand All @@ -190,7 +194,7 @@ class DB {
}

transaction() {
return this._db.transaction(...arguments).then(tx => new Transaction());
return new Transaction(this._db.transaction(...arguments));
}
}

Expand All @@ -200,18 +204,27 @@ proxyProperties(DB, '_db', [
'objectStoreNames'
]);

proxyMethod(DB, '_db', [
proxyMethods(DB, '_db', [
'close'
]);

export function openIDB(name, version, upgradeCallback) {
var request = indexedDB.open(name, version);

request.onupgradeneeded = function(event) {
upgradeCallback(new UpgradeDB(request.result, event.oldVersion, request.transaction));
};

return promisifyRequest(request).then(db => new DB(db));
}

export function deleteIDB(name) {
return promisifyRequest(indexedDB.deleteDatabase(name));
}

if (process.browser) {
window.IDB = {
open: openIDB,
delete: deleteIDB
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"gulp-sourcemaps": "^1.5.2",
"gulp-util": "^3.0.6",
"merge-stream": "^0.1.8",
"mocha": "^2.2.5",
"run-sequence": "^1.1.2",
"uglifyify": "^3.0.1",
"vinyl-buffer": "^1.0.0",
Expand Down
41 changes: 41 additions & 0 deletions test/idb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import "babelify/node_modules/babel-core/node_modules/regenerator/runtime";
import assert from "assert";

describe('IDB interface', () => {
before(() => {
return IDB.delete('tmp-db');
});

it('exists on window', () => {
assert('IDB' in self);
});

it('has open and delete methods', () => {
assert('open' in IDB);
assert('delete' in IDB);
});

it('stuff', async () => {
let db = await IDB.open('tmp-db', 1, upgradeDb => {
switch (upgradeDb.oldVersion) {
case 0:
upgradeDb.createObjectStore("my-store", {
keyPath: ''
});
}
});

let tx = db.transaction('my-store', 'readwrite');
let store = tx.objectStore('my-store');
store.put('foo');
store.put('bar');
store.put('baz');

let cursor = await db.transaction('my-store').objectStore('my-store').openCursor();

while (cursor) {
console.log(cursor.value);
cursor = await cursor.continue();
}
});
});
8 changes: 5 additions & 3 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="mocha.css">
<title>Mocha Tests</title>
</head>
<body>
<div id="mocha"></div>

<script src="mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../idb.js"></script>
<script src="idb.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
//mocha.checkLeaks();
//mocha.globals(['IDB']);
mocha.run();
</script>
</body>
Expand Down

0 comments on commit c8cd1a9

Please sign in to comment.