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

toHaveClass matcher to check if all elements in a set have expected class #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/jasmine-jquery.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
Jasmine-jQuery: a set of jQuery helpers for Jasmine tests.

Version 2.0.7
Version 2.1.0

https://github.com/velesin/jasmine-jquery

Expand Down Expand Up @@ -371,7 +371,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toHaveClass: function () {
return {
compare: function (actual, className) {
return { pass: $(actual).hasClass(className) }
var passed = true
$(actual).each(function (i, el) {
passed &= $(el).hasClass(className)
})
return { pass: !!passed }
}
}
},
Expand Down
17 changes: 15 additions & 2 deletions spec/suites/jasmine-jquery-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ describe("jasmine.Fixtures using real AJAX call", function () {
expect($("#anchor_01").length).toBe(1)
})
})

describe("When the fixture contains a HTML 5 style checked checkbox", function () {
var fixtureUrl = "fixture_with_checkbox_with_checked.html"

it("Then the fixture is loaded successfully", function () {
jasmine.getFixtures().load(fixtureUrl)
expect('#' + jasmine.getFixtures().containerId).toContainElement('#checked-box')
Expand Down Expand Up @@ -466,6 +466,19 @@ describe("jQuery matcher", function () {
doc.removeClass(className)
expect(doc).not.toHaveClass(className)
})

it("should pass negated for multiple elements", function () {
setFixtures($('<div class="some-class" /> <div class="some-class" /> <div class="some-another-class" />'))
var divs = $('#jasmine-fixtures div')
expect(divs).not.toHaveClass('some-class');
})

it("should pass only if all elements have class", function () {
setFixtures($('<div class="some-class" /> <div class="some-class" /> <div class="some-another-class" />'))
var divs = $('#jasmine-fixtures div')
expect(divs.slice(0, 1)).toHaveClass('some-class')
expect(divs.eq(2)).not.toHaveClass('some-class')
})
})

describe("toHaveAttr", function () {
Expand Down