jasminerice 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,548 @@
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
+ return jasmine.JQuery.events.spyOn(selector, eventName)
31
+ }
32
+
33
+ var preloadStyleFixtures = function() {
34
+ jasmine.getStyleFixtures().proxyCallTo_('preload', arguments)
35
+ }
36
+
37
+ var loadStyleFixtures = function() {
38
+ jasmine.getStyleFixtures().proxyCallTo_('load', arguments)
39
+ }
40
+
41
+ var appendLoadStyleFixtures = function() {
42
+ jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments)
43
+ }
44
+
45
+ var setStyleFixtures = function(html) {
46
+ jasmine.getStyleFixtures().proxyCallTo_('set', arguments)
47
+ }
48
+
49
+ var appendSetStyleFixtures = function(html) {
50
+ jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments)
51
+ }
52
+
53
+ var loadJSONFixtures = function() {
54
+ return jasmine.getJSONFixtures().proxyCallTo_('load', arguments)
55
+ }
56
+
57
+ var getJSONFixture = function(url) {
58
+ return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url]
59
+ }
60
+
61
+ jasmine.spiedEventsKey = function (selector, eventName) {
62
+ return [$(selector).selector, eventName].toString()
63
+ }
64
+
65
+ jasmine.getFixtures = function() {
66
+ return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
67
+ }
68
+
69
+ jasmine.getStyleFixtures = function() {
70
+ return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures()
71
+ }
72
+
73
+ jasmine.Fixtures = function() {
74
+ this.containerId = 'jasmine-fixtures'
75
+ this.fixturesCache_ = {}
76
+ this.fixturesPath = 'spec/javascripts/fixtures'
77
+ }
78
+
79
+ jasmine.Fixtures.prototype.set = function(html) {
80
+ this.cleanUp()
81
+ this.createContainer_(html)
82
+ }
83
+
84
+ jasmine.Fixtures.prototype.appendSet= function(html) {
85
+ this.addToContainer_(html)
86
+ }
87
+
88
+ jasmine.Fixtures.prototype.preload = function() {
89
+ this.read.apply(this, arguments)
90
+ }
91
+
92
+ jasmine.Fixtures.prototype.load = function() {
93
+ this.cleanUp()
94
+ this.createContainer_(this.read.apply(this, arguments))
95
+ }
96
+
97
+ jasmine.Fixtures.prototype.appendLoad = function() {
98
+ this.addToContainer_(this.read.apply(this, arguments))
99
+ }
100
+
101
+ jasmine.Fixtures.prototype.read = function() {
102
+ var htmlChunks = []
103
+
104
+ var fixtureUrls = arguments
105
+ for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
106
+ htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
107
+ }
108
+
109
+ return htmlChunks.join('')
110
+ }
111
+
112
+ jasmine.Fixtures.prototype.clearCache = function() {
113
+ this.fixturesCache_ = {}
114
+ }
115
+
116
+ jasmine.Fixtures.prototype.cleanUp = function() {
117
+ jQuery('#' + this.containerId).remove()
118
+ }
119
+
120
+ jasmine.Fixtures.prototype.sandbox = function(attributes) {
121
+ var attributesToSet = attributes || {}
122
+ return jQuery('<div id="sandbox" />').attr(attributesToSet)
123
+ }
124
+
125
+ jasmine.Fixtures.prototype.createContainer_ = function(html) {
126
+ var container
127
+ if(html instanceof jQuery) {
128
+ container = jQuery('<div id="' + this.containerId + '" />')
129
+ container.html(html)
130
+ } else {
131
+ container = '<div id="' + this.containerId + '">' + html + '</div>'
132
+ }
133
+ jQuery('body').append(container)
134
+ }
135
+
136
+ jasmine.Fixtures.prototype.addToContainer_ = function(html){
137
+ var container = jQuery('body').find('#'+this.containerId).append(html)
138
+ if(!container.length){
139
+ this.createContainer_(html)
140
+ }
141
+ }
142
+
143
+ jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) {
144
+ if (typeof this.fixturesCache_[url] === 'undefined') {
145
+ this.loadFixtureIntoCache_(url)
146
+ }
147
+ return this.fixturesCache_[url]
148
+ }
149
+
150
+ jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
151
+ var url = this.makeFixtureUrl_(relativeUrl)
152
+ var request = jQuery.ajax({
153
+ type: "GET",
154
+ url: url + "?" + new Date().getTime(),
155
+ async: false
156
+ })
157
+ this.fixturesCache_[relativeUrl] = request.responseText
158
+ }
159
+
160
+ jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){
161
+ return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
162
+ }
163
+
164
+ jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
165
+ return this[methodName].apply(this, passedArguments)
166
+ }
167
+
168
+
169
+ jasmine.StyleFixtures = function() {
170
+ this.fixturesCache_ = {}
171
+ this.fixturesNodes_ = []
172
+ this.fixturesPath = 'spec/javascripts/fixtures'
173
+ }
174
+
175
+ jasmine.StyleFixtures.prototype.set = function(css) {
176
+ this.cleanUp()
177
+ this.createStyle_(css)
178
+ }
179
+
180
+ jasmine.StyleFixtures.prototype.appendSet = function(css) {
181
+ this.createStyle_(css)
182
+ }
183
+
184
+ jasmine.StyleFixtures.prototype.preload = function() {
185
+ this.read_.apply(this, arguments)
186
+ }
187
+
188
+ jasmine.StyleFixtures.prototype.load = function() {
189
+ this.cleanUp()
190
+ this.createStyle_(this.read_.apply(this, arguments))
191
+ }
192
+
193
+ jasmine.StyleFixtures.prototype.appendLoad = function() {
194
+ this.createStyle_(this.read_.apply(this, arguments))
195
+ }
196
+
197
+ jasmine.StyleFixtures.prototype.cleanUp = function() {
198
+ while(this.fixturesNodes_.length) {
199
+ this.fixturesNodes_.pop().remove()
200
+ }
201
+ }
202
+
203
+ jasmine.StyleFixtures.prototype.createStyle_ = function(html) {
204
+ var style = jQuery('<style></style>').text(html)
205
+
206
+ this.fixturesNodes_.push(style)
207
+
208
+ jQuery('head').append(style)
209
+ }
210
+
211
+ jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache
212
+
213
+ jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read
214
+
215
+ jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_
216
+
217
+ jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_
218
+
219
+ jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_
220
+
221
+ jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_
222
+
223
+ /** jasmine json fixtures */
224
+
225
+ jasmine.getJSONFixtures = function() {
226
+ return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures()
227
+ }
228
+
229
+ jasmine.JSONFixtures = function() {
230
+ this.fixturesCache_ = {}
231
+ this.fixturesPath = 'spec/javascripts/fixtures/json'
232
+ }
233
+
234
+ jasmine.JSONFixtures.prototype.load = function() {
235
+ this.read.apply(this, arguments)
236
+ return this.fixturesCache_
237
+ }
238
+
239
+ jasmine.JSONFixtures.prototype.read = function() {
240
+ var fixtureUrls = arguments
241
+ for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
242
+ this.getFixtureData_(fixtureUrls[urlIndex])
243
+ }
244
+ return this.fixturesCache_
245
+ }
246
+
247
+ jasmine.JSONFixtures.prototype.clearCache = function() {
248
+ this.fixturesCache_ = {}
249
+ }
250
+
251
+ jasmine.JSONFixtures.prototype.getFixtureData_ = function(url) {
252
+ this.loadFixtureIntoCache_(url)
253
+ return this.fixturesCache_[url]
254
+ }
255
+
256
+ jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
257
+ var self = this
258
+ var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
259
+ jQuery.ajax({
260
+ async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
261
+ cache: false,
262
+ dataType: 'json',
263
+ url: url,
264
+ success: function(data) {
265
+ console.log("Loading data into " + relativeUrl)
266
+ self.fixturesCache_[relativeUrl] = data
267
+ },
268
+ error: function(jqXHR, status, errorThrown) {
269
+ throw Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
270
+ }
271
+ })
272
+ }
273
+
274
+ jasmine.JSONFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
275
+ return this[methodName].apply(this, passedArguments)
276
+ }
277
+
278
+ jasmine.JQuery = function() {}
279
+
280
+ jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
281
+ return jQuery('<div/>').append(html).html()
282
+ }
283
+
284
+ jasmine.JQuery.elementToString = function(element) {
285
+ var domEl = $(element).get(0)
286
+ if (domEl == undefined || domEl.cloneNode)
287
+ return jQuery('<div />').append($(element).clone()).html()
288
+ else
289
+ return element.toString()
290
+ }
291
+
292
+ jasmine.JQuery.matchersClass = {}
293
+
294
+ !function(namespace) {
295
+ var data = {
296
+ spiedEvents: {},
297
+ handlers: []
298
+ }
299
+
300
+ namespace.events = {
301
+ spyOn: function(selector, eventName) {
302
+ var handler = function(e) {
303
+ data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = e
304
+ }
305
+ jQuery(selector).bind(eventName, handler)
306
+ data.handlers.push(handler)
307
+ return {
308
+ selector: selector,
309
+ eventName: eventName,
310
+ handler: handler,
311
+ reset: function(){
312
+ delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
313
+ }
314
+ }
315
+ },
316
+
317
+ wasTriggered: function(selector, eventName) {
318
+ return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)])
319
+ },
320
+
321
+ wasPrevented: function(selector, eventName) {
322
+ return data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)].isDefaultPrevented()
323
+ },
324
+
325
+ cleanUp: function() {
326
+ data.spiedEvents = {}
327
+ data.handlers = []
328
+ }
329
+ }
330
+ }(jasmine.JQuery)
331
+
332
+ !function(){
333
+ var jQueryMatchers = {
334
+ toHaveClass: function(className) {
335
+ return this.actual.hasClass(className)
336
+ },
337
+
338
+ toHaveCss: function(css){
339
+ for (var prop in css){
340
+ if (this.actual.css(prop) !== css[prop]) return false
341
+ }
342
+ return true
343
+ },
344
+
345
+ toBeVisible: function() {
346
+ return this.actual.is(':visible')
347
+ },
348
+
349
+ toBeHidden: function() {
350
+ return this.actual.is(':hidden')
351
+ },
352
+
353
+ toBeSelected: function() {
354
+ return this.actual.is(':selected')
355
+ },
356
+
357
+ toBeChecked: function() {
358
+ return this.actual.is(':checked')
359
+ },
360
+
361
+ toBeEmpty: function() {
362
+ return this.actual.is(':empty')
363
+ },
364
+
365
+ toExist: function() {
366
+ return $(document).find(this.actual).length
367
+ },
368
+
369
+ toHaveAttr: function(attributeName, expectedAttributeValue) {
370
+ return hasProperty(this.actual.attr(attributeName), expectedAttributeValue)
371
+ },
372
+
373
+ toHaveProp: function(propertyName, expectedPropertyValue) {
374
+ return hasProperty(this.actual.prop(propertyName), expectedPropertyValue)
375
+ },
376
+
377
+ toHaveId: function(id) {
378
+ return this.actual.attr('id') == id
379
+ },
380
+
381
+ toHaveHtml: function(html) {
382
+ return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
383
+ },
384
+
385
+ toContainHtml: function(html){
386
+ var actualHtml = this.actual.html()
387
+ var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
388
+ return (actualHtml.indexOf(expectedHtml) >= 0)
389
+ },
390
+
391
+ toHaveText: function(text) {
392
+ var trimmedText = $.trim(this.actual.text())
393
+ if (text && jQuery.isFunction(text.test)) {
394
+ return text.test(trimmedText)
395
+ } else {
396
+ return trimmedText == text
397
+ }
398
+ },
399
+
400
+ toHaveValue: function(value) {
401
+ return this.actual.val() == value
402
+ },
403
+
404
+ toHaveData: function(key, expectedValue) {
405
+ return hasProperty(this.actual.data(key), expectedValue)
406
+ },
407
+
408
+ toBe: function(selector) {
409
+ return this.actual.is(selector)
410
+ },
411
+
412
+ toContain: function(selector) {
413
+ return this.actual.find(selector).length
414
+ },
415
+
416
+ toBeDisabled: function(selector){
417
+ return this.actual.is(':disabled')
418
+ },
419
+
420
+ toBeFocused: function(selector) {
421
+ return this.actual.is(':focus')
422
+ },
423
+
424
+ toHandle: function(event) {
425
+
426
+ var events = $._data(this.actual.get(0), "events")
427
+
428
+ if(!events || !event || typeof event !== "string") {
429
+ return false
430
+ }
431
+
432
+ var namespaces = event.split(".")
433
+ var eventType = namespaces.shift()
434
+ var sortedNamespaces = namespaces.slice(0).sort()
435
+ var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
436
+
437
+ if(events[eventType] && namespaces.length) {
438
+ for(var i = 0; i < events[eventType].length; i++) {
439
+ var namespace = events[eventType][i].namespace
440
+ if(namespaceRegExp.test(namespace)) {
441
+ return true
442
+ }
443
+ }
444
+ } else {
445
+ return events[eventType] && events[eventType].length > 0
446
+ }
447
+ },
448
+
449
+ // tests the existence of a specific event binding + handler
450
+ toHandleWith: function(eventName, eventHandler) {
451
+ var stack = $._data(this.actual.get(0), "events")[eventName]
452
+ for (var i = 0; i < stack.length; i++) {
453
+ if (stack[i].handler == eventHandler) return true
454
+ }
455
+ return false
456
+ }
457
+ }
458
+
459
+ var hasProperty = function(actualValue, expectedValue) {
460
+ if (expectedValue === undefined) return actualValue !== undefined
461
+ return actualValue == expectedValue
462
+ }
463
+
464
+ var bindMatcher = function(methodName) {
465
+ var builtInMatcher = jasmine.Matchers.prototype[methodName]
466
+
467
+ jasmine.JQuery.matchersClass[methodName] = function() {
468
+ if (this.actual
469
+ && (this.actual instanceof jQuery
470
+ || jasmine.isDomNode(this.actual))) {
471
+ this.actual = $(this.actual)
472
+ var result = jQueryMatchers[methodName].apply(this, arguments)
473
+ var element
474
+ if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML")
475
+ this.actual = jasmine.JQuery.elementToString(this.actual)
476
+ return result
477
+ }
478
+
479
+ if (builtInMatcher) {
480
+ return builtInMatcher.apply(this, arguments)
481
+ }
482
+
483
+ return false
484
+ }
485
+ }
486
+
487
+ for(var methodName in jQueryMatchers) {
488
+ bindMatcher(methodName)
489
+ }
490
+ }()
491
+
492
+ beforeEach(function() {
493
+ this.addMatchers(jasmine.JQuery.matchersClass)
494
+ this.addMatchers({
495
+ toHaveBeenTriggeredOn: function(selector) {
496
+ this.message = function() {
497
+ return [
498
+ "Expected event " + this.actual + " to have been triggered on " + selector,
499
+ "Expected event " + this.actual + " not to have been triggered on " + selector
500
+ ]
501
+ }
502
+ return jasmine.JQuery.events.wasTriggered(selector, this.actual)
503
+ }
504
+ })
505
+ this.addMatchers({
506
+ toHaveBeenTriggered: function(){
507
+ var eventName = this.actual.eventName,
508
+ selector = this.actual.selector
509
+ this.message = function() {
510
+ return [
511
+ "Expected event " + eventName + " to have been triggered on " + selector,
512
+ "Expected event " + eventName + " not to have been triggered on " + selector
513
+ ]
514
+ }
515
+ return jasmine.JQuery.events.wasTriggered(selector, eventName)
516
+ }
517
+ })
518
+ this.addMatchers({
519
+ toHaveBeenPreventedOn: function(selector) {
520
+ this.message = function() {
521
+ return [
522
+ "Expected event " + this.actual + " to have been prevented on " + selector,
523
+ "Expected event " + this.actual + " not to have been prevented on " + selector
524
+ ]
525
+ }
526
+ return jasmine.JQuery.events.wasPrevented(selector, this.actual)
527
+ }
528
+ })
529
+ this.addMatchers({
530
+ toHaveBeenPrevented: function() {
531
+ var eventName = this.actual.eventName,
532
+ selector = this.actual.selector
533
+ this.message = function() {
534
+ return [
535
+ "Expected event " + eventName + " to have been prevented on " + selector,
536
+ "Expected event " + eventName + " not to have been prevented on " + selector
537
+ ]
538
+ }
539
+ return jasmine.JQuery.events.wasPrevented(selector, eventName)
540
+ }
541
+ })
542
+ })
543
+
544
+ afterEach(function() {
545
+ jasmine.getFixtures().cleanUp()
546
+ jasmine.getStyleFixtures().cleanUp()
547
+ jasmine.JQuery.events.cleanUp()
548
+ })