Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

incorporated dpree toHandle matchers

  • Loading branch information...
commit d32a59ad2a61cb0b6d8fbe843b68198743555b79 2 parents 598558c + 535c63a
@velesin authored
View
1  .gitignore
@@ -1 +1,2 @@
lib/external
+.idea
View
6 README.markdown
@@ -47,7 +47,11 @@ jasmine-jquery provides following custom matchers (in alphabetical order):
- e.g. `expect($('<input type="text" value="some text"/>')).toHaveValue('some text')`
- `toBeDisabled()`
- e.g. 'expect('<input type='submit' disabled='disabled'/>').toBeDisabled()'
-
+- `toHandle(eventName)`
+ - e.g. 'expect($form).toHandle("submit")'
+- `toHandleWith(eventName, eventHandler)`
+ - e.g. 'expect($form).toHandle("submit", yourSubmitCallback)'
+
The same as with standard Jasmine matchers, all of above custom matchers may be inverted by using `.not` prefix, e.g.:
expect($('<div>some text</div>')).not.toHaveText(/other/)
View
18 lib/jasmine-jquery.js
@@ -200,6 +200,24 @@ jasmine.JQuery.matchersClass = {};
toBeDisabled: function(selector){
return this.actual.is(':disabled');
+ },
+
+ // tests the existence of a specific event binding
+ toHandle: function(eventName) {
+ var events = this.actual.data("events");
+ return events && events[eventName].length > 0;
+ },
+
+ // tests the existence of a specific event binding + handler
+ toHandleWith: function(eventName, eventHandler) {
+ var stack = this.actual.data("events")[eventName];
+ var i;
+ for (i = 0; i < stack.length; i++) {
+ if (stack[i].handler == eventHandler) {
+ return true;
+ }
+ }
+ return false;
}
};
View
42 spec/suites/jasmine-jquery-spec.js
@@ -572,5 +572,47 @@ describe("jQuery matchers", function() {
expect('click').not.toHaveBeenTriggeredOn($('#clickme'));
});
});
+
+ describe('toHandle', function() {
+ beforeEach(function() {
+ setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'));
+ });
+
+ it('should pass if the event is bound', function() {
+ var handler = function(){ }; // noop
+ $('#clickme').bind("click", handler);
+ expect($('#clickme')).toHandle("click");
+ });
+
+ it('should pass if the event is not bound', function() {
+ expect($('#clickme')).not.toHandle("click");
+ });
+
+ });
+
+ describe('toHandleWith', function() {
+ beforeEach(function() {
+ setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'));
+ });
+
+ it('should pass if the event is bound with the given handler', function() {
+ var handler = function(){ }; // noop
+ $('#clickme').bind("click", handler);
+ expect($('#clickme')).toHandleWith("click", handler);
+ });
+
+ it('should pass if the event is not bound with the given handler', function() {
+ var handler = function(){ };
+ $('#clickme').bind("click", handler);
+
+ var aDifferentHandler = function(){ };
+ expect($('#clickme')).not.toHandleWith("click", aDifferentHandler);
+ });
+
+ it('should pass if the event is not bound at all', function() {
+ expect($('#clickme')).not.toHandle("click");
+ });
+
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.