Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'libs/bower_components/angular-mocks/angular-mocks.js')
-rw-r--r--libs/bower_components/angular-mocks/angular-mocks.js1361
1 files changed, 1123 insertions, 238 deletions
diff --git a/libs/bower_components/angular-mocks/angular-mocks.js b/libs/bower_components/angular-mocks/angular-mocks.js
index ab2ba68318..00167503d4 100644
--- a/libs/bower_components/angular-mocks/angular-mocks.js
+++ b/libs/bower_components/angular-mocks/angular-mocks.js
@@ -1,9 +1,9 @@
/**
- * @license AngularJS v1.4.10
- * (c) 2010-2015 Google, Inc. http://angularjs.org
+ * @license AngularJS v1.6.5
+ * (c) 2010-2017 Google, Inc. http://angularjs.org
* License: MIT
*/
-(function(window, angular, undefined) {
+(function(window, angular) {
'use strict';
@@ -13,6 +13,7 @@
* @description
*
* Namespace from 'angular-mocks.js' which contains testing related code.
+ *
*/
angular.mock = {};
@@ -24,7 +25,7 @@ angular.mock = {};
* @description
* This service is a mock implementation of {@link ng.$browser}. It provides fake
* implementation for commonly used browser apis that are hard to test, e.g. setTimeout, xhr,
- * cookies, etc...
+ * cookies, etc.
*
* The api of this service is the same as that of the real {@link ng.$browser $browser}, except
* that there are several helper methods available which can be used in tests.
@@ -39,14 +40,34 @@ angular.mock.$Browser = function() {
var self = this;
this.isMock = true;
- self.$$url = "http://server/";
+ self.$$url = 'http://server/';
self.$$lastUrl = self.$$url; // used by url polling fn
self.pollFns = [];
- // TODO(vojta): remove this temporary api
- self.$$completeOutstandingRequest = angular.noop;
- self.$$incOutstandingRequestCount = angular.noop;
-
+ // Testability API
+
+ var outstandingRequestCount = 0;
+ var outstandingRequestCallbacks = [];
+ self.$$incOutstandingRequestCount = function() { outstandingRequestCount++; };
+ self.$$completeOutstandingRequest = function(fn) {
+ try {
+ fn();
+ } finally {
+ outstandingRequestCount--;
+ if (!outstandingRequestCount) {
+ while (outstandingRequestCallbacks.length) {
+ outstandingRequestCallbacks.pop()();
+ }
+ }
+ }
+ };
+ self.notifyWhenNoOutstandingRequests = function(callback) {
+ if (outstandingRequestCount) {
+ outstandingRequestCallbacks.push(callback);
+ } else {
+ callback();
+ }
+ };
// register url polling fn
@@ -71,6 +92,8 @@ angular.mock.$Browser = function() {
self.deferredNextId = 0;
self.defer = function(fn, delay) {
+ // Note that we do not use `$$incOutstandingRequestCount` or `$$completeOutstandingRequest`
+ // in this mock implementation.
delay = delay || 0;
self.deferredFns.push({time:(self.defer.now + delay), fn:fn, id: self.deferredNextId});
self.deferredFns.sort(function(a, b) { return a.time - b.time;});
@@ -112,19 +135,29 @@ angular.mock.$Browser = function() {
* @param {number=} number of milliseconds to flush. See {@link #defer.now}
*/
self.defer.flush = function(delay) {
+ var nextTime;
+
if (angular.isDefined(delay)) {
- self.defer.now += delay;
+ // A delay was passed so compute the next time
+ nextTime = self.defer.now + delay;
} else {
if (self.deferredFns.length) {
- self.defer.now = self.deferredFns[self.deferredFns.length - 1].time;
+ // No delay was passed so set the next time so that it clears the deferred queue
+ nextTime = self.deferredFns[self.deferredFns.length - 1].time;
} else {
+ // No delay passed, but there are no deferred tasks so flush - indicates an error!
throw new Error('No deferred tasks to be flushed');
}
}
- while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
+ while (self.deferredFns.length && self.deferredFns[0].time <= nextTime) {
+ // Increment the time and call the next deferred function
+ self.defer.now = self.deferredFns[0].time;
self.deferredFns.shift().fn();
}
+
+ // Ensure that the current time is correct
+ self.defer.now = nextTime;
};
self.$$baseHref = '/';
@@ -134,12 +167,12 @@ angular.mock.$Browser = function() {
};
angular.mock.$Browser.prototype = {
-/**
- * @name $browser#poll
- *
- * @description
- * run all fns in pollFns
- */
+ /**
+ * @name $browser#poll
+ *
+ * @description
+ * run all fns in pollFns
+ */
poll: function poll() {
angular.forEach(this.pollFns, function(pollFn) {
pollFn();
@@ -162,10 +195,6 @@ angular.mock.$Browser.prototype = {
state: function() {
return this.$$state;
- },
-
- notifyWhenNoOutstandingRequests: function(fn) {
- fn();
}
};
@@ -226,13 +255,13 @@ angular.mock.$ExceptionHandlerProvider = function() {
* @param {string} mode Mode of operation, defaults to `rethrow`.
*
* - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
- * mode stores an array of errors in `$exceptionHandler.errors`, to allow later
- * assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
- * {@link ngMock.$log#reset reset()}
+ * mode stores an array of errors in `$exceptionHandler.errors`, to allow later assertion of
+ * them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
+ * {@link ngMock.$log#reset reset()}.
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
- * is a bug in the application or test, so this mock will make these tests fail.
- * For any implementations that expect exceptions to be thrown, the `rethrow` mode
- * will also maintain a log of thrown errors.
+ * is a bug in the application or test, so this mock will make these tests fail. For any
+ * implementations that expect exceptions to be thrown, the `rethrow` mode will also maintain
+ * a log of thrown errors in `$exceptionHandler.errors`.
*/
this.mode = function(mode) {
@@ -241,19 +270,19 @@ angular.mock.$ExceptionHandlerProvider = function() {
case 'rethrow':
var errors = [];
handler = function(e) {
- if (arguments.length == 1) {
+ if (arguments.length === 1) {
errors.push(e);
} else {
errors.push([].slice.call(arguments, 0));
}
- if (mode === "rethrow") {
+ if (mode === 'rethrow') {
throw e;
}
};
handler.errors = errors;
break;
default:
- throw new Error("Unknown mode '" + mode + "', only 'log'/'rethrow' modes are allowed!");
+ throw new Error('Unknown mode \'' + mode + '\', only \'log\'/\'rethrow\' modes are allowed!');
}
};
@@ -403,8 +432,8 @@ angular.mock.$LogProvider = function() {
});
});
if (errors.length) {
- errors.unshift("Expected $log to be empty! Either a message was logged unexpectedly, or " +
- "an expected log message was not checked and removed:");
+ errors.unshift('Expected $log to be empty! Either a message was logged unexpectedly, or ' +
+ 'an expected log message was not checked and removed:');
errors.push('');
throw new Error(errors.join('\n---------\n'));
}
@@ -452,7 +481,7 @@ angular.mock.$IntervalProvider = function() {
promise = deferred.promise;
count = (angular.isDefined(count)) ? count : 0;
- promise.then(null, null, (!hasParams) ? fn : function() {
+ promise.then(null, function() {}, (!hasParams) ? fn : function() {
fn.apply(null, args);
});
@@ -482,8 +511,8 @@ angular.mock.$IntervalProvider = function() {
}
repeatFns.push({
- nextTime:(now + delay),
- delay: delay,
+ nextTime: (now + (delay || 0)),
+ delay: delay || 1,
fn: tick,
id: nextRepeatId,
deferred: deferred
@@ -512,6 +541,7 @@ angular.mock.$IntervalProvider = function() {
});
if (angular.isDefined(fnIndex)) {
+ repeatFns[fnIndex].deferred.promise.then(undefined, function() {});
repeatFns[fnIndex].deferred.reject('canceled');
repeatFns.splice(fnIndex, 1);
return true;
@@ -532,10 +562,16 @@ angular.mock.$IntervalProvider = function() {
* @return {number} The amount of time moved forward.
*/
$interval.flush = function(millis) {
+ var before = now;
now += millis;
while (repeatFns.length && repeatFns[0].nextTime <= now) {
var task = repeatFns[0];
task.fn();
+ if (task.nextTime === before) {
+ // this can only happen the first time
+ // a zero-delay interval gets triggered
+ task.nextTime++;
+ }
task.nextTime += task.delay;
repeatFns.sort(function(a, b) { return a.nextTime - b.nextTime;});
}
@@ -547,16 +583,13 @@ angular.mock.$IntervalProvider = function() {
};
-/* jshint -W101 */
-/* The R_ISO8061_STR regex is never going to fit into the 100 char limit!
- * This directive should go inside the anonymous function but a bug in JSHint means that it would
- * not be enacted early enough to prevent the warning.
- */
-var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
-
function jsonStringToDate(string) {
+ // The R_ISO8061_STR regex is never going to fit into the 100 char limit!
+ // eslit-disable-next-line max-len
+ var R_ISO8061_STR = /^(-?\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
+
var match;
- if (match = string.match(R_ISO8061_STR)) {
+ if ((match = string.match(R_ISO8061_STR))) {
var date = new Date(0),
tzHour = 0,
tzMin = 0;
@@ -578,7 +611,7 @@ function toInt(str) {
return parseInt(str, 10);
}
-function padNumber(num, digits, trim) {
+function padNumberInMock(num, digits, trim) {
var neg = '';
if (num < 0) {
neg = '-';
@@ -639,9 +672,10 @@ angular.mock.TzDate = function(offset, timestamp) {
timestamp = self.origDate.getTime();
if (isNaN(timestamp)) {
+ // eslint-disable-next-line no-throw-literal
throw {
- name: "Illegal Argument",
- message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
+ name: 'Illegal Argument',
+ message: 'Arg \'' + tsStr + '\' passed into TzDate constructor is not a valid date string'
};
}
} else {
@@ -727,13 +761,13 @@ angular.mock.TzDate = function(offset, timestamp) {
// provide this method only on browsers that already have it
if (self.toISOString) {
self.toISOString = function() {
- return padNumber(self.origDate.getUTCFullYear(), 4) + '-' +
- padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' +
- padNumber(self.origDate.getUTCDate(), 2) + 'T' +
- padNumber(self.origDate.getUTCHours(), 2) + ':' +
- padNumber(self.origDate.getUTCMinutes(), 2) + ':' +
- padNumber(self.origDate.getUTCSeconds(), 2) + '.' +
- padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z';
+ return padNumberInMock(self.origDate.getUTCFullYear(), 4) + '-' +
+ padNumberInMock(self.origDate.getUTCMonth() + 1, 2) + '-' +
+ padNumberInMock(self.origDate.getUTCDate(), 2) + 'T' +
+ padNumberInMock(self.origDate.getUTCHours(), 2) + ':' +
+ padNumberInMock(self.origDate.getUTCMinutes(), 2) + ':' +
+ padNumberInMock(self.origDate.getUTCSeconds(), 2) + '.' +
+ padNumberInMock(self.origDate.getUTCMilliseconds(), 3) + 'Z';
};
}
@@ -747,7 +781,7 @@ angular.mock.TzDate = function(offset, timestamp) {
angular.forEach(unimplementedMethods, function(methodName) {
self[methodName] = function() {
- throw new Error("Method '" + methodName + "' is not implemented in the TzDate mock");
+ throw new Error('Method \'' + methodName + '\' is not implemented in the TzDate mock');
};
});
@@ -756,7 +790,6 @@ angular.mock.TzDate = function(offset, timestamp) {
//make "tzDateInstance instanceof Date" return true
angular.mock.TzDate.prototype = Date.prototype;
-/* jshint +W101 */
/**
@@ -766,8 +799,11 @@ angular.mock.TzDate.prototype = Date.prototype;
* @description
* Mock implementation of the {@link ng.$animate `$animate`} service. Exposes two additional methods
* for testing animations.
+ *
+ * You need to require the `ngAnimateMock` module in your test suite for instance `beforeEach(module('ngAnimateMock'))`
*/
angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
+ .info({ angularVersion: '1.6.5' })
.config(['$provide', function($provide) {
@@ -931,13 +967,10 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
* @name angular.mock.dump
* @description
*
- * *NOTE*: this is not an injectable instance, just a globally available function.
- *
- * Method for serializing common angular objects (scope, elements, etc..) into strings, useful for
- * debugging.
+ * *NOTE*: This is not an injectable instance, just a globally available function.
*
- * This method is also available on window, where it can be used to display objects on debug
- * console.
+ * Method for serializing common angular objects (scope, elements, etc..) into strings.
+ * It is useful for logging objects to the console when debugging.
*
* @param {*} object - any object to turn into string.
* @return {string} a serialized string of the argument
@@ -1003,8 +1036,10 @@ angular.mock.dump = function(object) {
* Fake HTTP backend implementation suitable for unit testing applications that use the
* {@link ng.$http $http service}.
*
- * *Note*: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
+ * <div class="alert alert-info">
+ * **Note**: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
* development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
+ * </div>
*
* During unit testing, we want our unit tests to run quickly and have no external dependencies so
* we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or
@@ -1028,7 +1063,7 @@ angular.mock.dump = function(object) {
* - `$httpBackend.when` - specifies a backend definition
*
*
- * # Request Expectations vs Backend Definitions
+ * ## Request Expectations vs Backend Definitions
*
* Request expectations provide a way to make assertions about requests made by the application and
* to define responses for those requests. The test will fail if the expected requests are not made
@@ -1084,7 +1119,7 @@ angular.mock.dump = function(object) {
* the request. The response from the first matched definition is returned.
*
*
- * # Flushing HTTP requests
+ * ## Flushing HTTP requests
*
* The $httpBackend used in production always responds to requests asynchronously. If we preserved
* this behavior in unit testing, we'd have to create async unit tests, which are hard to write,
@@ -1094,7 +1129,7 @@ angular.mock.dump = function(object) {
* the async api of the backend, while allowing the test to execute synchronously.
*
*
- * # Unit testing with mock $httpBackend
+ * ## Unit testing with mock $httpBackend
* The following code shows how to setup and use the mock backend when unit testing a controller.
* First we create the controller under test:
*
@@ -1108,18 +1143,20 @@ angular.mock.dump = function(object) {
function MyController($scope, $http) {
var authToken;
- $http.get('/auth.py').success(function(data, status, headers) {
- authToken = headers('A-Token');
- $scope.user = data;
+ $http.get('/auth.py').then(function(response) {
+ authToken = response.headers('A-Token');
+ $scope.user = response.data;
+ }).catch(function() {
+ $scope.status = 'Failed...';
});
$scope.saveMessage = function(message) {
var headers = { 'Authorization': authToken };
$scope.status = 'Saving...';
- $http.post('/add-msg.py', message, { headers: headers } ).success(function(response) {
+ $http.post('/add-msg.py', message, { headers: headers } ).then(function(response) {
$scope.status = '';
- }).error(function() {
+ }).catch(function() {
$scope.status = 'Failed...';
});
};
@@ -1203,18 +1240,97 @@ angular.mock.dump = function(object) {
$httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
// check if the header was sent, if it wasn't the expectation won't
// match the request and the test will fail
- return headers['Authorization'] == 'xxx';
+ return headers['Authorization'] === 'xxx';
}).respond(201, '');
$rootScope.saveMessage('whatever');
$httpBackend.flush();
});
});
- ```
+ ```
+ *
+ * ## Dynamic responses
+ *
+ * You define a response to a request by chaining a call to `respond()` onto a definition or expectation.
+ * If you provide a **callback** as the first parameter to `respond(callback)` then you can dynamically generate
+ * a response based on the properties of the request.
+ *
+ * The `callback` function should be of the form `function(method, url, data, headers, params)`.
+ *
+ * ### Query parameters
+ *
+ * By default, query parameters on request URLs are parsed into the `params` object. So a request URL
+ * of `/list?q=searchstr&orderby=-name` would set `params` to be `{q: 'searchstr', orderby: '-name'}`.
+ *
+ * ### Regex parameter matching
+ *
+ * If an expectation or definition uses a **regex** to match the URL, you can provide an array of **keys** via a
+ * `params` argument. The index of each **key** in the array will match the index of a **group** in the
+ * **regex**.
+ *
+ * The `params` object in the **callback** will now have properties with these keys, which hold the value of the
+ * corresponding **group** in the **regex**.
+ *
+ * This also applies to the `when` and `expect` shortcut methods.
+ *
+ *
+ * ```js
+ * $httpBackend.expect('GET', /\/user\/(.+)/, undefined, undefined, ['id'])
+ * .respond(function(method, url, data, headers, params) {
+ * // for requested url of '/user/1234' params is {id: '1234'}
+ * });
+ *
+ * $httpBackend.whenPATCH(/\/user\/(.+)\/article\/(.+)/, undefined, undefined, ['user', 'article'])
+ * .respond(function(method, url, data, headers, params) {
+ * // for url of '/user/1234/article/567' params is {user: '1234', article: '567'}
+ * });
+ * ```
+ *
+ * ## Matching route requests
+ *
+ * For extra convenience, `whenRoute` and `expectRoute` shortcuts are available. These methods offer colon
+ * delimited matching of the url path, ignoring the query string. This allows declarations
+ * similar to how application routes are configured with `$routeProvider`. Because these methods convert
+ * the definition url to regex, declaration order is important. Combined with query parameter parsing,
+ * the following is possible:
+ *
+ ```js
+ $httpBackend.whenRoute('GET', '/users/:id')
+ .respond(function(method, url, data, headers, params) {
+ return [200, MockUserList[Number(params.id)]];
+ });
+
+ $httpBackend.whenRoute('GET', '/users')
+ .respond(function(method, url, data, headers, params) {
+ var userList = angular.copy(MockUserList),
+ defaultSort = 'lastName',
+ count, pages, isPrevious, isNext;
+
+ // paged api response '/v1/users?page=2'
+ params.page = Number(params.page) || 1;
+
+ // query for last names '/v1/users?q=Archer'
+ if (params.q) {
+ userList = $filter('filter')({lastName: params.q});
+ }
+
+ pages = Math.ceil(userList.length / pagingLength);
+ isPrevious = params.page > 1;
+ isNext = params.page < pages;
+
+ return [200, {
+ count: userList.length,
+ previous: isPrevious,
+ next: isNext,
+ // sort field -> '/v1/users?sortBy=firstName'
+ results: $filter('orderBy')(userList, params.sortBy || defaultSort)
+ .splice((params.page - 1) * pagingLength, pagingLength)
+ }];
+ });
+ ```
*/
-angular.mock.$HttpBackendProvider = function() {
- this.$get = ['$rootScope', '$timeout', createHttpBackendMock];
-};
+angular.mock.$httpBackendDecorator =
+ ['$rootScope', '$timeout', '$delegate', createHttpBackendMock];
/**
* General factory function for $httpBackend mock.
@@ -1235,7 +1351,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
expectations = [],
responses = [],
responsesPush = angular.bind(responses, responses.push),
- copy = angular.copy;
+ copy = angular.copy,
+ // We cache the original backend so that if both ngMock and ngMockE2E override the
+ // service the ngMockE2E version can pass through to the real backend
+ originalHttpBackend = $delegate.$$originalHttpBackend || $delegate;
function createResponse(status, data, headers, statusText) {
if (angular.isFunction(status)) return status;
@@ -1248,12 +1367,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
}
// TODO(vojta): change params to: method, url, data, headers, callback
- function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType) {
+ function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) {
var xhr = new MockXhr(),
expectation = expectations[0],
wasExpected = false;
+ xhr.$$events = eventHandlers;
+ xhr.upload.$$events = uploadEventHandlers;
+
function prettyPrint(data) {
return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
? data
@@ -1262,13 +1384,18 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
function wrapResponse(wrapped) {
if (!$browser && timeout) {
- timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout);
+ if (timeout.then) {
+ timeout.then(handleTimeout);
+ } else {
+ $timeout(handleTimeout, timeout);
+ }
}
+ handleResponse.description = method + ' ' + url;
return handleResponse;
function handleResponse() {
- var response = wrapped.response(method, url, data, headers);
+ var response = wrapped.response(method, url, data, headers, wrapped.params(url));
xhr.$$respHeaders = response[2];
callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
copy(response[3] || ''));
@@ -1313,7 +1440,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
// if $browser specified, we do auto flush all requests
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
} else if (definition.passThrough) {
- $delegate(method, url, data, callback, headers, timeout, withCredentials, responseType);
+ originalHttpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
} else throw new Error('No response defined !');
return;
}
@@ -1331,26 +1458,32 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* Creates a new backend definition.
*
* @param {string} method HTTP method.
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current definition.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
*
* - respond –
- * `{function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers)}`
+ * ```js
+ * {function([status,] data[, headers, statusText])
+ * | function(function(method, url, data, headers, params)}
+ * ```
* – The respond method takes a set of static data to be returned or a function that can
- * return an array containing response status (number), response data (string), response
- * headers (Object), and the text for the status (string). The respond method returns the
- * `requestHandler` object for possible overrides.
+ * return an array containing response status (number), response data (Array|Object|string),
+ * response headers (Object), and the text for the status (string). The respond method returns
+ * the `requestHandler` object for possible overrides.
*/
- $httpBackend.when = function(method, url, data, headers) {
- var definition = new MockHttpExpectation(method, url, data, headers),
+ $httpBackend.when = function(method, url, data, headers, keys) {
+
+ assertArgDefined(arguments, 1, 'url');
+
+ var definition = new MockHttpExpectation(method, url, data, headers, keys),
chain = {
respond: function(status, data, headers, statusText) {
definition.passThrough = undefined;
@@ -1377,9 +1510,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for GET requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1391,9 +1525,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for HEAD requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1405,9 +1540,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for DELETE requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1419,11 +1555,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for POST requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1435,11 +1572,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for PUT requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1451,14 +1589,61 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for JSONP requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
*/
createShortMethods('when');
+ /**
+ * @ngdoc method
+ * @name $httpBackend#whenRoute
+ * @description
+ * Creates a new backend definition that compares only with the requested route.
+ *
+ * @param {string} method HTTP method.
+ * @param {string} url HTTP url string that supports colon param matching.
+ * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
+ * request is handled. You can save this object for later use and invoke `respond` again in
+ * order to change how a matched request is handled. See #when for more info.
+ */
+ $httpBackend.whenRoute = function(method, url) {
+ var pathObj = parseRoute(url);
+ return $httpBackend.when(method, pathObj.regexp, undefined, undefined, pathObj.keys);
+ };
+
+ function parseRoute(url) {
+ var ret = {
+ regexp: url
+ },
+ keys = ret.keys = [];
+
+ if (!url || !angular.isString(url)) return ret;
+
+ url = url
+ .replace(/([().])/g, '\\$1')
+ .replace(/(\/)?:(\w+)([?*])?/g, function(_, slash, key, option) {
+ var optional = option === '?' ? option : null;
+ var star = option === '*' ? option : null;
+ keys.push({ name: key, optional: !!optional });
+ slash = slash || '';
+ return ''
+ + (optional ? '' : slash)
+ + '(?:'
+ + (optional ? slash : '')
+ + (star && '(.+?)' || '([^/]+)')
+ + (optional || '')
+ + ')'
+ + (optional || '');
+ })
+ .replace(/([/$*])/g, '\\$1');
+
+ ret.regexp = new RegExp('^' + url, 'i');
+ return ret;
+ }
/**
* @ngdoc method
@@ -1467,27 +1652,33 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* Creates a new request expectation.
*
* @param {string} method HTTP method.
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current expectation.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
*
* - respond –
- * `{function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers)}`
+ * ```
+ * { function([status,] data[, headers, statusText])
+ * | function(function(method, url, data, headers, params)}
+ * ```
* – The respond method takes a set of static data to be returned or a function that can
- * return an array containing response status (number), response data (string), response
- * headers (Object), and the text for the status (string). The respond method returns the
- * `requestHandler` object for possible overrides.
+ * return an array containing response status (number), response data (Array|Object|string),
+ * response headers (Object), and the text for the status (string). The respond method returns
+ * the `requestHandler` object for possible overrides.
*/
- $httpBackend.expect = function(method, url, data, headers) {
- var expectation = new MockHttpExpectation(method, url, data, headers),
+ $httpBackend.expect = function(method, url, data, headers, keys) {
+
+ assertArgDefined(arguments, 1, 'url');
+
+ var expectation = new MockHttpExpectation(method, url, data, headers, keys),
chain = {
respond: function(status, data, headers, statusText) {
expectation.response = createResponse(status, data, headers, statusText);
@@ -1499,16 +1690,16 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
return chain;
};
-
/**
* @ngdoc method
* @name $httpBackend#expectGET
* @description
* Creates a new request expectation for GET requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled. See #expect for more info.
@@ -1520,9 +1711,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for HEAD requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1534,9 +1726,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for DELETE requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1548,12 +1741,13 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for POST requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1565,12 +1759,13 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for PUT requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1582,12 +1777,13 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format.
* @param {Object=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
@@ -1599,37 +1795,65 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for JSONP requests. For more info see `expect()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives an url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives an url
* and returns true if the url matches the current definition.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. You can save this object for later use and invoke `respond` again in
* order to change how a matched request is handled.
*/
createShortMethods('expect');
+ /**
+ * @ngdoc method
+ * @name $httpBackend#expectRoute
+ * @description
+ * Creates a new request expectation that compares only with the requested route.
+ *
+ * @param {string} method HTTP method.
+ * @param {string} url HTTP url string that supports colon param matching.
+ * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
+ * request is handled. You can save this object for later use and invoke `respond` again in
+ * order to change how a matched request is handled. See #expect for more info.
+ */
+ $httpBackend.expectRoute = function(method, url) {
+ var pathObj = parseRoute(url);
+ return $httpBackend.expect(method, pathObj.regexp, undefined, undefined, pathObj.keys);
+ };
+
/**
* @ngdoc method
* @name $httpBackend#flush
* @description
- * Flushes all pending requests using the trained responses.
+ * Flushes pending requests using the trained responses. Requests are flushed in the order they
+ * were made, but it is also possible to skip one or more requests (for example to have them
+ * flushed later). This is useful for simulating scenarios where responses arrive from the server
+ * in any order.
*
- * @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
- * all pending requests will be flushed. If there are no pending requests when the flush method
- * is called an exception is thrown (as this typically a sign of programming error).
+ * If there are no pending requests to flush when the method is called, an exception is thrown (as
+ * this is typically a sign of programming error).
+ *
+ * @param {number=} count - Number of responses to flush. If undefined/null, all pending requests
+ * (starting after `skip`) will be flushed.
+ * @param {number=} [skip=0] - Number of pending requests to skip. For example, a value of `5`
+ * would skip the first 5 pending requests and start flushing from the 6th onwards.
*/
- $httpBackend.flush = function(count, digest) {
+ $httpBackend.flush = function(count, skip, digest) {
if (digest !== false) $rootScope.$digest();
- if (!responses.length) throw new Error('No pending request to flush !');
+
+ skip = skip || 0;
+ if (skip >= responses.length) throw new Error('No pending request to flush !');
if (angular.isDefined(count) && count !== null) {
while (count--) {
- if (!responses.length) throw new Error('No more pending request to flush !');
- responses.shift()();
+ var part = responses.splice(skip, 1);
+ if (!part.length) throw new Error('No more pending request to flush !');
+ part[0]();
}
} else {
- while (responses.length) {
- responses.shift()();
+ while (responses.length > skip) {
+ responses.splice(skip, 1)[0]();
}
}
$httpBackend.verifyNoOutstandingExpectation(digest);
@@ -1671,9 +1895,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* afterEach($httpBackend.verifyNoOutstandingRequest);
* ```
*/
- $httpBackend.verifyNoOutstandingRequest = function() {
+ $httpBackend.verifyNoOutstandingRequest = function(digest) {
+ if (digest !== false) $rootScope.$digest();
if (responses.length) {
- throw new Error('Unflushed requests: ' + responses.length);
+ var unflushedDescriptions = responses.map(function(res) { return res.description; });
+ throw new Error('Unflushed requests: ' + responses.length + '\n ' +
+ unflushedDescriptions.join('\n '));
}
};
@@ -1691,31 +1918,60 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
responses.length = 0;
};
+ $httpBackend.$$originalHttpBackend = originalHttpBackend;
+
return $httpBackend;
function createShortMethods(prefix) {
angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) {
- $httpBackend[prefix + method] = function(url, headers) {
- return $httpBackend[prefix](method, url, undefined, headers);
+ $httpBackend[prefix + method] = function(url, headers, keys) {
+ assertArgDefined(arguments, 0, 'url');
+
+ // Change url to `null` if `undefined` to stop it throwing an exception further down
+ if (angular.isUndefined(url)) url = null;
+
+ return $httpBackend[prefix](method, url, undefined, headers, keys);
};
});
angular.forEach(['PUT', 'POST', 'PATCH'], function(method) {
- $httpBackend[prefix + method] = function(url, data, headers) {
- return $httpBackend[prefix](method, url, data, headers);
+ $httpBackend[prefix + method] = function(url, data, headers, keys) {
+ assertArgDefined(arguments, 0, 'url');
+
+ // Change url to `null` if `undefined` to stop it throwing an exception further down
+ if (angular.isUndefined(url)) url = null;
+
+ return $httpBackend[prefix](method, url, data, headers, keys);
};
});
}
}
-function MockHttpExpectation(method, url, data, headers) {
+function assertArgDefined(args, index, name) {
+ if (args.length > index && angular.isUndefined(args[index])) {
+ throw new Error('Undefined argument `' + name + '`; the argument is provided but not defined');
+ }
+}
+
+
+function MockHttpExpectation(method, url, data, headers, keys) {
+
+ function getUrlParams(u) {
+ var params = u.slice(u.indexOf('?') + 1).split('&');
+ return params.sort();
+ }
+
+ function compareUrl(u) {
+ return (url.slice(0, url.indexOf('?')) === u.slice(0, u.indexOf('?')) &&
+ getUrlParams(url).join() === getUrlParams(u).join());
+ }
this.data = data;
this.headers = headers;
this.match = function(m, u, d, h) {
- if (method != m) return false;
+ if (method !== m) return false;
if (!this.matchUrl(u)) return false;
if (angular.isDefined(d) && !this.matchData(d)) return false;
if (angular.isDefined(h) && !this.matchHeaders(h)) return false;
@@ -1726,7 +1982,7 @@ function MockHttpExpectation(method, url, data, headers) {
if (!url) return true;
if (angular.isFunction(url.test)) return url.test(u);
if (angular.isFunction(url)) return url(u);
- return url == u;
+ return (url === u || compareUrl(u));
};
this.matchHeaders = function(h) {
@@ -1742,12 +1998,66 @@ function MockHttpExpectation(method, url, data, headers) {
if (data && !angular.isString(data)) {
return angular.equals(angular.fromJson(angular.toJson(data)), angular.fromJson(d));
}
+ // eslint-disable-next-line eqeqeq
return data == d;
};
this.toString = function() {
return method + ' ' + url;
};
+
+ this.params = function(u) {
+ return angular.extend(parseQuery(), pathParams());
+
+ function pathParams() {
+ var keyObj = {};
+ if (!url || !angular.isFunction(url.test) || !keys || keys.length === 0) return keyObj;
+
+ var m = url.exec(u);
+ if (!m) return keyObj;
+ for (var i = 1, len = m.length; i < len; ++i) {
+ var key = keys[i - 1];
+ var val = m[i];
+ if (key && val) {
+ keyObj[key.name || key] = val;
+ }
+ }
+
+ return keyObj;
+ }
+
+ function parseQuery() {
+ var obj = {}, key_value, key,
+ queryStr = u.indexOf('?') > -1
+ ? u.substring(u.indexOf('?') + 1)
+ : '';
+
+ angular.forEach(queryStr.split('&'), function(keyValue) {
+ if (keyValue) {
+ key_value = keyValue.replace(/\+/g,'%20').split('=');
+ key = tryDecodeURIComponent(key_value[0]);
+ if (angular.isDefined(key)) {
+ var val = angular.isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
+ if (!hasOwnProperty.call(obj, key)) {
+ obj[key] = val;
+ } else if (angular.isArray(obj[key])) {
+ obj[key].push(val);
+ } else {
+ obj[key] = [obj[key],val];
+ }
+ }
+ }
+ });
+ return obj;
+ }
+ function tryDecodeURIComponent(value) {
+ try {
+ return decodeURIComponent(value);
+ } catch (e) {
+ // Ignore any invalid uri component
+ }
+ }
+ };
}
function createMockXhr() {
@@ -1787,7 +2097,7 @@ function MockXhr() {
header = undefined;
angular.forEach(this.$$respHeaders, function(headerVal, headerName) {
- if (!header && angular.lowercase(headerName) == name) header = headerVal;
+ if (!header && angular.lowercase(headerName) === name) header = headerVal;
});
return header;
};
@@ -1802,6 +2112,20 @@ function MockXhr() {
};
this.abort = angular.noop;
+
+ // This section simulates the events on a real XHR object (and the upload object)
+ // When we are testing $httpBackend (inside the angular project) we make partial use of this
+ // but store the events directly ourselves on `$$events`, instead of going through the `addEventListener`
+ this.$$events = {};
+ this.addEventListener = function(name, listener) {
+ if (angular.isUndefined(this.$$events[name])) this.$$events[name] = [];
+ this.$$events[name].push(listener);
+ };
+
+ this.upload = {
+ $$events: {},
+ addEventListener: this.addEventListener
+ };
}
@@ -1846,7 +2170,7 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
function formatPendingTasksAsString(tasks) {
var result = [];
angular.forEach(tasks, function(task) {
- result.push('{id: ' + task.id + ', ' + 'time: ' + task.time + '}');
+ result.push('{id: ' + task.id + ', time: ' + task.time + '}');
});
return result.join(', ');
@@ -1886,10 +2210,12 @@ angular.mock.$RAFDecorator = ['$delegate', function($delegate) {
/**
*
*/
+var originalRootElement;
angular.mock.$RootElementProvider = function() {
- this.$get = function() {
- return angular.element('<div ng-app></div>');
- };
+ this.$get = ['$injector', function($injector) {
+ originalRootElement = angular.element('<div ng-app></div>').data('$injector', $injector);
+ return originalRootElement;
+ }];
};
/**
@@ -1899,6 +2225,10 @@ angular.mock.$RootElementProvider = function() {
* A decorator for {@link ng.$controller} with additional `bindings` parameter, useful when testing
* controllers of directives that use {@link $compile#-bindtocontroller- `bindToController`}.
*
+ * Depending on the value of
+ * {@link ng.$compileProvider#preAssignBindingsEnabled `preAssignBindingsEnabled()`}, the properties
+ * will be bound before or after invoking the constructor.
+ *
*
* ## Example
*
@@ -1917,18 +2247,24 @@ angular.mock.$RootElementProvider = function() {
* // Controller definition ...
*
* myMod.controller('MyDirectiveController', ['$log', function($log) {
- * $log.info(this.name);
+ * this.log = function() {
+ * $log.info(this.name);
+ * };
* }]);
*
*
* // In a test ...
*
* describe('myDirectiveController', function() {
- * it('should write the bound name to the log', inject(function($controller, $log) {
- * var ctrl = $controller('MyDirectiveController', { /* no locals &#42;/ }, { name: 'Clark Kent' });
- * expect(ctrl.name).toEqual('Clark Kent');
- * expect($log.info.logs).toEqual(['Clark Kent']);
- * }));
+ * describe('log()', function() {
+ * it('should write the bound name to the log', inject(function($controller, $log) {
+ * var ctrl = $controller('MyDirectiveController', { /* no locals &#42;/ }, { name: 'Clark Kent' });
+ * ctrl.log();
+ *
+ * expect(ctrl.name).toEqual('Clark Kent');
+ * expect($log.info.logs).toEqual(['Clark Kent']);
+ * }));
+ * });
* });
*
* ```
@@ -1940,26 +2276,94 @@ angular.mock.$RootElementProvider = function() {
* * check if a controller with given name is registered via `$controllerProvider`
* * check if evaluating the string on the current scope returns a constructor
* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
- * `window` object (not recommended)
+ * `window` object (deprecated, not recommended)
*
* The string can use the `controller as property` syntax, where the controller instance is published
* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
* to work correctly.
*
* @param {Object} locals Injection locals for Controller.
+ * @param {Object=} bindings Properties to add to the controller instance. This is used to simulate
+ * the `bindToController` feature and simplify certain kinds of tests.
+ * @return {Object} Instance of given controller.
+ */
+function createControllerDecorator(compileProvider) {
+ angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
+ return function(expression, locals, later, ident) {
+ if (later && typeof later === 'object') {
+ var preAssignBindingsEnabled = compileProvider.preAssignBindingsEnabled();
+
+ var instantiate = $delegate(expression, locals, true, ident);
+ if (preAssignBindingsEnabled) {
+ angular.extend(instantiate.instance, later);
+ }
+
+ var instance = instantiate();
+ if (!preAssignBindingsEnabled || instance !== instantiate.instance) {
+ angular.extend(instance, later);
+ }
+
+ return instance;
+ }
+ return $delegate(expression, locals, later, ident);
+ };
+ }];
+
+ return angular.mock.$ControllerDecorator;
+}
+
+/**
+ * @ngdoc service
+ * @name $componentController
+ * @description
+ * A service that can be used to create instances of component controllers. Useful for unit-testing.
+ *
+ * Be aware that the controller will be instantiated and attached to the scope as specified in
+ * the component definition object. If you do not provide a `$scope` object in the `locals` param
+ * then the helper will create a new isolated scope as a child of `$rootScope`.
+ *
+ * If you are using `$element` or `$attrs` in the controller, make sure to provide them as `locals`.
+ * The `$element` must be a jqLite-wrapped DOM element, and `$attrs` should be an object that
+ * has all properties / functions that you are using in the controller. If this is getting too complex,
+ * you should compile the component instead and access the component's controller via the
+ * {@link angular.element#methods `controller`} function.
+ *
+ * See also the section on {@link guide/component#unit-testing-component-controllers unit-testing component controllers}
+ * in the guide.
+ *
+ * @param {string} componentName the name of the component whose controller we want to instantiate
+ * @param {Object} locals Injection locals for Controller.
* @param {Object=} bindings Properties to add to the controller before invoking the constructor. This is used
* to simulate the `bindToController` feature and simplify certain kinds of tests.
- * @return {Object} Instance of given controller.
+ * @param {string=} ident Override the property name to use when attaching the controller to the scope.
+ * @return {Object} Instance of requested controller.
*/
-angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
- return function(expression, locals, later, ident) {
- if (later && typeof later === 'object') {
- var create = $delegate(expression, locals, true, ident);
- angular.extend(create.instance, later);
- return create();
- }
- return $delegate(expression, locals, later, ident);
- };
+angular.mock.$ComponentControllerProvider = ['$compileProvider',
+ function ComponentControllerProvider($compileProvider) {
+ this.$get = ['$controller','$injector', '$rootScope', function($controller, $injector, $rootScope) {
+ return function $componentController(componentName, locals, bindings, ident) {
+ // get all directives associated to the component name
+ var directives = $injector.get(componentName + 'Directive');
+ // look for those directives that are components
+ var candidateDirectives = directives.filter(function(directiveInfo) {
+ // components have controller, controllerAs and restrict:'E'
+ return directiveInfo.controller && directiveInfo.controllerAs && directiveInfo.restrict === 'E';
+ });
+ // check if valid directives found
+ if (candidateDirectives.length === 0) {
+ throw new Error('No component found');
+ }
+ if (candidateDirectives.length > 1) {
+ throw new Error('Too many components found');
+ }
+ // get the info of the component
+ var directiveInfo = candidateDirectives[0];
+ // create a scope if needed
+ locals = locals || {};
+ locals.$scope = locals.$scope || $rootScope.$new(true);
+ return $controller(directiveInfo.controller, locals, bindings, ident || directiveInfo.controllerAs);
+ };
+ }];
}];
@@ -1978,20 +2382,50 @@ angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
*
* <div doc-module-components="ngMock"></div>
*
+ * @installation
+ *
+ * First, download the file:
+ * * [Google CDN](https://developers.google.com/speed/libraries/devguide#angularjs) e.g.
+ * `"//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-mocks.js"`
+ * * [NPM](https://www.npmjs.com/) e.g. `npm install angular-mocks@X.Y.Z`
+ * * [Yarn](https://yarnpkg.com) e.g. `yarn add angular-mocks@X.Y.Z`
+ * * [Bower](http://bower.io) e.g. `bower install angular-mocks#X.Y.Z`
+ * * [code.angularjs.org](https://code.angularjs.org/) (discouraged for production use) e.g.
+ * `"//code.angularjs.org/X.Y.Z/angular-mocks.js"`
+ *
+ * where X.Y.Z is the AngularJS version you are running.
+ *
+ * Then, configure your test runner to load `angular-mocks.js` after `angular.js`.
+ * This example uses <a href="http://karma-runner.github.io/">Karma</a>:
+ *
+ * ```
+ * config.set({
+ * files: [
+ * 'build/angular.js', // and other module files you need
+ * 'build/angular-mocks.js',
+ * '<path/to/application/files>',
+ * '<path/to/spec/files>'
+ * ]
+ * });
+ * ```
+ *
+ * Including the `angular-mocks.js` file automatically adds the `ngMock` module, so your tests
+ * are ready to go!
*/
angular.module('ngMock', ['ng']).provider({
$browser: angular.mock.$BrowserProvider,
$exceptionHandler: angular.mock.$ExceptionHandlerProvider,
$log: angular.mock.$LogProvider,
$interval: angular.mock.$IntervalProvider,
- $httpBackend: angular.mock.$HttpBackendProvider,
- $rootElement: angular.mock.$RootElementProvider
-}).config(['$provide', function($provide) {
+ $rootElement: angular.mock.$RootElementProvider,
+ $componentController: angular.mock.$ComponentControllerProvider
+}).config(['$provide', '$compileProvider', function($provide, $compileProvider) {
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
$provide.decorator('$rootScope', angular.mock.$RootScopeDecorator);
- $provide.decorator('$controller', angular.mock.$ControllerDecorator);
-}]);
+ $provide.decorator('$controller', createControllerDecorator($compileProvider));
+ $provide.decorator('$httpBackend', angular.mock.$httpBackendDecorator);
+}]).info({ angularVersion: '1.6.5' });
/**
* @ngdoc module
@@ -2006,7 +2440,7 @@ angular.module('ngMock', ['ng']).provider({
*/
angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
-}]);
+}]).info({ angularVersion: '1.6.5' });
/**
* @ngdoc service
@@ -2016,8 +2450,10 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* Fake HTTP backend implementation suitable for end-to-end testing or backend-less development of
* applications that use the {@link ng.$http $http service}.
*
- * *Note*: For fake http backend implementation suitable for unit testing please see
+ * <div class="alert alert-info">
+ * **Note**: For fake http backend implementation suitable for unit testing please see
* {@link ngMock.$httpBackend unit-testing $httpBackend mock}.
+ * </div>
*
* This implementation can be used to respond with static or dynamic responses via the `when` api
* and its shortcuts (`whenGET`, `whenPOST`, etc) and optionally pass through requests to the
@@ -2038,9 +2474,9 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* on the `ngMockE2E` and your application modules and defines the fake backend:
*
* ```js
- * myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
+ * var myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
* myAppDev.run(function($httpBackend) {
- * phones = [{name: 'phone1'}, {name: 'phone2'}];
+ * var phones = [{name: 'phone1'}, {name: 'phone2'}];
*
* // returns the current list of phones
* $httpBackend.whenGET('/phones').respond(phones);
@@ -2051,12 +2487,74 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* phones.push(phone);
* return [200, phone, {}];
* });
- * $httpBackend.whenGET(/^\/templates\//).passThrough();
+ * $httpBackend.whenGET(/^\/templates\//).passThrough(); // Requests for templates are handled by the real server
* //...
* });
* ```
*
* Afterwards, bootstrap your app with this new module.
+ *
+ * ## Example
+ * <example name="httpbackend-e2e-testing" module="myAppE2E" deps="angular-mocks.js">
+ * <file name="app.js">
+ * var myApp = angular.module('myApp', []);
+ *
+ * myApp.controller('MainCtrl', function MainCtrl($http) {
+ * var ctrl = this;
+ *
+ * ctrl.phones = [];
+ * ctrl.newPhone = {
+ * name: ''
+ * };
+ *
+ * ctrl.getPhones = function() {
+ * $http.get('/phones').then(function(response) {
+ * ctrl.phones = response.data;
+ * });
+ * };
+ *
+ * ctrl.addPhone = function(phone) {
+ * $http.post('/phones', phone).then(function() {
+ * ctrl.newPhone = {name: ''};
+ * return ctrl.getPhones();
+ * });
+ * };
+ *
+ * ctrl.getPhones();
+ * });
+ * </file>
+ * <file name="e2e.js">
+ * var myAppDev = angular.module('myAppE2E', ['myApp', 'ngMockE2E']);
+ *
+ * myAppDev.run(function($httpBackend) {
+ * var phones = [{name: 'phone1'}, {name: 'phone2'}];
+ *
+ * // returns the current list of phones
+ * $httpBackend.whenGET('/phones').respond(phones);
+ *
+ * // adds a new phone to the phones array
+ * $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
+ * var phone = angular.fromJson(data);
+ * phones.push(phone);
+ * return [200, phone, {}];
+ * });
+ * });
+ * </file>
+ * <file name="index.html">
+ * <div ng-controller="MainCtrl as $ctrl">
+ * <form name="newPhoneForm" ng-submit="$ctrl.addPhone($ctrl.newPhone)">
+ * <input type="text" ng-model="$ctrl.newPhone.name">
+ * <input type="submit" value="Add Phone">
+ * </form>
+ * <h1>Phones</h1>
+ * <ul>
+ * <li ng-repeat="phone in $ctrl.phones">{{phone.name}}</li>
+ * </ul>
+ * </div>
+ * </file>
+ * </example>
+ *
+ *
*/
/**
@@ -2067,21 +2565,26 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* Creates a new backend definition.
*
* @param {string} method HTTP method.
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
- * @param {(string|RegExp)=} data HTTP request body.
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
+ * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current definition.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
*
* - respond –
- * `{function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers)}`
+ * ```
+ * { function([status,] data[, headers, statusText])
+ * | function(function(method, url, data, headers, params)}
+ * ```
* – The respond method takes a set of static data to be returned or a function that can return
- * an array containing response status (number), response data (string), response headers
- * (Object), and the text for the status (string).
+ * an array containing response status (number), response data (Array|Object|string), response
+ * headers (Object), and the text for the status (string).
* - passThrough – `{function()}` – Any request matching a backend definition with
* `passThrough` handler will be passed through to the real backend (an XHR request will be made
* to the server.)
@@ -2095,9 +2598,11 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for GET requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2110,9 +2615,11 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for HEAD requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2125,9 +2632,11 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for DELETE requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2140,10 +2649,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for POST requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
- * @param {(string|RegExp)=} data HTTP request body.
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
+ * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2156,10 +2668,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for PUT requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
- * @param {(string|RegExp)=} data HTTP request body.
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
+ * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2172,10 +2687,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for PATCH requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
- * @param {(string|RegExp)=} data HTTP request body.
+ * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
+ * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2188,8 +2706,23 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for JSONP requests. For more info see `when()`.
*
- * @param {string|RegExp|function(string)} url HTTP url or function that receives a url
+ * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
+ * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
+ * {@link ngMock.$httpBackend $httpBackend mock}.
+ * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
+ * control how a matched request is handled. You can save this object for later use and invoke
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
+ */
+/**
+ * @ngdoc method
+ * @name $httpBackend#whenRoute
+ * @module ngMockE2E
+ * @description
+ * Creates a new backend definition that compares only with the requested route.
+ *
+ * @param {string} method HTTP method.
+ * @param {string} url HTTP url string that supports colon param matching.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. You can save this object for later use and invoke
* `respond` or `passThrough` again in order to change how a matched request is handled.
@@ -2225,6 +2758,7 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
* @ngdoc method
* @name $rootScope.Scope#$countChildScopes
* @module ngMock
+ * @this $rootScope.Scope
* @description
* Counts all the direct and indirect child scopes of the current scope.
*
@@ -2233,7 +2767,6 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
* @returns {number} Total number of child scopes.
*/
function countChildScopes() {
- // jshint validthis: true
var count = 0; // exclude the current scope
var pendingChildHeads = [this.$$childHead];
var currentScope;
@@ -2255,6 +2788,7 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
/**
* @ngdoc method
* @name $rootScope.Scope#$countWatchers
+ * @this $rootScope.Scope
* @module ngMock
* @description
* Counts all the watchers of direct and indirect child scopes of the current scope.
@@ -2265,7 +2799,6 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
* @returns {number} Total number of watchers.
*/
function countWatchers() {
- // jshint validthis: true
var count = this.$$watchers ? this.$$watchers.length : 0; // include the current scope
var pendingChildHeads = [this.$$childHead];
var currentScope;
@@ -2285,11 +2818,16 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
}];
-if (window.jasmine || window.mocha) {
+(function(jasmineOrMocha) {
+
+ if (!jasmineOrMocha) {
+ return;
+ }
var currentSpec = null,
+ injectorState = new InjectorState(),
annotatedFunctions = [],
- isSpecRunning = function() {
+ wasInjectorCreated = function() {
return !!currentSpec;
};
@@ -2301,46 +2839,6 @@ if (window.jasmine || window.mocha) {
return angular.mock.$$annotate.apply(this, arguments);
};
-
- (window.beforeEach || window.setup)(function() {
- annotatedFunctions = [];
- currentSpec = this;
- });
-
- (window.afterEach || window.teardown)(function() {
- var injector = currentSpec.$injector;
-
- annotatedFunctions.forEach(function(fn) {
- delete fn.$inject;
- });
-
- angular.forEach(currentSpec.$modules, function(module) {
- if (module && module.$$hashKey) {
- module.$$hashKey = undefined;
- }
- });
-
- currentSpec.$injector = null;
- currentSpec.$modules = null;
- currentSpec = null;
-
- if (injector) {
- injector.get('$rootElement').off();
- }
-
- // clean up jquery's fragment cache
- angular.forEach(angular.element.fragments, function(val, key) {
- delete angular.element.fragments[key];
- });
-
- MockXhr.$$lastInstance = null;
-
- angular.forEach(angular.callbacks, function(val, key) {
- delete angular.callbacks[key];
- });
- angular.callbacks.counter = 0;
- });
-
/**
* @ngdoc function
* @name angular.mock.module
@@ -2361,30 +2859,188 @@ if (window.jasmine || window.mocha) {
* {@link auto.$provide $provide}.value, the key being the string name (or token) to associate
* with the value on the injector.
*/
- window.module = angular.mock.module = function() {
+ var module = window.module = angular.mock.module = function() {
var moduleFns = Array.prototype.slice.call(arguments, 0);
- return isSpecRunning() ? workFn() : workFn;
+ return wasInjectorCreated() ? workFn() : workFn;
/////////////////////
function workFn() {
if (currentSpec.$injector) {
throw new Error('Injector already created, can not register a module!');
} else {
- var modules = currentSpec.$modules || (currentSpec.$modules = []);
+ var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
angular.forEach(moduleFns, function(module) {
if (angular.isObject(module) && !angular.isArray(module)) {
- modules.push(function($provide) {
+ fn = ['$provide', function($provide) {
angular.forEach(module, function(value, key) {
$provide.value(key, value);
});
- });
+ }];
+ } else {
+ fn = module;
+ }
+ if (currentSpec.$providerInjector) {
+ currentSpec.$providerInjector.invoke(fn);
} else {
- modules.push(module);
+ modules.push(fn);
}
});
}
}
};
+ module.$$beforeAllHook = (window.before || window.beforeAll);
+ module.$$afterAllHook = (window.after || window.afterAll);
+
+ // purely for testing ngMock itself
+ module.$$currentSpec = function(to) {
+ if (arguments.length === 0) return to;
+ currentSpec = to;
+ };
+
+ /**
+ * @ngdoc function
+ * @name angular.mock.module.sharedInjector
+ * @description
+ *
+ * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
+ *
+ * This function ensures a single injector will be used for all tests in a given describe context.
+ * This contrasts with the default behaviour where a new injector is created per test case.
+ *
+ * Use sharedInjector when you want to take advantage of Jasmine's `beforeAll()`, or mocha's
+ * `before()` methods. Call `module.sharedInjector()` before you setup any other hooks that
+ * will create (i.e call `module()`) or use (i.e call `inject()`) the injector.
+ *
+ * You cannot call `sharedInjector()` from within a context already using `sharedInjector()`.
+ *
+ * ## Example
+ *
+ * Typically beforeAll is used to make many assertions about a single operation. This can
+ * cut down test run-time as the test setup doesn't need to be re-run, and enabling focussed
+ * tests each with a single assertion.
+ *
+ * ```js
+ * describe("Deep Thought", function() {
+ *
+ * module.sharedInjector();
+ *
+ * beforeAll(module("UltimateQuestion"));
+ *
+ * beforeAll(inject(function(DeepThought) {
+ * expect(DeepThought.answer).toBeUndefined();
+ * DeepThought.generateAnswer();
+ * }));
+ *
+ * it("has calculated the answer correctly", inject(function(DeepThought) {
+ * // Because of sharedInjector, we have access to the instance of the DeepThought service
+ * // that was provided to the beforeAll() hook. Therefore we can test the generated answer
+ * expect(DeepThought.answer).toBe(42);
+ * }));
+ *
+ * it("has calculated the answer within the expected time", inject(function(DeepThought) {
+ * expect(DeepThought.runTimeMillennia).toBeLessThan(8000);
+ * }));
+ *
+ * it("has double checked the answer", inject(function(DeepThought) {
+ * expect(DeepThought.absolutelySureItIsTheRightAnswer).toBe(true);
+ * }));
+ *
+ * });
+ *
+ * ```
+ */
+ module.sharedInjector = function() {
+ if (!(module.$$beforeAllHook && module.$$afterAllHook)) {
+ throw Error('sharedInjector() cannot be used unless your test runner defines beforeAll/afterAll');
+ }
+
+ var initialized = false;
+
+ module.$$beforeAllHook(/** @this */ function() {
+ if (injectorState.shared) {
+ injectorState.sharedError = Error('sharedInjector() cannot be called inside a context that has already called sharedInjector()');
+ throw injectorState.sharedError;
+ }
+ initialized = true;
+ currentSpec = this;
+ injectorState.shared = true;
+ });
+
+ module.$$afterAllHook(function() {
+ if (initialized) {
+ injectorState = new InjectorState();
+ module.$$cleanup();
+ } else {
+ injectorState.sharedError = null;
+ }
+ });
+ };
+
+ module.$$beforeEach = function() {
+ if (injectorState.shared && currentSpec && currentSpec !== this) {
+ var state = currentSpec;
+ currentSpec = this;
+ angular.forEach(['$injector','$modules','$providerInjector', '$injectorStrict'], function(k) {
+ currentSpec[k] = state[k];
+ state[k] = null;
+ });
+ } else {
+ currentSpec = this;
+ originalRootElement = null;
+ annotatedFunctions = [];
+ }
+ };
+
+ module.$$afterEach = function() {
+ if (injectorState.cleanupAfterEach()) {
+ module.$$cleanup();
+ }
+ };
+
+ module.$$cleanup = function() {
+ var injector = currentSpec.$injector;
+
+ annotatedFunctions.forEach(function(fn) {
+ delete fn.$inject;
+ });
+
+ currentSpec.$injector = null;
+ currentSpec.$modules = null;
+ currentSpec.$providerInjector = null;
+ currentSpec = null;
+
+ if (injector) {
+ // Ensure `$rootElement` is instantiated, before checking `originalRootElement`
+ var $rootElement = injector.get('$rootElement');
+ var rootNode = $rootElement && $rootElement[0];
+ var cleanUpNodes = !originalRootElement ? [] : [originalRootElement[0]];
+ if (rootNode && (!originalRootElement || rootNode !== originalRootElement[0])) {
+ cleanUpNodes.push(rootNode);
+ }
+ angular.element.cleanData(cleanUpNodes);
+
+ // Ensure `$destroy()` is available, before calling it
+ // (a mocked `$rootScope` might not implement it (or not even be an object at all))
+ var $rootScope = injector.get('$rootScope');
+ if ($rootScope && $rootScope.$destroy) $rootScope.$destroy();
+ }
+
+ // clean up jquery's fragment cache
+ angular.forEach(angular.element.fragments, function(val, key) {
+ delete angular.element.fragments[key];
+ });
+
+ MockXhr.$$lastInstance = null;
+
+ angular.forEach(angular.callbacks, function(val, key) {
+ delete angular.callbacks[key];
+ });
+ angular.callbacks.$$counter = 0;
+ };
+
+ (window.beforeEach || window.setup)(module.$$beforeEach);
+ (window.afterEach || window.teardown)(module.$$afterEach);
+
/**
* @ngdoc function
* @name angular.mock.inject
@@ -2409,7 +3065,7 @@ if (window.jasmine || window.mocha) {
* These are ignored by the injector when the reference name is resolved.
*
* For example, the parameter `_myService_` would be resolved as the reference `myService`.
- * Since it is available in the function body as _myService_, we can then assign it to a variable
+ * Since it is available in the function body as `_myService_`, we can then assign it to a variable
* defined in an outer scope.
*
* ```
@@ -2473,7 +3129,7 @@ if (window.jasmine || window.mocha) {
- var ErrorAddingDeclarationLocationStack = function(e, errorForStack) {
+ var ErrorAddingDeclarationLocationStack = function ErrorAddingDeclarationLocationStack(e, errorForStack) {
this.message = e.message;
this.name = e.name;
if (e.line) this.line = e.line;
@@ -2482,16 +3138,25 @@ if (window.jasmine || window.mocha) {
this.stack = e.stack + '\n' + errorForStack.stack;
if (e.stackArray) this.stackArray = e.stackArray;
};
- ErrorAddingDeclarationLocationStack.prototype.toString = Error.prototype.toString;
+ ErrorAddingDeclarationLocationStack.prototype = Error.prototype;
window.inject = angular.mock.inject = function() {
var blockFns = Array.prototype.slice.call(arguments, 0);
var errorForStack = new Error('Declaration Location');
- return isSpecRunning() ? workFn.call(currentSpec) : workFn;
+ // IE10+ and PhanthomJS do not set stack trace information, until the error is thrown
+ if (!errorForStack.stack) {
+ try {
+ throw errorForStack;
+ } catch (e) { /* empty */ }
+ }
+ return wasInjectorCreated() ? WorkFn.call(currentSpec) : WorkFn;
/////////////////////
- function workFn() {
+ function WorkFn() {
var modules = currentSpec.$modules || [];
var strictDi = !!currentSpec.$injectorStrict;
+ modules.unshift(['$injector', function($injector) {
+ currentSpec.$providerInjector = $injector;
+ }]);
modules.unshift('ngMock');
modules.unshift('ng');
var injector = currentSpec.$injector;
@@ -2499,7 +3164,7 @@ if (window.jasmine || window.mocha) {
if (strictDi) {
// If strictDi is enabled, annotate the providerInjector blocks
angular.forEach(modules, function(moduleFn) {
- if (typeof moduleFn === "function") {
+ if (typeof moduleFn === 'function') {
angular.injector.$$annotate(moduleFn);
}
});
@@ -2514,9 +3179,7 @@ if (window.jasmine || window.mocha) {
injector.annotate(blockFns[i]);
}
try {
- /* jshint -W040 *//* Jasmine explicitly provides a `this` object when calling functions */
injector.invoke(blockFns[i] || angular.noop, this);
- /* jshint +W040 */
} catch (e) {
if (e.stack && errorForStack) {
throw new ErrorAddingDeclarationLocationStack(e, errorForStack);
@@ -2532,7 +3195,7 @@ if (window.jasmine || window.mocha) {
angular.mock.inject.strictDi = function(value) {
value = arguments.length ? !!value : true;
- return isSpecRunning() ? workFn() : workFn;
+ return wasInjectorCreated() ? workFn() : workFn;
function workFn() {
if (value !== currentSpec.$injectorStrict) {
@@ -2544,7 +3207,229 @@ if (window.jasmine || window.mocha) {
}
}
};
-}
+
+ function InjectorState() {
+ this.shared = false;
+ this.sharedError = null;
+
+ this.cleanupAfterEach = function() {
+ return !this.shared || this.sharedError;
+ };
+ }
+})(window.jasmine || window.mocha);
+
+'use strict';
+
+(function() {
+ /**
+ * Triggers a browser event. Attempts to choose the right event if one is
+ * not specified.
+ *
+ * @param {Object} element Either a wrapped jQuery/jqLite node or a DOMElement
+ * @param {string} eventType Optional event type
+ * @param {Object=} eventData An optional object which contains additional event data (such as x,y
+ * coordinates, keys, etc...) that are passed into the event when triggered
+ */
+ window.browserTrigger = function browserTrigger(element, eventType, eventData) {
+ if (element && !element.nodeName) element = element[0];
+ if (!element) return;
+
+ eventData = eventData || {};
+ var relatedTarget = eventData.relatedTarget || element;
+ var keys = eventData.keys;
+ var x = eventData.x;
+ var y = eventData.y;
+
+ var inputType = (element.type) ? element.type.toLowerCase() : null,
+ nodeName = element.nodeName.toLowerCase();
+ if (!eventType) {
+ eventType = {
+ 'text': 'change',
+ 'textarea': 'change',
+ 'hidden': 'change',
+ 'password': 'change',
+ 'button': 'click',
+ 'submit': 'click',
+ 'reset': 'click',
+ 'image': 'click',
+ 'checkbox': 'click',
+ 'radio': 'click',
+ 'select-one': 'change',
+ 'select-multiple': 'change',
+ '_default_': 'click'
+ }[inputType || '_default_'];
+ }
+
+ if (nodeName === 'option') {
+ element.parentNode.value = element.value;
+ element = element.parentNode;
+ eventType = 'change';
+ }
+
+ keys = keys || [];
+ function pressed(key) {
+ return keys.indexOf(key) !== -1;
+ }
+
+ var evnt;
+ if (/transitionend/.test(eventType)) {
+ if (window.WebKitTransitionEvent) {
+ evnt = new window.WebKitTransitionEvent(eventType, eventData);
+ evnt.initEvent(eventType, false, true);
+ } else {
+ try {
+ evnt = new window.TransitionEvent(eventType, eventData);
+ } catch (e) {
+ evnt = window.document.createEvent('TransitionEvent');
+ evnt.initTransitionEvent(eventType, null, null, null, eventData.elapsedTime || 0);
+ }
+ }
+ } else if (/animationend/.test(eventType)) {
+ if (window.WebKitAnimationEvent) {
+ evnt = new window.WebKitAnimationEvent(eventType, eventData);
+ evnt.initEvent(eventType, false, true);
+ } else {
+ try {
+ evnt = new window.AnimationEvent(eventType, eventData);
+ } catch (e) {
+ evnt = window.document.createEvent('AnimationEvent');
+ evnt.initAnimationEvent(eventType, null, null, null, eventData.elapsedTime || 0);
+ }
+ }
+ } else if (/touch/.test(eventType) && supportsTouchEvents()) {
+ evnt = createTouchEvent(element, eventType, x, y);
+ } else if (/key/.test(eventType)) {
+ evnt = window.document.createEvent('Events');
+ evnt.initEvent(eventType, eventData.bubbles, eventData.cancelable);
+ evnt.view = window;
+ evnt.ctrlKey = pressed('ctrl');
+ evnt.altKey = pressed('alt');
+ evnt.shiftKey = pressed('shift');
+ evnt.metaKey = pressed('meta');
+ evnt.keyCode = eventData.keyCode;
+ evnt.charCode = eventData.charCode;
+ evnt.which = eventData.which;
+ } else {
+ evnt = window.document.createEvent('MouseEvents');
+ x = x || 0;
+ y = y || 0;
+ evnt.initMouseEvent(eventType, true, true, window, 0, x, y, x, y, pressed('ctrl'),
+ pressed('alt'), pressed('shift'), pressed('meta'), 0, relatedTarget);
+ }
+
+ /* we're unable to change the timeStamp value directly so this
+ * is only here to allow for testing where the timeStamp value is
+ * read */
+ evnt.$manualTimeStamp = eventData.timeStamp;
+
+ if (!evnt) return;
+
+ var originalPreventDefault = evnt.preventDefault,
+ appWindow = element.ownerDocument.defaultView,
+ fakeProcessDefault = true,
+ finalProcessDefault,
+ angular = appWindow.angular || {};
+
+ // igor: temporary fix for https://bugzilla.mozilla.org/show_bug.cgi?id=684208
+ angular['ff-684208-preventDefault'] = false;
+ evnt.preventDefault = function() {
+ fakeProcessDefault = false;
+ return originalPreventDefault.apply(evnt, arguments);
+ };
+
+ if (!eventData.bubbles || supportsEventBubblingInDetachedTree() || isAttachedToDocument(element)) {
+ element.dispatchEvent(evnt);
+ } else {
+ triggerForPath(element, evnt);
+ }
+
+ finalProcessDefault = !(angular['ff-684208-preventDefault'] || !fakeProcessDefault);
+
+ delete angular['ff-684208-preventDefault'];
+
+ return finalProcessDefault;
+ };
+
+ function supportsTouchEvents() {
+ if ('_cached' in supportsTouchEvents) {
+ return supportsTouchEvents._cached;
+ }
+ if (!window.document.createTouch || !window.document.createTouchList) {
+ supportsTouchEvents._cached = false;
+ return false;
+ }
+ try {
+ window.document.createEvent('TouchEvent');
+ } catch (e) {
+ supportsTouchEvents._cached = false;
+ return false;
+ }
+ supportsTouchEvents._cached = true;
+ return true;
+ }
+
+ function createTouchEvent(element, eventType, x, y) {
+ var evnt = new window.Event(eventType);
+ x = x || 0;
+ y = y || 0;
+
+ var touch = window.document.createTouch(window, element, Date.now(), x, y, x, y);
+ var touches = window.document.createTouchList(touch);
+
+ evnt.touches = touches;
+
+ return evnt;
+ }
+
+ function supportsEventBubblingInDetachedTree() {
+ if ('_cached' in supportsEventBubblingInDetachedTree) {
+ return supportsEventBubblingInDetachedTree._cached;
+ }
+ supportsEventBubblingInDetachedTree._cached = false;
+ var doc = window.document;
+ if (doc) {
+ var parent = doc.createElement('div'),
+ child = parent.cloneNode();
+ parent.appendChild(child);
+ parent.addEventListener('e', function() {
+ supportsEventBubblingInDetachedTree._cached = true;
+ });
+ var evnt = window.document.createEvent('Events');
+ evnt.initEvent('e', true, true);
+ child.dispatchEvent(evnt);
+ }
+ return supportsEventBubblingInDetachedTree._cached;
+ }
+
+ function triggerForPath(element, evnt) {
+ var stop = false;
+
+ var _stopPropagation = evnt.stopPropagation;
+ evnt.stopPropagation = function() {
+ stop = true;
+ _stopPropagation.apply(evnt, arguments);
+ };
+ patchEventTargetForBubbling(evnt, element);
+ do {
+ element.dispatchEvent(evnt);
+ // eslint-disable-next-line no-unmodified-loop-condition
+ } while (!stop && (element = element.parentNode));
+ }
+
+ function patchEventTargetForBubbling(event, target) {
+ event._target = target;
+ Object.defineProperty(event, 'target', {get: function() { return this._target;}});
+ }
+
+ function isAttachedToDocument(element) {
+ while ((element = element.parentNode)) {
+ if (element === window) {
+ return true;
+ }
+ }
+ return false;
+ }
+})();
})(window, window.angular);