1 var readFixtures = function() {
2 return jasmine.getFixtures().proxyCallTo_('read', arguments)
5 var preloadFixtures = function() {
6 jasmine.getFixtures().proxyCallTo_('preload', arguments)
9 var loadFixtures = function() {
10 jasmine.getFixtures().proxyCallTo_('load', arguments)
13 var appendLoadFixtures = function() {
14 jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
17 var setFixtures = function(html) {
18 jasmine.getFixtures().proxyCallTo_('set', arguments)
21 var appendSetFixtures = function() {
22 jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
25 var sandbox = function(attributes) {
26 return jasmine.getFixtures().sandbox(attributes)
29 var spyOnEvent = function(selector, eventName) {
30 jasmine.JQuery.events.spyOn(selector, eventName)
33 jasmine.getFixtures = function() {
34 return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
37 jasmine.Fixtures = function() {
38 this.containerId = 'jasmine-fixtures'
39 this.fixturesCache_ = {}
40 this.fixturesPath = 'spec/javascripts/fixtures'
43 jasmine.Fixtures.prototype.set = function(html) {
45 this.createContainer_(html)
48 jasmine.Fixtures.prototype.appendSet= function(html) {
49 this.addToContainer_(html)
52 jasmine.Fixtures.prototype.preload = function() {
53 this.read.apply(this, arguments)
56 jasmine.Fixtures.prototype.load = function() {
58 this.createContainer_(this.read.apply(this, arguments))
61 jasmine.Fixtures.prototype.appendLoad = function() {
62 this.addToContainer_(this.read.apply(this, arguments))
65 jasmine.Fixtures.prototype.read = function() {
68 var fixtureUrls = arguments
69 for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
70 htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
73 return htmlChunks.join('')
76 jasmine.Fixtures.prototype.clearCache = function() {
77 this.fixturesCache_ = {}
80 jasmine.Fixtures.prototype.cleanUp = function() {
81 jQuery('#' + this.containerId).remove()
84 jasmine.Fixtures.prototype.sandbox = function(attributes) {
85 var attributesToSet = attributes || {}
86 return jQuery('<div id="sandbox" />').attr(attributesToSet)
89 jasmine.Fixtures.prototype.createContainer_ = function(html) {
91 if(html instanceof jQuery) {
92 container = jQuery('<div id="' + this.containerId + '" />')
95 container = '<div id="' + this.containerId + '">' + html + '</div>'
97 jQuery('body').append(container)
100 jasmine.Fixtures.prototype.addToContainer_ = function(html){
101 var container = jQuery('body').find('#'+this.containerId).append(html)
102 if(!container.length){
103 this.createContainer_(html)
107 jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) {
108 if (typeof this.fixturesCache_[url] === 'undefined') {
109 this.loadFixtureIntoCache_(url)
111 return this.fixturesCache_[url]
114 jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
115 var url = this.makeFixtureUrl_(relativeUrl)
116 var request = new XMLHttpRequest()
117 request.open("GET", url + "?" + new Date().getTime(), false)
119 this.fixturesCache_[relativeUrl] = request.responseText
122 jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){
123 return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
126 jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
127 return this[methodName].apply(this, passedArguments)
131 jasmine.JQuery = function() {}
133 jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
134 return jQuery('<div/>').append(html).html()
137 jasmine.JQuery.elementToString = function(element) {
138 var domEl = $(element).get(0)
139 if (domEl == undefined || domEl.cloneNode)
140 return jQuery('<div />').append($(element).clone()).html()
142 return element.toString()
145 jasmine.JQuery.matchersClass = {};
147 !function(namespace) {
154 spyOn: function(selector, eventName) {
155 var handler = function(e) {
156 data.spiedEvents[[selector, eventName]] = e
158 jQuery(selector).bind(eventName, handler)
159 data.handlers.push(handler)
162 wasTriggered: function(selector, eventName) {
163 return !!(data.spiedEvents[[selector, eventName]])
166 wasPrevented: function(selector, eventName) {
167 return data.spiedEvents[[selector, eventName]].isDefaultPrevented()
170 cleanUp: function() {
171 data.spiedEvents = {}
178 var jQueryMatchers = {
179 toHaveClass: function(className) {
180 return this.actual.hasClass(className)
183 toHaveCss: function(css){
184 for (var prop in css){
185 if (this.actual.css(prop) !== css[prop]) return false
190 toBeVisible: function() {
191 return this.actual.is(':visible')
194 toBeHidden: function() {
195 return this.actual.is(':hidden')
198 toBeSelected: function() {
199 return this.actual.is(':selected')
202 toBeChecked: function() {
203 return this.actual.is(':checked')
206 toBeEmpty: function() {
207 return this.actual.is(':empty')
210 toExist: function() {
211 return $(document).find(this.actual).length
214 toHaveAttr: function(attributeName, expectedAttributeValue) {
215 return hasProperty(this.actual.attr(attributeName), expectedAttributeValue)
218 toHaveProp: function(propertyName, expectedPropertyValue) {
219 return hasProperty(this.actual.prop(propertyName), expectedPropertyValue)
222 toHaveId: function(id) {
223 return this.actual.attr('id') == id
226 toHaveHtml: function(html) {
227 return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
230 toContainHtml: function(html){
231 var actualHtml = this.actual.html()
232 var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
233 return (actualHtml.indexOf(expectedHtml) >= 0)
236 toHaveText: function(text) {
237 var trimmedText = $.trim(this.actual.text())
238 if (text && jQuery.isFunction(text.test)) {
239 return text.test(trimmedText)
241 return trimmedText == text
245 toHaveValue: function(value) {
246 return this.actual.val() == value
249 toHaveData: function(key, expectedValue) {
250 return hasProperty(this.actual.data(key), expectedValue)
253 toBe: function(selector) {
254 return this.actual.is(selector)
257 toContain: function(selector) {
258 return this.actual.find(selector).length
261 toBeDisabled: function(selector){
262 return this.actual.is(':disabled')
265 toBeFocused: function(selector) {
266 return this.actual.is(':focus')
269 toHandle: function(event) {
271 var events = this.actual.data('events')
273 if(!events || !event || typeof event !== "string") {
277 var namespaces = event.split(".")
278 var eventType = namespaces.shift()
279 var sortedNamespaces = namespaces.slice(0).sort()
280 var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
282 if(events[eventType] && namespaces.length) {
283 for(var i = 0; i < events[eventType].length; i++) {
284 var namespace = events[eventType][i].namespace
285 if(namespaceRegExp.test(namespace)) {
290 return events[eventType] && events[eventType].length > 0
294 // tests the existence of a specific event binding + handler
295 toHandleWith: function(eventName, eventHandler) {
296 var stack = this.actual.data("events")[eventName]
297 for (var i = 0; i < stack.length; i++) {
298 if (stack[i].handler == eventHandler) return true
304 var hasProperty = function(actualValue, expectedValue) {
305 if (expectedValue === undefined) return actualValue !== undefined
306 return actualValue == expectedValue
309 var bindMatcher = function(methodName) {
310 var builtInMatcher = jasmine.Matchers.prototype[methodName]
312 jasmine.JQuery.matchersClass[methodName] = function() {
314 && (this.actual instanceof jQuery
315 || jasmine.isDomNode(this.actual))) {
316 this.actual = $(this.actual)
317 var result = jQueryMatchers[methodName].apply(this, arguments)
319 if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML")
320 this.actual = jasmine.JQuery.elementToString(this.actual)
324 if (builtInMatcher) {
325 return builtInMatcher.apply(this, arguments)
332 for(var methodName in jQueryMatchers) {
333 bindMatcher(methodName)
337 beforeEach(function() {
338 this.addMatchers(jasmine.JQuery.matchersClass)
340 toHaveBeenTriggeredOn: function(selector) {
341 this.message = function() {
343 "Expected event " + this.actual + " to have been triggered on " + selector,
344 "Expected event " + this.actual + " not to have been triggered on " + selector
347 return jasmine.JQuery.events.wasTriggered($(selector), this.actual)
351 toHaveBeenPreventedOn: function(selector) {
352 this.message = function() {
354 "Expected event " + this.actual + " to have been prevented on " + selector,
355 "Expected event " + this.actual + " not to have been prevented on " + selector
358 return jasmine.JQuery.events.wasPrevented(selector, this.actual)
363 afterEach(function() {
364 jasmine.getFixtures().cleanUp()
365 jasmine.JQuery.events.cleanUp()