Skip to content

Instantly share code, notes, and snippets.

@eethann
Created March 6, 2012 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eethann/1983867 to your computer and use it in GitHub Desktop.
Save eethann/1983867 to your computer and use it in GitHub Desktop.
Drupal QUnit Async Testing Example
// 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