Skip to content

Instantly share code, notes, and snippets.

@akirchmyer
Created November 17, 2013 05:06
Show Gist options
  • Select an option

  • Save akirchmyer/7509512 to your computer and use it in GitHub Desktop.

Select an option

Save akirchmyer/7509512 to your computer and use it in GitHub Desktop.
A Pen by Andrew Kirchmyer.
var app = {
model: {}
};
app.model.getAttributeLater = function (attribute) {
var dfd = $.Deferred();
return dfd.promise();
};
app.model.handleAttribute = function (attribute) {
this.getAttributeLater(attribute)
.done(function (result) {
this.doSomething(result);
})
.fail(function (result) {
this.youFail();
});
};
app.model.doSomething = function() {};
app.model.youFail = function() {};
qunit.test('Test model.handleAttributes', function() {
this.stub(app.model, 'getAttributeLater')
.withArgs('good')
.returns($.Deferred().resolve('gold'))
.withArgs('bad')
.returns($.Deferred().reject());
this.stub(app.model, 'doSomething');
this.stub(app.model, 'youFail');
expect(2);
app.model.handleAttribute('good').done(function(result) {
equal(app.model.doSomething.args[0][0], 'gold', 'do something called with expected param when promise resolved.');
});
app.model.handleAttribute('bad').done(function(){
ok(app.model.youFail.calledOnce, 'youFail called when promise is rejected');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment