1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-13 12:45:04 -05:00
mail/test/new-unit/app-controller-test.js

87 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-08-20 09:19:13 -04:00
define(function(require) {
'use strict';
2013-08-20 09:19:13 -04:00
var controller = require('js/app-controller'),
EmailDAO = require('js/dao/email-dao'),
2013-09-26 11:17:47 -04:00
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
2013-08-20 09:19:13 -04:00
$ = require('jquery'),
expect = chai.expect;
var appControllerTest = {
user: 'test@exmaple.com',
passphrase: 'asdf'
};
describe('App Controller unit tests', function() {
2013-08-20 09:19:13 -04:00
beforeEach(function() {
2013-10-12 13:39:09 -04:00
sinon.stub(controller, 'init', function(userId, password, token, callback) {
2013-08-20 09:19:13 -04:00
controller._emailDao = sinon.createStubInstance(EmailDAO);
callback();
});
sinon.stub($, 'get');
sinon.stub($, 'ajax').yieldsTo('success', {
email: appControllerTest.user
});
window.chrome = window.chrome || {};
window.chrome.identity = window.chrome.identity || {};
if (typeof window.chrome.identity.getAuthToken !== 'function') {
window.chrome.identity.getAuthToken = function() {};
}
sinon.stub(window.chrome.identity, 'getAuthToken');
window.chrome.identity.getAuthToken.yields('token42');
});
afterEach(function() {
2013-10-12 13:39:09 -04:00
controller.init.restore();
2013-08-20 09:19:13 -04:00
$.get.restore();
$.ajax.restore();
window.chrome.identity.getAuthToken.restore();
});
describe('start', function() {
it('should not explode', function(done) {
controller.start(function(err) {
expect(err).to.not.exist;
done();
});
});
});
describe('fetchOAuthToken', function() {
beforeEach(function() {
controller._appConfigStore = sinon.createStubInstance(DeviceStorageDAO);
});
2013-09-26 11:17:47 -04:00
it('should work the first time', function(done) {
controller._appConfigStore.listItems.yields(null, []);
controller._appConfigStore.storeList.yields();
2013-09-26 11:17:47 -04:00
controller.fetchOAuthToken(appControllerTest.passphrase, function(err) {
expect(err).to.not.exist;
2013-10-11 21:19:01 -04:00
expect(controller._appConfigStore.listItems.calledOnce).to.be.true;
expect(controller._appConfigStore.storeList.calledOnce).to.be.true;
expect(window.chrome.identity.getAuthToken.calledOnce).to.be.true;
expect($.ajax.calledOnce).to.be.true;
done();
2013-09-26 11:17:47 -04:00
});
});
it('should work when the email address is cached', function(done) {
controller._appConfigStore.listItems.yields(null, ['asdf']);
controller.fetchOAuthToken(appControllerTest.passphrase, function(err) {
expect(err).to.not.exist;
2013-10-11 21:19:01 -04:00
expect(controller._appConfigStore.listItems.calledOnce).to.be.true;
expect(window.chrome.identity.getAuthToken.calledOnce).to.be.true;
2013-09-26 11:17:47 -04:00
expect($.ajax.called).to.be.false;
done();
});
});
});
});
});