1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-21 23:08:50 -05:00

start rewrite of unit tests using mocha, sinon and chai

This commit is contained in:
Tankred Hase 2013-08-19 21:13:32 +02:00
parent 83afb77adc
commit f06fe5e545
13 changed files with 208 additions and 20 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules
npm-debug.log
.DS_Store
*-browserified.js
test/new-unit/lib/

View File

@ -31,7 +31,16 @@
"start",
"chrome",
"define",
"self"
"self",
"describe",
"it",
"chai",
"sinon",
"mocha",
"before",
"beforeEach",
"after",
"afterEach"
],
"globals": {

View File

@ -40,7 +40,7 @@ module.exports = function(grunt) {
},
jshint: {
all: ['Gruntfile.js', 'src/*.js', 'src/js/**/*.js', 'test/unit/*.js', 'test/integration/*.js'],
all: ['Gruntfile.js', 'src/*.js', 'src/js/**/*.js', 'test/new-unit/*.js', 'test/unit/*.js', 'test/integration/*.js'],
options: {
jshintrc: '.jshintrc'
}
@ -56,6 +56,15 @@ module.exports = function(grunt) {
]
}
}
},
mocha: {
all: {
options: {
urls: ['http://localhost:<%= connect.test.options.port %>/test/new-unit/index.html'],
run: false
}
}
}
});
@ -63,10 +72,11 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-mocha');
// Default task(s).
grunt.registerTask('dev', ['connect:dev']);
grunt.registerTask('test', ['jshint', 'connect:test', 'qunit']);
grunt.registerTask('test', ['jshint', 'connect:test', 'mocha', 'qunit']);
grunt.registerTask('prod', ['connect:prod']);
};

View File

@ -16,8 +16,13 @@
},
"devDependencies": {
"grunt": "~0.4.1",
"mocha": "~1.12.0",
"chai": "~1.7.2",
"sinon": "~1.7.3",
"grunt-contrib-connect": "~0.3.0",
"grunt-contrib-jshint": "~0.5.3",
"grunt-contrib-qunit": "~0.2.1"
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-qunit": "~0.2.2",
"grunt-mocha-test": "~0.6.3",
"grunt-mocha": "~0.4.1"
}
}

View File

