mirror of
https://github.com/moparisthebest/mail
synced 2024-12-21 23:08:50 -05:00
Test device storage dao
This commit is contained in:
parent
fb89ffd35a
commit
3944b1741e
@ -22,6 +22,8 @@
|
||||
"inject",
|
||||
"Promise",
|
||||
"qMock",
|
||||
"resolves",
|
||||
"rejects",
|
||||
"self",
|
||||
"importScripts",
|
||||
"console",
|
||||
|
@ -3,13 +3,13 @@
|
||||
var ngModule = angular.module('woServices');
|
||||
|
||||
// expose an instance with the static dbName 'app-config' to store configuration data
|
||||
ngModule.factory('appConfigStore', function(appConfigLawnchair) {
|
||||
return new DeviceStorage(appConfigLawnchair);
|
||||
ngModule.factory('appConfigStore', function(appConfigLawnchair, $q) {
|
||||
return new DeviceStorage(appConfigLawnchair, $q);
|
||||
});
|
||||
|
||||
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
|
||||
ngModule.factory('accountStore', function(accountLawnchair) {
|
||||
return new DeviceStorage(accountLawnchair);
|
||||
ngModule.factory('accountStore', function(accountLawnchair, $q) {
|
||||
return new DeviceStorage(accountLawnchair, $q);
|
||||
});
|
||||
|
||||
module.exports = DeviceStorage;
|
||||
@ -21,57 +21,65 @@ module.exports = DeviceStorage;
|
||||
/**
|
||||
* High level storage api that handles all persistence of a user's data on the device.
|
||||
*/
|
||||
function DeviceStorage(lawnchairDAO) {
|
||||
function DeviceStorage(lawnchairDAO, $q) {
|
||||
this._lawnchairDAO = lawnchairDAO;
|
||||
this._q = $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the lawnchair database
|
||||
* @param {String} dbName The name of the database
|
||||
* @return {Promise}
|
||||
*/
|
||||
DeviceStorage.prototype.init = function(dbName, callback) {
|
||||
this._lawnchairDAO.init(dbName, callback);
|
||||
DeviceStorage.prototype.init = function(dbName) {
|
||||
return this._lawnchairDAO.init(dbName);
|
||||
};
|
||||
|
||||
/**
|
||||
* Stores a list of encrypted items in the object store
|
||||
* @param list [Array] The list of items to be persisted
|
||||
* @param type [String] The type of item to be persisted e.g. 'email'
|
||||
* @return {Promise}
|
||||
*/
|
||||
DeviceStorage.prototype.storeList = function(list, type, callback) {
|
||||
var key, items = [];
|
||||
DeviceStorage.prototype.storeList = function(list, type) {
|
||||
var self = this;
|
||||
return self._q(function(resolve) {
|
||||
var key, items = [];
|
||||
list = list || [];
|
||||
|
||||
// nothing to store
|
||||
if (!list || list.length === 0) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
// validate type
|
||||
if (!type) {
|
||||
callback({
|
||||
errMsg: 'Type is not set!'
|
||||
// validate type
|
||||
if (!type) {
|
||||
throw new Error('Type is not set!');
|
||||
}
|
||||
|
||||
// format items for batch storing in dao
|
||||
list.forEach(function(i) {
|
||||
key = createKey(i, type);
|
||||
|
||||
items.push({
|
||||
key: key,
|
||||
object: i
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// format items for batch storing in dao
|
||||
list.forEach(function(i) {
|
||||
key = createKey(i, type);
|
||||
resolve(items);
|
||||
|
||||
items.push({
|
||||
key: key,
|
||||
object: i
|
||||
});
|
||||
}).then(function(items) {
|
||||
// nothing to store
|
||||
if (items.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return self._lawnchairDAO.batch(items);
|
||||
});
|
||||
|
||||
this._lawnchairDAO.batch(items, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes items of a certain type from storage
|
||||
* Deletes items of a certain type from storage
|
||||
* @return {Promise}
|
||||
*/
|
||||
DeviceStorage.prototype.removeList = function(type, callback) {
|
||||
this._lawnchairDAO.removeList(type, callback);
|
||||
DeviceStorage.prototype.removeList = function(type) {
|
||||
return this._lawnchairDAO.removeList(type);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -79,17 +87,19 @@ DeviceStorage.prototype.removeList = function(type, callback) {
|
||||
* @param type [String] The type of item e.g. 'email'
|
||||
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
|
||||
* @param num [Number] The number of items to fetch (null means fetch all)
|
||||
* @return {Promise}
|
||||
*/
|
||||
DeviceStorage.prototype.listItems = function(type, offset, num, callback) {
|
||||
DeviceStorage.prototype.listItems = function(type, offset, num) {
|
||||
// fetch all items of a certain type from the data-store
|
||||
this._lawnchairDAO.list(type, offset, num, callback);
|
||||
return this._lawnchairDAO.list(type, offset, num);
|
||||
};
|
||||
|
||||
/**
|
||||
* Clear the whole device data-store
|
||||
* @return {Promise}
|
||||
*/
|
||||
DeviceStorage.prototype.clear = function(callback) {
|
||||
this._lawnchairDAO.clear(callback);
|
||||
DeviceStorage.prototype.clear = function() {
|
||||
return this._lawnchairDAO.clear();
|
||||
};
|
||||
|
||||
//
|
||||
|
12
test/main.js
12
test/main.js
@ -51,4 +51,16 @@ require('../src/js/email');
|
||||
|
||||
window.qMock = function(res, rej) {
|
||||
return new Promise(res, rej);
|
||||
};
|
||||
|
||||
window.resolves = function(val) {
|
||||
return new Promise(function(res) {
|
||||
res(val);
|
||||
});
|
||||
};
|
||||
|
||||
window.rejects = function(val) {
|
||||
return new Promise(function(res, rej) {
|
||||
rej(val);
|
||||
});
|
||||
};
|
@ -11,27 +11,28 @@ describe('Device Storage DAO unit tests', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
lawnchairDaoStub = sinon.createStubInstance(LawnchairDAO);
|
||||
storageDao = new DeviceStorageDAO(lawnchairDaoStub);
|
||||
storageDao = new DeviceStorageDAO(lawnchairDaoStub, window.qMock);
|
||||
});
|
||||
|
||||
afterEach(function() {});
|
||||
|
||||
describe('init', function() {
|
||||
it('should work', function() {
|
||||
lawnchairDaoStub.init.yields();
|
||||
it('should work', function(done) {
|
||||
lawnchairDaoStub.init.returns(resolves());
|
||||
|
||||
storageDao.init(testUser, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
storageDao.init(testUser).then(function() {
|
||||
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail', function() {
|
||||
lawnchairDaoStub.init.yields(new Error());
|
||||
it('should fail', function(done) {
|
||||
lawnchairDaoStub.init.returns(rejects(new Error()));
|
||||
|
||||
storageDao.init(testUser, function(err) {
|
||||
storageDao.init(testUser).catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
expect(lawnchairDaoStub.init.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -40,7 +41,7 @@ describe('Device Storage DAO unit tests', function() {
|
||||
it('should fail', function(done) {
|
||||
var list = [{}];
|
||||
|
||||
storageDao.storeList(list, '', function(err) {
|
||||
storageDao.storeList(list, '').catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
done();
|
||||
});
|
||||
@ -49,21 +50,17 @@ describe('Device Storage DAO unit tests', function() {
|
||||
it('should work with empty list', function(done) {
|
||||
var list = [];
|
||||
|
||||
storageDao.storeList(list, 'email', function(err) {
|
||||
expect(err).to.not.exist;
|
||||
done();
|
||||
});
|
||||
storageDao.storeList(list, 'email').then(done);
|
||||
});
|
||||
|
||||
it('should work', function(done) {
|
||||
lawnchairDaoStub.batch.yields();
|
||||
lawnchairDaoStub.batch.returns(resolves());
|
||||
|
||||
var list = [{
|
||||
foo: 'bar'
|
||||
}];
|
||||
|
||||
storageDao.storeList(list, 'email', function(err) {
|
||||
expect(err).to.not.exist;
|
||||
storageDao.storeList(list, 'email').then(function() {
|
||||
expect(lawnchairDaoStub.batch.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
@ -72,10 +69,9 @@ describe('Device Storage DAO unit tests', function() {
|
||||
|
||||
describe('remove list', function() {
|
||||
it('should work', function(done) {
|
||||
lawnchairDaoStub.removeList.yields();
|
||||
lawnchairDaoStub.removeList.returns(resolves());
|
||||
|
||||
storageDao.removeList('email', function(err) {
|
||||
expect(err).to.not.exist;
|
||||
storageDao.removeList('email').then(function() {
|
||||
expect(lawnchairDaoStub.removeList.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
@ -84,10 +80,9 @@ describe('Device Storage DAO unit tests', function() {
|
||||
|
||||
describe('list items', function() {
|
||||
it('should work', function(done) {
|
||||
lawnchairDaoStub.list.yields();
|
||||
lawnchairDaoStub.list.returns(resolves());
|
||||
|
||||
storageDao.listItems('email', 0, null, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
storageDao.listItems('email', 0, null).then(function() {
|
||||
expect(lawnchairDaoStub.list.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
@ -96,10 +91,9 @@ describe('Device Storage DAO unit tests', function() {
|
||||
|
||||
describe('clear', function() {
|
||||
it('should work', function(done) {
|
||||
lawnchairDaoStub.clear.yields();
|
||||
lawnchairDaoStub.clear.returns(resolves());
|
||||
|
||||
storageDao.clear(function(err) {
|
||||
expect(err).to.not.exist;
|
||||
storageDao.clear().then(function() {
|
||||
expect(lawnchairDaoStub.clear.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
|
@ -23,9 +23,7 @@ describe('Invitation DAO unit tests', function() {
|
||||
|
||||
describe('invite', function() {
|
||||
it('should invite the recipient', function(done) {
|
||||
restDaoStub.put.returns(new Promise(function(res) {
|
||||
res();
|
||||
}));
|
||||
restDaoStub.put.returns(resolves());
|
||||
|
||||
invitationDao.invite({
|
||||
recipient: alice,
|
||||
@ -37,7 +35,7 @@ describe('Invitation DAO unit tests', function() {
|
||||
});
|
||||
|
||||
it('should not work for http error', function(done) {
|
||||
restDaoStub.put.throws(new Error());
|
||||
restDaoStub.put.returns(rejects(new Error()));
|
||||
|
||||
invitationDao.invite({
|
||||
recipient: alice,
|
||||
|
Loading…
Reference in New Issue
Block a user