Browse files

adding a clearer expectation failed message for toHaveData matcher

  • Loading branch information...
1 parent 6cd9f40 commit e50a6a4966f9d2c638a8642984405cae3fb02d04 @fmobus committed Mar 5, 2013
Showing with 38 additions and 3 deletions.
  1. +19 −3 lib/jasmine-jquery.js
  2. +19 −0 spec/suites/jasmine-jquery-spec.js
View
22 lib/jasmine-jquery.js
@@ -1,8 +1,8 @@
/*!
Jasmine-jQuery: a set of jQuery helpers for Jasmine tests.
-
+
Version 1.5.0
-
+
https://github.com/velesin/jasmine-jquery
Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery
@@ -433,7 +433,23 @@ jasmine.JQuery.matchersClass = {}
},
toHaveData: function(key, expectedValue) {
- return hasProperty(this.actual.data(key), expectedValue)
+ var node = this.actual;
+ var actualValue = node.data(key);
+ this.message = function() {
+ if (!expectedValue || !actualValue) {
+ return [
+ "Expected '" + node.get(0).outerHTML + "' to have data '" + key +"'.",
+ "Expected '" + node.get(0).outerHTML + "' not to have data '" + key +"'."
+ ]
+ }
+ else {
+ return [
+ "Expected node to have '" + expectedValue + "' in data-" + key + " attribute, found " + actualValue + "instead.",
+ "Expected node not to have '" + expectedValue + "' in data-" + key + " attribute",
+ ]
+ }
+ }
+ return hasProperty(actualValue, expectedValue)
},
toBe: function(selector) {
View
19 spec/suites/jasmine-jquery-spec.js
@@ -677,6 +677,8 @@ describe("jQuery matchers", function() {
setFixtures(sandbox().data(key, value))
})
+ var failing_spec = it("a mock spec just to test expectation failure messages", function() { });
+
describe("when only key is provided", function() {
it("should pass if element has matching data key", function() {
expect($('#sandbox')).toHaveData(key)
@@ -687,6 +689,14 @@ describe("jQuery matchers", function() {
expect($('#sandbox')).not.toHaveData(wrongKey)
expect($('#sandbox').get(0)).not.toHaveData(wrongKey)
})
+
+ it("should give a message that helps understanding how the expectation was not met", function() {
+ var node = $("#sandbox");
+ var matcher = failing_spec.expect(node);
+ matcher.toHaveData(wrongKey);
+ var result = failing_spec.results().getItems()[0];
+ expect(result.message).toEqual("Expected '"+node.get(0).outerHTML+"' to have data 'wrong key'.");
+ })
})
describe("when both key and value are provided", function() {
@@ -704,6 +714,15 @@ describe("jQuery matchers", function() {
expect($('#sandbox')).not.toHaveData(wrongKey, value)
expect($('#sandbox').get(0)).not.toHaveData(wrongKey, value)
})
+
+ it("should give a message that helps understanding how the expectation was not met", function() {
+ var node = $("#sandbox");
+ var matcher = failing_spec.expect(node.get(0));
+ matcher.toHaveData(key, wrongValue);
+ var result = failing_spec.results().getItems()[0];
+
+ expect(result.message).toEqual("Expected '"+node.get(0).outerHTML+"' to have data 'wrong key'.");
+ })
})
})

0 comments on commit e50a6a4

Please sign in to comment.