// This is Wojciech's jasmine-jquery library without the fixtures. See https://github.com/velesin/jasmine-jquery // Copyright (c) 2010 Wojciech Zawistowski // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. jasmine.JQuery = function() {}; jasmine.JQuery.browserTagCaseIndependentHtml = function(html) { return $('
').append(html).html(); }; jasmine.JQuery.elementToString = function(element) { return $('').append(element.clone()).html(); }; jasmine.JQuery.matchersClass = {}; (function(){ var jQueryMatchers = { toHaveClass: function(className) { return this.actual.hasClass(className); }, toBeVisible: function() { return this.actual.is(':visible'); }, toBeHidden: function() { return this.actual.is(':hidden'); }, toBeSelected: function() { return this.actual.is(':selected'); }, toBeChecked: function() { return this.actual.is(':checked'); }, toBeEmpty: function() { return this.actual.is(':empty'); }, toExist: function() { return this.actual.size() > 0; }, toHaveAttr: function(attributeName, expectedAttributeValue) { return hasProperty(this.actual.attr(attributeName), expectedAttributeValue); }, toHaveProp: function(attributeName, expectedAttributeValue) { return hasProperty(this.actual.prop(attributeName), expectedAttributeValue); }, toHaveId: function(id) { return this.actual.attr('id') == id; }, toHaveHtml: function(html) { return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html); }, toHaveText: function(text) { return this.actual.text() == text; }, toHaveValue: function(value) { return this.actual.val() == value; }, toHaveData: function(key, expectedValue) { return hasProperty(this.actual.data(key), expectedValue); }, toBe: function(selector) { return this.actual.is(selector); }, toContain: function(selector) { return this.actual.find(selector).size() > 0; } }; var hasProperty = function(actualValue, expectedValue) { if (expectedValue === undefined) { return actualValue !== undefined; } return actualValue == expectedValue; }; var bindMatcher = function(methodName) { var builtInMatcher = jasmine.Matchers.prototype[methodName]; jasmine.JQuery.matchersClass[methodName] = function() { if (this.actual instanceof jQuery) { var result = jQueryMatchers[methodName].apply(this, arguments); this.actual = jasmine.JQuery.elementToString(this.actual); return result; } if (builtInMatcher) { return builtInMatcher.apply(this, arguments); } else { throw ("There is no " + methodName + " method on this object. Did you forget a $ jQuery call in your expect?"); // Added to help debugging. } return false; }; }; for(var methodName in jQueryMatchers) { bindMatcher(methodName); } })(); beforeEach(function() { this.addMatchers(jasmine.JQuery.matchersClass); });