Skip to content

Instantly share code, notes, and snippets.

@jicksta
Created June 1, 2011 03:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jicksta/1001736 to your computer and use it in GitHub Desktop.
Save jicksta/1001736 to your computer and use it in GitHub Desktop.
Jasmine shared behavior for asserting that a certain event on an element exposed as a Backbone view property properly executes the expected view instance method
function assertViewAction(viewClass, eventCallbackName, elementAccessorName, eventName, viewReference) {
it("should run the " + eventCallbackName + " action when the " + elementAccessorName + " gets a " + eventName + " event (assertViewAction)", function() {
var isPrototypal = eventCallbackName in viewClass.prototype;
if (isPrototypal) {
spyOn(viewClass.prototype, eventCallbackName);
}
var view = viewReference();
if (!isPrototypal) {
spyOn(view, eventCallbackName);
}
view.delegateEvents();
var eventTarget = view[elementAccessorName];
expect(eventTarget).toBeDefined();
if(eventTarget.jQuery) {
expect(eventTarget.length).toBeGreaterThan(0);
}
eventTarget.trigger(eventName);
expect(view[eventCallbackName]).toHaveBeenCalled();
});
}
// Example use:
assertViewAction(MyView, "openPopup", "$popupOpenerButton", "click", function() {
var model = new MyModel({name: "Jay"});
var html = $(readFixture("some_html_fixture"));
return new MyView({el: html, model: model});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment