jasmine-jquery.js 9.77 KB
Newer Older
1
var readFixtures = function() {
2 3
  return jasmine.getFixtures().proxyCallTo_('read', arguments)
}
4 5

var preloadFixtures = function() {
6 7
  jasmine.getFixtures().proxyCallTo_('preload', arguments)
}
8 9

var loadFixtures = function() {
10 11 12 13 14 15
  jasmine.getFixtures().proxyCallTo_('load', arguments)
}

var appendLoadFixtures = function() {
  jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
}
16 17

var setFixtures = function(html) {
18 19 20 21 22 23
  jasmine.getFixtures().proxyCallTo_('set', arguments)
}

var appendSetFixtures = function() {
  jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
}
24 25

var sandbox = function(attributes) {
26 27
  return jasmine.getFixtures().sandbox(attributes)
}
28 29

var spyOnEvent = function(selector, eventName) {
30 31
  jasmine.JQuery.events.spyOn(selector, eventName)
}
32 33

jasmine.getFixtures = function() {
34 35
  return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
}
36 37

jasmine.Fixtures = function() {
38 39 40 41
  this.containerId = 'jasmine-fixtures'
  this.fixturesCache_ = {}
  this.fixturesPath = 'spec/javascripts/fixtures'
}
42 43

jasmine.Fixtures.prototype.set = function(html) {
44 45 46 47 48 49 50
  this.cleanUp()
  this.createContainer_(html)
}

jasmine.Fixtures.prototype.appendSet= function(html) {
  this.addToContainer_(html)
}
51 52

jasmine.Fixtures.prototype.preload = function() {
53 54
  this.read.apply(this, arguments)
}
55 56

jasmine.Fixtures.prototype.load = function() {
57 58 59 60 61 62 63
  this.cleanUp()
  this.createContainer_(this.read.apply(this, arguments))
}

jasmine.Fixtures.prototype.appendLoad = function() {
  this.addToContainer_(this.read.apply(this, arguments))
}
64 65

jasmine.Fixtures.prototype.read = function() {
66
  var htmlChunks = []
67

68
  var fixtureUrls = arguments
69
  for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
70
    htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
71 72
  }

73 74
  return htmlChunks.join('')
}
75 76

jasmine.Fixtures.prototype.clearCache = function() {
77 78
  this.fixturesCache_ = {}
}
79 80

jasmine.Fixtures.prototype.cleanUp = function() {
81 82
  jQuery('#' + this.containerId).remove()
}
83 84

jasmine.Fixtures.prototype.sandbox = function(attributes) {
85 86 87
  var attributesToSet = attributes || {}
  return jQuery('<div id="sandbox" />').attr(attributesToSet)
}
88 89

jasmine.Fixtures.prototype.createContainer_ = function(html) {
90
  var container
91
  if(html instanceof jQuery) {
92 93
    container = jQuery('<div id="' + this.containerId + '" />')
    container.html(html)
94 95 96
  } else {
    container = '<div id="' + this.containerId + '">' + html + '</div>'
  }
97 98 99 100 101 102 103 104 105
  jQuery('body').append(container)
}

jasmine.Fixtures.prototype.addToContainer_ = function(html){
  var container = jQuery('body').find('#'+this.containerId).append(html)
  if(!container.length){
    this.createContainer_(html)
  }
}
106 107

jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) {
108 109
  if (typeof this.fixturesCache_[url] === 'undefined') {
    this.loadFixtureIntoCache_(url)
110
  }
111 112
  return this.fixturesCache_[url]
}
113 114

jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
115 116 117 118 119 120
  var url = this.makeFixtureUrl_(relativeUrl)
  var request = new XMLHttpRequest()
  request.open("GET", url + "?" + new Date().getTime(), false)
  request.send(null)
  this.fixturesCache_[relativeUrl] = request.responseText
}
121 122

jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){
123 124
  return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
}
125 126

jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
127 128
  return this[methodName].apply(this, passedArguments)
}
129 130


131
jasmine.JQuery = function() {}
132 133

jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
134 135
  return jQuery('<div/>').append(html).html()
}
136 137

jasmine.JQuery.elementToString = function(element) {
138 139 140
  var domEl = $(element).get(0)
  if (domEl == undefined || domEl.cloneNode)
    return jQuery('<div />').append($(element).clone()).html()
141
  else
142 143
    return element.toString()
}
144 145 146

jasmine.JQuery.matchersClass = {};

147
!function(namespace) {
148 149 150
  var data = {
    spiedEvents: {},
    handlers:    []
151
  }
152 153 154 155

  namespace.events = {
    spyOn: function(selector, eventName) {
      var handler = function(e) {
156 157 158 159
        data.spiedEvents[[selector, eventName]] = e
      }
      jQuery(selector).bind(eventName, handler)
      data.handlers.push(handler)
160 161 162
    },

    wasTriggered: function(selector, eventName) {
163
      return !!(data.spiedEvents[[selector, eventName]])
164 165 166
    },

    wasPrevented: function(selector, eventName) {
167
      return data.spiedEvents[[selector, eventName]].isDefaultPrevented()
168 169 170
    },

    cleanUp: function() {
171 172
      data.spiedEvents = {}
      data.handlers    = []
173 174
    }
  }
175
}(jasmine.JQuery)
176

