Skip to content

Commit b9f68a8

Browse files
committed
Update the async assertions. Removed outdated docs and tests (to be rewritten).
1 parent 6b8fa7e commit b9f68a8

File tree

3 files changed

+105
-111
lines changed

3 files changed

+105
-111
lines changed

jquery.expect.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
}
280280

281281
AssertionError.prototype = new Error();
282+
AssertionError.prototype.constructor = AssertionError;
282283

283284
global.$expect = $expect;
284285
$expect.Assertion = Assertion;
@@ -299,7 +300,7 @@
299300
if ($.isFunction(msg)) {
300301
error = msg = msg.call(this, !ok);
301302
}
302-
303+
303304
msg = this.flags.not ? error : msg;
304305

305306
if (!ok) {
@@ -392,7 +393,7 @@
392393
return $.inArray(el, b) > -1 ? true : null;
393394
}).length === a.length;
394395
};
395-
396+
396397
// Arrays are equal if every element in this.obj
397398
// appears in $el, and vice-versa.
398399
var eq = injSurj(this.obj, $el) && injSurj($el, this.obj);
@@ -825,13 +826,23 @@
825826
};
826827

827828
/**
828-
* Async testing signals. Meant to be overridden.
829+
* Async testing signals.
829830
*
830-
* @param {String} evt
831-
* @param {Deferred} dfd
831+
* @param {String} event
832+
* @param {Deferred} deferred
832833
*/
833-
Assertion.asyncWait = function (/*evt, dfd*/) {};
834-
Assertion.asyncDone = function (/*evt, dfd*/) {};
834+
function DeferredSignal (deferred, event, args) {
835+
this.deferred = deferred;
836+
this.event = event;
837+
this.args = args;
838+
}
839+
840+
DeferredSignal.prototype = new Error();
841+
$expect.DeferredSignal = DeferredSignal;
842+
843+
function signal (deferred, event /* args... */) {
844+
throw new DeferredSignal(deferred, event, [].slice.call(arguments, 2));
845+
}
835846

