| 1 | /*! |
|---|
| 2 | Jasmine-jQuery: a set of jQuery helpers for Jasmine tests. |
|---|
| 3 | |
|---|
| 4 | Version 1.5.8 |
|---|
| 5 | |
|---|
| 6 | https://github.com/velesin/jasmine-jquery |
|---|
| 7 | |
|---|
| 8 | Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery |
|---|
| 9 | |
|---|
| 10 | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 11 | a copy of this software and associated documentation files (the |
|---|
| 12 | "Software"), to deal in the Software without restriction, including |
|---|
| 13 | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 14 | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 15 | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 16 | the following conditions: |
|---|
| 17 | |
|---|
| 18 | The above copyright notice and this permission notice shall be |
|---|
| 19 | included in all copies or substantial portions of the Software. |
|---|
| 20 | |
|---|
| 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 22 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 24 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|---|
| 25 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 26 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|---|
| 27 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 28 | */ |
|---|
| 29 | var readFixtures = function() { |
|---|
| 30 | return jasmine.getFixtures().proxyCallTo_('read', arguments) |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | var preloadFixtures = function() { |
|---|
| 34 | jasmine.getFixtures().proxyCallTo_('preload', arguments) |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | var loadFixtures = function() { |
|---|
| 38 | jasmine.getFixtures().proxyCallTo_('load', arguments) |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | var appendLoadFixtures = function() { |
|---|
| 42 | jasmine.getFixtures().proxyCallTo_('appendLoad', arguments) |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | var setFixtures = function(html) { |
|---|
| 46 | return jasmine.getFixtures().proxyCallTo_('set', arguments) |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | var appendSetFixtures = function() { |
|---|
| 50 | jasmine.getFixtures().proxyCallTo_('appendSet', arguments) |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | var sandbox = function(attributes) { |
|---|
| 54 | return jasmine.getFixtures().sandbox(attributes) |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | var spyOnEvent = function(selector, eventName) { |
|---|
| 58 | return jasmine.JQuery.events.spyOn(selector, eventName) |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | var preloadStyleFixtures = function() { |
|---|
| 62 | jasmine.getStyleFixtures().proxyCallTo_('preload', arguments) |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | var loadStyleFixtures = function() { |
|---|
| 66 | jasmine.getStyleFixtures().proxyCallTo_('load', arguments) |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | var appendLoadStyleFixtures = function() { |
|---|
| 70 | jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments) |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | var setStyleFixtures = function(html) { |
|---|
| 74 | jasmine.getStyleFixtures().proxyCallTo_('set', arguments) |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | var appendSetStyleFixtures = function(html) { |
|---|
| 78 | jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments) |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | var loadJSONFixtures = function() { |
|---|
| 82 | return jasmine.getJSONFixtures().proxyCallTo_('load', arguments) |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | var getJSONFixture = function(url) { |
|---|
| 86 | return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url] |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | jasmine.spiedEventsKey = function (selector, eventName) { |
|---|
| 90 | return [$(selector).selector, eventName].toString() |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | jasmine.getFixtures = function() { |
|---|
| 94 | return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures() |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | jasmine.getStyleFixtures = function() { |
|---|
| 98 | return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures() |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | jasmine.Fixtures = function() { |
|---|
| 102 | this.containerId = 'jasmine-fixtures' |
|---|
| 103 | this.fixturesCache_ = {} |
|---|
| 104 | this.fixturesPath = 'spec/javascripts/fixtures' |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | jasmine.Fixtures.prototype.set = function(html) { |
|---|
| 108 | this.cleanUp() |
|---|
| 109 | return this.createContainer_(html) |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | jasmine.Fixtures.prototype.appendSet= function(html) { |
|---|
| 113 | this.addToContainer_(html) |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | jasmine.Fixtures.prototype.preload = function() { |
|---|
| 117 | this.read.apply(this, arguments) |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | jasmine.Fixtures.prototype.load = function() { |
|---|
| 121 | this.cleanUp() |
|---|
| 122 | this.createContainer_(this.read.apply(this, arguments)) |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | jasmine.Fixtures.prototype.appendLoad = function() { |
|---|
| 126 | this.addToContainer_(this.read.apply(this, arguments)) |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | jasmine.Fixtures.prototype.read = function() { |
|---|
| 130 | var htmlChunks = [] |
|---|
| 131 | |
|---|
| 132 | var fixtureUrls = arguments |
|---|
| 133 | for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { |
|---|
| 134 | htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])) |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return htmlChunks.join('') |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | jasmine.Fixtures.prototype.clearCache = function() { |
|---|
| 141 | this.fixturesCache_ = {} |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | jasmine.Fixtures.prototype.cleanUp = function() { |
|---|
| 145 | $('#' + this.containerId).remove() |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | jasmine.Fixtures.prototype.sandbox = function(attributes) { |
|---|
| 149 | var attributesToSet = attributes || {} |
|---|
| 150 | return $('<div id="sandbox" />').attr(attributesToSet) |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | jasmine.Fixtures.prototype.createContainer_ = function(html) { |
|---|
| 154 | var container = $('<div>') |
|---|
| 155 | .attr('id', this.containerId) |
|---|
| 156 | .html(html); |
|---|
| 157 | $(document.body).append(container) |
|---|
| 158 | return container |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | jasmine.Fixtures.prototype.addToContainer_ = function(html){ |
|---|
| 162 | var container = $(document.body).find('#'+this.containerId).append(html) |
|---|
| 163 | if(!container.length){ |
|---|
| 164 | this.createContainer_(html) |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { |
|---|
| 169 | if (typeof this.fixturesCache_[url] === 'undefined') { |
|---|
| 170 | this.loadFixtureIntoCache_(url) |
|---|
| 171 | } |
|---|
| 172 | return this.fixturesCache_[url] |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { |
|---|
| 176 | var url = this.makeFixtureUrl_(relativeUrl) |
|---|
| 177 | var request = $.ajax({ |
|---|
| 178 | type: "GET", |
|---|
| 179 | url: url + "?" + new Date().getTime(), |
|---|
| 180 | async: false |
|---|
| 181 | }) |
|---|
| 182 | this.fixturesCache_[relativeUrl] = request.responseText |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){ |
|---|
| 186 | return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { |
|---|
| 190 | return this[methodName].apply(this, passedArguments) |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | jasmine.StyleFixtures = function() { |
|---|
| 195 | this.fixturesCache_ = {} |
|---|
| 196 | this.fixturesNodes_ = [] |
|---|
| 197 | this.fixturesPath = 'spec/javascripts/fixtures' |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | jasmine.StyleFixtures.prototype.set = function(css) { |
|---|
| 201 | this.cleanUp() |
|---|
| 202 | this.createStyle_(css) |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | jasmine.StyleFixtures.prototype.appendSet = function(css) { |
|---|
| 206 | this.createStyle_(css) |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | jasmine.StyleFixtures.prototype.preload = function() { |
|---|
| 210 | this.read_.apply(this, arguments) |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | jasmine.StyleFixtures.prototype.load = function() { |
|---|
| 214 | this.cleanUp() |
|---|
| 215 | this.createStyle_(this.read_.apply(this, arguments)) |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | jasmine.StyleFixtures.prototype.appendLoad = function() { |
|---|
| 219 | this.createStyle_(this.read_.apply(this, arguments)) |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | jasmine.StyleFixtures.prototype.cleanUp = function() { |
|---|
| 223 | while(this.fixturesNodes_.length) { |
|---|
| 224 | this.fixturesNodes_.pop().remove() |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | jasmine.StyleFixtures.prototype.createStyle_ = function(html) { |
|---|
| 229 | var styleText = $('<div></div>').html(html).text(), |
|---|
| 230 | style = $('<style>' + styleText + '</style>') |
|---|
| 231 | |
|---|
| 232 | this.fixturesNodes_.push(style) |
|---|
| 233 | |
|---|
| 234 | $('head').append(style) |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache |
|---|
| 238 | |
|---|
| 239 | jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read |
|---|
| 240 | |
|---|
| 241 | jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_ |
|---|
| 242 | |
|---|
| 243 | jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_ |
|---|
| 244 | |
|---|
| 245 | jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_ |
|---|
| 246 | |
|---|
| 247 | jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_ |
|---|
| 248 | |
|---|
| 249 | jasmine.getJSONFixtures = function() { |
|---|
| 250 | return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures() |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | jasmine.JSONFixtures = function() { |
|---|
| 254 | this.fixturesCache_ = {} |
|---|
| 255 | this.fixturesPath = 'spec/javascripts/fixtures/json' |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | jasmine.JSONFixtures.prototype.load = function() { |
|---|
| 259 | this.read.apply(this, arguments) |
|---|
| 260 | return this.fixturesCache_ |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | jasmine.JSONFixtures.prototype.read = function() { |
|---|
| 264 | var fixtureUrls = arguments |
|---|
| 265 | for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { |
|---|
| 266 | this.getFixtureData_(fixtureUrls[urlIndex]) |
|---|
| 267 | } |
|---|
| 268 | return this.fixturesCache_ |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | jasmine.JSONFixtures.prototype.clearCache = function() { |
|---|
| 272 | this.fixturesCache_ = {} |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | jasmine.JSONFixtures.prototype.getFixtureData_ = function(url) { |
|---|
| 276 | this.loadFixtureIntoCache_(url) |
|---|
| 277 | return this.fixturesCache_[url] |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { |
|---|
| 281 | var self = this |
|---|
| 282 | var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl |
|---|
| 283 | $.ajax({ |
|---|
| 284 | async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded |
|---|
| 285 | cache: false, |
|---|
| 286 | dataType: 'json', |
|---|
| 287 | url: url, |
|---|
| 288 | success: function(data) { |
|---|
| 289 | self.fixturesCache_[relativeUrl] = data |
|---|
| 290 | }, |
|---|
| 291 | error: function(jqXHR, status, errorThrown) { |
|---|
| 292 | throw Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') |
|---|
| 293 | } |
|---|
| 294 | }) |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | jasmine.JSONFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { |
|---|
| 298 | return this[methodName].apply(this, passedArguments) |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | jasmine.JQuery = function() {} |
|---|
| 302 | |
|---|
| 303 | jasmine.JQuery.browserTagCaseIndependentHtml = function(html) { |
|---|
| 304 | return $('<div/>').append(html).html() |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | jasmine.JQuery.elementToString = function(element) { |
|---|
| 308 | var domEl = $(element).get(0) |
|---|
| 309 | if (domEl == undefined || domEl.cloneNode) |
|---|
| 310 | return $('<div />').append($(element).clone()).html() |
|---|
| 311 | else |
|---|
| 312 | return element.toString() |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | jasmine.JQuery.matchersClass = {} |
|---|
| 316 | |
|---|
| 317 | !function(namespace) { |
|---|
| 318 | var data = { |
|---|
| 319 | spiedEvents: {}, |
|---|
| 320 | handlers: [] |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | namespace.events = { |
|---|
| 324 | spyOn: function(selector, eventName) { |
|---|
| 325 | var handler = function(e) { |
|---|
| 326 | data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = jasmine.util.argsToArray(arguments) |
|---|
| 327 | } |
|---|
| 328 | $(selector).on(eventName, handler) |
|---|
| 329 | data.handlers.push(handler) |
|---|
| 330 | return { |
|---|
| 331 | selector: selector, |
|---|
| 332 | eventName: eventName, |
|---|
| 333 | handler: handler, |
|---|
| 334 | reset: function(){ |
|---|
| 335 | delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | }, |
|---|
| 339 | |
|---|
| 340 | args: function(selector, eventName) { |
|---|
| 341 | var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]; |
|---|
| 342 | |
|---|
| 343 | if (!actualArgs) { |
|---|
| 344 | throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent."; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | return actualArgs; |
|---|
| 348 | }, |
|---|
| 349 | |
|---|
| 350 | wasTriggered: function(selector, eventName) { |
|---|
| 351 | return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]) |
|---|
| 352 | }, |
|---|
| 353 | |
|---|
| 354 | wasTriggeredWith: function(selector, eventName, expectedArgs, env) { |
|---|
| 355 | var actualArgs = jasmine.JQuery.events.args(selector, eventName).slice(1); |
|---|
| 356 | if (Object.prototype.toString.call(expectedArgs) !== '[object Array]') { |
|---|
| 357 | actualArgs = actualArgs[0]; |
|---|
| 358 | } |
|---|
| 359 | return env.equals_(expectedArgs, actualArgs); |
|---|
| 360 | }, |
|---|
| 361 | |
|---|
| 362 | wasPrevented: function(selector, eventName) { |
|---|
| 363 | var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)], |
|---|
| 364 | e = args ? args[0] : undefined; |
|---|
| 365 | return e && e.isDefaultPrevented() |
|---|
| 366 | }, |
|---|
| 367 | |
|---|
| 368 | wasStopped: function(selector, eventName) { |
|---|
| 369 | var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)], |
|---|
| 370 | e = args ? args[0] : undefined; |
|---|
| 371 | return e && e.isPropagationStopped() |
|---|
| 372 | }, |
|---|
| 373 | |
|---|
| 374 | cleanUp: function() { |
|---|
| 375 | data.spiedEvents = {} |
|---|
| 376 | data.handlers = [] |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | }(jasmine.JQuery) |
|---|
| 380 | |
|---|
| 381 | !function(){ |
|---|
| 382 | var jQueryMatchers = { |
|---|
| 383 | toHaveClass: function(className) { |
|---|
| 384 | return this.actual.hasClass(className) |
|---|
| 385 | }, |
|---|
| 386 | |
|---|
| 387 | toHaveCss: function(css){ |
|---|
| 388 | for (var prop in css){ |
|---|
| 389 | if (this.actual.css(prop) !== css[prop]) return false |
|---|
| 390 | } |
|---|
| 391 | return true |
|---|
| 392 | }, |
|---|
| 393 | |
|---|
| 394 | toBeVisible: function() { |
|---|
| 395 | return this.actual.is(':visible') |
|---|
| 396 | }, |
|---|
| 397 | |
|---|
| 398 | toBeHidden: function() { |
|---|
| 399 | return this.actual.is(':hidden') |
|---|
| 400 | }, |
|---|
| 401 | |
|---|
| 402 | toBeSelected: function() { |
|---|
| 403 | return this.actual.is(':selected') |
|---|
| 404 | }, |
|---|
| 405 | |
|---|
| 406 | toBeChecked: function() { |
|---|
| 407 | return this.actual.is(':checked') |
|---|
| 408 | }, |
|---|
| 409 | |
|---|
| 410 | toBeEmpty: function() { |
|---|
| 411 | return this.actual.is(':empty') |
|---|
| 412 | }, |
|---|
| 413 | |
|---|
| 414 | toExist: function() { |
|---|
| 415 | return $(document).find(this.actual).length |
|---|
| 416 | }, |
|---|
| 417 | |
|---|
| 418 | toHaveLength: function(length) { |
|---|
| 419 | return this.actual.length === length |
|---|
| 420 | }, |
|---|
| 421 | |
|---|
| 422 | toHaveAttr: function(attributeName, expectedAttributeValue) { |
|---|
| 423 | return hasProperty(this.actual.attr(attributeName), expectedAttributeValue) |
|---|
| 424 | }, |
|---|
| 425 | |
|---|
| 426 | toHaveProp: function(propertyName, expectedPropertyValue) { |
|---|
| 427 | return hasProperty(this.actual.prop(propertyName), expectedPropertyValue) |
|---|
| 428 | }, |
|---|
| 429 | |
|---|
| 430 | toHaveId: function(id) { |
|---|
| 431 | return this.actual.attr('id') == id |
|---|
| 432 | }, |
|---|
| 433 | |
|---|
| 434 | toHaveHtml: function(html) { |
|---|
| 435 | return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html) |
|---|
| 436 | }, |
|---|
| 437 | |
|---|
| 438 | toContainHtml: function(html){ |
|---|
| 439 | var actualHtml = this.actual.html() |
|---|
| 440 | var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html) |
|---|
| 441 | return (actualHtml.indexOf(expectedHtml) >= 0) |
|---|
| 442 | }, |
|---|
| 443 | |
|---|
| 444 | toHaveText: function(text) { |
|---|
| 445 | var trimmedText = $.trim(this.actual.text()) |
|---|
| 446 | if (text && $.isFunction(text.test)) { |
|---|
| 447 | return text.test(trimmedText) |
|---|
| 448 | } else { |
|---|
| 449 | return trimmedText == text |
|---|
| 450 | } |
|---|
| 451 | }, |
|---|
| 452 | |
|---|
| 453 | toContainText: function(text) { |
|---|
| 454 | var trimmedText = $.trim(this.actual.text()) |
|---|
| 455 | if (text && $.isFunction(text.test)) { |
|---|
| 456 | return text.test(trimmedText) |
|---|
| 457 | } else { |
|---|
| 458 | return trimmedText.indexOf(text) != -1; |
|---|
| 459 | } |
|---|
| 460 | }, |
|---|
| 461 | |
|---|
| 462 | toHaveValue: function(value) { |
|---|
| 463 | return this.actual.val() === value |
|---|
| 464 | }, |
|---|
| 465 | |
|---|
| 466 | toHaveData: function(key, expectedValue) { |
|---|
| 467 | return hasProperty(this.actual.data(key), expectedValue) |
|---|
| 468 | }, |
|---|
| 469 | |
|---|
| 470 | toBe: function(selector) { |
|---|
| 471 | return this.actual.is(selector) |
|---|
| 472 | }, |
|---|
| 473 | |
|---|
| 474 | toContain: function(selector) { |
|---|
| 475 | return this.actual.find(selector).length |
|---|
| 476 | }, |
|---|
| 477 | |
|---|
| 478 | toBeMatchedBy: function(selector) { |
|---|
| 479 | return this.actual.filter(selector).length |
|---|
| 480 | }, |
|---|
| 481 | |
|---|
| 482 | toBeDisabled: function(selector){ |
|---|
| 483 | return this.actual.is(':disabled') |
|---|
| 484 | }, |
|---|
| 485 | |
|---|
| 486 | toBeFocused: function(selector) { |
|---|
| 487 | return this.actual[0] === this.actual[0].ownerDocument.activeElement |
|---|
| 488 | }, |
|---|
| 489 | |
|---|
| 490 | toHandle: function(event) { |
|---|
| 491 | |
|---|
| 492 | var events = $._data(this.actual.get(0), "events") |
|---|
| 493 | |
|---|
| 494 | if(!events || !event || typeof event !== "string") { |
|---|
| 495 | return false |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | var namespaces = event.split(".") |
|---|
| 499 | var eventType = namespaces.shift() |
|---|
| 500 | var sortedNamespaces = namespaces.slice(0).sort() |
|---|
| 501 | var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") |
|---|
| 502 | |
|---|
| 503 | if(events[eventType] && namespaces.length) { |
|---|
| 504 | for(var i = 0; i < events[eventType].length; i++) { |
|---|
| 505 | var namespace = events[eventType][i].namespace |
|---|
| 506 | if(namespaceRegExp.test(namespace)) { |
|---|
| 507 | return true |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | } else { |
|---|
| 511 | return events[eventType] && events[eventType].length > 0 |
|---|
| 512 | } |
|---|
| 513 | }, |
|---|
| 514 | |
|---|
| 515 | // tests the existence of a specific event binding + handler |
|---|
| 516 | toHandleWith: function(eventName, eventHandler) { |
|---|
| 517 | var normalizedEventName = eventName.split('.')[0]; |
|---|
| 518 | var stack = $._data(this.actual.get(0), "events")[normalizedEventName] |
|---|
| 519 | for (var i = 0; i < stack.length; i++) { |
|---|
| 520 | if (stack[i].handler == eventHandler) return true |
|---|
| 521 | } |
|---|
| 522 | return false |
|---|
| 523 | } |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | var hasProperty = function(actualValue, expectedValue) { |
|---|
| 527 | if (expectedValue === undefined) return actualValue !== undefined |
|---|
| 528 | return actualValue == expectedValue |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | var bindMatcher = function(methodName) { |
|---|
| 532 | var builtInMatcher = jasmine.Matchers.prototype[methodName] |
|---|
| 533 | |
|---|
| 534 | jasmine.JQuery.matchersClass[methodName] = function() { |
|---|
| 535 | if (this.actual |
|---|
| 536 | && (this.actual instanceof $ |
|---|
| 537 | || jasmine.isDomNode(this.actual))) { |
|---|
| 538 | this.actual = $(this.actual) |
|---|
| 539 | var result = jQueryMatchers[methodName].apply(this, arguments) |
|---|
| 540 | var element |
|---|
| 541 | if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML") |
|---|
| 542 | this.actual = jasmine.JQuery.elementToString(this.actual) |
|---|
| 543 | return result |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | if (builtInMatcher) { |
|---|
| 547 | return builtInMatcher.apply(this, arguments) |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | return false |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | for(var methodName in jQueryMatchers) { |
|---|
| 555 | bindMatcher(methodName) |
|---|
| 556 | } |
|---|
| 557 | }() |
|---|
| 558 | |
|---|
| 559 | beforeEach(function() { |
|---|
| 560 | this.addMatchers(jasmine.JQuery.matchersClass) |
|---|
| 561 | this.addMatchers({ |
|---|
| 562 | toHaveBeenTriggeredOn: function(selector) { |
|---|
| 563 | this.message = function() { |
|---|
| 564 | return [ |
|---|
| 565 | "Expected event " + this.actual + " to have been triggered on " + selector, |
|---|
| 566 | "Expected event " + this.actual + " not to have been triggered on " + selector |
|---|
| 567 | ] |
|---|
| 568 | } |
|---|
| 569 | return jasmine.JQuery.events.wasTriggered(selector, this.actual) |
|---|
| 570 | } |
|---|
| 571 | }) |
|---|
| 572 | this.addMatchers({ |
|---|
| 573 | toHaveBeenTriggered: function(){ |
|---|
| 574 | var eventName = this.actual.eventName, |
|---|
| 575 | selector = this.actual.selector |
|---|
| 576 | this.message = function() { |
|---|
| 577 | return [ |
|---|
| 578 | "Expected event " + eventName + " to have been triggered on " + selector, |
|---|
| 579 | "Expected event " + eventName + " not to have been triggered on " + selector |
|---|
| 580 | ] |
|---|
| 581 | } |
|---|
| 582 | return jasmine.JQuery.events.wasTriggered(selector, eventName) |
|---|
| 583 | } |
|---|
| 584 | }) |
|---|
| 585 | this.addMatchers({ |
|---|
| 586 | toHaveBeenTriggeredOnAndWith: function() { |
|---|
| 587 | var selector = arguments[0], |
|---|
| 588 | expectedArgs = arguments[1], |
|---|
| 589 | wasTriggered = jasmine.JQuery.events.wasTriggered(selector, this.actual); |
|---|
| 590 | this.message = function() { |
|---|
| 591 | if (wasTriggered) { |
|---|
| 592 | var actualArgs = jasmine.JQuery.events.args(selector, this.actual, expectedArgs)[1]; |
|---|
| 593 | return [ |
|---|
| 594 | "Expected event " + this.actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs), |
|---|
| 595 | "Expected event " + this.actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) |
|---|
| 596 | ] |
|---|
| 597 | } else { |
|---|
| 598 | return [ |
|---|
| 599 | "Expected event " + this.actual + " to have been triggered on " + selector, |
|---|
| 600 | "Expected event " + this.actual + " not to have been triggered on " + selector |
|---|
| 601 | ] |
|---|
| 602 | } |
|---|
| 603 | } |
|---|
| 604 | return wasTriggered && jasmine.JQuery.events.wasTriggeredWith(selector, this.actual, expectedArgs, this.env); |
|---|
| 605 | } |
|---|
| 606 | }) |
|---|
| 607 | this.addMatchers({ |
|---|
| 608 | toHaveBeenPreventedOn: function(selector) { |
|---|
| 609 | this.message = function() { |
|---|
| 610 | return [ |
|---|
| 611 | "Expected event " + this.actual + " to have been prevented on " + selector, |
|---|
| 612 | "Expected event " + this.actual + " not to have been prevented on " + selector |
|---|
| 613 | ] |
|---|
| 614 | } |
|---|
| 615 | return jasmine.JQuery.events.wasPrevented(selector, this.actual) |
|---|
| 616 | } |
|---|
| 617 | }) |
|---|
| 618 | this.addMatchers({ |
|---|
| 619 | toHaveBeenPrevented: function() { |
|---|
| 620 | var eventName = this.actual.eventName, |
|---|
| 621 | selector = this.actual.selector |
|---|
| 622 | this.message = function() { |
|---|
| 623 | return [ |
|---|
| 624 | "Expected event " + eventName + " to have been prevented on " + selector, |
|---|
| 625 | "Expected event " + eventName + " not to have been prevented on " + selector |
|---|
| 626 | ] |
|---|
| 627 | } |
|---|
| 628 | return jasmine.JQuery.events.wasPrevented(selector, eventName) |
|---|
| 629 | } |
|---|
| 630 | }) |
|---|
| 631 | this.addMatchers({ |
|---|
| 632 | toHaveBeenStoppedOn: function(selector) { |
|---|
| 633 | this.message = function() { |
|---|
| 634 | return [ |
|---|
| 635 | "Expected event " + this.actual + " to have been stopped on " + selector, |
|---|
| 636 | "Expected event " + this.actual + " not to have been stopped on " + selector |
|---|
| 637 | ] |
|---|
| 638 | } |
|---|
| 639 | return jasmine.JQuery.events.wasStopped(selector, this.actual) |
|---|
| 640 | } |
|---|
| 641 | }) |
|---|
| 642 | this.addMatchers({ |
|---|
| 643 | toHaveBeenStopped: function() { |
|---|
| 644 | var eventName = this.actual.eventName, |
|---|
| 645 | selector = this.actual.selector |
|---|
| 646 | this.message = function() { |
|---|
| 647 | return [ |
|---|
| 648 | "Expected event " + eventName + " to have been stopped on " + selector, |
|---|
| 649 | "Expected event " + eventName + " not to have been stopped on " + selector |
|---|
| 650 | ] |
|---|
| 651 | } |
|---|
| 652 | return jasmine.JQuery.events.wasStopped(selector, eventName) |
|---|
| 653 | } |
|---|
| 654 | }) |
|---|
| 655 | jasmine.getEnv().addEqualityTester(function(a, b) { |
|---|
| 656 | if(a instanceof jQuery && b instanceof jQuery) { |
|---|
| 657 | if(a.size() != b.size()) { |
|---|
| 658 | return jasmine.undefined; |
|---|
| 659 | } |
|---|
| 660 | else if(a.is(b)) { |
|---|
| 661 | return true; |
|---|
| 662 | } |
|---|
| 663 | } |
|---|
| 664 | return jasmine.undefined; |
|---|
| 665 | |
|---|
| 666 | }) |
|---|
| 667 | }) |
|---|
| 668 | |
|---|
| 669 | afterEach(function() { |
|---|
| 670 | jasmine.getFixtures().cleanUp() |
|---|
| 671 | jasmine.getStyleFixtures().cleanUp() |
|---|
| 672 | jasmine.JQuery.events.cleanUp() |
|---|
| 673 | }) |
|---|