diff --git a/src/js/util/update/update-handler.js b/src/js/util/update/update-handler.js index 243a57a..e8faba6 100644 --- a/src/js/util/update/update-handler.js +++ b/src/js/util/update/update-handler.js @@ -15,7 +15,8 @@ var axe = require('axe-logger'), /** * Handles database migration */ -function UpdateHandler(appConfigStore, accountStore, auth, dialog) { +function UpdateHandler($q, appConfigStore, accountStore, auth, dialog) { + this._q = $q; this._appConfigStorage = appConfigStore; this._userStorage = accountStore; this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5]; @@ -25,76 +26,67 @@ function UpdateHandler(appConfigStore, accountStore, auth, dialog) { /** * Executes all the necessary updates - * @param {Function} callback(error) Invoked when all the database updates were executed, or if an error occurred */ -UpdateHandler.prototype.update = function(callback) { +UpdateHandler.prototype.update = function() { var self = this, currentVersion = 0, targetVersion = cfg.dbVersion, versionDbType = 'dbVersion'; - self._appConfigStorage.listItems(versionDbType, 0, null, function(err, items) { - if (err) { - callback(err); - return; - } - + return self._appConfigStorage.listItems(versionDbType, 0, null).then(function(items) { // parse the database version number if (items && items.length > 0) { currentVersion = parseInt(items[0], 10); } - self._applyUpdate({ + return self._applyUpdate({ currentVersion: currentVersion, targetVersion: targetVersion - }, callback); + }); }); }; /** * Schedules necessary updates and executes thom in order */ -UpdateHandler.prototype._applyUpdate = function(options, callback) { - var self = this, - scriptOptions, - queue = []; +UpdateHandler.prototype._applyUpdate = function(options) { + var self = this; + return self._q(function(resolve, reject) { + var scriptOptions, + queue = []; - if (options.currentVersion >= options.targetVersion) { - // the current database version is up to date - callback(); - return; - } - - scriptOptions = { - appConfigStorage: self._appConfigStorage, - userStorage: self._userStorage, - auth: self._auth - }; - - // add all the necessary database updates to the queue - for (var i = options.currentVersion; i < options.targetVersion; i++) { - queue.push(self._updateScripts[i]); - } - - // takes the next update from the queue and executes it - function executeNextUpdate(err) { - if (err) { - callback(err); + if (options.currentVersion >= options.targetVersion) { + // the current database version is up to date + resolve(); return; } - if (queue.length < 1) { - // we're done - callback(); - return; + scriptOptions = { + appConfigStorage: self._appConfigStorage, + userStorage: self._userStorage, + auth: self._auth + }; + + // add all the necessary database updates to the queue + for (var i = options.currentVersion; i < options.targetVersion; i++) { + queue.push(self._updateScripts[i]); } - // process next update - var script = queue.shift(); - script(scriptOptions, executeNextUpdate); - } + // takes the next update from the queue and executes it + function executeNextUpdate() { + if (queue.length < 1) { + // we're done + resolve(); + return; + } - executeNextUpdate(); + // process next update + var script = queue.shift(); + script(scriptOptions).then(executeNextUpdate).catch(reject); + } + + executeNextUpdate(); + }); }; /** diff --git a/src/js/util/update/update-v1.js b/src/js/util/update/update-v1.js index 2e04afa..b41c4bc 100644 --- a/src/js/util/update/update-v1.js +++ b/src/js/util/update/update-v1.js @@ -7,20 +7,15 @@ * every non-prefixed mail in the IMAP folders would be nuked due to the implementation * of the delta sync. */ -function updateV1(options, callback) { +function updateV1(options) { var emailDbType = 'email_', versionDbType = 'dbVersion', postUpdateDbVersion = 1; // remove the emails - options.userStorage.removeList(emailDbType, function(err) { - if (err) { - callback(err); - return; - } - + return options.userStorage.removeList(emailDbType).then(function() { // update the database version to postUpdateDbVersion - options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); + return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType); }); } diff --git a/src/js/util/update/update-v2.js b/src/js/util/update/update-v2.js index 9ebd0d6..816cfce 100644 --- a/src/js/util/update/update-v2.js +++ b/src/js/util/update/update-v2.js @@ -6,20 +6,15 @@ * In database version 2, the stored email objects have to be purged, because the * new data model stores information about the email structure in the property 'bodyParts'. */ -function updateV2(options, callback) { +function updateV2(options) { var emailDbType = 'email_', versionDbType = 'dbVersion', postUpdateDbVersion = 2; // remove the emails - options.userStorage.removeList(emailDbType, function(err) { - if (err) { - callback(err); - return; - } - + return options.userStorage.removeList(emailDbType).then(function() { // update the database version to postUpdateDbVersion - options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); + return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType); }); } diff --git a/src/js/util/update/update-v3.js b/src/js/util/update/update-v3.js index 3408bf6..9cecf12 100644 --- a/src/js/util/update/update-v3.js +++ b/src/js/util/update/update-v3.js @@ -6,20 +6,15 @@ * In database version 3, we introduced new flags to the messages, also * the outbox uses artificial uids */ -function update(options, callback) { +function update(options) { var emailDbType = 'email_', versionDbType = 'dbVersion', postUpdateDbVersion = 3; // remove the emails - options.userStorage.removeList(emailDbType, function(err) { - if (err) { - callback(err); - return; - } - + return options.userStorage.removeList(emailDbType).then(function() { // update the database version to postUpdateDbVersion - options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType, callback); + return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType); }); } diff --git a/src/js/util/update/update-v4.js b/src/js/util/update/update-v4.js index ece5848..e44971d 100644 --- a/src/js/util/update/update-v4.js +++ b/src/js/util/update/update-v4.js @@ -7,7 +7,7 @@ * indexeddb. only gmail was allowed as a mail service provider before, * so let's add this... */ -function update(options, callback) { +function update(options) { var VERSION_DB_TYPE = 'dbVersion', EMAIL_ADDR_DB_KEY = 'emailaddress', USERNAME_DB_KEY = 'username', @@ -29,77 +29,45 @@ function update(options, callback) { }; // load the email address (if existing) - loadFromDB(EMAIL_ADDR_DB_KEY, function(err, emailAddress) { - if (err) { - return callback(err); + var emailAddress; + return loadFromDB(EMAIL_ADDR_DB_KEY).then(function(address) { + emailAddress = address; + // load the provider (if existing) + return loadFromDB(PROVIDER_DB_KEY); + + }).then(function(provider) { + // if there is an email address without a provider, we need to add the missing provider entry + // for any other situation, we're good. + if (!(emailAddress && !provider)) { + // update the database version to POST_UPDATE_DB_VERSION + return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE); } - // load the provider (if existing) - loadFromDB(PROVIDER_DB_KEY, function(err, provider) { - if (err) { - return callback(err); - } + // add the missing provider key + var storeProvider = options.appConfigStorage.storeList(['gmail'], PROVIDER_DB_KEY); + // add the missing user name key + var storeAdress = options.appConfigStorage.storeList([emailAddress], USERNAME_DB_KEY); + // add the missing imap host info key + var storeImap = options.appConfigStorage.storeList([imap], IMAP_DB_KEY); + // add the missing empty real name + var storeEmptyName = options.appConfigStorage.storeList([''], REALNAME_DB_KEY); + // add the missing smtp host info key + var storeSmtp = options.appConfigStorage.storeList([smtp], SMTP_DB_KEY); - // if there is an email address without a provider, we need to add the missing provider entry - // for any other situation, we're good. + return Promise.all([storeProvider, storeAdress, storeImap, storeEmptyName, storeSmtp]).then(function() { + // reload the credentials + options.auth.initialized = false; + return options.auth._loadCredentials(); - if (!(emailAddress && !provider)) { - // update the database version to POST_UPDATE_DB_VERSION - return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback); - } - - // add the missing provider key - options.appConfigStorage.storeList(['gmail'], PROVIDER_DB_KEY, function(err) { - if (err) { - return callback(err); - } - - // add the missing user name key - options.appConfigStorage.storeList([emailAddress], USERNAME_DB_KEY, function(err) { - if (err) { - return callback(err); - } - - // add the missing imap host info key - options.appConfigStorage.storeList([imap], IMAP_DB_KEY, function(err) { - if (err) { - return callback(err); - } - - // add the missing empty real name - options.appConfigStorage.storeList([''], REALNAME_DB_KEY, function(err) { - if (err) { - return callback(err); - } - - // add the missing smtp host info key - options.appConfigStorage.storeList([smtp], SMTP_DB_KEY, function(err) { - if (err) { - return callback(err); - } - - // reload the credentials - options.auth.initialized = false; - options.auth._loadCredentials(function(err) { - if (err) { - return callback(err); - } - - - // update the database version to POST_UPDATE_DB_VERSION - options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback); - }); - }); - }); - }); - }); - }); + }).then(function() { + // update the database version to POST_UPDATE_DB_VERSION + return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE); }); }); - function loadFromDB(key, callback) { - options.appConfigStorage.listItems(key, 0, null, function(err, cachedItems) { - callback(err, (!err && cachedItems && cachedItems[0])); + function loadFromDB(key) { + return options.appConfigStorage.listItems(key, 0, null).then(function(cachedItems) { + return cachedItems && cachedItems[0]; }); } } diff --git a/src/js/util/update/update-v5.js b/src/js/util/update/update-v5.js index 5284b10..f431cc2 100644 --- a/src/js/util/update/update-v5.js +++ b/src/js/util/update/update-v5.js @@ -16,14 +16,9 @@ var POST_UPDATE_DB_VERSION = 5; * Due to an overlooked issue, there may be multiple folders, e.g. for sent mails. * This removes the "duplicate" folders. */ -function update(options, callback) { - +function update(options) { // remove the emails - options.userStorage.listItems(FOLDER_DB_TYPE, 0, null, function(err, stored) { - if (err) { - return callback(err); - } - + return options.userStorage.listItems(FOLDER_DB_TYPE, 0, null).then(function(stored) { var folders = stored[0] || []; [FOLDER_TYPE_INBOX, FOLDER_TYPE_SENT, FOLDER_TYPE_DRAFTS, FOLDER_TYPE_TRASH].forEach(function(mbxType) { var foldersForType = folders.filter(function(mbx) { @@ -39,15 +34,11 @@ function update(options, callback) { folders.splice(folders.indexOf(foldersForType[i]), 1); } }); + return options.userStorage.storeList([folders], FOLDER_DB_TYPE); - options.userStorage.storeList([folders], FOLDER_DB_TYPE, function(err) { - if (err) { - return callback(err); - } - - // update the database version to POST_UPDATE_DB_VERSION - options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE, callback); - }); + }).then(function() { + // update the database version to POST_UPDATE_DB_VERSION + return options.appConfigStorage.storeList([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE); }); } diff --git a/test/unit/util/update-handler-test.js b/test/unit/util/update-handler-test.js index 6d70d6d..b6cd558 100644 --- a/test/unit/util/update-handler-test.js +++ b/test/unit/util/update-handler-test.js @@ -17,7 +17,7 @@ describe('UpdateHandler', function() { userStorageStub = sinon.createStubInstance(DeviceStorageDAO); authStub = sinon.createStubInstance(Auth); dialogStub = sinon.createStubInstance(Dialog); - updateHandler = new UpdateHandler(appConfigStorageStub, userStorageStub, authStub, dialogStub); + updateHandler = new UpdateHandler(qMock, appConfigStorageStub, userStorageStub, authStub, dialogStub); }); afterEach(function() { @@ -40,10 +40,9 @@ describe('UpdateHandler', function() { it('should not update when up to date', function(done) { cfg.dbVersion = 10; // app requires database version 10 - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['10']); // database version is 10 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['10'])); // database version is 10 - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(appConfigStorageStub.listItems.calledOnce).to.be.true; done(); @@ -55,7 +54,7 @@ describe('UpdateHandler', function() { beforeEach(function() { updateCounter = 0; - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, ['2']); // database version is 0 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves(['2'])); // database version is 0 }); afterEach(function() { @@ -67,17 +66,16 @@ describe('UpdateHandler', function() { cfg.dbVersion = 4; // app requires database version 4 // a simple dummy update to executed that only increments the update counter - function dummyUpdate(options, callback) { + function dummyUpdate() { updateCounter++; - callback(); + return resolves(); } // inject the dummy updates instead of live ones updateHandler._updateScripts = [dummyUpdate, dummyUpdate, dummyUpdate, dummyUpdate]; // execute test - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(updateCounter).to.equal(2); done(); @@ -87,20 +85,20 @@ describe('UpdateHandler', function() { it('should fail while updating to v3', function(done) { cfg.dbVersion = 4; // app requires database version 4 - function dummyUpdate(options, callback) { + function dummyUpdate() { updateCounter++; - callback(); + return resolves(); } - function failingUpdate(options, callback) { - callback({}); + function failingUpdate() { + return rejects({}); } // inject the dummy updates instead of live ones updateHandler._updateScripts = [dummyUpdate, dummyUpdate, failingUpdate, dummyUpdate]; // execute test - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(updateCounter).to.equal(0); @@ -115,7 +113,7 @@ describe('UpdateHandler', function() { beforeEach(function() { cfg.dbVersion = 1; // app requires database version 1 - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(); // database version is 0 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves()); // database version is 0 }); afterEach(function() { @@ -125,11 +123,10 @@ describe('UpdateHandler', function() { }); it('should work', function(done) { - userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([1], versionDbType).yieldsAsync(); + userStorageStub.removeList.withArgs(emailDbType).returns(resolves()); + appConfigStorageStub.storeList.withArgs([1], versionDbType).returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -138,10 +135,10 @@ describe('UpdateHandler', function() { }); it('should fail when persisting database version fails', function(done) { - userStorageStub.removeList.yieldsAsync(); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(resolves()); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -151,9 +148,9 @@ describe('UpdateHandler', function() { }); it('should fail when wiping emails from database fails', function(done) { - userStorageStub.removeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.called).to.be.false; @@ -168,7 +165,7 @@ describe('UpdateHandler', function() { beforeEach(function() { cfg.dbVersion = 2; // app requires database version 2 - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [1]); // database version is 0 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([1])); // database version is 0 }); afterEach(function() { @@ -178,11 +175,10 @@ describe('UpdateHandler', function() { }); it('should work', function(done) { - userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([2], versionDbType).yieldsAsync(); + userStorageStub.removeList.withArgs(emailDbType).returns(resolves()); + appConfigStorageStub.storeList.withArgs([2], versionDbType).returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -191,10 +187,10 @@ describe('UpdateHandler', function() { }); it('should fail when persisting database version fails', function(done) { - userStorageStub.removeList.yieldsAsync(); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(resolves()); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -204,9 +200,9 @@ describe('UpdateHandler', function() { }); it('should fail when wiping emails from database fails', function(done) { - userStorageStub.removeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.called).to.be.false; @@ -221,7 +217,7 @@ describe('UpdateHandler', function() { beforeEach(function() { cfg.dbVersion = 3; // app requires database version 2 - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [2]); // database version is 0 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([2])); // database version is 0 }); afterEach(function() { @@ -231,11 +227,10 @@ describe('UpdateHandler', function() { }); it('should work', function(done) { - userStorageStub.removeList.withArgs(emailDbType).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([3], versionDbType).yieldsAsync(); + userStorageStub.removeList.withArgs(emailDbType).returns(resolves()); + appConfigStorageStub.storeList.withArgs([3], versionDbType).returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -244,10 +239,10 @@ describe('UpdateHandler', function() { }); it('should fail when persisting database version fails', function(done) { - userStorageStub.removeList.yieldsAsync(); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(resolves()); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -257,9 +252,9 @@ describe('UpdateHandler', function() { }); it('should fail when wiping emails from database fails', function(done) { - userStorageStub.removeList.yieldsAsync(new Error()); + userStorageStub.removeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.removeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.called).to.be.false; @@ -290,22 +285,21 @@ describe('UpdateHandler', function() { beforeEach(function() { cfg.dbVersion = 4; // app requires database version 4 - appConfigStorageStub.listItems.withArgs(versionDbType).yieldsAsync(null, [3]); // database version is 3 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([3])); // database version is 3 }); it('should add gmail as mail service provider with email address and no provider present in db', function(done) { - appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(null, [emailaddress]); - appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).yieldsAsync(null, []); - appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync(); - appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).yieldsAsync(); - appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).yieldsAsync(); - authStub._loadCredentials.yieldsAsync(); + appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([emailaddress])); + appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([])); + appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves()); + appConfigStorageStub.storeList.withArgs(['gmail'], PROVIDER_DB_KEY).returns(resolves()); + appConfigStorageStub.storeList.withArgs([emailaddress], USERNAME_DB_KEY).returns(resolves()); + appConfigStorageStub.storeList.withArgs([imap], IMAP_DB_KEY).returns(resolves()); + appConfigStorageStub.storeList.withArgs([smtp], SMTP_DB_KEY).returns(resolves()); + appConfigStorageStub.storeList.withArgs([''], REALNAME_DB_KEY).returns(resolves()); + authStub._loadCredentials.returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(appConfigStorageStub.storeList.callCount).to.equal(6); expect(appConfigStorageStub.listItems.calledThrice).to.be.true; expect(authStub._loadCredentials.calledOnce).to.be.true; @@ -315,12 +309,11 @@ describe('UpdateHandler', function() { }); it('should not add a provider when no email adress is in db', function(done) { - appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(null, []); - appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).yieldsAsync(null, []); - appConfigStorageStub.storeList.withArgs([4], versionDbType).yieldsAsync(); + appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(resolves([])); + appConfigStorageStub.listItems.withArgs(PROVIDER_DB_KEY).returns(resolves([])); + appConfigStorageStub.storeList.withArgs([4], versionDbType).returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(appConfigStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.listItems.calledThrice).to.be.true; @@ -329,10 +322,10 @@ describe('UpdateHandler', function() { }); it('should fail when appConfigStore write fails', function(done) { - appConfigStorageStub.listItems.yieldsAsync(null, []); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + appConfigStorageStub.listItems.returns(resolves([])); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(appConfigStorageStub.listItems.calledThrice).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -342,10 +335,10 @@ describe('UpdateHandler', function() { }); it('should fail when appConfigStore read fails', function(done) { - appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).yieldsAsync(new Error()); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + appConfigStorageStub.listItems.withArgs(EMAIL_ADDR_DB_KEY).returns(rejects(new Error())); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(appConfigStorageStub.listItems.calledTwice).to.be.true; expect(appConfigStorageStub.storeList.called).to.be.false; @@ -368,7 +361,7 @@ describe('UpdateHandler', function() { beforeEach(function() { cfg.dbVersion = 5; // app requires database version 4 - appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).yieldsAsync(null, [4]); // database version is 4 + appConfigStorageStub.listItems.withArgs(VERSION_DB_TYPE).returns(resolves([4])); // database version is 4 }); afterEach(function() { @@ -378,7 +371,7 @@ describe('UpdateHandler', function() { }); it('should work', function(done) { - userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).yieldsAsync(null, [ + userStorageStub.listItems.withArgs(FOLDER_DB_TYPE).returns(resolves([ [{ name: 'inbox1', type: FOLDER_TYPE_INBOX @@ -404,7 +397,7 @@ describe('UpdateHandler', function() { name: 'trash2', type: FOLDER_TYPE_TRASH }] - ]); + ])); userStorageStub.storeList.withArgs([ [{ @@ -420,12 +413,11 @@ describe('UpdateHandler', function() { name: 'trash1', type: FOLDER_TYPE_TRASH }] - ], FOLDER_DB_TYPE).yieldsAsync(); + ], FOLDER_DB_TYPE).returns(resolves()); - appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).yieldsAsync(); + appConfigStorageStub.storeList.withArgs([POST_UPDATE_DB_VERSION], VERSION_DB_TYPE).returns(resolves()); - updateHandler.update(function(error) { - expect(error).to.not.exist; + updateHandler.update().then(function() { expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true; expect(appConfigStorageStub.storeList.calledOnce).to.be.true; @@ -435,11 +427,11 @@ describe('UpdateHandler', function() { }); it('should fail when persisting database version fails', function(done) { - userStorageStub.listItems.yieldsAsync(null, []); - userStorageStub.storeList.yieldsAsync(); - appConfigStorageStub.storeList.yieldsAsync(new Error()); + userStorageStub.listItems.returns(resolves([])); + userStorageStub.storeList.returns(resolves()); + appConfigStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true; @@ -450,10 +442,10 @@ describe('UpdateHandler', function() { }); it('should fail when persisting folders fails', function(done) { - userStorageStub.listItems.yieldsAsync(null, []); - userStorageStub.storeList.yieldsAsync(new Error()); + userStorageStub.listItems.returns(resolves([])); + userStorageStub.storeList.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.storeList.calledOnce).to.be.true; @@ -464,9 +456,9 @@ describe('UpdateHandler', function() { }); it('should fail when listing folders fails', function(done) { - userStorageStub.listItems.yieldsAsync(new Error()); + userStorageStub.listItems.returns(rejects(new Error())); - updateHandler.update(function(error) { + updateHandler.update().catch(function(error) { expect(error).to.exist; expect(userStorageStub.listItems.calledOnce).to.be.true; expect(userStorageStub.storeList.called).to.be.false;