Created
October 31, 2010 09:05
-
-
Save filmaj/656329 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.
This file contains hidden or 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
var chain = function(tests, delay) { | |
if (typeof tests == 'object') { | |
if (tests.length > 0) { | |
if (typeof delay != 'undefined') { | |
setTimeout(function() { | |
tests.shift()(); | |
chain(tests, delay); | |
}, delay); | |
} else { | |
return function() { | |
tests.shift().apply({ | |
nextCallback:function() { | |
return chain(tests); | |
} | |
}, arguments); | |
} | |
} | |
} else QUnit.start(); | |
} | |
}; | |
// now instead of nesting functions inside timeouts when testing async code, you | |
// can call chain with an array of anonymous functions that execute your assertions. | |
// each function will then call the next function in an async chain. | |
test('some random timeout test with multiple timed-out assertions', function() { | |
QUnit.stop(); | |
chain([function() { | |
// first example async tests with assertions | |
var obj = new TestItem(); | |
obj.testAPI('testValue'); | |
equals(obj.test, 'testValue', 'testAPI function sets proper value'); | |
}, function() { | |
// second example async tests with assertions | |
var obj = new TestItem(); | |
obj.testAPI('test2Value'); | |
equals(obj.test, 'test2Value', 'testAPI function sets proper different value'); | |
}], 2000); | |
}); | |
// can we do it for callback-type assertions? | |
test( 'nuke()', function() { | |
QUnit.stop(); | |
expect(4); | |
store.nuke(); | |
/* | |
Example: old async callback tests for Lawnchair: | |
store.all(function(r) { | |
equals(r.length, 0, "should have 0 length when using full callback syntax"); | |
store.nuke(); | |
furtherassertions = function() { | |
same(store.nuke(), store, "should be chainable on nuke"); | |
store.save(me); | |
store.nuke(); | |
store.all(function(r) { | |
equals(r.length, 0, "should have 0 length after saving, then nuking"); | |
start(); | |
}); | |
} | |
store.all('equals(r.length, 0, "should have 0 length when using shorthand syntax"); furtherassertions();'); | |
});*/ | |
// New syntax using chain(): | |
store.all(chain([function(r) { | |
console.log('assert one'); | |
equals(r.length, 0, "should have 0 length when using full callback syntax"); | |
store.nuke(); | |
var self = this; | |
console.log(self); | |
store.all(this.nextCallback()); | |
},function(r) { | |
equals(r.length, 0, "should have 0 length when using shorthand syntax"); | |
console.log('assert two'); | |
same(store.nuke(), store, "should be chainable on nuke"); | |
store.save(me); | |
store.nuke(); | |
store.all(this.nextCallback()); | |
},function(r) { | |
console.log('assert three'); | |
equals(r.length, 0, "should have 0 length after saving, then nuking"); | |
this.nextCallback(); | |
} | |
])); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thumbs up, Fil.