Last active
August 29, 2015 13:58
-
-
Save cmlenz/9978669 to your computer and use it in GitHub Desktop.
Some simple first async tests for the AngularJS IndexedDB wrapper I'm hacking on. Using Karma, Mocha and Chai.Somewhat painful but it it seems to be working rather well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('IndexedDB', function() { | |
var provider; | |
beforeEach(function(done) { | |
module('cmlenz.indexedDB', function(indexedDBProvider) { | |
provider = indexedDBProvider; | |
provider.connect("angular-indexeddb-tests", 1, function(evt, db) { | |
db.createObjectStore('test', {keyPath: 'key'}); | |
}); | |
}); | |
// Need to call inject to make sure the database setup above is actually | |
// performed before test setup is done | |
inject(function(indexedDB) { | |
indexedDB.transaction('test', function(test) { | |
}).then(function() { | |
done(); | |
}); | |
}); | |
}); | |
it('provider should exist', function(done) { | |
inject(function(indexedDB) { | |
expect(provider).to.exist; | |
expect(indexedDB).to.exist; | |
done(); | |
}); | |
}); | |
it('should insert a new object', function(done) { | |
inject(function($q, indexedDB) { | |
indexedDB.transaction('test', 'readwrite', function(test) { | |
test.add({key: 42, value: "foo"}).then(function() { | |
$q.all([ | |
test.count().then(function(num) { expect(num).to.equal(1); }), | |
test.get(42).then(function(obj) { expect(obj.value).to.equal("foo"); }) | |
]).then(function() { | |
done(); | |
}) | |
}); | |
}); | |
}); | |
}); | |
it('should update an existing object', function(done) { | |
inject(function($q, indexedDB) { | |
indexedDB.transaction('test', 'readwrite', function(test) { | |
test.add({key: 42, value: "foo"}).then(function() { | |
test.put({key: 42, value: "bar"}).then(function() { | |
$q.all([ | |
test.count().then(function(num) { expect(num).to.equal(1); }), | |
test.get(42).then(function(obj) { expect(obj.value).to.equal("bar"); }) | |
]).then(function() { | |
done(); | |
}) | |
}); | |
}); | |
}); | |
}); | |
}); | |
afterEach(function(done) { | |
inject(function($rootScope, indexedDB) { | |
$rootScope.$apply(function() { | |
indexedDB.delete().then(function() { | |
done(); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment