Created
March 6, 2012 05:41
-
-
Save eethann/1983867 to your computer and use it in GitHub Desktop.
Drupal QUnit Async Testing Example
This file contains 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
// Using a jQuery queue is a bit more complicated, but worth it. | |
(function($) { | |
Drupal.tests.myBackboneApp = { | |
getInfo: function() { | |
return { | |
name: 'Backbone App Example', | |
description: 'Example of a Backbone test in Drupal QUnit', | |
group: 'BackboneApp' | |
}; | |
}, | |
setup: function() { | |
this.testNodeNew = new Drupal.Backbone.NodeModel({ | |
'title': 'Backbone Test Node', | |
'type': 'page' | |
}); | |
this.testNodeLoad = new Drupal.Backbone.NodeModel(); | |
this.asyncQueue = $({}); | |
}, | |
test: function() { | |
expect(3); | |
stop(); | |
// We need to use self instead of this in our callbacks, since scope changes. | |
var self=this; | |
self.asyncQueue.queue('asyncQueue', function() { | |
ok(self.testNodeNew.isNew(), Drupal.t('New model isNew() is true before saving.')); | |
self.testNodeNew.save({}, { | |
success: function() { | |
self.asyncQueue.dequeue('asyncQueue'); | |
} | |
}); | |
}); | |
self.asyncQueue.queue('asyncQueue', function() { | |
ok(!self.testNodeNew.isNew(), Drupal.t('New model isNew() is false after saving.')); | |
self.testNodeLoad.set('nid', self.testNodeNew.get('nid')); | |
self.testNodeLoad.fetch({ | |
success: function() { | |
self.asyncQueue.dequeue('asyncQueue'); | |
} | |
}); | |
}); | |
self.asyncQueue.queue('asyncQueue', function() { | |
equals('Backbone Test Node', self.testNodeLoad.get('title'), Drupal.t('Title saved and retrieved correctly.')); | |
start(); | |
}); | |
self.asyncQueue.dequeue('asyncQueue'); | |
}, | |
teardown: function() { | |
this.testNodeNew.destroy(); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment