support_center 0.1.5

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