@ -19,4 +19,10 @@ cd ./node_modules/smtp-client/
node build.js && cp ./src-gen/*.js ../../src/lib/
cd ../../
# copy test dependencies
mkdir ./test/new-unit/lib/
cp ./node_modules/mocha/mocha.css ./node_modules/mocha/mocha.js ./test/new-unit/lib/
cp ./node_modules/chai/chai.js ./test/new-unit/lib/
cp ./node_modules/sinon/pkg/sinon.js ./test/new-unit/lib/
echo "\n--> finished copying dependencies.\n"

View File

@ -156,8 +156,6 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha
// init email dao
var account = new app.model.Account({
imapOptions: imapOptions,
smtpOptions: smtpOptions,
emailAddress: userId,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,

View File

@ -1,16 +1,21 @@
/**
* A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage
*/
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao',
'js/dao/devicestorage-dao', 'js/app-config', 'js/model/account-model'
], function(_, util, crypto, jsonDB, devicestorage, app) {
define(function(require) {
'use strict';
var EmailDAO = function(cloudstorage, keychain, imapClient, smtpClient) {
var _ = require('underscore'),
util = require('cryptoLib/util'),
crypto = require('js/crypto/crypto'),
jsonDB = require('js/dao/lawnchair-dao'),
devicestorage = require('js/dao/devicestorage-dao'),
app = require('js/app-config');
require('js/model/account-model');
/**
* A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage
*/
var EmailDAO = function(keychain, imapClient, smtpClient) {
var self = this;
self._cloudstorage = cloudstorage;
self._keychain = keychain;
self._imapClient = imapClient;
self._smtpClient = smtpClient;
@ -35,8 +40,11 @@ define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-da
// login IMAP client if existent
if (self._imapClient) {
self._imapClient.login(function() {
console.log('logged into imap.');
self._imapClient.login(function(err) {
if (err) {
callback(err);
return;
}
initKeychain();
});
} else {

View File

@ -23,4 +23,5 @@ define(['backbone', 'js/app-config', 'js/model/folder-model'], function(Backbone
});
return app.model.Account;
});

View File

@ -0,0 +1,20 @@
define(function() {
'use strict';
var expect = chai.expect;
describe('Cloudstorage DAO unit tests', function() {
beforeEach(function() {});
afterEach(function() {});
describe('init', function() {
it('should fail due to error in imap login', function() {
expect(true).to.be.ok;
});
});
});
});

View File

@ -0,0 +1,79 @@
define(function(require) {
'use strict';
var KeychainDAO = require('js/dao/keychain-dao'),
EmailDAO = require('js/dao/email-dao'),
SmtpClient = require('SmtpClient'),
ImapClient = require('ImapClient'),
Account = require('js/model/account-model'),
app = require('js/app-config'),
expect = chai.expect;
var emaildaoTest = {
user: "whiteout.test@t-online.de",
passphrase: 'asdf',
asymKeySize: 512
};
describe('Email DAO unit tests', function() {
var emailDao, account,
keychainStub, imapClientStub, smtpClientStub;
beforeEach(function() {
account = new Account({
emailAddress: emaildaoTest.user,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,
asymKeySize: emaildaoTest.asymKeySize
});
keychainStub = sinon.createStubInstance(KeychainDAO);
imapClientStub = sinon.createStubInstance(ImapClient);
smtpClientStub = sinon.createStubInstance(SmtpClient);
emailDao = new EmailDAO(keychainStub, imapClientStub, smtpClientStub);
});
afterEach(function() {});
describe('init', function() {
it('should fail due to error in imap login', function(done) {
imapClientStub.login.yields(42);
emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(err).to.equal(42);
done();
});
});
it('should fail due to error in getUserKeyPair', function(done) {
imapClientStub.login.yields();
keychainStub.getUserKeyPair.yields(42);
emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(imapClientStub.login.calledOnce).to.be.true;
expect(err).to.equal(42);
done();
});
});
it('should initialize', function(done) {
imapClientStub.login.yields();
keychainStub.getUserKeyPair.yields();
keychainStub.putUserKeyPair.yields();
emailDao.init(account, emaildaoTest.passphrase, function(err) {
expect(imapClientStub.login.calledOnce).to.be.true;
expect(keychainStub.getUserKeyPair.calledOnce).to.be.true;
expect(keychainStub.putUserKeyPair.calledOnce).to.be.true;
expect(err).to.not.exist;
done();
});
});
});
});
});

19
test/new-unit/index.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html style="overflow-y: auto">
<head>
<meta charset="utf-8">
<title>JavaScript Unit Tests</title>
<link rel="stylesheet" href="lib/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="lib/chai.js"></script>
<script src="lib/sinon.js"></script>
<script src="lib/mocha.js"></script>
<script data-main="main.js" src="../../src/lib/require.js"></script>
</body>
</html>

32
test/new-unit/main.js Normal file
View File

@ -0,0 +1,32 @@
'use strict';
require(['../../src/require-config'], function() {
require.config({
baseUrl: '../../src/lib'
});
// Start the main app logic.
require(['js/app-config', 'cordova'], function(app) {
// clear session storage of failed tests, so async order is correct after fail & refresh
window.sessionStorage.clear();
window.Worker = undefined;
app.config.workerPath = '../../src/js';
startTests();
});
});
function startTests() {
mocha.setup('bdd');
require(
[
'test/new-unit/email-dao-test',
'test/new-unit/cloudstorage-dao-test'
], function() {
//Tests loaded, run tests
mocha.run();
}
);
}

View File

@ -25,8 +25,7 @@ function startTests() {
'test/unit/lawnchair-dao-test',
'test/unit/keychain-dao-test',
'test/unit/crypto-test',
'test/unit/devicestorage-dao-test',
'test/unit/email-dao-test'
'test/unit/devicestorage-dao-test'
], function() {
//Tests loaded, run tests
QUnit.start();