Skip to content

Commit

Permalink
more solid async Ajax tests
Browse files Browse the repository at this point in the history
Be careful that callbacks/events for one Ajax request don't occur after
the test case where it originated already finished.
  • Loading branch information
mislav committed Oct 31, 2011
1 parent a9d554d commit 49af32b
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions test/ajax.html
Expand Up @@ -24,22 +24,30 @@ <h1>Zepto Ajax unit tests</h1>
<script>
(function(){

function deferredResume(t, fn) {
setTimeout(function() { t.resume(fn || function(){}) }, 5);
}

Evidence('ZeptoAjaxTest', {

tearDown: function() {
$(document).off();
},

testAjaxBase: function(t){
var xhr = $.ajax({ url: 'fixtures/ajax_load_simple.html' });
this.assertEqual('function', typeof xhr['getResponseHeader']);
t.pause();
var xhr = $.ajax({
url: 'fixtures/ajax_load_simple.html',
complete: function() { deferredResume(t) }
});
t.assertEqual('function', typeof xhr['getResponseHeader']);
},

testAjaxGet: function(t){
t.pause();
var xhr = $.get('fixtures/ajax_load_simple.html', function(response){
var that = this;
t.resume(function(){
deferredResume(t, function(){
this.assert(response);
this.assertIdentical(window, that);
});
Expand All @@ -50,28 +58,13 @@ <h1>Zepto Ajax unit tests</h1>
testAjaxPost: function(t){
t.pause();
var xhr = $.post('fixtures/ajax_load_simple.html', function(response){
t.resume(function(){
deferredResume(t, function(){
this.assert(response);
});
});
t.assertIn('abort', xhr);
},

testAjaxNullTimeout: function (t) {
var startTime = new Date();
t.pause();
$.ajax({
url: 'fixtures/ajax_load_simple.html',
timeout: 0,
beforeSend: function () {
t.resume(function () {
this.assert(new Date() - startTime < 10);
});
}
});
},

// FIXME: this test sometimes passes even if purposely broken. Something stinks
testNumberOfActiveRequests: function(t) {
var maxActive = 0, ajaxStarted = 0, ajaxEnded = 0, requestsCompleted = 0;
t.assertIdentical(0, $.active);
Expand All @@ -82,24 +75,23 @@ <h1>Zepto Ajax unit tests</h1>
if ($.active > maxActive) maxActive = $.active;
})
.on('ajaxComplete', function() {
if (++requestsCompleted == 3) setTimeout(function() {
t.resume(function() {
t.assertEqual(3, maxActive);
t.assertIdentical(0, $.active);
if (++requestsCompleted == 3)
deferredResume(t, function() {
this.assertEqual(3, maxActive);
this.assertIdentical(0, $.active);
});
}, 10)
});

t.pause();
$.ajax({url: 'fixtures/ajax_load_simple.html'});
$.ajax({url: 'fixtures/ajax_load_simple.html'});
$.ajax({url: 'fixtures/ajax_load_simple.html'});
t.pause();
},

testAjaxPostWithAcceptType: function(t) {
t.pause();
$.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
t.resume(function() {
deferredResume(t, function() {
this.assert(response);
});
}, 'text/plain');
Expand All @@ -108,7 +100,7 @@ <h1>Zepto Ajax unit tests</h1>
testAjaxGetJSON: function(t){
t.pause();
var xhr = $.getJSON('fixtures/zepto.json', function(data){
t.resume(function(){
deferredResume(t, function(){
this.assertEqual('awesomeness', data.zepto);
});
});
Expand All @@ -118,7 +110,7 @@ <h1>Zepto Ajax unit tests</h1>
testAjaxGetJSONP: function(t){
t.pause();
var xhr = $.getJSON('fixtures/jsonp.js?callback=?&timestamp='+(+new Date), function(data){
t.resume(function(){
deferredResume(t, function(){
this.assertEqual(data.items.length, 0);
this.assertEqual(0, $('script[src^=fixtures]').size());
});
Expand All @@ -132,7 +124,7 @@ <h1>Zepto Ajax unit tests</h1>
t.pause();
var el = testEl.load('fixtures/ajax_load_simple.html', function(){
var that = this;
t.resume(function() {
deferredResume(t, function() {
this.assertIdentical(testEl, that);
this.assertEqual('simple ajax load', testEl.html().trim());
});
Expand All @@ -144,17 +136,12 @@ <h1>Zepto Ajax unit tests</h1>
var testEl = $('#ajax_load');
t.pause();
testEl.load('fixtures/ajax_load_selector.html #ajax_load_test_div', function() {
t.resume(function() {
deferredResume(t, function() {
this.assertEqual('ajax load with selector', testEl.html().trim());
});
});
},

testAjaxRequestHeader: function(t){
// @see Net Panel/Resources
// TODO
},

testAjaxWithContext: function(t) {
t.pause();
var body = $('body');
Expand All @@ -163,7 +150,7 @@ <h1>Zepto Ajax unit tests</h1>
context: body,
complete: function () {
var that = this;
t.resume(function () {
deferredResume(t, function() {
this.assertIdentical(body, that);
});
}
Expand Down

0 comments on commit 49af32b

Please sign in to comment.