177
!function(){
178 179
  var jQueryMatchers = {
    toHaveClass: function(className) {
180 181 182 183 184 185 186 187
      return this.actual.hasClass(className)
    },

    toHaveCss: function(css){
      for (var prop in css){
        if (this.actual.css(prop) !== css[prop]) return false
      }
      return true
188 189 190
    },

    toBeVisible: function() {
191
      return this.actual.is(':visible')
192 193 194
    },

    toBeHidden: function() {
195
      return this.actual.is(':hidden')
196 197 198
    },

    toBeSelected: function() {
199
      return this.actual.is(':selected')
200 201 202
    },

    toBeChecked: function() {
203
      return this.actual.is(':checked')
204 205 206
    },

    toBeEmpty: function() {
207
      return this.actual.is(':empty')
208 209 210
    },

    toExist: function() {
211
      return $(document).find(this.actual).length
212 213 214
    },

    toHaveAttr: function(attributeName, expectedAttributeValue) {
215
      return hasProperty(this.actual.attr(attributeName), expectedAttributeValue)
216 217 218
    },

    toHaveProp: function(propertyName, expectedPropertyValue) {
219
      return hasProperty(this.actual.prop(propertyName), expectedPropertyValue)
220 221 222
    },

    toHaveId: function(id) {
223
      return this.actual.attr('id') == id
224 225 226
    },

    toHaveHtml: function(html) {
227 228 229 230 231 232 233
      return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
    },

    toContainHtml: function(html){
      var actualHtml = this.actual.html()
      var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
      return (actualHtml.indexOf(expectedHtml) >= 0)
234 235 236
    },

    toHaveText: function(text) {
237
      var trimmedText = $.trim(this.actual.text())
238
      if (text && jQuery.isFunction(text.test)) {
239
        return text.test(trimmedText)
240
      } else {
241
        return trimmedText == text
242 243 244 245
      }
    },

    toHaveValue: function(value) {
246
      return this.actual.val() == value
247 248 249
    },

    toHaveData: function(key, expectedValue) {
250
      return hasProperty(this.actual.data(key), expectedValue)
251 252 253
    },

    toBe: function(selector) {
254
      return this.actual.is(selector)
255 256 257
    },

    toContain: function(selector) {
258
      return this.actual.find(selector).length
259 260 261
    },

    toBeDisabled: function(selector){
262
      return this.actual.is(':disabled')
263 264 265
    },

    toBeFocused: function(selector) {
266
      return this.actual.is(':focus')
267 268
    },

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    toHandle: function(event) {

      var events = this.actual.data('events')

      if(!events || !event || typeof event !== "string") {
        return false
      }

      var namespaces = event.split(".")
      var eventType = namespaces.shift()
      var sortedNamespaces = namespaces.slice(0).sort()
      var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")

      if(events[eventType] && namespaces.length) {
        for(var i = 0; i < events[eventType].length; i++) {
          var namespace = events[eventType][i].namespace
          if(namespaceRegExp.test(namespace)) {
            return true
          }
        }
      } else {
        return events[eventType] && events[eventType].length > 0
      }
292 293 294 295
    },

    // tests the existence of a specific event binding + handler
    toHandleWith: function(eventName, eventHandler) {
296 297 298
      var stack = this.actual.data("events")[eventName]
      for (var i = 0; i < stack.length; i++) {
        if (stack[i].handler == eventHandler) return true
299
      }
300
      return false
301
    }
302
  }
303 304

  var hasProperty = function(actualValue, expectedValue) {
305 306 307
    if (expectedValue === undefined) return actualValue !== undefined
    return actualValue == expectedValue
  }
308 309

  var bindMatcher = function(methodName) {
310
    var builtInMatcher = jasmine.Matchers.prototype[methodName]
311 312 313

    jasmine.JQuery.matchersClass[methodName] = function() {
      if (this.actual
314 315 316 317
        && (this.actual instanceof jQuery
          || jasmine.isDomNode(this.actual))) {
            this.actual = $(this.actual)
            var result = jQueryMatchers[methodName].apply(this, arguments)
318 319
            var element;
            if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML")
320 321 322 323 324 325 326 327 328 329 330
              this.actual = jasmine.JQuery.elementToString(this.actual)
            return result
          }

          if (builtInMatcher) {
            return builtInMatcher.apply(this, arguments)
          }

          return false
    }
  }
331 332

  for(var methodName in jQueryMatchers) {
333
    bindMatcher(methodName)
334
  }
335
}()
336 337

beforeEach(function() {
338
  this.addMatchers(jasmine.JQuery.matchersClass)
339 340 341 342 343 344
  this.addMatchers({
    toHaveBeenTriggeredOn: function(selector) {
      this.message = function() {
        return [
          "Expected event " + this.actual + " to have been triggered on " + selector,
          "Expected event " + this.actual + " not to have been triggered on " + selector
345 346 347
        ]
      }
      return jasmine.JQuery.events.wasTriggered($(selector), this.actual)
348
    }
349
  })
350 351 352 353 354 355
  this.addMatchers({
    toHaveBeenPreventedOn: function(selector) {
      this.message = function() {
        return [
          "Expected event " + this.actual + " to have been prevented on " + selector,
          "Expected event " + this.actual + " not to have been prevented on " + selector
356 357 358
        ]
      }
      return jasmine.JQuery.events.wasPrevented(selector, this.actual)
359
    }
360 361
  })
})
362 363

afterEach(function() {
364 365 366
  jasmine.getFixtures().cleanUp()
  jasmine.JQuery.events.cleanUp()
})