Skip to content

Instantly share code, notes, and snippets.

@mattheworiordan
Forked from filmaj/QUnit.chain
Created September 1, 2011 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattheworiordan/1186641 to your computer and use it in GitHub Desktop.
Save mattheworiordan/1186641 to your computer and use it in GitHub Desktop.
Tiny QUnit async test helper, eliminates pyramid code in async tests with multiple assertions, each requiring its own delay.
/**
* testAsyncStepsWithPause
* Executes any number of async Qunit tests with a pause between each step
*
* Author: Matthew O'Riordan, http://mattheworiordan.com
*
* Params:
* @timeToWait: milliseconds between running method and running tests
* @methods: 1+ function arguments passed in the format:
* function() {
* // execute code here which requires the tests to wait for @timeToWait
* return function() {
* // execute assertions here, will be executed after @timeToWait
* }
* }, ...
*/
function testAsyncStepsWithPause(pause) {
var args = arguments;
if (args.length > 1) {
stop();
var asyncTestFunction = args[1]();
setTimeout(function() {
asyncTestFunction();
start();
var params = [pause].concat(Array.prototype.slice.call(args, 2));
testAsyncStepsWithPause.apply(this, params);
}, pause)
}
}
$(document).ready(function() {
module("Simple drag up and down without options");
test("drag down", function() {
$('#sortable').sortable();
testAsyncStepsWithPause(35,
function() {
$('#elem1').simulateDragSortable({ move: 1 });
return function() {
equal( $('#elem1')[0], $('#sortable li:nth-child(2)')[0], "Element one should have moved to second position" );
equal( $('#elem2')[0], $('#sortable li:nth-child(1)')[0], "Element two should now have moved to the top" );
};
},
function() {
$('#elem1').simulateDragSortable({ move: 2 });
return function() {
equal( $('#elem1')[0], $('#sortable li:nth-child(4)')[0], "Element one should have moved to position four" );
};
},
function() {
$('#elem1').simulateDragSortable({ move: 2 });
return function() {
equal( $('#elem1')[0], $('#sortable li:nth-child(5)')[0], "Element one should have moved to position five (boundary)" );
};
},
function() {
$('#elem1').simulateDragSortable({ move: -2 });
return function() {
equal( $('#elem1')[0], $('#sortable li:nth-child(3)')[0], "Element one should have moved to position three" );
};
}
);
});
});
@fliptopbox
Copy link

How ironic after trawling google i find you :D I am using your helper to delay a stopwatch counter for a webkit app that needs a delayed (a)syncronous test to ensure the chronometer stops, pauses and restarts correctly. It was difficult to find anything in QUnit forums to achieve this type of testing. Thanks for sharing your work. Regards. Bruce.

@mattheworiordan
Copy link
Author

Ah, glad to help ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment