Created
June 1, 2011 03:19
-
-
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
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
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