Created
February 23, 2012 13:24
-
-
Save cou929/1892770 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
describe('Mocha itself on client-side', function() { | |
it('should works well!', function() { | |
expect(5).to.be.a('number'); | |
}); | |
}); | |
if (!Object.create) { | |
Object.create = function (o) { | |
if (arguments.length > 1) { | |
throw new Error('Object.create implementation only accepts the first parameter.'); | |
} | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
}; | |
} | |
var ajax = tddjs.ajax; | |
function stabFn(return_value) { | |
var fn = function() { | |
fn.called = true; | |
fn.args = arguments; | |
return return_value; | |
} | |
fn.called = false; | |
return fn; | |
} | |
function forceStatusAndReadyState(xhr, status, rs) { | |
var success = stabFn(); | |
var failure = stabFn(); | |
ajax.get('/url', { | |
'method': 'GET', | |
'success': success, | |
'failure': failure | |
}); | |
xhr.status = status; | |
xhr.readyStateChange(rs); | |
return { | |
'success': success.called, | |
'failure': failure.called | |
}; | |
} | |
var fake_xhr = { | |
'open': stabFn(), | |
'send': stabFn(), | |
readyStateChange: function(readyState) { | |
this.readyState = readyState; | |
this.onreadystatechange(); | |
} | |
}; | |
describe('tddjs.ajax', function() { | |
if (!ajax.create) { | |
return; | |
} | |
describe('#create()', function() { | |
var xhr = ajax.create(); | |
it('should return XMLHttpRequest object', function() { | |
expect(xhr.readyState).to.be.a('number'); | |
expect(xhr.open).to.be.a('function'); | |
expect(xhr.send).to.be.a('function'); | |
expect(xhr.setRequestHeader).to.be.a('function'); | |
}); | |
}); | |
describe('#get()', function() { | |
before(function(done) { | |
this.tddjsIsLocal = tddjs.isLocal; | |
this.ajaxCreate = ajax.create; | |
this.xhr = Object.create(fake_xhr); | |
ajax.create = stabFn(this.xhr); | |
done(); | |
}); | |
after(function(done) { | |
ajax.create = this.ajaxCreate; | |
tddjs.isLocal = this.tddjsIsLocal; | |
done(); | |
}); | |
it('should be a function', function() { | |
expect(ajax.get).to.be.a('function'); | |
}); | |
it('should throw error without url', function() { | |
expect(function() { | |
ajax.get(); | |
}).to.throwException(function(e) { | |
expect(e).to.be.a(TypeError); | |
}); | |
}); | |
it('should call ajax.create internally', function() { | |
ajax.get('/url', { | |
'method': 'GET' | |
}); | |
expect(ajax.create.called).to.be.ok(); | |
}); | |
it('should call xhr.open() in right way', function() { | |
var url = '/url'; | |
ajax.get(url,{ | |
'method': 'GET' | |
}); | |
expect(this.xhr.open.args).to.eql(['GET', url, true]); | |
}); | |
it('should set onreadystatechange internally', function() { | |
ajax.get('/url', { | |
'method': 'GET' | |
}); | |
expect(this.xhr.onreadystatechange).to.be.a('function'); | |
}); | |
it('should call xhr.send() internally', function() { | |
var url = '/url'; | |
ajax.get(url, { | |
'method': 'GET' | |
}); | |
expect(this.xhr.send.called).to.be.ok(); | |
}); | |
it('should call onreadystatechange callback if readyState is 4 and statsu is 200', function() { | |
this.xhr.readyState = 4; | |
this.xhr.status = 200; | |
var success = stabFn(); | |
ajax.get('/url', { | |
'method': 'GET', | |
'success': success | |
}); | |
this.xhr.onreadystatechange(); | |
expect(success.called).to.be.ok(); | |
}) | |
it('should not throw error without success handler', function() { | |
this.xhr.readyState = 4; | |
this.xhr.status = 200; | |
var self = this; | |
ajax.get('/url', { | |
'method': 'GET' | |
}); | |
expect(function() { | |
self.xhr.onreadystatechange(); | |
}).to.not.throwException(); | |
}); | |
it('should call xhr.send(null)', function() { | |
ajax.get('/url', { | |
'method': 'GET' | |
}); | |
expect(this.xhr.send.args[0]).to.eql(null); | |
}); | |
it('should call success handler for local requests', function() { | |
this.xhr.readyState = 4; | |
this.xhr.status = 0; | |
var success = stabFn(); | |
tddjs.isLocal = stabFn(true); | |
ajax.get('file.html', { | |
'method': 'GET', | |
'success': success | |
}); | |
this.xhr.onreadystatechange(); | |
expect(success.called).to.be.ok(); | |
tddjs.isLocal = function() {}; | |
}); | |
it('should call success handler for all 2** status', function() { | |
var i, request; | |
for (i = 200; i < 300; i++) { | |
request = forceStatusAndReadyState(this.xhr, i, 4); | |
expect(request.success).to.be.ok(); | |
expect(request.failure).to.not.be.ok(); | |
} | |
}); | |
it('should call failure handler for all status from 100 to 500 except for 304 and 2**', function() { | |
var i, request; | |
for (i = 100; i <= 500; i++) { | |
if (i === 200) { | |
i = 300; | |
continue; | |
} else if (i === 304) { | |
continue; | |
} | |
request = forceStatusAndReadyState(this.xhr, i, 4); | |
expect(request.success).to.not.be.ok(); | |
expect(request.failure).to.be.ok(); | |
} | |
}); | |
it('should call success handler for all 304 status', function() { | |
request = forceStatusAndReadyState(this.xhr, 304, 4); | |
expect(request.success).to.be.ok(); | |
expect(request.failure).to.not.be.ok(); | |
}); | |
}); | |
describe('#request()', function() { | |
before(function(done) { | |
this.tddjsIsLocal = tddjs.isLocal; | |
this.ajaxCreate = ajax.create; | |
this.xhr = Object.create(fake_xhr); | |
ajax.create = stabFn(this.xhr); | |
this.tddjsUrlParams = tddjs.urlParams; | |
done(); | |
}); | |
after(function(done) { | |
ajax.create = this.ajaxCreate; | |
tddjs.isLocal = this.tddjsIsLocal; | |
tddjs.urlParams = this.tddjsUrlParams; | |
done(); | |
}); | |
it('should use specified request method', function() { | |
ajax.request('/url', { | |
'method': 'POST' | |
}); | |
expect(this.xhr.open.args[0]).to.be.eql('POST'); | |
}); | |
it('should encode data', function() { | |
tddjs.urlParams = stabFn(); | |
var object = { | |
'field1': '13', | |
'field2': 'Lots of data!' | |
}; | |
ajax.request('/url', { | |
'data': object, | |
'method': 'POST' | |
}); | |
expect(tddjs.urlParams.args[0]).to.be.eql(object); | |
tddjs.urlParams = this.tddjsUrlParams; | |
}); | |
it('should send data with send() for POST', function() { | |
var object = { | |
'field1': '13', | |
'field2': 'Lots of data!' | |
}; | |
var expected = tddjs.urlParams(object); | |
ajax.request('/url', { | |
'data': object, | |
'method': 'POST' | |
}); | |
expect(this.xhr.send.args[0]).to.be.eql(expected); | |
}); | |
it('should send data on URL for GET', function() { | |
var url = '/url'; | |
var object = { | |
'field1': '13', | |
'field2': 'Lots of data!' | |
}; | |
var expected = url + '?' + tddjs.urlParams(object); | |
ajax.request(url, { | |
'data': object, | |
'method': 'GET' | |
}); | |
expect(this.xhr.open.args[1]).to.be.eql(expected); | |
}); | |
}); | |
describe('#post()', function() { | |
before(function(done) { | |
this.tddjsIsLocal = tddjs.isLocal; | |
this.ajaxCreate = ajax.create; | |
this.xhr = Object.create(fake_xhr); | |
ajax.create = stabFn(this.xhr); | |
this.ajaxRequest = ajax.request; | |
done(); | |
}); | |
after(function(done) { | |
ajax.create = this.ajaxCreate; | |
tddjs.isLocal = this.tddjsIsLocal; | |
ajax.request = this.ajaxRequest; | |
done(); | |
}); | |
it('should call ajax.request() with mathod: "POST"', function() { | |
ajax.request = stabFn(); | |
ajax.post('/url'); | |
expect(ajax.request.args[1].method).to.be.eql('POST'); | |
}); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment