1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-24 08:18:48 -05:00
mail/test/new-unit/rest-dao-test.js

221 lines
7.6 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var RestDAO = require('js/dao/rest-dao'),
$ = require('jquery'),
expect = chai.expect;
describe('Rest DAO unit tests', function() {
var restDao;
beforeEach(function() {
restDao = new RestDAO();
});
afterEach(function() {
2013-11-20 06:17:21 -05:00
if (typeof $.ajax.callCount !== 'undefined') {
$.ajax.restore();
}
});
describe('contructor', function() {
it('should set default base uri', function() {
restDao = new RestDAO();
expect(restDao).to.exist;
expect(restDao._baseUri).to.exist;
});
it('should accept default base uri', function() {
var baseUri = 'http://custom.com';
restDao = new RestDAO({
baseUri: baseUri
});
expect(restDao).to.exist;
expect(restDao._baseUri).to.equal(baseUri);
});
});
describe('get', function() {
it('should work with json as default type', function(done) {
var spy = sinon.stub($, 'ajax').yieldsTo('success', {
foo: 'bar'
2013-11-20 06:17:21 -05:00
}, 'success', {
status: 200
});
restDao.get({
uri: '/asdf',
type: 'json'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
expect(data.foo).to.equal('bar');
expect(spy.calledWith(sinon.match(function(request) {
return request.headers.Accept === 'application/json' && request.dataType === 'json';
}))).to.be.true;
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
done();
});
});
it('should work with json', function(done) {
var spy = sinon.stub($, 'ajax').yieldsTo('success', {
foo: 'bar'
2013-11-20 06:17:21 -05:00
}, 'success', {
status: 200
});
restDao.get({
uri: '/asdf',
type: 'json'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
expect(data.foo).to.equal('bar');
expect(spy.calledWith(sinon.match(function(request) {
return request.headers.Accept === 'application/json' && request.dataType === 'json';
}))).to.be.true;
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
done();
});
});
it('should work with plain text', function(done) {
2013-11-20 06:17:21 -05:00
var spy = sinon.stub($, 'ajax').yieldsTo('success', 'foobar!', 'success', {
status: 200
});
restDao.get({
uri: '/asdf',
type: 'text'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
expect(data).to.equal('foobar!');
expect(spy.calledWith(sinon.match(function(request) {
return request.headers.Accept === 'text/plain' && request.dataType === 'text';
}))).to.be.true;
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
done();
});
});
it('should work with xml', function(done) {
2013-11-20 06:17:21 -05:00
var spy = sinon.stub($, 'ajax').yieldsTo('success', '<foo>bar</foo>', 'success', {
status: 200
});
restDao.get({
uri: '/asdf',
type: 'xml'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
expect(data).to.equal('<foo>bar</foo>'); // that's probably not right, but in the unit test, it is :)
expect(spy.calledWith(sinon.match(function(request) {
return request.headers.Accept === 'application/xml' && request.dataType === 'xml';
}))).to.be.true;
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
done();
});
});
it('should fail for missing uri parameter', function(done) {
restDao.get({}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(400);
expect(data).to.not.exist;
done();
});
});
it('should fail for unhandled data type', function(done) {
restDao.get({
uri: '/asdf',
type: 'snafu'
}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(400);
expect(data).to.not.exist;
done();
});
});
it('should fail for server error', function(done) {
sinon.stub($, 'ajax').yieldsTo('error', {
status: 500
}, {
statusText: 'Internal error'
}, {});
restDao.get({
uri: '/asdf'
}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(500);
expect(data).to.not.exist;
done();
});
});
});
describe('put', function() {
it('should fail', function(done) {
sinon.stub($, 'ajax').yieldsTo('error', {
status: 500
}, {
statusText: 'Internal error'
}, {});
restDao.put('/asdf', {}, function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500);
done();
});
});
it('should work', function(done) {
2013-11-20 06:17:21 -05:00
var spy = sinon.stub($, 'ajax').yieldsTo('success', undefined, 'success', {
status: 201
});
restDao.put('/asdf', {}, function(err, res, status) {
expect(err).to.not.exist;
2013-11-20 06:17:21 -05:00
expect(res).to.not.exist;
expect(spy.callCount).to.equal(1);
expect(status).to.equal(201);
done();
});
});
});
describe('remove', function() {
it('should fail', function(done) {
sinon.stub($, 'ajax').yieldsTo('error', {
status: 500
}, {
statusText: 'Internal error'
}, {});
restDao.remove('/asdf', function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500);
done();
});
});
it('should work', function(done) {
2013-11-20 06:17:21 -05:00
var spy = sinon.stub($, 'ajax').yieldsTo('success', undefined, 'success', {
status: 204
});
restDao.remove('/asdf', function(err, res, status) {
expect(err).to.not.exist;
2013-11-20 06:17:21 -05:00
expect(res).to.not.exist;
expect(spy.callCount).to.equal(1);
expect(status).to.equal(204);
done();
});
});
});
});
});