From 12201849f8f1728f811cff9b07efc692e1df9d84 Mon Sep 17 00:00:00 2001 From: Falko Schmidt Date: Tue, 3 Nov 2015 08:42:48 +0100 Subject: [PATCH 1/3] Handle empty event handler list when asserting with an event handler. --- lib/jasmine-jquery.js | 4 ++++ spec/suites/jasmine-jquery-spec.js | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index 266bf67c..b0a605ca 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -621,6 +621,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. var normalizedEventName = eventName.split('.')[0] , stack = $._data($(actual).get(0), "events")[normalizedEventName] + if (!stack) { + return { pass: false } + } + for (var i = 0; i < stack.length; i++) { if (stack[i].handler == eventHandler) return { pass: true } } diff --git a/spec/suites/jasmine-jquery-spec.js b/spec/suites/jasmine-jquery-spec.js index 4df743f2..a46ffa3f 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') @@ -578,11 +578,11 @@ describe("jQuery matcher", function () { }) it("should pass for string properties with quote characters (double quotes)", function() { - var fontFace = '"Courier New", monospace'; + var fontFace = '"Courier New", monospace'; $("#sandbox").css("font-family", fontFace); expect($("#sandbox")).toHaveCss({'font-family': fontFace}); }) - + it("should pass for string properties with quote characters (single quotes)", function() { var fontFace = "'Courier New', monospace"; $("#sandbox").css("font-family", fontFace); @@ -594,7 +594,7 @@ describe("jQuery matcher", function () { $("#sandbox").css("font-family", fontFace); expect($("#sandbox")).toHaveCss({'font-family': fontFace}); }); - + it("should pass for string properties with no space", function() { var fontFace = '"Courier New",monospace'; $("#sandbox").css("font-family", fontFace); @@ -1471,7 +1471,10 @@ describe("jQuery matcher", function () { }) it('should pass if the namespaced event is not bound at all', function () { + var handler = function (){} + expect($('#clickme')).not.toHandle("click.namespaced") + expect($('#clickme')).not.toHandleWith("click.namespaced", handler) expect($('#clickme').get(0)).not.toHandle("click.namespaced") }) @@ -1495,7 +1498,6 @@ describe("jQuery matcher", function () { it('should not fail when actual is null', function (){ expect(null).not.toHandleWith('click') }) - }) }) From e08e96f2205269b5db4b078c1b78e58692dff8ce Mon Sep 17 00:00:00 2001 From: Falko Schmidt Date: Tue, 3 Nov 2015 10:11:28 +0100 Subject: [PATCH 2/3] Add more sanity checks. --- lib/jasmine-jquery.js | 11 +++++++---- spec/suites/jasmine-jquery-spec.js | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index b0a605ca..9a325cf5 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -619,11 +619,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. compare: function (actual, eventName, eventHandler) { if ( !actual || actual.length === 0 ) return { pass: false }; var normalizedEventName = eventName.split('.')[0] - , stack = $._data($(actual).get(0), "events")[normalizedEventName] + , events = $._data($(actual).get(0), "events") + , stack - if (!stack) { - return { pass: false } - } + if (!events) return { pass: false }; + + stack = $._data($(actual).get(0), "events")[normalizedEventName] + + if (!stack) return { pass: false }; for (var i = 0; i < stack.length; i++) { if (stack[i].handler == eventHandler) return { pass: true } diff --git a/spec/suites/jasmine-jquery-spec.js b/spec/suites/jasmine-jquery-spec.js index a46ffa3f..374c2f49 100644 --- a/spec/suites/jasmine-jquery-spec.js +++ b/spec/suites/jasmine-jquery-spec.js @@ -1470,11 +1470,14 @@ describe("jQuery matcher", function () { expect($('#clickme').get(0)).not.toHandleWith("click.namespaced", aDifferentHandler) }) - it('should pass if the namespaced event is not bound at all', function () { + it('should fail if there is no event bound at all', function () { var handler = function (){} + expect($('#clickme')).not.toHandleWith('click', handler) + expect($('#clickme')).not.toHandleWith('click.namespaced', handler) + }) + it('should pass if the namespaced event is not bound at all', function () { expect($('#clickme')).not.toHandle("click.namespaced") - expect($('#clickme')).not.toHandleWith("click.namespaced", handler) expect($('#clickme').get(0)).not.toHandle("click.namespaced") }) From e216765e758a9266e5e44bf47bc3ea0a1afb3536 Mon Sep 17 00:00:00 2001 From: Falko Schmidt Date: Tue, 3 Nov 2015 10:12:49 +0100 Subject: [PATCH 3/3] DRY up selectors. --- lib/jasmine-jquery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jasmine-jquery.js b/lib/jasmine-jquery.js index 9a325cf5..dbb17500 100644 --- a/lib/jasmine-jquery.js +++ b/lib/jasmine-jquery.js @@ -624,7 +624,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. if (!events) return { pass: false }; - stack = $._data($(actual).get(0), "events")[normalizedEventName] + stack = events[normalizedEventName] if (!stack) return { pass: false };