836847
/**
837848
* Attaches a *once* callback to an event.
@@ -856,13 +867,11 @@
856867
} catch (e) {
857868
dfd.rejectWith(obj, [e, ret]);
858869
}
859-
860-
Assertion.asyncDone(evt, dfd);
861870
return ret;
862871
};
863872

864873
this.obj.on(evt, callback);
865-
Assertion.asyncWait(evt, dfd);
874+
return signal(dfd, evt, String(i(this.obj)));
866875
};
867876

868877
/**
@@ -880,11 +889,37 @@
880889
} catch (e) {
881890
dfd.reject(e);
882891
}
883-
Assertion.asyncDone(null, dfd);
884892
}, delay);
885-
Assertion.asyncWait(null, dfd);
893+
894+
return signal(dfd, 'wait', delay);
886895
};
887896

897+
/**
898+
* Wait for a callback to call our done callback to reject or resolve a
899+
* deferred with a string that would be made an assertion error.
900+
* @api public
901+
* @param {Function} cb
902+
*/
903+
$expect.async = function (cb) {
904+
var dfd = $.Deferred();
905+
var done = function (res) {
906+
if (res) {
907+
var err = new AssertionError(String(res));
908+
dfd.reject(err);
909+
} else {
910+
dfd.resolve();
911+
}
912+
};
913+
setTimeout(function () {
914+
try {
915+
cb(done);
916+
} catch (e) {
917+
dfd.reject(e);
918+
}
919+
}, 0);
920+
return signal(dfd, 'async');
921+
};
922+
888923
/**
889924
* Shorthand methods for event binding. Just like jQuery!
890925
*
@@ -938,4 +973,4 @@
938973
};
939974
});
940975

941-
})(this);
976+
})(this);

readme.md

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -211,47 +211,6 @@ $expect('div.container').to.have.siblings('.pane').that.has.css('float', 'left')
211211
end().to.be('.loading');
212212
```
213213

214-
### Testing events
215-
$expect defines two static methods on its exposed `Assertion` class that works as asynchronous signals
216-
for testing events:
217-
218-
* `asyncWait`: is called when we start waiting for an async event to happen. Called with two arguments
219-
a) event type string b) a deferred object that is resolved when the event is fired.
220-
* `asyncDone`: is called when the event is fired and passed a) event type string b) a deferred object
221-
that is either rejected with the an error if the async callback threw one or resolved
222-
with the return value from it.
223-
224-
225-
```javascript
226-
it('should test for setting the navigation to position fixed after scrolling the page'
227-
, function (next) {
228-
// Assign the fail and pass handlers of the deferred object to be the mocha next function.
229-
// Incase of an error it would be passed and caught by mocha.
230-
$expect.Assertion.asyncWait = function (evt, dfd) {
231-
dfd.then(next, next);
232-
};
233-
234-
// Called after the event has fired.
235-
$expect.Assertion.asyncDone = function (evt, dfd)
236-
// Here we are using the expect.js library to check the state of the deferred object.
237-
// If everything went as expected it should be resolved.
238-
expect(dfd.state()).to.be('resolved');
239-
};
240-
241-
// The actual call to the async event. Expect the window to scroll.
242-
// And when that happens expect the nav-bar to become position fixed.
243-
$expect(window).to.scroll(function () {
244-
$expect('.nav-bar').to.have.css('position', 'fixed');
245-
});
246-
247-
// Now emulate the actual scrolling.
248-
setTimeout(function () {
249-
$(window).scrollTop(500);
250-
}, 100);
251-
}
252-
);
253-
```
254-
255214
## License
256215
MIT License.
257216
Copyright (c) 2012 Amjad Masad <amjad@codecademy.com> Ryzac, Inc.

test/test.js

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -315,36 +315,36 @@ describe('$expect', function () {
315315
}, 'bar the foo');
316316
});
317317

318-
it('should test event tests', function (n) {
319-
var s = 0;
320-
$(window).on('scroll.once', function () {
321-
$(window).off('scroll.once');
322-
$('body').append('<div class="after-scroll" />');
323-
$(window).scrollTop(s);
324-
});
325-
326-
$expect.Assertion.asyncWait = function (evt, dfd) {
327-
dfd.then(n, n);
328-
};
329-
330-
$expect.Assertion.asyncDone = function (evt, dfd) {
331-
expect(dfd.state()).to.be('resolved');
332-
};
333-
334-
$expect(window).to.scroll(function () {
335-
$expect('body').to.have('.after-scroll');
336-
337-
err(function () {
338-
// Old api. contain used to alias find.
339-
$expect('body').to.contain('.after-scroll');
340-
}, 'expected body to contain ".after-scroll"');
341-
});
342-
343-
setTimeout(function () {
344-
s = $(window).scrollTop();
345-
$(window).scrollTop(500);
346-
});
347-
});
318+
// it('should test event tests', function (n) {
319+
// var s = 0;
320+
// $(window).on('scroll.once', function () {
321+
// $(window).off('scroll.once');
322+
// $('body').append('<div class="after-scroll" />');
323+
// $(window).scrollTop(s);
324+
// });
325+
326+
// $expect.Assertion.asyncWait = function (evt, dfd) {
327+
// dfd.then(n, n);
328+
// };
329+
330+
// $expect.Assertion.asyncDone = function (evt, dfd) {
331+
// expect(dfd.state()).to.be('resolved');
332+
// };
333+
334+
// $expect(window).to.scroll(function () {
335+
// $expect('body').to.have('.after-scroll');
336+
337+
// err(function () {
338+
// // Old api. contain used to alias find.
339+
// $expect('body').to.contain('.after-scroll');
340+
// }, 'expected body to contain ".after-scroll"');
341+
// });
342+
343+
// setTimeout(function () {
344+
// s = $(window).scrollTop();
345+
// $(window).scrollTop(500);
346+
// });
347+
// });
348348

349349
it('should test for a', function () {
350350
$expect('div').to.be.a('div');
@@ -413,33 +413,33 @@ describe('$expect', function () {
413413
}, 'foo bar error');
414414
});
415415

416-
it('should test wait', function (next) {
417-
$expect.Assertion.asyncWait = function (evt, dfd) {
418-
dfd.then(next, next);
419-
};
420-
421-
$expect.Assertion.asyncDone = function (evt, dfd) {
422-
expect(dfd.state()).to.be('resolved');
423-
};
424-
425-
$expect.wait(200, function () {
426-
// noop
427-
});
428-
});
429-
430-
it('should test wait with a callback throwing an error', function (next) {
431-
$expect.Assertion.asyncWait = function (evt, dfd) {
432-
dfd.then(function () {}, function () {next();});
433-
};
434-
435-
$expect.Assertion.asyncDone = function (evt, dfd) {
436-
expect(dfd.state()).to.be('rejected');
437-
};
438-
439-
$expect.wait(200, function () {
440-
foobar;
441-
});
442-
});
416+
// it('should test wait', function (next) {
417+
// $expect.Assertion.asyncWait = function (evt, dfd) {
418+
// dfd.then(next, next);
419+
// };
420+
421+
// $expect.Assertion.asyncDone = function (evt, dfd) {
422+
// expect(dfd.state()).to.be('resolved');
423+
// };
424+
425+
// $expect.wait(200, function () {
426+
// // noop
427+
// });
428+
// });
429+
430+
// it('should test wait with a callback throwing an error', function (next) {
431+
// $expect.Assertion.asyncWait = function (evt, dfd) {
432+
// dfd.then(function () {}, function () {next();});
433+
// };
434+
435+
// $expect.Assertion.asyncDone = function (evt, dfd) {
436+
// expect(dfd.state()).to.be('rejected');
437+
// };
438+
439+
// $expect.wait(200, function () {
440+
// foobar;
441+
// });
442+
// });
443443

444444
it('should correctly test shorthand border-radius', function () {
445445
$expect('.radius').to.have.css('border-radius', '10px');

0 commit comments

Comments
 (0)