Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
pulled changes from jamesarosen
Browse files Browse the repository at this point in the history
  • Loading branch information
velesin committed Feb 5, 2011
2 parents 16f5f79 + 208051e commit d840834
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
17 changes: 16 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jasmine-jquery provides following custom matchers (in alphabetical order):
- `toExist()`
- `toHaveAttr(attributeName, attributeValue)`
- attribute value is optional, if omitted it will check only if attribute exists
- `toHaveBeenTriggeredOn(selector)`
- if event has been triggered on `selector` (see "Event Spies", below)
- `toHaveClass(className)`
- e.g. `expect($('<div class="some-class"></div>')).toHaveClass("some-class")`
- `toHaveData(key, value)`
Expand All @@ -38,6 +40,7 @@ jasmine-jquery provides following custom matchers (in alphabetical order):
- `toHaveId(id)`
- e.g. `expect($('<div id="some-id"></div>')).toHaveId("some-id")`
- `toHaveText(string)`
- accepts a String or regular expression
- e.g. `expect($('<div>some text</div>')).toHaveText('some text')`
- `toHaveValue(value)`
- only for tags that have value attribute
Expand All @@ -47,7 +50,7 @@ jasmine-jquery provides following custom matchers (in alphabetical order):

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 text')
expect($('<div>some text</div>')).not.toHaveText(/other/)

## Fixtures

Expand Down Expand Up @@ -131,6 +134,18 @@ Additionally, two clean up methods are provided:

These two methods do not have global short cut functions.

## Event Spies

Spying on jQuery events can be done with `spyOnEvent` and
`assert(eventName).toHaveBeenTriggeredOn(selector)`. First, spy on the event:

spyOnEvent($('#some_element'), 'click');
$('#some_element').click();
expect('click').toHaveBeenTriggeredOn($('#some_element'));

Much thanks to Luiz Fernando Ribeiro for his
[article on Jasmine event spies](http://luizfar.wordpress.com/2011/01/10/testing-events-on-jquery-objects-with-jasmine/).

## Supported browsers and jQuery versions

jasmine-jquery was tested for jQuery 1.4 on IE, FF, Chrome and Opera.
Expand Down
47 changes: 46 additions & 1 deletion lib/jasmine-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var sandbox = function(attributes) {
return jasmine.getFixtures().sandbox(attributes);
};

var spyOnEvent = function(selector, eventName) {
jasmine.JQuery.events.spyOn(selector, eventName);
}

jasmine.getFixtures = function() {
return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures();
};
Expand Down Expand Up @@ -102,6 +106,31 @@ jasmine.JQuery.elementToString = function(element) {

jasmine.JQuery.matchersClass = {};

(function(namespace) {
var data = {
spiedEvents: {},
handlers: []
};

namespace.events = {
spyOn: function(selector, eventName) {
var handler = function(e) {
data.spiedEvents[[selector, eventName]] = e;

This comment has been minimized.

Copy link
@luizfar

luizfar Feb 28, 2011

Hey!
You may want to add a 'return false' in the handler.
When running the tests with HtmlUnit and spying on a form submit, HtmlUnit will get lost thinking the form has been submitted and it will cause the Jasmine reporter to fail to be created, messing up the whole suite.

};
$(selector).bind(eventName, handler);
data.handlers.push(handler);
},

wasTriggered: function(selector, eventName) {
return !!(data.spiedEvents[[selector, eventName]]);
},

cleanUp: function() {
data.spiedEvents = {};
data.handlers = [];
}
}
})(jasmine.JQuery);

(function(){
var jQueryMatchers = {
Expand Down Expand Up @@ -146,7 +175,11 @@ jasmine.JQuery.matchersClass = {};
},

toHaveText: function(text) {
return this.actual.text() == text;
if (text && jQuery.isFunction(text.test)) {
return text.test(this.actual.text());
} else {
return this.actual.text() == text;
}
},

toHaveValue: function(value) {
Expand Down Expand Up @@ -202,8 +235,20 @@ jasmine.JQuery.matchersClass = {};

beforeEach(function() {
this.addMatchers(jasmine.JQuery.matchersClass);
this.addMatchers({
toHaveBeenTriggeredOn: function(selector) {
this.message = function() {
return [
"Expected event " + this.actual + " to have been triggered on" + selector,
"Expected event " + this.actual + " not to have been triggered on" + selector
];
};
return jasmine.JQuery.events.wasTriggered(selector, this.actual);
}
})
});

afterEach(function() {
jasmine.getFixtures().cleanUp();
jasmine.JQuery.events.cleanUp();
});
29 changes: 29 additions & 0 deletions spec/suites/jasmine-jquery-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ describe("jQuery matchers", function() {
it("should pass negated when text does not match", function() {
expect(element).not.toHaveText(wrongText);
});

it('should pass when text matches a regex', function() {
expect(element).toHaveText(/some/);
});

it('should pass negated when text does not match a regex', function() {
expect(element).not.toHaveText(/other/);
});
});

describe("toHaveValue", function() {
Expand Down Expand Up @@ -543,5 +551,26 @@ describe("jQuery matchers", function() {
expect($('#enabled')).not.toBeDisabled();
});
});

describe('toHaveBeenTriggeredOn', function() {
beforeEach(function() {
setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'));
spyOnEvent($('#clickme'), 'click');
});

it('should pass if the event was triggered on the object', function() {
$('#clickme').click();
expect('click').toHaveBeenTriggeredOn($('#clickme'));
});

it('should pass negated if the event was never triggered', function() {
expect('click').not.toHaveBeenTriggeredOn($('#clickme'));
});

it('should pass negated if the event was triggered on another non-descendant object', function() {
$('#otherlink').click();
expect('click').not.toHaveBeenTriggeredOn($('#clickme'));
});
});
});

0 comments on commit d840834

Please sign in to comment.