https://github.com/jareguo/qunit-assert-callback
A QUnit assert plugin to test callback. You can easily assert that the callback can (only) be called at the correct moment.
https://github.com/jareguo/qunit-assert-callback
qunit
Last synced: 7 months ago
JSON representation
A QUnit assert plugin to test callback. You can easily assert that the callback can (only) be called at the correct moment.
- Host: GitHub
- URL: https://github.com/jareguo/qunit-assert-callback
- Owner: jareguo
- License: mit
- Created: 2014-08-16T13:10:34.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-09-27T09:20:59.000Z (about 11 years ago)
- Last Synced: 2025-01-30T04:27:20.227Z (8 months ago)
- Topics: qunit
- Language: JavaScript
- Size: 191 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# QUnit callback assertion plugin
A QUnit assert plugin to test sync callback. You can easily assert that the callback can (only) be called at the correct moment, and has the correct times.
## Usage
```js
var obj;// Create and return a new callback wrapper for handling assertion.
obj.callback = assert.Callback(callbackFunction_opt);// By default, the callback is not allowed to call unless you call its enable method first.
// You can enable and disable repeatedly.
obj.callback.enable();
obj.callback.disable(msgWhenDisabled_opt);// Expect the callback to be called any times you wish
obj.callback.expect(count, message_opt);// Expect the callback to be called one time, and then reset the time to 0
// so that you can do this repeatedly.
obj.callback.once(message_opt);
```## Examples
### Make Assertion failed if called
```js
obj.onDown = assert.Callback(); // disabled by default
var foo = function() { obj.onDown() };foo(); // Assertion failed! onDown should not be called in foo!
```### Assert callback called
```js
obj.onDown = assert.Callback().enable(); // enabled this time ;)
var foo = function() { obj.onDown() };foo(); // No error this time ;)
obj.onDown.once('onDown should be called once'); // Assertion succeeded!
```### Expect the callback to be called any times you wish
```js
obj.onDown = assert.Callback().enable();
vaf down = function() { obj.onDown() };down();
obj.onDown.expect(1); // Okey!
down();
obj.onDown.expect(2); // Okey!
down();
down();
obj.onDown.expect(4); // Okey!
```### Disable the callback if you think it should not be called anymore.
```js
obj.onDown = assert.Callback().enable();
var foo = function() { obj.onDown() };foo();
obj.onDown.once('onDown should be called')
.disable('onDown should not be called from now on');
foo(); // Assertion failed: onDown should not be called from now on
obj.onDown.enable();
foo();
obj.onDown.once('onDown should be called again')
.disable('onDown should not be called anymore');
foo(); // Assertion failed: onDown should not be called anymore
```### You can register a callback when the wrapper is called.
```js
// callback 1
obj.onUp = assert.Callback().enable();
// callback 2
obj.onDown = assert.Callback( function(val) {
equal(this, obj, 'assert this');
equal(val, 520, 'assert argument');
equal(obj.onUp.calledCount, 1, 'onUp should be called before onDown!');
}).enable(); // dont forget enable!
obj.upAndDown = function() { obj.onUp(); obj.onDown(520) };
obj.upAndDown();
obj.onDown.expect(1, 'onDown should be called once!'); // Okey!
```