1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-30 04:42:15 -05:00
mail/test/new-unit/rest-dao-test.js

200 lines
6.7 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var RestDAO = require('js/dao/rest-dao'),
expect = chai.expect;
describe('Rest DAO unit tests', function() {
2014-02-28 14:14:32 -05:00
var restDao, xhrMock, requests;
beforeEach(function() {
restDao = new RestDAO();
2014-02-28 14:14:32 -05:00
xhrMock = sinon.useFakeXMLHttpRequest();
requests = [];
xhrMock.onCreate = function(xhr) {
requests.push(xhr);
};
});
afterEach(function() {
2014-02-28 14:14:32 -05:00
xhrMock.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);
expect(restDao).to.exist;
expect(restDao._baseUri).to.equal(baseUri);
});
});
describe('get', function() {
2014-02-28 14:14:32 -05:00
it('should work with json as default type', function() {
restDao.get({
2014-02-28 14:14:32 -05:00
uri: '/asdf'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
expect(data.foo).to.equal('bar');
2014-02-28 14:14:32 -05:00
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json');
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{"foo": "bar"}');
});
2014-02-28 14:14:32 -05:00
it('should work with jsonz', function() {
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');
2014-02-28 14:14:32 -05:00
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json');
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{"foo": "bar"}');
});
2014-02-28 14:14:32 -05:00
it('should work with plain text', function() {
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!');
2014-02-28 14:14:32 -05:00
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('text/plain');
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "text/plain"
}, 'foobar!');
});
2014-02-28 14:14:32 -05:00
it('should work with xml', function() {
restDao.get({
uri: '/asdf',
type: 'xml'
2013-11-20 06:17:21 -05:00
}, function(err, data, status) {
expect(err).to.not.exist;
2014-02-28 14:14:32 -05:00
expect(data).to.equal('<foo>bar</foo>');
var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/xml');
2013-11-20 06:17:21 -05:00
expect(status).to.equal(200);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(200, {
"Content-Type": "application/xml"
}, '<foo>bar</foo>');
});
2014-02-28 14:14:32 -05:00
it('should fail for missing uri parameter', function() {
restDao.get({}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(400);
expect(data).to.not.exist;
});
});
2014-02-28 14:14:32 -05:00
it('should fail for unhandled data type', function() {
restDao.get({
uri: '/asdf',
type: 'snafu'
}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(400);
expect(data).to.not.exist;
});
});
2014-02-28 14:14:32 -05:00
it('should fail for server error', function() {
restDao.get({
uri: '/asdf'
}, function(err, data) {
expect(err).to.exist;
expect(err.code).to.equal(500);
expect(data).to.not.exist;
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(500, {
"Content-Type": "text/plain"
}, 'Internal error');
});
});
describe('put', function() {
2014-02-28 14:14:32 -05:00
it('should fail', function() {
restDao.put('/asdf', {}, function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(500, {
"Content-Type": "text/plain"
}, 'Internal error');
});
2013-11-20 06:17:21 -05:00
2014-02-28 14:14:32 -05:00
it('should work', function() {
2013-11-20 06:17:21 -05:00
restDao.put('/asdf', {}, function(err, res, status) {
expect(err).to.not.exist;
2014-02-28 14:14:32 -05:00
expect(res).to.equal('');
2013-11-20 06:17:21 -05:00
expect(status).to.equal(201);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(201);
});
});
describe('remove', function() {
2014-02-28 14:14:32 -05:00
it('should fail', function() {
restDao.remove('/asdf', function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(500, {
"Content-Type": "text/plain"
}, 'Internal error');
});
2014-02-28 14:14:32 -05:00
it('should work', function() {
2013-11-20 06:17:21 -05:00
restDao.remove('/asdf', function(err, res, status) {
expect(err).to.not.exist;
2014-02-28 14:14:32 -05:00
expect(res).to.equal('');
expect(status).to.equal(200);
});
2014-02-28 14:14:32 -05:00
expect(requests.length).to.equal(1);
requests[0].respond(200);
});
});
});
});