1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

[WO-429] minor refactoring to fix the tests

This commit is contained in:
Felix Hammerl 2014-06-23 13:03:56 +02:00
parent 9465ce57ed
commit fa1fbda11f
2 changed files with 139 additions and 126 deletions

View File

@ -78,7 +78,7 @@ define(function(require) {
function initFolders() {
// try init folders from memory, since imap client not initiated yet
self._initFolders(function(err) {
self._initFoldersFromDisk(function(err) {
// dont handle offline case this time
if (err && err.code !== 42) {
callback(err);
@ -966,7 +966,7 @@ define(function(require) {
}
// init folders
self._initFolders(function(err) {
self._initFoldersFromImap(function(err) {
if (err) {
callback(err);
return;
@ -1121,37 +1121,50 @@ define(function(require) {
/**
* Updates the folder information from memory (if we're offline), or from imap (if we're online),
* and adds/removes folders in account.folders, if we added/removed folder in IMAP. If we have an
* uninitialized folder that lacks folder.messages, all the locally available messages are loaded
* from memory
* Updates the folder information from memory, and adds/removes folders in account.folders.
* The locally available messages are loaded from memory
*
* @param {Function} callback Invoked when the folders are up to date
*/
EmailDAO.prototype._initFolders = function(callback) {
EmailDAO.prototype._initFoldersFromDisk = function(callback) {
var self = this,
folderDbType = 'folders';
self._account.busy = true; // start the spinner
if (!self._account.online) {
// fetch list from local cache
self._devicestorage.listItems(folderDbType, 0, null, function(err, stored) {
if (err) {
done(err);
return;
return done(err);
}
self._account.folders = stored[0] || [];
readMessagesFromDisk();
self._initMessagesFromDisk(done);
});
return;
} else {
function done(err) {
self._account.busy = false; // stop the spinner
callback(err);
}
};
/**
* Updates the folder information from imap (if we're online). Adds/removes folders in account.folders,
* if we added/removed folder in IMAP. If we have an uninitialized folder that lacks folder.messages,
* all the locally available messages are loaded from memory.
*
* @param {Function} callback Invoked when the folders are up to date
*/
EmailDAO.prototype._initFoldersFromImap = function(callback) {
var self = this,
folderDbType = 'folders';
self._account.busy = true; // start the spinner
// fetch list from imap server
self._imapClient.listWellKnownFolders(function(err, wellKnownFolders) {
if (err) {
done(err);
return;
return done(err);
}
// this array is dropped directly into the ui to create the folder list
@ -1191,38 +1204,44 @@ define(function(require) {
// if folder have changed, we need to persist them to disk.
if (!foldersChanged) {
readMessagesFromDisk();
return;
return self._initMessagesFromDisk(done);
}
// persist encrypted list in device storage
// NB! persis the array we received from IMAP! do *not* persist self._account.folders with all the messages...
self._devicestorage.storeList([folders], folderDbType, function(err) {
if (err) {
done(err);
return;
return done(err);
}
readMessagesFromDisk();
self._initMessagesFromDisk(done);
});
});
return;
}
// fill uninitialized folders with the locally available messages
function readMessagesFromDisk() {
function done(err) {
self._account.busy = false; // stop the spinner
callback(err);
}
};
/**
* Fill uninitialized folders with the locally available messages.
*
* @param {Function} callback Invoked when the folders are filled with messages
*/
EmailDAO.prototype._initMessagesFromDisk = function(callback) {
var self = this;
if (!self._account.folders || self._account.folders.length === 0) {
done();
return;
return callback();
}
var after = _.after(self._account.folders.length, done);
var after = _.after(self._account.folders.length, callback);
self._account.folders.forEach(function(folder) {
if (folder.messages) {
// the folder is already initialized
after();
return;
return after();
}
// sync messages from disk to the folder model
@ -1230,19 +1249,12 @@ define(function(require) {
folder: folder
}, function(err) {
if (err) {
done(err);
return;
return callback(err);
}
after();
});
});
}
function done(err) {
self._account.busy = false; // stop the spinner
callback(err);
}
};

View File

@ -130,7 +130,7 @@ define(function(require) {
beforeEach(function() {
delete dao._account;
initFoldersStub = sinon.stub(dao, '_initFolders');
initFoldersStub = sinon.stub(dao, '_initFoldersFromDisk');
});
it('should initialize folders and return keypair', function(done) {
@ -1486,7 +1486,7 @@ define(function(require) {
var initFoldersStub;
beforeEach(function() {
initFoldersStub = sinon.stub(dao, '_initFolders');
initFoldersStub = sinon.stub(dao, '_initFoldersFromImap');
delete dao._imapClient;
delete dao._pgpMailer;
});
@ -1610,50 +1610,51 @@ define(function(require) {
describe('internal API', function() {
describe('#_initFolders', function() {
var refreshFolderStub;
describe('#_initFoldersFromDisk', function() {
beforeEach(function() {
refreshFolderStub = sinon.stub(dao, 'refreshFolder');
sinon.stub(dao, 'refreshFolder');
});
it('should initialize from disk if offline and not refresh folder', function(done) {
account.online = false;
devicestorageStub.listItems.withArgs('folders').yieldsAsync(null, [
[inboxFolder]
]);
refreshFolderStub.withArgs({
dao.refreshFolder.withArgs({
folder: inboxFolder
}).yieldsAsync();
dao._initFolders(function(err) {
dao._initFoldersFromDisk(function(err) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(refreshFolderStub.called).to.be.false;
expect(dao.refreshFolder.called).to.be.false;
done();
});
});
it('should initialize from disk if offline and refresh folder', function(done) {
account.online = false;
delete inboxFolder.messages;
devicestorageStub.listItems.withArgs('folders').yieldsAsync(null, [
[inboxFolder]
]);
refreshFolderStub.withArgs({
dao.refreshFolder.withArgs({
folder: inboxFolder
}).yieldsAsync();
dao._initFolders(function(err) {
dao._initFoldersFromDisk(function(err) {
expect(err).to.not.exist;
expect(devicestorageStub.listItems.calledOnce).to.be.true;
expect(refreshFolderStub.calledOnce).to.be.true;
expect(dao.refreshFolder.calledOnce).to.be.true;
done();
});
});
});
describe('#_initFoldersFromImap', function() {
beforeEach(function() {
sinon.stub(dao, 'refreshFolder');
});
it('should initialize from imap if online', function(done) {
account.online = true;
account.folders = [];
imapClientStub.listWellKnownFolders.yieldsAsync(null, {
inbox: inboxFolder,
@ -1671,9 +1672,9 @@ define(function(require) {
return true;
}), 'folders').yieldsAsync();
refreshFolderStub.yieldsAsync();
dao.refreshFolder.yieldsAsync();
dao._initFolders(function(err) {
dao._initFoldersFromImap(function(err) {
expect(err).to.not.exist;
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
expect(devicestorageStub.storeList.calledOnce).to.be.true;