angular-gem 1.2.23 → 1.2.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2177 @@
1
+ /**
2
+ * @license AngularJS v1.2.24
3
+ * (c) 2010-2014 Google, Inc. http://angularjs.org
4
+ * License: MIT
5
+ */
6
+ (function(window, angular, undefined) {
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * @ngdoc object
12
+ * @name angular.mock
13
+ * @description
14
+ *
15
+ * Namespace from 'angular-mocks.js' which contains testing related code.
16
+ */
17
+ angular.mock = {};
18
+
19
+ /**
20
+ * ! This is a private undocumented service !
21
+ *
22
+ * @name $browser
23
+ *
24
+ * @description
25
+ * This service is a mock implementation of {@link ng.$browser}. It provides fake
26
+ * implementation for commonly used browser apis that are hard to test, e.g. setTimeout, xhr,
27
+ * cookies, etc...
28
+ *
29
+ * The api of this service is the same as that of the real {@link ng.$browser $browser}, except
30
+ * that there are several helper methods available which can be used in tests.
31
+ */
32
+ angular.mock.$BrowserProvider = function() {
33
+ this.$get = function() {
34
+ return new angular.mock.$Browser();
35
+ };
36
+ };
37
+
38
+ angular.mock.$Browser = function() {
39
+ var self = this;
40
+
41
+ this.isMock = true;
42
+ self.$$url = "http://server/";
43
+ self.$$lastUrl = self.$$url; // used by url polling fn
44
+ self.pollFns = [];
45
+
46
+ // TODO(vojta): remove this temporary api
47
+ self.$$completeOutstandingRequest = angular.noop;
48
+ self.$$incOutstandingRequestCount = angular.noop;
49
+
50
+
51
+ // register url polling fn
52
+
53
+ self.onUrlChange = function(listener) {
54
+ self.pollFns.push(
55
+ function() {
56
+ if (self.$$lastUrl != self.$$url) {
57
+ self.$$lastUrl = self.$$url;
58
+ listener(self.$$url);
59
+ }
60
+ }
61
+ );
62
+
63
+ return listener;
64
+ };
65
+
66
+ self.$$checkUrlChange = angular.noop;
67
+
68
+ self.cookieHash = {};
69
+ self.lastCookieHash = {};
70
+ self.deferredFns = [];
71
+ self.deferredNextId = 0;
72
+
73
+ self.defer = function(fn, delay) {
74
+ delay = delay || 0;
75
+ self.deferredFns.push({time:(self.defer.now + delay), fn:fn, id: self.deferredNextId});
76
+ self.deferredFns.sort(function(a,b){ return a.time - b.time;});
77
+ return self.deferredNextId++;
78
+ };
79
+
80
+
81
+ /**
82
+ * @name $browser#defer.now
83
+ *
84
+ * @description
85
+ * Current milliseconds mock time.
86
+ */
87
+ self.defer.now = 0;
88
+
89
+
90
+ self.defer.cancel = function(deferId) {
91
+ var fnIndex;
92
+
93
+ angular.forEach(self.deferredFns, function(fn, index) {
94
+ if (fn.id === deferId) fnIndex = index;
95
+ });
96
+
97
+ if (fnIndex !== undefined) {
98
+ self.deferredFns.splice(fnIndex, 1);
99
+ return true;
100
+ }
101
+
102
+ return false;
103
+ };
104
+
105
+
106
+ /**
107
+ * @name $browser#defer.flush
108
+ *
109
+ * @description
110
+ * Flushes all pending requests and executes the defer callbacks.
111
+ *
112
+ * @param {number=} number of milliseconds to flush. See {@link #defer.now}
113
+ */
114
+ self.defer.flush = function(delay) {
115
+ if (angular.isDefined(delay)) {
116
+ self.defer.now += delay;
117
+ } else {
118
+ if (self.deferredFns.length) {
119
+ self.defer.now = self.deferredFns[self.deferredFns.length-1].time;
120
+ } else {
121
+ throw new Error('No deferred tasks to be flushed');
122
+ }
123
+ }
124
+
125
+ while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
126
+ self.deferredFns.shift().fn();
127
+ }
128
+ };
129
+
130
+ self.$$baseHref = '';
131
+ self.baseHref = function() {
132
+ return this.$$baseHref;
133
+ };
134
+ };
135
+ angular.mock.$Browser.prototype = {
136
+
137
+ /**
138
+ * @name $browser#poll
139
+ *
140
+ * @description
141
+ * run all fns in pollFns
142
+ */
143
+ poll: function poll() {
144
+ angular.forEach(this.pollFns, function(pollFn){
145
+ pollFn();
146
+ });
147
+ },
148
+
149
+ addPollFn: function(pollFn) {
150
+ this.pollFns.push(pollFn);
151
+ return pollFn;
152
+ },
153
+
154
+ url: function(url, replace) {
155
+ if (url) {
156
+ this.$$url = url;
157
+ return this;
158
+ }
159
+
160
+ return this.$$url;
161
+ },
162
+
163
+ cookies: function(name, value) {
164
+ if (name) {
165
+ if (angular.isUndefined(value)) {
166
+ delete this.cookieHash[name];
167
+ } else {
168
+ if (angular.isString(value) && //strings only
169
+ value.length <= 4096) { //strict cookie storage limits
170
+ this.cookieHash[name] = value;
171
+ }
172
+ }
173
+ } else {
174
+ if (!angular.equals(this.cookieHash, this.lastCookieHash)) {
175
+ this.lastCookieHash = angular.copy(this.cookieHash);
176
+ this.cookieHash = angular.copy(this.cookieHash);
177
+ }
178
+ return this.cookieHash;
179
+ }
180
+ },
181
+
182
+ notifyWhenNoOutstandingRequests: function(fn) {
183
+ fn();
184
+ }
185
+ };
186
+
187
+
188
+ /**
189
+ * @ngdoc provider
190
+ * @name $exceptionHandlerProvider
191
+ *
192
+ * @description
193
+ * Configures the mock implementation of {@link ng.$exceptionHandler} to rethrow or to log errors
194
+ * passed into the `$exceptionHandler`.
195
+ */
196
+
197
+ /**
198
+ * @ngdoc service
199
+ * @name $exceptionHandler
200
+ *
201
+ * @description
202
+ * Mock implementation of {@link ng.$exceptionHandler} that rethrows or logs errors passed
203
+ * into it. See {@link ngMock.$exceptionHandlerProvider $exceptionHandlerProvider} for configuration
204
+ * information.
205
+ *
206
+ *
207
+ * ```js
208
+ * describe('$exceptionHandlerProvider', function() {
209
+ *
210
+ * it('should capture log messages and exceptions', function() {
211
+ *
212
+ * module(function($exceptionHandlerProvider) {
213
+ * $exceptionHandlerProvider.mode('log');
214
+ * });
215
+ *
216
+ * inject(function($log, $exceptionHandler, $timeout) {
217
+ * $timeout(function() { $log.log(1); });
218
+ * $timeout(function() { $log.log(2); throw 'banana peel'; });
219
+ * $timeout(function() { $log.log(3); });
220
+ * expect($exceptionHandler.errors).toEqual([]);
221
+ * expect($log.assertEmpty());
222
+ * $timeout.flush();
223
+ * expect($exceptionHandler.errors).toEqual(['banana peel']);
224
+ * expect($log.log.logs).toEqual([[1], [2], [3]]);
225
+ * });
226
+ * });
227
+ * });
228
+ * ```
229
+ */
230
+
231
+ angular.mock.$ExceptionHandlerProvider = function() {
232
+ var handler;
233
+
234
+ /**
235
+ * @ngdoc method
236
+ * @name $exceptionHandlerProvider#mode
237
+ *
238
+ * @description
239
+ * Sets the logging mode.
240
+ *
241
+ * @param {string} mode Mode of operation, defaults to `rethrow`.
242
+ *
243
+ * - `rethrow`: If any errors are passed into the handler in tests, it typically
244
+ * means that there is a bug in the application or test, so this mock will
245
+ * make these tests fail.
246
+ * - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
247
+ * mode stores an array of errors in `$exceptionHandler.errors`, to allow later
248
+ * assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
249
+ * {@link ngMock.$log#reset reset()}
250
+ */
251
+ this.mode = function(mode) {
252
+ switch(mode) {
253
+ case 'rethrow':
254
+ handler = function(e) {
255
+ throw e;
256
+ };
257
+ break;
258
+ case 'log':
259
+ var errors = [];
260
+
261
+ handler = function(e) {
262
+ if (arguments.length == 1) {
263
+ errors.push(e);
264
+ } else {
265
+ errors.push([].slice.call(arguments, 0));
266
+ }
267
+ };
268
+
269
+ handler.errors = errors;
270
+ break;
271
+ default:
272
+ throw new Error("Unknown mode '" + mode + "', only 'log'/'rethrow' modes are allowed!");
273
+ }
274
+ };
275
+
276
+ this.$get = function() {
277
+ return handler;
278
+ };
279
+
280
+ this.mode('rethrow');
281
+ };
282
+
283
+
284
+ /**
285
+ * @ngdoc service
286
+ * @name $log
287
+ *
288
+ * @description
289
+ * Mock implementation of {@link ng.$log} that gathers all logged messages in arrays
290
+ * (one array per logging level). These arrays are exposed as `logs` property of each of the
291
+ * level-specific log function, e.g. for level `error` the array is exposed as `$log.error.logs`.
292
+ *
293
+ */
294
+ angular.mock.$LogProvider = function() {
295
+ var debug = true;
296
+
297
+ function concat(array1, array2, index) {
298
+ return array1.concat(Array.prototype.slice.call(array2, index));
299
+ }
300
+
301
+ this.debugEnabled = function(flag) {
302
+ if (angular.isDefined(flag)) {
303
+ debug = flag;
304
+ return this;
305
+ } else {
306
+ return debug;
307
+ }
308
+ };
309
+
310
+ this.$get = function () {
311
+ var $log = {
312
+ log: function() { $log.log.logs.push(concat([], arguments, 0)); },
313
+ warn: function() { $log.warn.logs.push(concat([], arguments, 0)); },
314
+ info: function() { $log.info.logs.push(concat([], arguments, 0)); },
315
+ error: function() { $log.error.logs.push(concat([], arguments, 0)); },
316
+ debug: function() {
317
+ if (debug) {
318
+ $log.debug.logs.push(concat([], arguments, 0));
319
+ }
320
+ }
321
+ };
322
+
323
+ /**
324
+ * @ngdoc method
325
+ * @name $log#reset
326
+ *
327
+ * @description
328
+ * Reset all of the logging arrays to empty.
329
+ */
330
+ $log.reset = function () {
331
+ /**
332
+ * @ngdoc property
333
+ * @name $log#log.logs
334
+ *
335
+ * @description
336
+ * Array of messages logged using {@link ngMock.$log#log}.
337
+ *
338
+ * @example
339
+ * ```js
340
+ * $log.log('Some Log');
341
+ * var first = $log.log.logs.unshift();
342
+ * ```
343
+ */
344
+ $log.log.logs = [];
345
+ /**
346
+ * @ngdoc property
347
+ * @name $log#info.logs
348
+ *
349
+ * @description
350
+ * Array of messages logged using {@link ngMock.$log#info}.
351
+ *
352
+ * @example
353
+ * ```js
354
+ * $log.info('Some Info');
355
+ * var first = $log.info.logs.unshift();
356
+ * ```
357
+ */
358
+ $log.info.logs = [];
359
+ /**
360
+ * @ngdoc property
361
+ * @name $log#warn.logs
362
+ *
363
+ * @description
364
+ * Array of messages logged using {@link ngMock.$log#warn}.
365
+ *
366
+ * @example
367
+ * ```js
368
+ * $log.warn('Some Warning');
369
+ * var first = $log.warn.logs.unshift();
370
+ * ```
371
+ */
372
+ $log.warn.logs = [];
373
+ /**
374
+ * @ngdoc property
375
+ * @name $log#error.logs
376
+ *
377
+ * @description
378
+ * Array of messages logged using {@link ngMock.$log#error}.
379
+ *
380
+ * @example
381
+ * ```js
382
+ * $log.error('Some Error');
383
+ * var first = $log.error.logs.unshift();
384
+ * ```
385
+ */
386
+ $log.error.logs = [];
387
+ /**
388
+ * @ngdoc property
389
+ * @name $log#debug.logs
390
+ *
391
+ * @description
392
+ * Array of messages logged using {@link ngMock.$log#debug}.
393
+ *
394
+ * @example
395
+ * ```js
396
+ * $log.debug('Some Error');
397
+ * var first = $log.debug.logs.unshift();
398
+ * ```
399
+ */
400
+ $log.debug.logs = [];
401
+ };
402
+
403
+ /**
404
+ * @ngdoc method
405
+ * @name $log#assertEmpty
406
+ *
407
+ * @description
408
+ * Assert that the all of the logging methods have no logged messages. If messages present, an
409
+ * exception is thrown.
410
+ */
411
+ $log.assertEmpty = function() {
412
+ var errors = [];
413
+ angular.forEach(['error', 'warn', 'info', 'log', 'debug'], function(logLevel) {
414
+ angular.forEach($log[logLevel].logs, function(log) {
415
+ angular.forEach(log, function (logItem) {
416
+ errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' +
417
+ (logItem.stack || ''));
418
+ });
419
+ });
420
+ });
421
+ if (errors.length) {
422
+ errors.unshift("Expected $log to be empty! Either a message was logged unexpectedly, or "+
423
+ "an expected log message was not checked and removed:");
424
+ errors.push('');
425
+ throw new Error(errors.join('\n---------\n'));
426
+ }
427
+ };
428
+
429
+ $log.reset();
430
+ return $log;
431
+ };
432
+ };
433
+
434
+
435
+ /**
436
+ * @ngdoc service
437
+ * @name $interval
438
+ *
439
+ * @description
440
+ * Mock implementation of the $interval service.
441
+ *
442
+ * Use {@link ngMock.$interval#flush `$interval.flush(millis)`} to
443
+ * move forward by `millis` milliseconds and trigger any functions scheduled to run in that
444
+ * time.
445
+ *
446
+ * @param {function()} fn A function that should be called repeatedly.
447
+ * @param {number} delay Number of milliseconds between each function call.
448
+ * @param {number=} [count=0] Number of times to repeat. If not set, or 0, will repeat
449
+ * indefinitely.
450
+ * @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
451
+ * will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
452
+ * @returns {promise} A promise which will be notified on each iteration.
453
+ */
454
+ angular.mock.$IntervalProvider = function() {
455
+ this.$get = ['$rootScope', '$q',
456
+ function($rootScope, $q) {
457
+ var repeatFns = [],
458
+ nextRepeatId = 0,
459
+ now = 0;
460
+
461
+ var $interval = function(fn, delay, count, invokeApply) {
462
+ var deferred = $q.defer(),
463
+ promise = deferred.promise,
464
+ iteration = 0,
465
+ skipApply = (angular.isDefined(invokeApply) && !invokeApply);
466
+
467
+ count = (angular.isDefined(count)) ? count : 0;
468
+ promise.then(null, null, fn);
469
+
470
+ promise.$$intervalId = nextRepeatId;
471
+
472
+ function tick() {
473
+ deferred.notify(iteration++);
474
+
475
+ if (count > 0 && iteration >= count) {
476
+ var fnIndex;
477
+ deferred.resolve(iteration);
478
+
479
+ angular.forEach(repeatFns, function(fn, index) {
480
+ if (fn.id === promise.$$intervalId) fnIndex = index;
481
+ });
482
+
483
+ if (fnIndex !== undefined) {
484
+ repeatFns.splice(fnIndex, 1);
485
+ }
486
+ }
487
+
488
+ if (!skipApply) $rootScope.$apply();
489
+ }
490
+
491
+ repeatFns.push({
492
+ nextTime:(now + delay),
493
+ delay: delay,
494
+ fn: tick,
495
+ id: nextRepeatId,
496
+ deferred: deferred
497
+ });
498
+ repeatFns.sort(function(a,b){ return a.nextTime - b.nextTime;});
499
+
500
+ nextRepeatId++;
501
+ return promise;
502
+ };
503
+ /**
504
+ * @ngdoc method
505
+ * @name $interval#cancel
506
+ *
507
+ * @description
508
+ * Cancels a task associated with the `promise`.
509
+ *
510
+ * @param {promise} promise A promise from calling the `$interval` function.
511
+ * @returns {boolean} Returns `true` if the task was successfully cancelled.
512
+ */
513
+ $interval.cancel = function(promise) {
514
+ if(!promise) return false;
515
+ var fnIndex;
516
+
517
+ angular.forEach(repeatFns, function(fn, index) {
518
+ if (fn.id === promise.$$intervalId) fnIndex = index;
519
+ });
520
+
521
+ if (fnIndex !== undefined) {
522
+ repeatFns[fnIndex].deferred.reject('canceled');
523
+ repeatFns.splice(fnIndex, 1);
524
+ return true;
525
+ }
526
+
527
+ return false;
528
+ };
529
+
530
+ /**
531
+ * @ngdoc method
532
+ * @name $interval#flush
533
+ * @description
534
+ *
535
+ * Runs interval tasks scheduled to be run in the next `millis` milliseconds.
536
+ *
537
+ * @param {number=} millis maximum timeout amount to flush up until.
538
+ *
539
+ * @return {number} The amount of time moved forward.
540
+ */
541
+ $interval.flush = function(millis) {
542
+ now += millis;
543
+ while (repeatFns.length && repeatFns[0].nextTime <= now) {
544
+ var task = repeatFns[0];
545
+ task.fn();
546
+ task.nextTime += task.delay;
547
+ repeatFns.sort(function(a,b){ return a.nextTime - b.nextTime;});
548
+ }
549
+ return millis;
550
+ };
551
+
552
+ return $interval;
553
+ }];
554
+ };
555
+
556
+
557
+ /* jshint -W101 */
558
+ /* The R_ISO8061_STR regex is never going to fit into the 100 char limit!
559
+ * This directive should go inside the anonymous function but a bug in JSHint means that it would
560
+ * not be enacted early enough to prevent the warning.
561
+ */
562
+ var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
563
+
564
+ function jsonStringToDate(string) {
565
+ var match;
566
+ if (match = string.match(R_ISO8061_STR)) {
567
+ var date = new Date(0),
568
+ tzHour = 0,
569
+ tzMin = 0;
570
+ if (match[9]) {
571
+ tzHour = int(match[9] + match[10]);
572
+ tzMin = int(match[9] + match[11]);
573
+ }
574
+ date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3]));
575
+ date.setUTCHours(int(match[4]||0) - tzHour,
576
+ int(match[5]||0) - tzMin,
577
+ int(match[6]||0),
578
+ int(match[7]||0));
579
+ return date;
580
+ }
581
+ return string;
582
+ }
583
+
584
+ function int(str) {
585
+ return parseInt(str, 10);
586
+ }
587
+
588
+ function padNumber(num, digits, trim) {
589
+ var neg = '';
590
+ if (num < 0) {
591
+ neg = '-';
592
+ num = -num;
593
+ }
594
+ num = '' + num;
595
+ while(num.length < digits) num = '0' + num;
596
+ if (trim)
597
+ num = num.substr(num.length - digits);
598
+ return neg + num;
599
+ }
600
+
601
+
602
+ /**
603
+ * @ngdoc type
604
+ * @name angular.mock.TzDate
605
+ * @description
606
+ *
607
+ * *NOTE*: this is not an injectable instance, just a globally available mock class of `Date`.
608
+ *
609
+ * Mock of the Date type which has its timezone specified via constructor arg.
610
+ *
611
+ * The main purpose is to create Date-like instances with timezone fixed to the specified timezone
612
+ * offset, so that we can test code that depends on local timezone settings without dependency on
613
+ * the time zone settings of the machine where the code is running.
614
+ *
615
+ * @param {number} offset Offset of the *desired* timezone in hours (fractions will be honored)
616
+ * @param {(number|string)} timestamp Timestamp representing the desired time in *UTC*
617
+ *
618
+ * @example
619
+ * !!!! WARNING !!!!!
620
+ * This is not a complete Date object so only methods that were implemented can be called safely.
621
+ * To make matters worse, TzDate instances inherit stuff from Date via a prototype.
622
+ *
623
+ * We do our best to intercept calls to "unimplemented" methods, but since the list of methods is
624
+ * incomplete we might be missing some non-standard methods. This can result in errors like:
625
+ * "Date.prototype.foo called on incompatible Object".
626
+ *
627
+ * ```js
628
+ * var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00Z');
629
+ * newYearInBratislava.getTimezoneOffset() => -60;
630
+ * newYearInBratislava.getFullYear() => 2010;
631
+ * newYearInBratislava.getMonth() => 0;
632
+ * newYearInBratislava.getDate() => 1;
633
+ * newYearInBratislava.getHours() => 0;
634
+ * newYearInBratislava.getMinutes() => 0;
635
+ * newYearInBratislava.getSeconds() => 0;
636
+ * ```
637
+ *
638
+ */
639
+ angular.mock.TzDate = function (offset, timestamp) {
640
+ var self = new Date(0);
641
+ if (angular.isString(timestamp)) {
642
+ var tsStr = timestamp;
643
+
644
+ self.origDate = jsonStringToDate(timestamp);
645
+
646
+ timestamp = self.origDate.getTime();
647
+ if (isNaN(timestamp))
648
+ throw {
649
+ name: "Illegal Argument",
650
+ message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
651
+ };
652
+ } else {
653
+ self.origDate = new Date(timestamp);
654
+ }
655
+
656
+ var localOffset = new Date(timestamp).getTimezoneOffset();
657
+ self.offsetDiff = localOffset*60*1000 - offset*1000*60*60;
658
+ self.date = new Date(timestamp + self.offsetDiff);
659
+
660
+ self.getTime = function() {
661
+ return self.date.getTime() - self.offsetDiff;
662
+ };
663
+
664
+ self.toLocaleDateString = function() {
665
+ return self.date.toLocaleDateString();
666
+ };
667
+
668
+ self.getFullYear = function() {
669
+ return self.date.getFullYear();
670
+ };
671
+
672
+ self.getMonth = function() {
673
+ return self.date.getMonth();
674
+ };
675
+
676
+ self.getDate = function() {
677
+ return self.date.getDate();
678
+ };
679
+
680
+ self.getHours = function() {
681
+ return self.date.getHours();
682
+ };
683
+
684
+ self.getMinutes = function() {
685
+ return self.date.getMinutes();
686
+ };
687
+
688
+ self.getSeconds = function() {
689
+ return self.date.getSeconds();
690
+ };
691
+
692
+ self.getMilliseconds = function() {
693
+ return self.date.getMilliseconds();
694
+ };
695
+
696
+ self.getTimezoneOffset = function() {
697
+ return offset * 60;
698
+ };
699
+
700
+ self.getUTCFullYear = function() {
701
+ return self.origDate.getUTCFullYear();
702
+ };
703
+
704
+ self.getUTCMonth = function() {
705
+ return self.origDate.getUTCMonth();
706
+ };
707
+
708
+ self.getUTCDate = function() {
709
+ return self.origDate.getUTCDate();
710
+ };
711
+
712
+ self.getUTCHours = function() {
713
+ return self.origDate.getUTCHours();
714
+ };
715
+
716
+ self.getUTCMinutes = function() {
717
+ return self.origDate.getUTCMinutes();
718
+ };
719
+
720
+ self.getUTCSeconds = function() {
721
+ return self.origDate.getUTCSeconds();
722
+ };
723
+
724
+ self.getUTCMilliseconds = function() {
725
+ return self.origDate.getUTCMilliseconds();
726
+ };
727
+
728
+ self.getDay = function() {
729
+ return self.date.getDay();
730
+ };
731
+
732
+ // provide this method only on browsers that already have it
733
+ if (self.toISOString) {
734
+ self.toISOString = function() {
735
+ return padNumber(self.origDate.getUTCFullYear(), 4) + '-' +
736
+ padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' +
737
+ padNumber(self.origDate.getUTCDate(), 2) + 'T' +
738
+ padNumber(self.origDate.getUTCHours(), 2) + ':' +
739
+ padNumber(self.origDate.getUTCMinutes(), 2) + ':' +
740
+ padNumber(self.origDate.getUTCSeconds(), 2) + '.' +
741
+ padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z';
742
+ };
743
+ }
744
+
745
+ //hide all methods not implemented in this mock that the Date prototype exposes
746
+ var unimplementedMethods = ['getUTCDay',
747
+ 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
748
+ 'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
749
+ 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
750
+ 'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString',
751
+ 'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
752
+
753
+ angular.forEach(unimplementedMethods, function(methodName) {
754
+ self[methodName] = function() {
755
+ throw new Error("Method '" + methodName + "' is not implemented in the TzDate mock");
756
+ };
757
+ });
758
+
759
+ return self;
760
+ };
761
+
762
+ //make "tzDateInstance instanceof Date" return true
763
+ angular.mock.TzDate.prototype = Date.prototype;
764
+ /* jshint +W101 */
765
+
766
+ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
767
+
768
+ .config(['$provide', function($provide) {
769
+
770
+ var reflowQueue = [];
771
+ $provide.value('$$animateReflow', function(fn) {
772
+ var index = reflowQueue.length;
773
+ reflowQueue.push(fn);
774
+ return function cancel() {
775
+ reflowQueue.splice(index, 1);
776
+ };
777
+ });
778
+
779
+ $provide.decorator('$animate', function($delegate, $$asyncCallback) {
780
+ var animate = {
781
+ queue : [],
782
+ enabled : $delegate.enabled,
783
+ triggerCallbacks : function() {
784
+ $$asyncCallback.flush();
785
+ },
786
+ triggerReflow : function() {
787
+ angular.forEach(reflowQueue, function(fn) {
788
+ fn();
789
+ });
790
+ reflowQueue = [];
791
+ }
792
+ };
793
+
794
+ angular.forEach(
795
+ ['enter','leave','move','addClass','removeClass','setClass'], function(method) {
796
+ animate[method] = function() {
797
+ animate.queue.push({
798
+ event : method,
799
+ element : arguments[0],
800
+ args : arguments
801
+ });
802
+ $delegate[method].apply($delegate, arguments);
803
+ };
804
+ });
805
+
806
+ return animate;
807
+ });
808
+
809
+ }]);
810
+
811
+
812
+ /**
813
+ * @ngdoc function
814
+ * @name angular.mock.dump
815
+ * @description
816
+ *
817
+ * *NOTE*: this is not an injectable instance, just a globally available function.
818
+ *
819
+ * Method for serializing common angular objects (scope, elements, etc..) into strings, useful for
820
+ * debugging.
821
+ *
822
+ * This method is also available on window, where it can be used to display objects on debug
823
+ * console.
824
+ *
825
+ * @param {*} object - any object to turn into string.
826
+ * @return {string} a serialized string of the argument
827
+ */
828
+ angular.mock.dump = function(object) {
829
+ return serialize(object);
830
+
831
+ function serialize(object) {
832
+ var out;
833
+
834
+ if (angular.isElement(object)) {
835
+ object = angular.element(object);
836
+ out = angular.element('<div></div>');
837
+ angular.forEach(object, function(element) {
838
+ out.append(angular.element(element).clone());
839
+ });
840
+ out = out.html();
841
+ } else if (angular.isArray(object)) {
842
+ out = [];
843
+ angular.forEach(object, function(o) {
844
+ out.push(serialize(o));
845
+ });
846
+ out = '[ ' + out.join(', ') + ' ]';
847
+ } else if (angular.isObject(object)) {
848
+ if (angular.isFunction(object.$eval) && angular.isFunction(object.$apply)) {
849
+ out = serializeScope(object);
850
+ } else if (object instanceof Error) {
851
+ out = object.stack || ('' + object.name + ': ' + object.message);
852
+ } else {
853
+ // TODO(i): this prevents methods being logged,
854
+ // we should have a better way to serialize objects
855
+ out = angular.toJson(object, true);
856
+ }
857
+ } else {
858
+ out = String(object);
859
+ }
860
+
861
+ return out;
862
+ }
863
+
864
+ function serializeScope(scope, offset) {
865
+ offset = offset || ' ';
866
+ var log = [offset + 'Scope(' + scope.$id + '): {'];
867
+ for ( var key in scope ) {
868
+ if (Object.prototype.hasOwnProperty.call(scope, key) && !key.match(/^(\$|this)/)) {
869
+ log.push(' ' + key + ': ' + angular.toJson(scope[key]));
870
+ }
871
+ }
872
+ var child = scope.$$childHead;
873
+ while(child) {
874
+ log.push(serializeScope(child, offset + ' '));
875
+ child = child.$$nextSibling;
876
+ }
877
+ log.push('}');
878
+ return log.join('\n' + offset);
879
+ }
880
+ };
881
+
882
+ /**
883
+ * @ngdoc service
884
+ * @name $httpBackend
885
+ * @description
886
+ * Fake HTTP backend implementation suitable for unit testing applications that use the
887
+ * {@link ng.$http $http service}.
888
+ *
889
+ * *Note*: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
890
+ * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
891
+ *
892
+ * During unit testing, we want our unit tests to run quickly and have no external dependencies so
893
+ * we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or
894
+ * [JSONP](http://en.wikipedia.org/wiki/JSONP) requests to a real server. All we really need is
895
+ * to verify whether a certain request has been sent or not, or alternatively just let the
896
+ * application make requests, respond with pre-trained responses and assert that the end result is
897
+ * what we expect it to be.
898
+ *
899
+ * This mock implementation can be used to respond with static or dynamic responses via the
900
+ * `expect` and `when` apis and their shortcuts (`expectGET`, `whenPOST`, etc).
901
+ *
902
+ * When an Angular application needs some data from a server, it calls the $http service, which
903
+ * sends the request to a real server using $httpBackend service. With dependency injection, it is
904
+ * easy to inject $httpBackend mock (which has the same API as $httpBackend) and use it to verify
905
+ * the requests and respond with some testing data without sending a request to a real server.
906
+ *
907
+ * There are two ways to specify what test data should be returned as http responses by the mock
908
+ * backend when the code under test makes http requests:
909
+ *
910
+ * - `$httpBackend.expect` - specifies a request expectation
911
+ * - `$httpBackend.when` - specifies a backend definition
912
+ *
913
+ *
914
+ * # Request Expectations vs Backend Definitions
915
+ *
916
+ * Request expectations provide a way to make assertions about requests made by the application and
917
+ * to define responses for those requests. The test will fail if the expected requests are not made
918
+ * or they are made in the wrong order.
919
+ *
920
+ * Backend definitions allow you to define a fake backend for your application which doesn't assert
921
+ * if a particular request was made or not, it just returns a trained response if a request is made.
922
+ * The test will pass whether or not the request gets made during testing.
923
+ *
924
+ *
925
+ * <table class="table">
926
+ * <tr><th width="220px"></th><th>Request expectations</th><th>Backend definitions</th></tr>
927
+ * <tr>
928
+ * <th>Syntax</th>
929
+ * <td>.expect(...).respond(...)</td>
930
+ * <td>.when(...).respond(...)</td>
931
+ * </tr>
932
+ * <tr>
933
+ * <th>Typical usage</th>
934
+ * <td>strict unit tests</td>
935
+ * <td>loose (black-box) unit testing</td>
936
+ * </tr>
937
+ * <tr>
938
+ * <th>Fulfills multiple requests</th>
939
+ * <td>NO</td>
940
+ * <td>YES</td>
941
+ * </tr>
942
+ * <tr>
943
+ * <th>Order of requests matters</th>
944
+ * <td>YES</td>
945
+ * <td>NO</td>
946
+ * </tr>
947
+ * <tr>
948
+ * <th>Request required</th>
949
+ * <td>YES</td>
950
+ * <td>NO</td>
951
+ * </tr>
952
+ * <tr>
953
+ * <th>Response required</th>
954
+ * <td>optional (see below)</td>
955
+ * <td>YES</td>
956
+ * </tr>
957
+ * </table>
958
+ *
959
+ * In cases where both backend definitions and request expectations are specified during unit
960
+ * testing, the request expectations are evaluated first.
961
+ *
962
+ * If a request expectation has no response specified, the algorithm will search your backend
963
+ * definitions for an appropriate response.
964
+ *
965
+ * If a request didn't match any expectation or if the expectation doesn't have the response
966
+ * defined, the backend definitions are evaluated in sequential order to see if any of them match
967
+ * the request. The response from the first matched definition is returned.
968
+ *
969
+ *
970
+ * # Flushing HTTP requests
971
+ *
972
+ * The $httpBackend used in production always responds to requests asynchronously. If we preserved
973
+ * this behavior in unit testing, we'd have to create async unit tests, which are hard to write,
974
+ * to follow and to maintain. But neither can the testing mock respond synchronously; that would
975
+ * change the execution of the code under test. For this reason, the mock $httpBackend has a
976
+ * `flush()` method, which allows the test to explicitly flush pending requests. This preserves
977
+ * the async api of the backend, while allowing the test to execute synchronously.
978
+ *
979
+ *
980
+ * # Unit testing with mock $httpBackend
981
+ * The following code shows how to setup and use the mock backend when unit testing a controller.
982
+ * First we create the controller under test:
983
+ *
984
+ ```js
985
+ // The controller code
986
+ function MyController($scope, $http) {
987
+ var authToken;
988
+
989
+ $http.get('/auth.py').success(function(data, status, headers) {
990
+ authToken = headers('A-Token');
991
+ $scope.user = data;
992
+ });
993
+
994
+ $scope.saveMessage = function(message) {
995
+ var headers = { 'Authorization': authToken };
996
+ $scope.status = 'Saving...';
997
+
998
+ $http.post('/add-msg.py', message, { headers: headers } ).success(function(response) {
999
+ $scope.status = '';
1000
+ }).error(function() {
1001
+ $scope.status = 'ERROR!';
1002
+ });
1003
+ };
1004
+ }
1005
+ ```
1006
+ *
1007
+ * Now we setup the mock backend and create the test specs:
1008
+ *
1009
+ ```js
1010
+ // testing controller
1011
+ describe('MyController', function() {
1012
+ var $httpBackend, $rootScope, createController;
1013
+
1014
+ beforeEach(inject(function($injector) {
1015
+ // Set up the mock http service responses
1016
+ $httpBackend = $injector.get('$httpBackend');
1017
+ // backend definition common for all tests
1018
+ $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
1019
+
1020
+ // Get hold of a scope (i.e. the root scope)
1021
+ $rootScope = $injector.get('$rootScope');
1022
+ // The $controller service is used to create instances of controllers
1023
+ var $controller = $injector.get('$controller');
1024
+
1025
+ createController = function() {
1026
+ return $controller('MyController', {'$scope' : $rootScope });
1027
+ };
1028
+ }));
1029
+
1030
+
1031
+ afterEach(function() {
1032
+ $httpBackend.verifyNoOutstandingExpectation();
1033
+ $httpBackend.verifyNoOutstandingRequest();
1034
+ });
1035
+
1036
+
1037
+ it('should fetch authentication token', function() {
1038
+ $httpBackend.expectGET('/auth.py');
1039
+ var controller = createController();
1040
+ $httpBackend.flush();
1041
+ });
1042
+
1043
+
1044
+ it('should send msg to server', function() {
1045
+ var controller = createController();
1046
+ $httpBackend.flush();
1047
+
1048
+ // now you don’t care about the authentication, but
1049
+ // the controller will still send the request and
1050
+ // $httpBackend will respond without you having to
1051
+ // specify the expectation and response for this request
1052
+
1053
+ $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
1054
+ $rootScope.saveMessage('message content');
1055
+ expect($rootScope.status).toBe('Saving...');
1056
+ $httpBackend.flush();
1057
+ expect($rootScope.status).toBe('');
1058
+ });
1059
+
1060
+
1061
+ it('should send auth header', function() {
1062
+ var controller = createController();
1063
+ $httpBackend.flush();
1064
+
1065
+ $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
1066
+ // check if the header was send, if it wasn't the expectation won't
1067
+ // match the request and the test will fail
1068
+ return headers['Authorization'] == 'xxx';
1069
+ }).respond(201, '');
1070
+
1071
+ $rootScope.saveMessage('whatever');
1072
+ $httpBackend.flush();
1073
+ });
1074
+ });
1075
+ ```
1076
+ */
1077
+ angular.mock.$HttpBackendProvider = function() {
1078
+ this.$get = ['$rootScope', createHttpBackendMock];
1079
+ };
1080
+
1081
+ /**
1082
+ * General factory function for $httpBackend mock.
1083
+ * Returns instance for unit testing (when no arguments specified):
1084
+ * - passing through is disabled
1085
+ * - auto flushing is disabled
1086
+ *
1087
+ * Returns instance for e2e testing (when `$delegate` and `$browser` specified):
1088
+ * - passing through (delegating request to real backend) is enabled
1089
+ * - auto flushing is enabled
1090
+ *
1091
+ * @param {Object=} $delegate Real $httpBackend instance (allow passing through if specified)
1092
+ * @param {Object=} $browser Auto-flushing enabled if specified
1093
+ * @return {Object} Instance of $httpBackend mock
1094
+ */
1095
+ function createHttpBackendMock($rootScope, $delegate, $browser) {
1096
+ var definitions = [],
1097
+ expectations = [],
1098
+ responses = [],
1099
+ responsesPush = angular.bind(responses, responses.push),
1100
+ copy = angular.copy;
1101
+
1102
+ function createResponse(status, data, headers, statusText) {
1103
+ if (angular.isFunction(status)) return status;
1104
+
1105
+ return function() {
1106
+ return angular.isNumber(status)
1107
+ ? [status, data, headers, statusText]
1108
+ : [200, status, data];
1109
+ };
1110
+ }
1111
+
1112
+ // TODO(vojta): change params to: method, url, data, headers, callback
1113
+ function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
1114
+ var xhr = new MockXhr(),
1115
+ expectation = expectations[0],
1116
+ wasExpected = false;
1117
+
1118
+ function prettyPrint(data) {
1119
+ return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
1120
+ ? data
1121
+ : angular.toJson(data);
1122
+ }
1123
+
1124
+ function wrapResponse(wrapped) {
1125
+ if (!$browser && timeout && timeout.then) timeout.then(handleTimeout);
1126
+
1127
+ return handleResponse;
1128
+
1129
+ function handleResponse() {
1130
+ var response = wrapped.response(method, url, data, headers);
1131
+ xhr.$$respHeaders = response[2];
1132
+ callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
1133
+ copy(response[3] || ''));
1134
+ }
1135
+
1136
+ function handleTimeout() {
1137
+ for (var i = 0, ii = responses.length; i < ii; i++) {
1138
+ if (responses[i] === handleResponse) {
1139
+ responses.splice(i, 1);
1140
+ callback(-1, undefined, '');
1141
+ break;
1142
+ }
1143
+ }
1144
+ }
1145
+ }
1146
+
1147
+ if (expectation && expectation.match(method, url)) {
1148
+ if (!expectation.matchData(data))
1149
+ throw new Error('Expected ' + expectation + ' with different data\n' +
1150
+ 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
1151
+
1152
+ if (!expectation.matchHeaders(headers))
1153
+ throw new Error('Expected ' + expectation + ' with different headers\n' +
1154
+ 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
1155
+ prettyPrint(headers));
1156
+
1157
+ expectations.shift();
1158
+
1159
+ if (expectation.response) {
1160
+ responses.push(wrapResponse(expectation));
1161
+ return;
1162
+ }
1163
+ wasExpected = true;
1164
+ }
1165
+
1166
+ var i = -1, definition;
1167
+ while ((definition = definitions[++i])) {
1168
+ if (definition.match(method, url, data, headers || {})) {
1169
+ if (definition.response) {
1170
+ // if $browser specified, we do auto flush all requests
1171
+ ($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
1172
+ } else if (definition.passThrough) {
1173
+ $delegate(method, url, data, callback, headers, timeout, withCredentials);
1174
+ } else throw new Error('No response defined !');
1175
+ return;
1176
+ }
1177
+ }
1178
+ throw wasExpected ?
1179
+ new Error('No response defined !') :
1180
+ new Error('Unexpected request: ' + method + ' ' + url + '\n' +
1181
+ (expectation ? 'Expected ' + expectation : 'No more request expected'));
1182
+ }
1183
+
1184
+ /**
1185
+ * @ngdoc method
1186
+ * @name $httpBackend#when
1187
+ * @description
1188
+ * Creates a new backend definition.
1189
+ *
1190
+ * @param {string} method HTTP method.
1191
+ * @param {string|RegExp} url HTTP url.
1192
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1193
+ * data string and returns true if the data is as expected.
1194
+ * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1195
+ * object and returns true if the headers match the current definition.
1196
+ * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
1197
+ * request is handled.
1198
+ *
1199
+ * - respond –
1200
+ * `{function([status,] data[, headers, statusText])
1201
+ * | function(function(method, url, data, headers)}`
1202
+ * – The respond method takes a set of static data to be returned or a function that can
1203
+ * return an array containing response status (number), response data (string), response
1204
+ * headers (Object), and the text for the status (string).
1205
+ */
1206
+ $httpBackend.when = function(method, url, data, headers) {
1207
+ var definition = new MockHttpExpectation(method, url, data, headers),
1208
+ chain = {
1209
+ respond: function(status, data, headers, statusText) {
1210
+ definition.response = createResponse(status, data, headers, statusText);
1211
+ }
1212
+ };
1213
+
1214
+ if ($browser) {
1215
+ chain.passThrough = function() {
1216
+ definition.passThrough = true;
1217
+ };
1218
+ }
1219
+
1220
+ definitions.push(definition);
1221
+ return chain;
1222
+ };
1223
+
1224
+ /**
1225
+ * @ngdoc method
1226
+ * @name $httpBackend#whenGET
1227
+ * @description
1228
+ * Creates a new backend definition for GET requests. For more info see `when()`.
1229
+ *
1230
+ * @param {string|RegExp} url HTTP url.
1231
+ * @param {(Object|function(Object))=} headers HTTP headers.
1232
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1233
+ * request is handled.
1234
+ */
1235
+
1236
+ /**
1237
+ * @ngdoc method
1238
+ * @name $httpBackend#whenHEAD
1239
+ * @description
1240
+ * Creates a new backend definition for HEAD requests. For more info see `when()`.
1241
+ *
1242
+ * @param {string|RegExp} url HTTP url.
1243
+ * @param {(Object|function(Object))=} headers HTTP headers.
1244
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1245
+ * request is handled.
1246
+ */
1247
+
1248
+ /**
1249
+ * @ngdoc method
1250
+ * @name $httpBackend#whenDELETE
1251
+ * @description
1252
+ * Creates a new backend definition for DELETE requests. For more info see `when()`.
1253
+ *
1254
+ * @param {string|RegExp} url HTTP url.
1255
+ * @param {(Object|function(Object))=} headers HTTP headers.
1256
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1257
+ * request is handled.
1258
+ */
1259
+
1260
+ /**
1261
+ * @ngdoc method
1262
+ * @name $httpBackend#whenPOST
1263
+ * @description
1264
+ * Creates a new backend definition for POST requests. For more info see `when()`.
1265
+ *
1266
+ * @param {string|RegExp} url HTTP url.
1267
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1268
+ * data string and returns true if the data is as expected.
1269
+ * @param {(Object|function(Object))=} headers HTTP headers.
1270
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1271
+ * request is handled.
1272
+ */
1273
+
1274
+ /**
1275
+ * @ngdoc method
1276
+ * @name $httpBackend#whenPUT
1277
+ * @description
1278
+ * Creates a new backend definition for PUT requests. For more info see `when()`.
1279
+ *
1280
+ * @param {string|RegExp} url HTTP url.
1281
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1282
+ * data string and returns true if the data is as expected.
1283
+ * @param {(Object|function(Object))=} headers HTTP headers.
1284
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1285
+ * request is handled.
1286
+ */
1287
+
1288
+ /**
1289
+ * @ngdoc method
1290
+ * @name $httpBackend#whenJSONP
1291
+ * @description
1292
+ * Creates a new backend definition for JSONP requests. For more info see `when()`.
1293
+ *
1294
+ * @param {string|RegExp} url HTTP url.
1295
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1296
+ * request is handled.
1297
+ */
1298
+ createShortMethods('when');
1299
+
1300
+
1301
+ /**
1302
+ * @ngdoc method
1303
+ * @name $httpBackend#expect
1304
+ * @description
1305
+ * Creates a new request expectation.
1306
+ *
1307
+ * @param {string} method HTTP method.
1308
+ * @param {string|RegExp} url HTTP url.
1309
+ * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
1310
+ * receives data string and returns true if the data is as expected, or Object if request body
1311
+ * is in JSON format.
1312
+ * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1313
+ * object and returns true if the headers match the current expectation.
1314
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1315
+ * request is handled.
1316
+ *
1317
+ * - respond –
1318
+ * `{function([status,] data[, headers, statusText])
1319
+ * | function(function(method, url, data, headers)}`
1320
+ * – The respond method takes a set of static data to be returned or a function that can
1321
+ * return an array containing response status (number), response data (string), response
1322
+ * headers (Object), and the text for the status (string).
1323
+ */
1324
+ $httpBackend.expect = function(method, url, data, headers) {
1325
+ var expectation = new MockHttpExpectation(method, url, data, headers);
1326
+ expectations.push(expectation);
1327
+ return {
1328
+ respond: function (status, data, headers, statusText) {
1329
+ expectation.response = createResponse(status, data, headers, statusText);
1330
+ }
1331
+ };
1332
+ };
1333
+
1334
+
1335
+ /**
1336
+ * @ngdoc method
1337
+ * @name $httpBackend#expectGET
1338
+ * @description
1339
+ * Creates a new request expectation for GET requests. For more info see `expect()`.
1340
+ *
1341
+ * @param {string|RegExp} url HTTP url.
1342
+ * @param {Object=} headers HTTP headers.
1343
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1344
+ * request is handled. See #expect for more info.
1345
+ */
1346
+
1347
+ /**
1348
+ * @ngdoc method
1349
+ * @name $httpBackend#expectHEAD
1350
+ * @description
1351
+ * Creates a new request expectation for HEAD requests. For more info see `expect()`.
1352
+ *
1353
+ * @param {string|RegExp} url HTTP url.
1354
+ * @param {Object=} headers HTTP headers.
1355
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1356
+ * request is handled.
1357
+ */
1358
+
1359
+ /**
1360
+ * @ngdoc method
1361
+ * @name $httpBackend#expectDELETE
1362
+ * @description
1363
+ * Creates a new request expectation for DELETE requests. For more info see `expect()`.
1364
+ *
1365
+ * @param {string|RegExp} url HTTP url.
1366
+ * @param {Object=} headers HTTP headers.
1367
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1368
+ * request is handled.
1369
+ */
1370
+
1371
+ /**
1372
+ * @ngdoc method
1373
+ * @name $httpBackend#expectPOST
1374
+ * @description
1375
+ * Creates a new request expectation for POST requests. For more info see `expect()`.
1376
+ *
1377
+ * @param {string|RegExp} url HTTP url.
1378
+ * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
1379
+ * receives data string and returns true if the data is as expected, or Object if request body
1380
+ * is in JSON format.
1381
+ * @param {Object=} headers HTTP headers.
1382
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1383
+ * request is handled.
1384
+ */
1385
+
1386
+ /**
1387
+ * @ngdoc method
1388
+ * @name $httpBackend#expectPUT
1389
+ * @description
1390
+ * Creates a new request expectation for PUT requests. For more info see `expect()`.
1391
+ *
1392
+ * @param {string|RegExp} url HTTP url.
1393
+ * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
1394
+ * receives data string and returns true if the data is as expected, or Object if request body
1395
+ * is in JSON format.
1396
+ * @param {Object=} headers HTTP headers.
1397
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1398
+ * request is handled.
1399
+ */
1400
+
1401
+ /**
1402
+ * @ngdoc method
1403
+ * @name $httpBackend#expectPATCH
1404
+ * @description
1405
+ * Creates a new request expectation for PATCH requests. For more info see `expect()`.
1406
+ *
1407
+ * @param {string|RegExp} url HTTP url.
1408
+ * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
1409
+ * receives data string and returns true if the data is as expected, or Object if request body
1410
+ * is in JSON format.
1411
+ * @param {Object=} headers HTTP headers.
1412
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1413
+ * request is handled.
1414
+ */
1415
+
1416
+ /**
1417
+ * @ngdoc method
1418
+ * @name $httpBackend#expectJSONP
1419
+ * @description
1420
+ * Creates a new request expectation for JSONP requests. For more info see `expect()`.
1421
+ *
1422
+ * @param {string|RegExp} url HTTP url.
1423
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1424
+ * request is handled.
1425
+ */
1426
+ createShortMethods('expect');
1427
+
1428
+
1429
+ /**
1430
+ * @ngdoc method
1431
+ * @name $httpBackend#flush
1432
+ * @description
1433
+ * Flushes all pending requests using the trained responses.
1434
+ *
1435
+ * @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1436
+ * all pending requests will be flushed. If there are no pending requests when the flush method
1437
+ * is called an exception is thrown (as this typically a sign of programming error).
1438
+ */
1439
+ $httpBackend.flush = function(count) {
1440
+ $rootScope.$digest();
1441
+ if (!responses.length) throw new Error('No pending request to flush !');
1442
+
1443
+ if (angular.isDefined(count)) {
1444
+ while (count--) {
1445
+ if (!responses.length) throw new Error('No more pending request to flush !');
1446
+ responses.shift()();
1447
+ }
1448
+ } else {
1449
+ while (responses.length) {
1450
+ responses.shift()();
1451
+ }
1452
+ }
1453
+ $httpBackend.verifyNoOutstandingExpectation();
1454
+ };
1455
+
1456
+
1457
+ /**
1458
+ * @ngdoc method
1459
+ * @name $httpBackend#verifyNoOutstandingExpectation
1460
+ * @description
1461
+ * Verifies that all of the requests defined via the `expect` api were made. If any of the
1462
+ * requests were not made, verifyNoOutstandingExpectation throws an exception.
1463
+ *
1464
+ * Typically, you would call this method following each test case that asserts requests using an
1465
+ * "afterEach" clause.
1466
+ *
1467
+ * ```js
1468
+ * afterEach($httpBackend.verifyNoOutstandingExpectation);
1469
+ * ```
1470
+ */
1471
+ $httpBackend.verifyNoOutstandingExpectation = function() {
1472
+ $rootScope.$digest();
1473
+ if (expectations.length) {
1474
+ throw new Error('Unsatisfied requests: ' + expectations.join(', '));
1475
+ }
1476
+ };
1477
+
1478
+
1479
+ /**
1480
+ * @ngdoc method
1481
+ * @name $httpBackend#verifyNoOutstandingRequest
1482
+ * @description
1483
+ * Verifies that there are no outstanding requests that need to be flushed.
1484
+ *
1485
+ * Typically, you would call this method following each test case that asserts requests using an
1486
+ * "afterEach" clause.
1487
+ *
1488
+ * ```js
1489
+ * afterEach($httpBackend.verifyNoOutstandingRequest);
1490
+ * ```
1491
+ */
1492
+ $httpBackend.verifyNoOutstandingRequest = function() {
1493
+ if (responses.length) {
1494
+ throw new Error('Unflushed requests: ' + responses.length);
1495
+ }
1496
+ };
1497
+
1498
+
1499
+ /**
1500
+ * @ngdoc method
1501
+ * @name $httpBackend#resetExpectations
1502
+ * @description
1503
+ * Resets all request expectations, but preserves all backend definitions. Typically, you would
1504
+ * call resetExpectations during a multiple-phase test when you want to reuse the same instance of
1505
+ * $httpBackend mock.
1506
+ */
1507
+ $httpBackend.resetExpectations = function() {
1508
+ expectations.length = 0;
1509
+ responses.length = 0;
1510
+ };
1511
+
1512
+ return $httpBackend;
1513
+
1514
+
1515
+ function createShortMethods(prefix) {
1516
+ angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) {
1517
+ $httpBackend[prefix + method] = function(url, headers) {
1518
+ return $httpBackend[prefix](method, url, undefined, headers);
1519
+ };
1520
+ });
1521
+
1522
+ angular.forEach(['PUT', 'POST', 'PATCH'], function(method) {
1523
+ $httpBackend[prefix + method] = function(url, data, headers) {
1524
+ return $httpBackend[prefix](method, url, data, headers);
1525
+ };
1526
+ });
1527
+ }
1528
+ }
1529
+
1530
+ function MockHttpExpectation(method, url, data, headers) {
1531
+
1532
+ this.data = data;
1533
+ this.headers = headers;
1534
+
1535
+ this.match = function(m, u, d, h) {
1536
+ if (method != m) return false;
1537
+ if (!this.matchUrl(u)) return false;
1538
+ if (angular.isDefined(d) && !this.matchData(d)) return false;
1539
+ if (angular.isDefined(h) && !this.matchHeaders(h)) return false;
1540
+ return true;
1541
+ };
1542
+
1543
+ this.matchUrl = function(u) {
1544
+ if (!url) return true;
1545
+ if (angular.isFunction(url.test)) return url.test(u);
1546
+ return url == u;
1547
+ };
1548
+
1549
+ this.matchHeaders = function(h) {
1550
+ if (angular.isUndefined(headers)) return true;
1551
+ if (angular.isFunction(headers)) return headers(h);
1552
+ return angular.equals(headers, h);
1553
+ };
1554
+
1555
+ this.matchData = function(d) {
1556
+ if (angular.isUndefined(data)) return true;
1557
+ if (data && angular.isFunction(data.test)) return data.test(d);
1558
+ if (data && angular.isFunction(data)) return data(d);
1559
+ if (data && !angular.isString(data)) return angular.equals(data, angular.fromJson(d));
1560
+ return data == d;
1561
+ };
1562
+
1563
+ this.toString = function() {
1564
+ return method + ' ' + url;
1565
+ };
1566
+ }
1567
+
1568
+ function createMockXhr() {
1569
+ return new MockXhr();
1570
+ }
1571
+
1572
+ function MockXhr() {
1573
+
1574
+ // hack for testing $http, $httpBackend
1575
+ MockXhr.$$lastInstance = this;
1576
+
1577
+ this.open = function(method, url, async) {
1578
+ this.$$method = method;
1579
+ this.$$url = url;
1580
+ this.$$async = async;
1581
+ this.$$reqHeaders = {};
1582
+ this.$$respHeaders = {};
1583
+ };
1584
+
1585
+ this.send = function(data) {
1586
+ this.$$data = data;
1587
+ };
1588
+
1589
+ this.setRequestHeader = function(key, value) {
1590
+ this.$$reqHeaders[key] = value;
1591
+ };
1592
+
1593
+ this.getResponseHeader = function(name) {
1594
+ // the lookup must be case insensitive,
1595
+ // that's why we try two quick lookups first and full scan last
1596
+ var header = this.$$respHeaders[name];
1597
+ if (header) return header;
1598
+
1599
+ name = angular.lowercase(name);
1600
+ header = this.$$respHeaders[name];
1601
+ if (header) return header;
1602
+
1603
+ header = undefined;
1604
+ angular.forEach(this.$$respHeaders, function(headerVal, headerName) {
1605
+ if (!header && angular.lowercase(headerName) == name) header = headerVal;
1606
+ });
1607
+ return header;
1608
+ };
1609
+
1610
+ this.getAllResponseHeaders = function() {
1611
+ var lines = [];
1612
+
1613
+ angular.forEach(this.$$respHeaders, function(value, key) {
1614
+ lines.push(key + ': ' + value);
1615
+ });
1616
+ return lines.join('\n');
1617
+ };
1618
+
1619
+ this.abort = angular.noop;
1620
+ }
1621
+
1622
+
1623
+ /**
1624
+ * @ngdoc service
1625
+ * @name $timeout
1626
+ * @description
1627
+ *
1628
+ * This service is just a simple decorator for {@link ng.$timeout $timeout} service
1629
+ * that adds a "flush" and "verifyNoPendingTasks" methods.
1630
+ */
1631
+
1632
+ angular.mock.$TimeoutDecorator = function($delegate, $browser) {
1633
+
1634
+ /**
1635
+ * @ngdoc method
1636
+ * @name $timeout#flush
1637
+ * @description
1638
+ *
1639
+ * Flushes the queue of pending tasks.
1640
+ *
1641
+ * @param {number=} delay maximum timeout amount to flush up until
1642
+ */
1643
+ $delegate.flush = function(delay) {
1644
+ $browser.defer.flush(delay);
1645
+ };
1646
+
1647
+ /**
1648
+ * @ngdoc method
1649
+ * @name $timeout#verifyNoPendingTasks
1650
+ * @description
1651
+ *
1652
+ * Verifies that there are no pending tasks that need to be flushed.
1653
+ */
1654
+ $delegate.verifyNoPendingTasks = function() {
1655
+ if ($browser.deferredFns.length) {
1656
+ throw new Error('Deferred tasks to flush (' + $browser.deferredFns.length + '): ' +
1657
+ formatPendingTasksAsString($browser.deferredFns));
1658
+ }
1659
+ };
1660
+
1661
+ function formatPendingTasksAsString(tasks) {
1662
+ var result = [];
1663
+ angular.forEach(tasks, function(task) {
1664
+ result.push('{id: ' + task.id + ', ' + 'time: ' + task.time + '}');
1665
+ });
1666
+
1667
+ return result.join(', ');
1668
+ }
1669
+
1670
+ return $delegate;
1671
+ };
1672
+
1673
+ angular.mock.$RAFDecorator = function($delegate) {
1674
+ var queue = [];
1675
+ var rafFn = function(fn) {
1676
+ var index = queue.length;
1677
+ queue.push(fn);
1678
+ return function() {
1679
+ queue.splice(index, 1);
1680
+ };
1681
+ };
1682
+
1683
+ rafFn.supported = $delegate.supported;
1684
+
1685
+ rafFn.flush = function() {
1686
+ if(queue.length === 0) {
1687
+ throw new Error('No rAF callbacks present');
1688
+ }
1689
+
1690
+ var length = queue.length;
1691
+ for(var i=0;i<length;i++) {
1692
+ queue[i]();
1693
+ }
1694
+
1695
+ queue = [];
1696
+ };
1697
+
1698
+ return rafFn;
1699
+ };
1700
+
1701
+ angular.mock.$AsyncCallbackDecorator = function($delegate) {
1702
+ var callbacks = [];
1703
+ var addFn = function(fn) {
1704
+ callbacks.push(fn);
1705
+ };
1706
+ addFn.flush = function() {
1707
+ angular.forEach(callbacks, function(fn) {
1708
+ fn();
1709
+ });
1710
+ callbacks = [];
1711
+ };
1712
+ return addFn;
1713
+ };
1714
+
1715
+ /**
1716
+ *
1717
+ */
1718
+ angular.mock.$RootElementProvider = function() {
1719
+ this.$get = function() {
1720
+ return angular.element('<div ng-app></div>');
1721
+ };
1722
+ };
1723
+
1724
+ /**
1725
+ * @ngdoc module
1726
+ * @name ngMock
1727
+ * @packageName angular-mocks
1728
+ * @description
1729
+ *
1730
+ * # ngMock
1731
+ *
1732
+ * The `ngMock` module provides support to inject and mock Angular services into unit tests.
1733
+ * In addition, ngMock also extends various core ng services such that they can be
1734
+ * inspected and controlled in a synchronous manner within test code.
1735
+ *
1736
+ *
1737
+ * <div doc-module-components="ngMock"></div>
1738
+ *
1739
+ */
1740
+ angular.module('ngMock', ['ng']).provider({
1741
+ $browser: angular.mock.$BrowserProvider,
1742
+ $exceptionHandler: angular.mock.$ExceptionHandlerProvider,
1743
+ $log: angular.mock.$LogProvider,
1744
+ $interval: angular.mock.$IntervalProvider,
1745
+ $httpBackend: angular.mock.$HttpBackendProvider,
1746
+ $rootElement: angular.mock.$RootElementProvider
1747
+ }).config(['$provide', function($provide) {
1748
+ $provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
1749
+ $provide.decorator('$$rAF', angular.mock.$RAFDecorator);
1750
+ $provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator);
1751
+ }]);
1752
+
1753
+ /**
1754
+ * @ngdoc module
1755
+ * @name ngMockE2E
1756
+ * @module ngMockE2E
1757
+ * @packageName angular-mocks
1758
+ * @description
1759
+ *
1760
+ * The `ngMockE2E` is an angular module which contains mocks suitable for end-to-end testing.
1761
+ * Currently there is only one mock present in this module -
1762
+ * the {@link ngMockE2E.$httpBackend e2e $httpBackend} mock.
1763
+ */
1764
+ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1765
+ $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
1766
+ }]);
1767
+
1768
+ /**
1769
+ * @ngdoc service
1770
+ * @name $httpBackend
1771
+ * @module ngMockE2E
1772
+ * @description
1773
+ * Fake HTTP backend implementation suitable for end-to-end testing or backend-less development of
1774
+ * applications that use the {@link ng.$http $http service}.
1775
+ *
1776
+ * *Note*: For fake http backend implementation suitable for unit testing please see
1777
+ * {@link ngMock.$httpBackend unit-testing $httpBackend mock}.
1778
+ *
1779
+ * This implementation can be used to respond with static or dynamic responses via the `when` api
1780
+ * and its shortcuts (`whenGET`, `whenPOST`, etc) and optionally pass through requests to the
1781
+ * real $httpBackend for specific requests (e.g. to interact with certain remote apis or to fetch
1782
+ * templates from a webserver).
1783
+ *
1784
+ * As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application
1785
+ * is being developed with the real backend api replaced with a mock, it is often desirable for
1786
+ * certain category of requests to bypass the mock and issue a real http request (e.g. to fetch
1787
+ * templates or static files from the webserver). To configure the backend with this behavior
1788
+ * use the `passThrough` request handler of `when` instead of `respond`.
1789
+ *
1790
+ * Additionally, we don't want to manually have to flush mocked out requests like we do during unit
1791
+ * testing. For this reason the e2e $httpBackend flushes mocked out requests
1792
+ * automatically, closely simulating the behavior of the XMLHttpRequest object.
1793
+ *
1794
+ * To setup the application to run with this http backend, you have to create a module that depends
1795
+ * on the `ngMockE2E` and your application modules and defines the fake backend:
1796
+ *
1797
+ * ```js
1798
+ * myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
1799
+ * myAppDev.run(function($httpBackend) {
1800
+ * phones = [{name: 'phone1'}, {name: 'phone2'}];
1801
+ *
1802
+ * // returns the current list of phones
1803
+ * $httpBackend.whenGET('/phones').respond(phones);
1804
+ *
1805
+ * // adds a new phone to the phones array
1806
+ * $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
1807
+ * var phone = angular.fromJson(data);
1808
+ * phones.push(phone);
1809
+ * return [200, phone, {}];
1810
+ * });
1811
+ * $httpBackend.whenGET(/^\/templates\//).passThrough();
1812
+ * //...
1813
+ * });
1814
+ * ```
1815
+ *
1816
+ * Afterwards, bootstrap your app with this new module.
1817
+ */
1818
+
1819
+ /**
1820
+ * @ngdoc method
1821
+ * @name $httpBackend#when
1822
+ * @module ngMockE2E
1823
+ * @description
1824
+ * Creates a new backend definition.
1825
+ *
1826
+ * @param {string} method HTTP method.
1827
+ * @param {string|RegExp} url HTTP url.
1828
+ * @param {(string|RegExp)=} data HTTP request body.
1829
+ * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1830
+ * object and returns true if the headers match the current definition.
1831
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1832
+ * control how a matched request is handled.
1833
+ *
1834
+ * - respond –
1835
+ * `{function([status,] data[, headers, statusText])
1836
+ * | function(function(method, url, data, headers)}`
1837
+ * – The respond method takes a set of static data to be returned or a function that can return
1838
+ * an array containing response status (number), response data (string), response headers
1839
+ * (Object), and the text for the status (string).
1840
+ * - passThrough – `{function()}` – Any request matching a backend definition with
1841
+ * `passThrough` handler will be passed through to the real backend (an XHR request will be made
1842
+ * to the server.)
1843
+ */
1844
+
1845
+ /**
1846
+ * @ngdoc method
1847
+ * @name $httpBackend#whenGET
1848
+ * @module ngMockE2E
1849
+ * @description
1850
+ * Creates a new backend definition for GET requests. For more info see `when()`.
1851
+ *
1852
+ * @param {string|RegExp} url HTTP url.
1853
+ * @param {(Object|function(Object))=} headers HTTP headers.
1854
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1855
+ * control how a matched request is handled.
1856
+ */
1857
+
1858
+ /**
1859
+ * @ngdoc method
1860
+ * @name $httpBackend#whenHEAD
1861
+ * @module ngMockE2E
1862
+ * @description
1863
+ * Creates a new backend definition for HEAD requests. For more info see `when()`.
1864
+ *
1865
+ * @param {string|RegExp} url HTTP url.
1866
+ * @param {(Object|function(Object))=} headers HTTP headers.
1867
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1868
+ * control how a matched request is handled.
1869
+ */
1870
+
1871
+ /**
1872
+ * @ngdoc method
1873
+ * @name $httpBackend#whenDELETE
1874
+ * @module ngMockE2E
1875
+ * @description
1876
+ * Creates a new backend definition for DELETE requests. For more info see `when()`.
1877
+ *
1878
+ * @param {string|RegExp} url HTTP url.
1879
+ * @param {(Object|function(Object))=} headers HTTP headers.
1880
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1881
+ * control how a matched request is handled.
1882
+ */
1883
+
1884
+ /**
1885
+ * @ngdoc method
1886
+ * @name $httpBackend#whenPOST
1887
+ * @module ngMockE2E
1888
+ * @description
1889
+ * Creates a new backend definition for POST requests. For more info see `when()`.
1890
+ *
1891
+ * @param {string|RegExp} url HTTP url.
1892
+ * @param {(string|RegExp)=} data HTTP request body.
1893
+ * @param {(Object|function(Object))=} headers HTTP headers.
1894
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1895
+ * control how a matched request is handled.
1896
+ */
1897
+
1898
+ /**
1899
+ * @ngdoc method
1900
+ * @name $httpBackend#whenPUT
1901
+ * @module ngMockE2E
1902
+ * @description
1903
+ * Creates a new backend definition for PUT requests. For more info see `when()`.
1904
+ *
1905
+ * @param {string|RegExp} url HTTP url.
1906
+ * @param {(string|RegExp)=} data HTTP request body.
1907
+ * @param {(Object|function(Object))=} headers HTTP headers.
1908
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1909
+ * control how a matched request is handled.
1910
+ */
1911
+
1912
+ /**
1913
+ * @ngdoc method
1914
+ * @name $httpBackend#whenPATCH
1915
+ * @module ngMockE2E
1916
+ * @description
1917
+ * Creates a new backend definition for PATCH requests. For more info see `when()`.
1918
+ *
1919
+ * @param {string|RegExp} url HTTP url.
1920
+ * @param {(string|RegExp)=} data HTTP request body.
1921
+ * @param {(Object|function(Object))=} headers HTTP headers.
1922
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1923
+ * control how a matched request is handled.
1924
+ */
1925
+
1926
+ /**
1927
+ * @ngdoc method
1928
+ * @name $httpBackend#whenJSONP
1929
+ * @module ngMockE2E
1930
+ * @description
1931
+ * Creates a new backend definition for JSONP requests. For more info see `when()`.
1932
+ *
1933
+ * @param {string|RegExp} url HTTP url.
1934
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
1935
+ * control how a matched request is handled.
1936
+ */
1937
+ angular.mock.e2e = {};
1938
+ angular.mock.e2e.$httpBackendDecorator =
1939
+ ['$rootScope', '$delegate', '$browser', createHttpBackendMock];
1940
+
1941
+
1942
+ angular.mock.clearDataCache = function() {
1943
+ var key,
1944
+ cache = angular.element.cache;
1945
+
1946
+ for(key in cache) {
1947
+ if (Object.prototype.hasOwnProperty.call(cache,key)) {
1948
+ var handle = cache[key].handle;
1949
+
1950
+ handle && angular.element(handle.elem).off();
1951
+ delete cache[key];
1952
+ }
1953
+ }
1954
+ };
1955
+
1956
+
1957
+ if(window.jasmine || window.mocha) {
1958
+
1959
+ var currentSpec = null,
1960
+ isSpecRunning = function() {
1961
+ return !!currentSpec;
1962
+ };
1963
+
1964
+
1965
+ (window.beforeEach || window.setup)(function() {
1966
+ currentSpec = this;
1967
+ });
1968
+
1969
+ (window.afterEach || window.teardown)(function() {
1970
+ var injector = currentSpec.$injector;
1971
+
1972
+ angular.forEach(currentSpec.$modules, function(module) {
1973
+ if (module && module.$$hashKey) {
1974
+ module.$$hashKey = undefined;
1975
+ }
1976
+ });
1977
+
1978
+ currentSpec.$injector = null;
1979
+ currentSpec.$modules = null;
1980
+ currentSpec = null;
1981
+
1982
+ if (injector) {
1983
+ injector.get('$rootElement').off();
1984
+ injector.get('$browser').pollFns.length = 0;
1985
+ }
1986
+
1987
+ angular.mock.clearDataCache();
1988
+
1989
+ // clean up jquery's fragment cache
1990
+ angular.forEach(angular.element.fragments, function(val, key) {
1991
+ delete angular.element.fragments[key];
1992
+ });
1993
+
1994
+ MockXhr.$$lastInstance = null;
1995
+
1996
+ angular.forEach(angular.callbacks, function(val, key) {
1997
+ delete angular.callbacks[key];
1998
+ });
1999
+ angular.callbacks.counter = 0;
2000
+ });
2001
+
2002
+ /**
2003
+ * @ngdoc function
2004
+ * @name angular.mock.module
2005
+ * @description
2006
+ *
2007
+ * *NOTE*: This function is also published on window for easy access.<br>
2008
+ * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
2009
+ *
2010
+ * This function registers a module configuration code. It collects the configuration information
2011
+ * which will be used when the injector is created by {@link angular.mock.inject inject}.
2012
+ *
2013
+ * See {@link angular.mock.inject inject} for usage example
2014
+ *
2015
+ * @param {...(string|Function|Object)} fns any number of modules which are represented as string
2016
+ * aliases or as anonymous module initialization functions. The modules are used to
2017
+ * configure the injector. The 'ng' and 'ngMock' modules are automatically loaded. If an
2018
+ * object literal is passed they will be registered as values in the module, the key being
2019
+ * the module name and the value being what is returned.
2020
+ */
2021
+ window.module = angular.mock.module = function() {
2022
+ var moduleFns = Array.prototype.slice.call(arguments, 0);
2023
+ return isSpecRunning() ? workFn() : workFn;
2024
+ /////////////////////
2025
+ function workFn() {
2026
+ if (currentSpec.$injector) {
2027
+ throw new Error('Injector already created, can not register a module!');
2028
+ } else {
2029
+ var modules = currentSpec.$modules || (currentSpec.$modules = []);
2030
+ angular.forEach(moduleFns, function(module) {
2031
+ if (angular.isObject(module) && !angular.isArray(module)) {
2032
+ modules.push(function($provide) {
2033
+ angular.forEach(module, function(value, key) {
2034
+ $provide.value(key, value);
2035
+ });
2036
+ });
2037
+ } else {
2038
+ modules.push(module);
2039
+ }
2040
+ });
2041
+ }
2042
+ }
2043
+ };
2044
+
2045
+ /**
2046
+ * @ngdoc function
2047
+ * @name angular.mock.inject
2048
+ * @description
2049
+ *
2050
+ * *NOTE*: This function is also published on window for easy access.<br>
2051
+ * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
2052
+ *
2053
+ * The inject function wraps a function into an injectable function. The inject() creates new
2054
+ * instance of {@link auto.$injector $injector} per test, which is then used for
2055
+ * resolving references.
2056
+ *
2057
+ *
2058
+ * ## Resolving References (Underscore Wrapping)
2059
+ * Often, we would like to inject a reference once, in a `beforeEach()` block and reuse this
2060
+ * in multiple `it()` clauses. To be able to do this we must assign the reference to a variable
2061
+ * that is declared in the scope of the `describe()` block. Since we would, most likely, want
2062
+ * the variable to have the same name of the reference we have a problem, since the parameter
2063
+ * to the `inject()` function would hide the outer variable.
2064
+ *
2065
+ * To help with this, the injected parameters can, optionally, be enclosed with underscores.
2066
+ * These are ignored by the injector when the reference name is resolved.
2067
+ *
2068
+ * For example, the parameter `_myService_` would be resolved as the reference `myService`.
2069
+ * Since it is available in the function body as _myService_, we can then assign it to a variable
2070
+ * defined in an outer scope.
2071
+ *
2072
+ * ```
2073
+ * // Defined out reference variable outside
2074
+ * var myService;
2075
+ *
2076
+ * // Wrap the parameter in underscores
2077
+ * beforeEach( inject( function(_myService_){
2078
+ * myService = _myService_;
2079
+ * }));
2080
+ *
2081
+ * // Use myService in a series of tests.
2082
+ * it('makes use of myService', function() {
2083
+ * myService.doStuff();
2084
+ * });
2085
+ *
2086
+ * ```
2087
+ *
2088
+ * See also {@link angular.mock.module angular.mock.module}
2089
+ *
2090
+ * ## Example
2091
+ * Example of what a typical jasmine tests looks like with the inject method.
2092
+ * ```js
2093
+ *
2094
+ * angular.module('myApplicationModule', [])
2095
+ * .value('mode', 'app')
2096
+ * .value('version', 'v1.0.1');
2097
+ *
2098
+ *
2099
+ * describe('MyApp', function() {
2100
+ *
2101
+ * // You need to load modules that you want to test,
2102
+ * // it loads only the "ng" module by default.
2103
+ * beforeEach(module('myApplicationModule'));
2104
+ *
2105
+ *
2106
+ * // inject() is used to inject arguments of all given functions
2107
+ * it('should provide a version', inject(function(mode, version) {
2108
+ * expect(version).toEqual('v1.0.1');
2109
+ * expect(mode).toEqual('app');
2110
+ * }));
2111
+ *
2112
+ *
2113
+ * // The inject and module method can also be used inside of the it or beforeEach
2114
+ * it('should override a version and test the new version is injected', function() {
2115
+ * // module() takes functions or strings (module aliases)
2116
+ * module(function($provide) {
2117
+ * $provide.value('version', 'overridden'); // override version here
2118
+ * });
2119
+ *
2120
+ * inject(function(version) {
2121
+ * expect(version).toEqual('overridden');
2122
+ * });
2123
+ * });
2124
+ * });
2125
+ *
2126
+ * ```
2127
+ *
2128
+ * @param {...Function} fns any number of functions which will be injected using the injector.
2129
+ */
2130
+
2131
+
2132
+
2133
+ var ErrorAddingDeclarationLocationStack = function(e, errorForStack) {
2134
+ this.message = e.message;
2135
+ this.name = e.name;
2136
+ if (e.line) this.line = e.line;
2137
+ if (e.sourceId) this.sourceId = e.sourceId;
2138
+ if (e.stack && errorForStack)
2139
+ this.stack = e.stack + '\n' + errorForStack.stack;
2140
+ if (e.stackArray) this.stackArray = e.stackArray;
2141
+ };
2142
+ ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;
2143
+
2144
+ window.inject = angular.mock.inject = function() {
2145
+ var blockFns = Array.prototype.slice.call(arguments, 0);
2146
+ var errorForStack = new Error('Declaration Location');
2147
+ return isSpecRunning() ? workFn.call(currentSpec) : workFn;
2148
+ /////////////////////
2149
+ function workFn() {
2150
+ var modules = currentSpec.$modules || [];
2151
+
2152
+ modules.unshift('ngMock');
2153
+ modules.unshift('ng');
2154
+ var injector = currentSpec.$injector;
2155
+ if (!injector) {
2156
+ injector = currentSpec.$injector = angular.injector(modules);
2157
+ }
2158
+ for(var i = 0, ii = blockFns.length; i < ii; i++) {
2159
+ try {
2160
+ /* jshint -W040 *//* Jasmine explicitly provides a `this` object when calling functions */
2161
+ injector.invoke(blockFns[i] || angular.noop, this);
2162
+ /* jshint +W040 */
2163
+ } catch (e) {
2164
+ if (e.stack && errorForStack) {
2165
+ throw new ErrorAddingDeclarationLocationStack(e, errorForStack);
2166
+ }
2167
+ throw e;
2168
+ } finally {
2169
+ errorForStack = null;
2170
+ }
2171
+ }
2172
+ }
2173
+ };
2174
+ }
2175
+
2176
+
2177
+ })(window, window.angular);