From e1028ab6e0265e34f2689a5da5d44c41138b7620 Mon Sep 17 00:00:00 2001 From: Yuriy Husnay Date: Tue, 31 Mar 2015 14:17:13 +0300 Subject: [PATCH] fix #237 toHaveClass matcher to check if all elements in a set have expected class --- lib/jasmine-jquery.js | 8 ++++++-- spec/suites/jasmine-jquery-spec.js | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index 44953412..907c10c2 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -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 @@ -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 } } } }, diff --git a/spec/suites/jasmine-jquery-spec.js b/spec/suites/jasmine-jquery-spec.js index bc911097..8dbe4994 100644 --- a/spec/suites/jasmine-jquery-spec.js +++ b/spec/suites/jasmine-jquery-spec.js @@ -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') @@ -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($('
')) + var divs = $('#jasmine-fixtures div') + expect(divs).not.toHaveClass('some-class'); + }) + + it("should pass only if all elements have class", function () { + setFixtures($('
')) + var divs = $('#jasmine-fixtures div') + expect(divs.slice(0, 1)).toHaveClass('some-class') + expect(divs.eq(2)).not.toHaveClass('some-class') + }) }) describe("toHaveAttr", function () {