10397ce46231b5e62b9171fb8e158476db2c5abb
[yaffs-website] / jasmine / jasmine-jquery-1.3.1.js
1 var readFixtures = function() {
2   return jasmine.getFixtures().proxyCallTo_('read', arguments)
3 }
4
5 var preloadFixtures = function() {
6   jasmine.getFixtures().proxyCallTo_('preload', arguments)
7 }
8
9 var loadFixtures = function() {
10   jasmine.getFixtures().proxyCallTo_('load', arguments)
11 }
12
13 var appendLoadFixtures = function() {
14   jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
15 }
16
17 var setFixtures = function(html) {
18   jasmine.getFixtures().proxyCallTo_('set', arguments)
19 }
20
21 var appendSetFixtures = function() {
22   jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
23 }
24
25 var sandbox = function(attributes) {
26   return jasmine.getFixtures().sandbox(attributes)
27 }
28
29 var spyOnEvent = function(selector, eventName) {
30   jasmine.JQuery.events.spyOn(selector, eventName)
31 }
32
33 jasmine.getFixtures = function() {
34   return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
35 }
36
37 jasmine.Fixtures = function() {
38   this.containerId = 'jasmine-fixtures'
39   this.fixturesCache_ = {}
40   this.fixturesPath = 'spec/javascripts/fixtures'
41 }
42
43 jasmine.Fixtures.prototype.set = function(html) {
44   this.cleanUp()
45   this.createContainer_(html)
46 }
47
48 jasmine.Fixtures.prototype.appendSet= function(html) {
49   this.addToContainer_(html)
50 }
51
52 jasmine.Fixtures.prototype.preload = function() {
53   this.read.apply(this, arguments)
54 }
55
56 jasmine.Fixtures.prototype.load = function() {
57   this.cleanUp()
58   this.createContainer_(this.read.apply(this, arguments))
59 }
60
61 jasmine.Fixtures.prototype.appendLoad = function() {
62   this.addToContainer_(this.read.apply(this, arguments))
63 }
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   return htmlChunks.join('')
74 }
75
76 jasmine.Fixtures.prototype.clearCache = function() {
77   this.fixturesCache_ = {}
78 }
79
80 jasmine.Fixtures.prototype.cleanUp = function() {
81   jQuery('#' + this.containerId).remove()
82 }
83
84 jasmine.Fixtures.prototype.sandbox = function(attributes) {
85   var attributesToSet = attributes || {}
86   return jQuery('<div id="sandbox" />').attr(attributesToSet)
87 }
88
89 jasmine.Fixtures.prototype.createContainer_ = function(html) {
90   var container
91   if(html instanceof jQuery) {
92     container = jQuery('<div id="' + this.containerId + '" />')
93     container.html(html)
94   } else {
95     container = '<div id="' + this.containerId + '">' + html + '</div>'
96   }
97   jQuery('body').append(container)
98 }
99
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)
104   }
105 }
106
107 jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) {
108   if (typeof this.fixturesCache_[url] === 'undefined') {
109     this.loadFixtureIntoCache_(url)
110   }
111   return this.fixturesCache_[url]
112 }
113
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)
118   request.send(null)
119   this.fixturesCache_[relativeUrl] = request.responseText
120 }
121
122 jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){
123   return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
124 }
125
126 jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
127   return this[methodName].apply(this, passedArguments)
128 }
129
130
131 jasmine.JQuery = function() {}
132
133 jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
134   return jQuery('<div/>').append(html).html()
135 }
136
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()
141   else
142     return element.toString()
143 }
144
145 jasmine.JQuery.matchersClass = {};
146
147 !function(namespace) {
148   var data = {
149     spiedEvents: {},
150     handlers:    []
151   }
152
153   namespace.events = {
154     spyOn: function(selector, eventName) {
155       var handler = function(e) {
156         data.spiedEvents[[selector, eventName]] = e
157       }
158       jQuery(selector).bind(eventName, handler)
159       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       data.spiedEvents = {}
172       data.handlers    = []
173     }
174   }
175 }(jasmine.JQuery)
176
177 !function(){
178   var jQueryMatchers = {
179     toHaveClass: function(className) {
180       return this.actual.hasClass(className)
181     },
182
183     toHaveCss: function(css){
184       for (var prop in css){
185         if (this.actual.css(prop) !== css[prop]) return false
186       }
187       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       return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
228     },
229
230     toContainHtml: function(html){
231       var actualHtml = this.actual.html()
232       var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
233       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     toHandle: function(event) {
270
271       var events = this.actual.data('events')
272
273       if(!events || !event || typeof event !== "string") {
274         return false
275       }
276
277       var namespaces = event.split(".")
278       var eventType = namespaces.shift()
279       var sortedNamespaces = namespaces.slice(0).sort()
280       var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
281
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)) {
286             return true
287           }
288         }
289       } else {
290         return events[eventType] && events[eventType].length > 0
291       }
292     },
293
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
299       }
300       return false
301     }
302   }
303
304   var hasProperty = function(actualValue, expectedValue) {
305     if (expectedValue === undefined) return actualValue !== undefined
306     return actualValue == expectedValue
307   }
308
309   var bindMatcher = function(methodName) {
310     var builtInMatcher = jasmine.Matchers.prototype[methodName]
311
312     jasmine.JQuery.matchersClass[methodName] = function() {
313       if (this.actual
314         && (this.actual instanceof jQuery
315           || jasmine.isDomNode(this.actual))) {
316             this.actual = $(this.actual)
317             var result = jQueryMatchers[methodName].apply(this, arguments)
318             var element;        
319             if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML") 
320               this.actual = jasmine.JQuery.elementToString(this.actual)
321             return result
322           }
323
324           if (builtInMatcher) {
325             return builtInMatcher.apply(this, arguments)
326           }
327
328           return false
329     }
330   }
331
332   for(var methodName in jQueryMatchers) {
333     bindMatcher(methodName)
334   }
335 }()
336
337 beforeEach(function() {
338   this.addMatchers(jasmine.JQuery.matchersClass)
339   this.addMatchers({
340     toHaveBeenTriggeredOn: function(selector) {
341       this.message = function() {
342         return [
343           "Expected event " + this.actual + " to have been triggered on " + selector,
344           "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   this.addMatchers({
351     toHaveBeenPreventedOn: function(selector) {
352       this.message = function() {
353         return [
354           "Expected event " + this.actual + " to have been prevented on " + selector,
355           "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   jasmine.getFixtures().cleanUp()
365   jasmine.JQuery.events.cleanUp()
366 })