[36] | 1 | /*!
|
---|
| 2 | Jasmine-jQuery: a set of jQuery helpers for Jasmine tests.
|
---|
| 3 |
|
---|
| 4 | Version 1.5.92
|
---|
| 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 |
|
---|
| 30 | +function (jasmine, $) { "use strict";
|
---|
| 31 |
|
---|
| 32 | jasmine.spiedEventsKey = function (selector, eventName) {
|
---|
| 33 | return [$(selector).selector, eventName].toString()
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | jasmine.getFixtures = function () {
|
---|
| 37 | return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | jasmine.getStyleFixtures = function () {
|
---|
| 41 | return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures()
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | jasmine.Fixtures = function () {
|
---|
| 45 | this.containerId = 'jasmine-fixtures'
|
---|
| 46 | this.fixturesCache_ = {}
|
---|
| 47 | this.fixturesPath = 'spec/javascripts/fixtures'
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | jasmine.Fixtures.prototype.set = function (html) {
|
---|
| 51 | this.cleanUp()
|
---|
| 52 | return this.createContainer_(html)
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | jasmine.Fixtures.prototype.appendSet= function (html) {
|
---|
| 56 | this.addToContainer_(html)
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | jasmine.Fixtures.prototype.preload = function () {
|
---|
| 60 | this.read.apply(this, arguments)
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | jasmine.Fixtures.prototype.load = function () {
|
---|
| 64 | this.cleanUp()
|
---|
| 65 | this.createContainer_(this.read.apply(this, arguments))
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | jasmine.Fixtures.prototype.appendLoad = function () {
|
---|
| 69 | this.addToContainer_(this.read.apply(this, arguments))
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | jasmine.Fixtures.prototype.read = function () {
|
---|
| 73 | var htmlChunks = []
|
---|
| 74 | , fixtureUrls = arguments
|
---|
| 75 |
|
---|
| 76 | for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
|
---|
| 77 | htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return htmlChunks.join('')
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | jasmine.Fixtures.prototype.clearCache = function () {
|
---|
| 84 | this.fixturesCache_ = {}
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | jasmine.Fixtures.prototype.cleanUp = function () {
|
---|
| 88 | $('#' + this.containerId).remove()
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | jasmine.Fixtures.prototype.sandbox = function (attributes) {
|
---|
| 92 | var attributesToSet = attributes || {}
|
---|
| 93 | return $('<div id="sandbox" />').attr(attributesToSet)
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | jasmine.Fixtures.prototype.createContainer_ = function (html) {
|
---|
| 97 | var container = $('<div>')
|
---|
| 98 | .attr('id', this.containerId)
|
---|
| 99 | .html(html)
|
---|
| 100 |
|
---|
| 101 | $(document.body).append(container)
|
---|
| 102 | return container
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | jasmine.Fixtures.prototype.addToContainer_ = function (html){
|
---|
| 106 | var container = $(document.body).find('#'+this.containerId).append(html)
|
---|
| 107 | if(!container.length){
|
---|
| 108 | this.createContainer_(html)
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | jasmine.Fixtures.prototype.getFixtureHtml_ = function (url) {
|
---|
| 113 | if (typeof this.fixturesCache_[url] === 'undefined') {
|
---|
| 114 | this.loadFixtureIntoCache_(url)
|
---|
| 115 | }
|
---|
| 116 | return this.fixturesCache_[url]
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
|
---|
| 120 | var self = this
|
---|
| 121 | , url = this.makeFixtureUrl_(relativeUrl)
|
---|
| 122 | , request = $.ajax({
|
---|
| 123 | async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
|
---|
| 124 | cache: false,
|
---|
| 125 | url: url,
|
---|
| 126 | success: function (data, status, $xhr) {
|
---|
| 127 | self.fixturesCache_[relativeUrl] = $xhr.responseText
|
---|
| 128 | },
|
---|
| 129 | error: function (jqXHR, status, errorThrown) {
|
---|
| 130 | throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
|
---|
| 131 | }
|
---|
| 132 | })
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){
|
---|
| 136 | return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | jasmine.Fixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
|
---|
| 140 | return this[methodName].apply(this, passedArguments)
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | jasmine.StyleFixtures = function () {
|
---|
| 145 | this.fixturesCache_ = {}
|
---|
| 146 | this.fixturesNodes_ = []
|
---|
| 147 | this.fixturesPath = 'spec/javascripts/fixtures'
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | jasmine.StyleFixtures.prototype.set = function (css) {
|
---|
| 151 | this.cleanUp()
|
---|
| 152 | this.createStyle_(css)
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | jasmine.StyleFixtures.prototype.appendSet = function (css) {
|
---|
| 156 | this.createStyle_(css)
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | jasmine.StyleFixtures.prototype.preload = function () {
|
---|
| 160 | this.read_.apply(this, arguments)
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | jasmine.StyleFixtures.prototype.load = function () {
|
---|
| 164 | this.cleanUp()
|
---|
| 165 | this.createStyle_(this.read_.apply(this, arguments))
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | jasmine.StyleFixtures.prototype.appendLoad = function () {
|
---|
| 169 | this.createStyle_(this.read_.apply(this, arguments))
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | jasmine.StyleFixtures.prototype.cleanUp = function () {
|
---|
| 173 | while(this.fixturesNodes_.length) {
|
---|
| 174 | this.fixturesNodes_.pop().remove()
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | jasmine.StyleFixtures.prototype.createStyle_ = function (html) {
|
---|
| 179 | var styleText = $('<div></div>').html(html).text()
|
---|
| 180 | , style = $('<style>' + styleText + '</style>')
|
---|
| 181 |
|
---|
| 182 | this.fixturesNodes_.push(style)
|
---|
| 183 | $('head').append(style)
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache
|
---|
| 187 | jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read
|
---|
| 188 | jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_
|
---|
| 189 | jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_
|
---|
| 190 | jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_
|
---|
| 191 | jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_
|
---|
| 192 |
|
---|
| 193 | jasmine.getJSONFixtures = function () {
|
---|
| 194 | return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures()
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | jasmine.JSONFixtures = function () {
|
---|
| 198 | this.fixturesCache_ = {}
|
---|
| 199 | this.fixturesPath = 'spec/javascripts/fixtures/json'
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | jasmine.JSONFixtures.prototype.load = function () {
|
---|
| 203 | this.read.apply(this, arguments)
|
---|
| 204 | return this.fixturesCache_
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | jasmine.JSONFixtures.prototype.read = function () {
|
---|
| 208 | var fixtureUrls = arguments
|
---|
| 209 |
|
---|
| 210 | for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
|
---|
| 211 | this.getFixtureData_(fixtureUrls[urlIndex])
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | return this.fixturesCache_
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | jasmine.JSONFixtures.prototype.clearCache = function () {
|
---|
| 218 | this.fixturesCache_ = {}
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | jasmine.JSONFixtures.prototype.getFixtureData_ = function (url) {
|
---|
| 222 | if (!this.fixturesCache_[url]) this.loadFixtureIntoCache_(url)
|
---|
| 223 | return this.fixturesCache_[url]
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
|
---|
| 227 | var self = this
|
---|
| 228 | , url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
|
---|
| 229 |
|
---|
| 230 | $.ajax({
|
---|
| 231 | async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
|
---|
| 232 | cache: false,
|
---|
| 233 | dataType: 'json',
|
---|
| 234 | url: url,
|
---|
| 235 | success: function (data) {
|
---|
| 236 | self.fixturesCache_[relativeUrl] = data
|
---|
| 237 | },
|
---|
| 238 | error: function (jqXHR, status, errorThrown) {
|
---|
| 239 | throw new Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
|
---|
| 240 | }
|
---|
| 241 | })
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | jasmine.JSONFixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
|
---|
| 245 | return this[methodName].apply(this, passedArguments)
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | jasmine.JQuery = function () {}
|
---|
| 249 |
|
---|
| 250 | jasmine.JQuery.browserTagCaseIndependentHtml = function (html) {
|
---|
| 251 | return $('<div/>').append(html).html()
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | jasmine.JQuery.elementToString = function (element) {
|
---|
| 255 | return $(element).map(function () { return this.outerHTML; }).toArray().join(', ')
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | !function (namespace) {
|
---|
| 259 | var data = {
|
---|
| 260 | spiedEvents: {}
|
---|
| 261 | , handlers: []
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | namespace.events = {
|
---|
| 265 | spyOn: function (selector, eventName) {
|
---|
| 266 | var handler = function (e) {
|
---|
| 267 | data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = jasmine.util.argsToArray(arguments)
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | $(selector).on(eventName, handler)
|
---|
| 271 | data.handlers.push(handler)
|
---|
| 272 |
|
---|
| 273 | return {
|
---|
| 274 | selector: selector,
|
---|
| 275 | eventName: eventName,
|
---|
| 276 | handler: handler,
|
---|
| 277 | reset: function (){
|
---|
| 278 | delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | },
|
---|
| 282 |
|
---|
| 283 | args: function (selector, eventName) {
|
---|
| 284 | var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
|
---|
| 285 |
|
---|
| 286 | if (!actualArgs) {
|
---|
| 287 | throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent."
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | return actualArgs
|
---|
| 291 | },
|
---|
| 292 |
|
---|
| 293 | wasTriggered: function (selector, eventName) {
|
---|
| 294 | return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)])
|
---|
| 295 | },
|
---|
| 296 |
|
---|
| 297 | wasTriggeredWith: function (selector, eventName, expectedArgs, util, customEqualityTesters) {
|
---|
| 298 | var actualArgs = jasmine.JQuery.events.args(selector, eventName).slice(1)
|
---|
| 299 | if (Object.prototype.toString.call(expectedArgs) !== '[object Array]') {
|
---|
| 300 | actualArgs = actualArgs[0]
|
---|
| 301 | }
|
---|
| 302 | return util.equals(expectedArgs, actualArgs, customEqualityTesters)
|
---|
| 303 | },
|
---|
| 304 |
|
---|
| 305 | wasPrevented: function (selector, eventName) {
|
---|
| 306 | var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
|
---|
| 307 | , e = args ? args[0] : undefined
|
---|
| 308 |
|
---|
| 309 | return e && e.isDefaultPrevented()
|
---|
| 310 | },
|
---|
| 311 |
|
---|
| 312 | wasStopped: function (selector, eventName) {
|
---|
| 313 | var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
|
---|
| 314 | , e = args ? args[0] : undefined
|
---|
| 315 | return e && e.isPropagationStopped()
|
---|
| 316 | },
|
---|
| 317 |
|
---|
| 318 | cleanUp: function () {
|
---|
| 319 | data.spiedEvents = {}
|
---|
| 320 | data.handlers = []
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | }(jasmine.JQuery)
|
---|
| 324 |
|
---|
| 325 | var hasProperty = function (actualValue, expectedValue) {
|
---|
| 326 | if (expectedValue === undefined) return actualValue !== undefined
|
---|
| 327 |
|
---|
| 328 | return actualValue == expectedValue
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | beforeEach(function () {
|
---|
| 332 | jasmine.addMatchers({
|
---|
| 333 | toHaveClass: function () {
|
---|
| 334 | return {
|
---|
| 335 | compare: function (actual, className) {
|
---|
| 336 | return { pass: $(actual).hasClass(className) }
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | },
|
---|
| 340 |
|
---|
| 341 | toHaveCss: function () {
|
---|
| 342 | return {
|
---|
| 343 | compare: function (actual, css){
|
---|
| 344 | for (var prop in css){
|
---|
| 345 | var value = css[prop]
|
---|
| 346 | // see issue #147 on gh
|
---|
| 347 | ;if (value === 'auto' && $(actual).get(0).style[prop] === 'auto') continue
|
---|
| 348 | if ($(actual).css(prop) !== value) return { pass: false }
|
---|
| 349 | }
|
---|
| 350 | return { pass: true }
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | },
|
---|
| 354 |
|
---|
| 355 | toBeVisible: function () {
|
---|
| 356 | return {
|
---|
| 357 | compare: function (actual) {
|
---|
| 358 | return { pass: $(actual).is(':visible') }
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | },
|
---|
| 362 |
|
---|
| 363 | toBeHidden: function () {
|
---|
| 364 | return {
|
---|
| 365 | compare: function (actual) {
|
---|
| 366 | return { pass: $(actual).is(':hidden') }
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 | },
|
---|
| 370 |
|
---|
| 371 | toBeSelected: function () {
|
---|
| 372 | return {
|
---|
| 373 | compare: function (actual) {
|
---|
| 374 | return { pass: $(actual).is(':selected') }
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | },
|
---|
| 378 |
|
---|
| 379 | toBeChecked: function () {
|
---|
| 380 | return {
|
---|
| 381 | compare: function (actual) {
|
---|
| 382 | return { pass: $(actual).is(':checked') }
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 | },
|
---|
| 386 |
|
---|
| 387 | toBeEmpty: function () {
|
---|
| 388 | return {
|
---|
| 389 | compare: function (actual) {
|
---|
| 390 | return { pass: $(actual).is(':empty') }
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 | },
|
---|
| 394 |
|
---|
| 395 | toExist: function () {
|
---|
| 396 | return {
|
---|
| 397 | compare: function (actual) {
|
---|
| 398 | return { pass: $(actual).length }
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | },
|
---|
| 402 |
|
---|
| 403 | toHaveLength: function () {
|
---|
| 404 | return {
|
---|
| 405 | compare: function (actual, length) {
|
---|
| 406 | return { pass: $(actual).length === length }
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 | },
|
---|
| 410 |
|
---|
| 411 | toHaveAttr: function () {
|
---|
| 412 | return {
|
---|
| 413 | compare: function (actual, attributeName, expectedAttributeValue) {
|
---|
| 414 | return { pass: hasProperty($(actual).attr(attributeName), expectedAttributeValue) }
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | },
|
---|
| 418 |
|
---|
| 419 | toHaveProp: function () {
|
---|
| 420 | return {
|
---|
| 421 | compare: function (actual, propertyName, expectedPropertyValue) {
|
---|
| 422 | return { pass: hasProperty($(actual).prop(propertyName), expectedPropertyValue) }
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | },
|
---|
| 426 |
|
---|
| 427 | toHaveId: function () {
|
---|
| 428 | return {
|
---|
| 429 | compare: function (actual, id) {
|
---|
| 430 | return { pass: $(actual).attr('id') == id }
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 | },
|
---|
| 434 |
|
---|
| 435 | toHaveHtml: function () {
|
---|
| 436 | return {
|
---|
| 437 | compare: function (actual, html) {
|
---|
| 438 | return { pass: $(actual).html() == jasmine.JQuery.browserTagCaseIndependentHtml(html) }
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | },
|
---|
| 442 |
|
---|
| 443 | toContainHtml: function () {
|
---|
| 444 | return {
|
---|
| 445 | compare: function (actual, html){
|
---|
| 446 | var actualHtml = $(actual).html()
|
---|
| 447 | , expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
|
---|
| 448 |
|
---|
| 449 | return { pass: (actualHtml.indexOf(expectedHtml) >= 0) }
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 | },
|
---|
| 453 |
|
---|
| 454 | toHaveText: function () {
|
---|
| 455 | return {
|
---|
| 456 | compare: function (actual, text) {
|
---|
| 457 | var trimmedText = $.trim($(actual).text())
|
---|
| 458 |
|
---|
| 459 | if (text && $.isFunction(text.test)) {
|
---|
| 460 | return { pass: text.test(trimmedText) }
|
---|
| 461 | } else {
|
---|
| 462 | return { pass: trimmedText == text }
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 | }
|
---|
| 466 | },
|
---|
| 467 |
|
---|
| 468 | toContainText: function () {
|
---|
| 469 | return {
|
---|
| 470 | compare: function (actual, text) {
|
---|
| 471 | var trimmedText = $.trim($(actual).text())
|
---|
| 472 |
|
---|
| 473 | if (text && $.isFunction(text.test)) {
|
---|
| 474 | return { pass: text.test(trimmedText) }
|
---|
| 475 | } else {
|
---|
| 476 | return { pass: trimmedText.indexOf(text) != -1 }
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 | },
|
---|
| 481 |
|
---|
| 482 | toHaveValue: function () {
|
---|
| 483 | return {
|
---|
| 484 | compare: function (actual, value) {
|
---|
| 485 | return { pass: $(actual).val() === value }
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | },
|
---|
| 489 |
|
---|
| 490 | toHaveData: function () {
|
---|
| 491 | return {
|
---|
| 492 | compare: function (actual, key, expectedValue) {
|
---|
| 493 | return { pass: hasProperty($(actual).data(key), expectedValue) }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 | },
|
---|
| 497 |
|
---|
| 498 | toContainElement: function () {
|
---|
| 499 | return {
|
---|
| 500 | compare: function (actual, selector) {
|
---|
| 501 | return { pass: $(actual).find(selector).length }
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 | },
|
---|
| 505 |
|
---|
| 506 | toBeMatchedBy: function () {
|
---|
| 507 | return {
|
---|
| 508 | compare: function (actual, selector) {
|
---|
| 509 | return { pass: $(actual).filter(selector).length }
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 | },
|
---|
| 513 |
|
---|
| 514 | toBeDisabled: function () {
|
---|
| 515 | return {
|
---|
| 516 | compare: function (actual, selector){
|
---|
| 517 | return { pass: $(actual).is(':disabled') }
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 | },
|
---|
| 521 |
|
---|
| 522 | toBeFocused: function () {
|
---|
| 523 | return {
|
---|
| 524 | compare: function (actual, selector) {
|
---|
| 525 | return { pass: $(actual)[0] === $(actual)[0].ownerDocument.activeElement }
|
---|
| 526 | }
|
---|
| 527 | }
|
---|
| 528 | },
|
---|
| 529 |
|
---|
| 530 | toHandle: function () {
|
---|
| 531 | return {
|
---|
| 532 | compare: function (actual, event) {
|
---|
| 533 | var events = $._data($(actual).get(0), "events")
|
---|
| 534 |
|
---|
| 535 | if(!events || !event || typeof event !== "string") {
|
---|
| 536 | return { pass: false }
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | var namespaces = event.split(".")
|
---|
| 540 | , eventType = namespaces.shift()
|
---|
| 541 | , sortedNamespaces = namespaces.slice(0).sort()
|
---|
| 542 | , namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
|
---|
| 543 |
|
---|
| 544 | if(events[eventType] && namespaces.length) {
|
---|
| 545 | for(var i = 0; i < events[eventType].length; i++) {
|
---|
| 546 | var namespace = events[eventType][i].namespace
|
---|
| 547 |
|
---|
| 548 | if(namespaceRegExp.test(namespace)) {
|
---|
| 549 | return { pass: true }
|
---|
| 550 | }
|
---|
| 551 | }
|
---|
| 552 | } else {
|
---|
| 553 | return { pass: events[eventType] && events[eventType].length > 0 }
|
---|
| 554 | }
|
---|
| 555 | return { pass: false }
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 | },
|
---|
| 559 | toHandleWith: function () {
|
---|
| 560 | return {
|
---|
| 561 | compare: function (actual, eventName, eventHandler) {
|
---|
| 562 | var normalizedEventName = eventName.split('.')[0]
|
---|
| 563 | , stack = $._data($(actual).get(0), "events")[normalizedEventName]
|
---|
| 564 |
|
---|
| 565 | for (var i = 0; i < stack.length; i++) {
|
---|
| 566 | if (stack[i].handler == eventHandler) return { pass: true }
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | return { pass: false }
|
---|
| 570 | }
|
---|
| 571 | }
|
---|
| 572 | },
|
---|
| 573 | toHaveBeenTriggeredOn: function () {
|
---|
| 574 | return {
|
---|
| 575 | compare: function (actual, selector) {
|
---|
| 576 | var result = { pass: jasmine.JQuery.events.wasTriggered(selector, actual) }
|
---|
| 577 | result.message = result.pass ?
|
---|
| 578 | "Expected event " + actual + " not to have been triggered on " + selector :
|
---|
| 579 | "Expected event " + actual + " to have been triggered on " + selector
|
---|
| 580 | return result
|
---|
| 581 | }
|
---|
| 582 | }
|
---|
| 583 | },
|
---|
| 584 | toHaveBeenTriggered: function () {
|
---|
| 585 | return {
|
---|
| 586 | compare: function (actual){
|
---|
| 587 | var eventName = actual.eventName
|
---|
| 588 | , selector = actual.selector
|
---|
| 589 | , result = { pass: jasmine.JQuery.events.wasTriggered(selector, eventName) }
|
---|
| 590 |
|
---|
| 591 | result.message = result.pass ?
|
---|
| 592 | "Expected event " + eventName + " not to have been triggered on " + selector :
|
---|
| 593 | "Expected event " + eventName + " to have been triggered on " + selector
|
---|
| 594 |
|
---|
| 595 | return result
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 | },
|
---|
| 599 | toHaveBeenTriggeredOnAndWith: function (j$, customEqualityTesters) {
|
---|
| 600 | return {
|
---|
| 601 | compare: function (actual, selector, expectedArgs) {
|
---|
| 602 | var wasTriggered = jasmine.JQuery.events.wasTriggered(selector, actual)
|
---|
| 603 | , result = { pass: wasTriggered && jasmine.JQuery.events.wasTriggeredWith(selector, actual, expectedArgs, j$, customEqualityTesters) }
|
---|
| 604 | console.log(actual, selector, expectedArgs, wasTriggered, result)
|
---|
| 605 |
|
---|
| 606 | if (wasTriggered) {
|
---|
| 607 | var actualArgs = jasmine.JQuery.events.args(selector, actual, expectedArgs)[1]
|
---|
| 608 | result.message = result.pass ?
|
---|
| 609 | "Expected event " + actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) :
|
---|
| 610 | "Expected event " + actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs)
|
---|
| 611 | } else {
|
---|
| 612 | result.message = result.pass ?
|
---|
| 613 | "Expected event " + actual + " not to have been triggered on " + selector :
|
---|
| 614 | "Expected event " + actual + " to have been triggered on " + selector
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | return result
|
---|
| 618 | }
|
---|
| 619 | }
|
---|
| 620 | },
|
---|
| 621 | toHaveBeenPreventedOn: function () {
|
---|
| 622 | return {
|
---|
| 623 | compare: function (actual, selector) {
|
---|
| 624 | var result = { pass: jasmine.JQuery.events.wasPrevented(selector, actual) }
|
---|
| 625 | result.message = result.pass ?
|
---|
| 626 | "Expected event " + actual + " not to have been prevented on " + selector :
|
---|
| 627 | "Expected event " + actual + " to have been prevented on " + selector
|
---|
| 628 |
|
---|
| 629 | return result
|
---|
| 630 | }
|
---|
| 631 | }
|
---|
| 632 | },
|
---|
| 633 | toHaveBeenPrevented: function () {
|
---|
| 634 | return {
|
---|
| 635 | compare: function (actual) {
|
---|
| 636 | var eventName = actual.eventName
|
---|
| 637 | , selector = actual.selector
|
---|
| 638 | , result = { pass: jasmine.JQuery.events.wasPrevented(selector, eventName) }
|
---|
| 639 |
|
---|
| 640 | result.message = result.pass ?
|
---|
| 641 | "Expected event " + eventName + " not to have been prevented on " + selector :
|
---|
| 642 | "Expected event " + eventName + " to have been prevented on " + selector
|
---|
| 643 |
|
---|
| 644 | return result
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 | },
|
---|
| 648 | toHaveBeenStoppedOn: function () {
|
---|
| 649 | return {
|
---|
| 650 | compare: function (actual, selector) {
|
---|
| 651 | var result = { pass: jasmine.JQuery.events.wasStopped(selector, actual) }
|
---|
| 652 | result.message = result.pass ?
|
---|
| 653 | "Expected event " + actual + " not to have been stopped on " + selector :
|
---|
| 654 | "Expected event " + actual + " to have been stopped on " + selector
|
---|
| 655 |
|
---|
| 656 | return result
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 | },
|
---|
| 660 | toHaveBeenStopped: function () {
|
---|
| 661 | return {
|
---|
| 662 | compare: function (actual) {
|
---|
| 663 | var eventName = actual.eventName
|
---|
| 664 | , selector = actual.selector
|
---|
| 665 | , result = { pass: jasmine.JQuery.events.wasStopped(selector, eventName) }
|
---|
| 666 |
|
---|
| 667 | result.message = result.pass ?
|
---|
| 668 | "Expected event " + eventName + " not to have been stopped on " + selector :
|
---|
| 669 | "Expected event " + eventName + " to have been stopped on " + selector
|
---|
| 670 |
|
---|
| 671 | return result
|
---|
| 672 | }
|
---|
| 673 | }
|
---|
| 674 | },
|
---|
| 675 | })
|
---|
| 676 |
|
---|
| 677 | jasmine.getEnv().addCustomEqualityTester(function(a, b) {
|
---|
| 678 | if (a && b) {
|
---|
| 679 | if (a instanceof jQuery || jasmine.isDomNode(a)) {
|
---|
| 680 | var $a = $(a)
|
---|
| 681 | if (b instanceof jQuery) {
|
---|
| 682 | return $a.length == b.length && a.is(b)
|
---|
| 683 | }
|
---|
| 684 | return $a.is(b);
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | if (b instanceof jQuery || jasmine.isDomNode(b)) {
|
---|
| 688 | var $b = $(b)
|
---|
| 689 | if (a instanceof jQuery) {
|
---|
| 690 | return a.length == $b.length && $b.is(a)
|
---|
| 691 | }
|
---|
| 692 | return $(b).is(a);
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 | })
|
---|
| 696 |
|
---|
| 697 | jasmine.getEnv().addCustomEqualityTester(function (a, b) {
|
---|
| 698 | if(a instanceof jQuery && b instanceof jQuery) {
|
---|
| 699 | if(a.size() == b.size()) {
|
---|
| 700 | return a.is(b);
|
---|
| 701 | }
|
---|
| 702 | }
|
---|
| 703 | })
|
---|
| 704 | })
|
---|
| 705 |
|
---|
| 706 | afterEach(function () {
|
---|
| 707 | jasmine.getFixtures().cleanUp()
|
---|
| 708 | jasmine.getStyleFixtures().cleanUp()
|
---|
| 709 | jasmine.JQuery.events.cleanUp()
|
---|
| 710 | })
|
---|
| 711 | }(window.jasmine, window.jQuery)
|
---|
| 712 |
|
---|
| 713 | +function (jasmine, global) { "use strict";
|
---|
| 714 |
|
---|
| 715 | global.readFixtures = function () {
|
---|
| 716 | return jasmine.getFixtures().proxyCallTo_('read', arguments)
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | global.preloadFixtures = function () {
|
---|
| 720 | jasmine.getFixtures().proxyCallTo_('preload', arguments)
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | global.loadFixtures = function () {
|
---|
| 724 | jasmine.getFixtures().proxyCallTo_('load', arguments)
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | global.appendLoadFixtures = function () {
|
---|
| 728 | jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | global.setFixtures = function (html) {
|
---|
| 732 | return jasmine.getFixtures().proxyCallTo_('set', arguments)
|
---|
| 733 | }
|
---|
| 734 |
|
---|
| 735 | global.appendSetFixtures = function () {
|
---|
| 736 | jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | global.sandbox = function (attributes) {
|
---|
| 740 | return jasmine.getFixtures().sandbox(attributes)
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | global.spyOnEvent = function (selector, eventName) {
|
---|
| 744 | return jasmine.JQuery.events.spyOn(selector, eventName)
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | global.preloadStyleFixtures = function () {
|
---|
| 748 | jasmine.getStyleFixtures().proxyCallTo_('preload', arguments)
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | global.loadStyleFixtures = function () {
|
---|
| 752 | jasmine.getStyleFixtures().proxyCallTo_('load', arguments)
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | global.appendLoadStyleFixtures = function () {
|
---|
| 756 | jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments)
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | global.setStyleFixtures = function (html) {
|
---|
| 760 | jasmine.getStyleFixtures().proxyCallTo_('set', arguments)
|
---|
| 761 | }
|
---|
| 762 |
|
---|
| 763 | global.appendSetStyleFixtures = function (html) {
|
---|
| 764 | jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments)
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | global.loadJSONFixtures = function () {
|
---|
| 768 | return jasmine.getJSONFixtures().proxyCallTo_('load', arguments)
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | global.getJSONFixture = function (url) {
|
---|
| 772 | return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url]
|
---|
| 773 | }
|
---|
| 774 | }(jasmine, window);
|
---|