2011-07-17 08:05:43 +00:00
|
|
|
|
2010-04-15 21:17:33 +00:00
|
|
|
/**
|
2011-07-22 22:49:07 +00:00
|
|
|
* @license AngularJS v"NG_VERSION_FULL"
|
|
|
|
|
* (c) 2010-2011 AngularJS http://angularjs.org
|
|
|
|
|
* License: MIT
|
2010-04-15 21:17:33 +00:00
|
|
|
*/
|
2010-04-05 18:46:53 +00:00
|
|
|
|
2010-10-01 23:06:32 +00:00
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
window.angular = window.angular || {};
|
|
|
|
|
angular.module = angular.module || {};
|
2010-10-01 23:06:32 +00:00
|
|
|
|
2011-01-26 04:44:44 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc overview
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock
|
2011-01-26 04:44:44 +00:00
|
|
|
* @description
|
2011-06-06 21:44:49 +00:00
|
|
|
*
|
2011-11-12 01:15:22 +00:00
|
|
|
* The `ngMock` is an angular module which is used with `ng` module and adds unit-test configuration as well as useful
|
2011-11-10 05:18:34 +00:00
|
|
|
* mocks to the {@link angular.module.AUTO.$injector $injector}.
|
2011-01-26 04:44:44 +00:00
|
|
|
*/
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock = function($provide){
|
|
|
|
|
$provide.service('$browser', angular.module.ngMock.$BrowserProvider);
|
|
|
|
|
$provide.service('$exceptionHandler', angular.module.ngMock.$ExceptionHandlerProvider);
|
|
|
|
|
$provide.service('$log', angular.module.ngMock.$LogProvider);
|
2011-08-16 19:24:53 +00:00
|
|
|
$provide.service('$httpBackend', angular.module.ngMock.$HttpBackendProvider);
|
2011-11-10 05:18:34 +00:00
|
|
|
};
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$inject = ['$provide'];
|
2011-01-26 04:44:44 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc object
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
2011-11-12 01:15:22 +00:00
|
|
|
* This service is a mock implementation of {@link angular.module.ng.$browser}. It provides fake
|
2011-06-28 01:38:02 +00:00
|
|
|
* implementation for commonly used browser apis that are hard to test, e.g. setTimeout, xhr,
|
2011-11-10 05:18:34 +00:00
|
|
|
* cookies, etc...
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
2011-11-12 01:15:22 +00:00
|
|
|
* The api of this service is the same as that of the real {@link angular.module.ng.$browser $browser}, except
|
2011-06-28 01:38:02 +00:00
|
|
|
* that there are several helper methods available which can be used in tests.
|
|
|
|
|
*
|
|
|
|
|
* The following apis can be used in tests:
|
|
|
|
|
*
|
|
|
|
|
* - $browser.defer — enables testing of code that uses
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ng.$defer $defer} for executing functions via the `setTimeout` api.
|
2011-01-26 04:44:44 +00:00
|
|
|
*/
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$BrowserProvider = function(){
|
2011-11-02 23:32:46 +00:00
|
|
|
this.$get = function(){
|
2011-11-12 01:15:22 +00:00
|
|
|
return new angular.module.ngMock.$Browser();
|
2011-11-02 23:32:46 +00:00
|
|
|
};
|
|
|
|
|
};
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$Browser = function() {
|
2010-04-27 18:18:08 +00:00
|
|
|
var self = this,
|
|
|
|
|
expectations = {},
|
|
|
|
|
requests = [];
|
2010-09-14 20:51:01 +00:00
|
|
|
|
2010-05-07 19:09:14 +00:00
|
|
|
this.isMock = true;
|
feat($browser): jQuery style url method, onUrlChange event
This is just basic implementation of $browser.url, $browser.onUrlChange methods:
$browser.url() - returns current location.href
$browser.url('/new') - set url to /new
If supported, history.pushState is used, location.href property otherwise.
$browser.url('/new', true) - replace current url with /new
If supported, history.replaceState is used, location.replace otherwise.
$browser.onUrlChange is only fired when url is changed from the browser:
- user types into address bar
- user clicks on back/forward button
- user clicks on link
It's not fired when url is changed using $browser.url()
Breaks Removed $browser.setUrl(), $browser.getUrl(), use $browser.url()
Breaks Removed $browser.onHashChange(), use $browser.onUrlChange()
2011-06-22 17:57:22 +00:00
|
|
|
self.$$url = "http://server";
|
|
|
|
|
self.$$lastUrl = self.$$url; // used by url polling fn
|
2010-09-22 11:24:40 +00:00
|
|
|
self.pollFns = [];
|
2010-04-05 21:09:25 +00:00
|
|
|
|
2011-01-05 01:54:37 +00:00
|
|
|
|
|
|
|
|
// register url polling fn
|
|
|
|
|
|
feat($browser): jQuery style url method, onUrlChange event
This is just basic implementation of $browser.url, $browser.onUrlChange methods:
$browser.url() - returns current location.href
$browser.url('/new') - set url to /new
If supported, history.pushState is used, location.href property otherwise.
$browser.url('/new', true) - replace current url with /new
If supported, history.replaceState is used, location.replace otherwise.
$browser.onUrlChange is only fired when url is changed from the browser:
- user types into address bar
- user clicks on back/forward button
- user clicks on link
It's not fired when url is changed using $browser.url()
Breaks Removed $browser.setUrl(), $browser.getUrl(), use $browser.url()
Breaks Removed $browser.onHashChange(), use $browser.onUrlChange()
2011-06-22 17:57:22 +00:00
|
|
|
self.onUrlChange = function(listener) {
|
2011-01-05 01:54:37 +00:00
|
|
|
self.pollFns.push(
|
|
|
|
|
function() {
|
feat($browser): jQuery style url method, onUrlChange event
This is just basic implementation of $browser.url, $browser.onUrlChange methods:
$browser.url() - returns current location.href
$browser.url('/new') - set url to /new
If supported, history.pushState is used, location.href property otherwise.
$browser.url('/new', true) - replace current url with /new
If supported, history.replaceState is used, location.replace otherwise.
$browser.onUrlChange is only fired when url is changed from the browser:
- user types into address bar
- user clicks on back/forward button
- user clicks on link
It's not fired when url is changed using $browser.url()
Breaks Removed $browser.setUrl(), $browser.getUrl(), use $browser.url()
Breaks Removed $browser.onHashChange(), use $browser.onUrlChange()
2011-06-22 17:57:22 +00:00
|
|
|
if (self.$$lastUrl != self.$$url) {
|
|
|
|
|
self.$$lastUrl = self.$$url;
|
|
|
|
|
listener(self.$$url);
|
2011-01-05 01:54:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2011-01-13 23:32:13 +00:00
|
|
|
|
|
|
|
|
return listener;
|
2011-01-05 01:54:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-06-28 01:38:02 +00:00
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Generic method for training browser to expect a request in a test and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* See also convenience methods for browser training:
|
|
|
|
|
*
|
2011-11-10 05:18:34 +00:00
|
|
|
* - {@link #xhr.expectGET}
|
|
|
|
|
* - {@link #xhr.expectPOST}
|
|
|
|
|
* - {@link #xhr.expectPUT}
|
|
|
|
|
* - {@link #xhr.expectDELETE}
|
|
|
|
|
* - {@link #xhr.expectJSON}
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* To flush pending requests in tests use
|
2011-11-10 05:18:34 +00:00
|
|
|
* {@link #xhr.flush}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @param {string} method Expected HTTP method.
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @param {(object|string)=} data Expected body of the (POST) HTTP request.
|
|
|
|
|
* @param {function(number, *)} callback Callback to call when response is flushed.
|
|
|
|
|
* @param {object} headers Key-value pairs of expected headers.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-10 05:18:34 +00:00
|
|
|
* {@link #xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2011-03-10 21:50:00 +00:00
|
|
|
self.xhr = function(method, url, data, callback, headers) {
|
|
|
|
|
headers = headers || {};
|
2010-05-07 19:09:14 +00:00
|
|
|
if (data && angular.isObject(data)) data = angular.toJson(data);
|
|
|
|
|
if (data && angular.isString(data)) url += "|" + data;
|
2010-04-05 21:09:25 +00:00
|
|
|
var expect = expectations[method] || {};
|
2011-03-10 21:50:00 +00:00
|
|
|
var expectation = expect[url];
|
|
|
|
|
if (!expectation) {
|
|
|
|
|
throw new Error("Unexpected request for method '" + method + "' and url '" + url + "'.");
|
2010-04-05 21:09:25 +00:00
|
|
|
}
|
2011-10-07 18:27:49 +00:00
|
|
|
requests.push(function() {
|
2011-03-15 23:13:11 +00:00
|
|
|
angular.forEach(expectation.headers, function(value, key){
|
2011-03-10 21:50:00 +00:00
|
|
|
if (headers[key] !== value) {
|
|
|
|
|
throw new Error("Missing HTTP request header: " + key + ": " + value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
callback(expectation.code, expectation.response);
|
2010-04-05 21:09:25 +00:00
|
|
|
});
|
2011-08-03 11:53:59 +00:00
|
|
|
// TODO(vojta): return mock request object
|
2010-04-05 21:09:25 +00:00
|
|
|
};
|
|
|
|
|
self.xhr.expectations = expectations;
|
|
|
|
|
self.xhr.requests = requests;
|
2011-03-10 21:50:00 +00:00
|
|
|
self.xhr.expect = function(method, url, data, headers) {
|
2010-05-07 19:09:14 +00:00
|
|
|
if (data && angular.isObject(data)) data = angular.toJson(data);
|
|
|
|
|
if (data && angular.isString(data)) url += "|" + data;
|
2010-04-05 21:09:25 +00:00
|
|
|
var expect = expectations[method] || (expectations[method] = {});
|
|
|
|
|
return {
|
2010-05-19 18:51:17 +00:00
|
|
|
respond: function(code, response) {
|
2010-05-19 23:00:20 +00:00
|
|
|
if (!angular.isNumber(code)) {
|
2010-05-19 18:51:17 +00:00
|
|
|
response = code;
|
|
|
|
|
code = 200;
|
|
|
|
|
}
|
2011-03-10 21:50:00 +00:00
|
|
|
expect[url] = {code:code, response:response, headers: headers || {}};
|
2010-04-05 21:09:25 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.expectGET
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Trains browser to expect a `GET` request and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ngMock.$browser#xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2010-04-27 18:18:08 +00:00
|
|
|
self.xhr.expectGET = angular.bind(self, self.xhr.expect, 'GET');
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.expectPOST
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Trains browser to expect a `POST` request and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ngMock.$browser#xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2010-04-27 18:18:08 +00:00
|
|
|
self.xhr.expectPOST = angular.bind(self, self.xhr.expect, 'POST');
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.expectDELETE
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Trains browser to expect a `DELETE` request and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ngMock.$browser#xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2010-04-27 18:18:08 +00:00
|
|
|
self.xhr.expectDELETE = angular.bind(self, self.xhr.expect, 'DELETE');
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.expectPUT
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Trains browser to expect a `PUT` request and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ngMock.$browser#xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2010-04-27 18:18:08 +00:00
|
|
|
self.xhr.expectPUT = angular.bind(self, self.xhr.expect, 'PUT');
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.expectJSON
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Trains browser to expect a `JSON` request and respond to it.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url Url path for which a request is expected.
|
|
|
|
|
* @returns {object} Response configuration object. You can call its `respond()` method to
|
|
|
|
|
* configure what should the browser mock return when the response is
|
2011-11-12 01:15:22 +00:00
|
|
|
* {@link angular.module.ngMock.$browser#xhr.flush flushed}.
|
2011-06-28 01:38:02 +00:00
|
|
|
*/
|
2010-07-22 18:18:32 +00:00
|
|
|
self.xhr.expectJSON = angular.bind(self, self.xhr.expect, 'JSON');
|
2011-06-28 01:38:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#xhr.flush
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-28 01:38:02 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Flushes all pending requests and executes xhr callbacks with the trained response as the
|
|
|
|
|
* argument.
|
|
|
|
|
*/
|
2010-04-05 21:09:25 +00:00
|
|
|
self.xhr.flush = function() {
|
2011-02-04 21:00:46 +00:00
|
|
|
if (requests.length == 0) {
|
|
|
|
|
throw new Error("No xhr requests to be flushed!");
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-05 21:09:25 +00:00
|
|
|
while(requests.length) {
|
|
|
|
|
requests.pop()();
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-09-14 20:51:01 +00:00
|
|
|
|
|
|
|
|
self.cookieHash = {};
|
2010-09-27 06:45:05 +00:00
|
|
|
self.lastCookieHash = {};
|
2010-12-05 07:49:26 +00:00
|
|
|
self.deferredFns = [];
|
2011-05-28 08:48:14 +00:00
|
|
|
self.deferredNextId = 0;
|
2010-12-05 07:49:26 +00:00
|
|
|
|
2011-05-17 20:13:16 +00:00
|
|
|
self.defer = function(fn, delay) {
|
|
|
|
|
delay = delay || 0;
|
2011-05-28 08:48:14 +00:00
|
|
|
self.deferredFns.push({time:(self.defer.now + delay), fn:fn, id: self.deferredNextId});
|
2011-05-17 20:13:16 +00:00
|
|
|
self.deferredFns.sort(function(a,b){ return a.time - b.time;});
|
2011-05-28 08:48:14 +00:00
|
|
|
return self.deferredNextId++;
|
2010-12-05 07:49:26 +00:00
|
|
|
};
|
|
|
|
|
|
2011-05-28 08:48:14 +00:00
|
|
|
|
2011-05-17 20:13:16 +00:00
|
|
|
self.defer.now = 0;
|
|
|
|
|
|
2011-05-28 08:48:14 +00:00
|
|
|
|
|
|
|
|
self.defer.cancel = function(deferId) {
|
|
|
|
|
var fnIndex;
|
|
|
|
|
|
2011-09-15 23:39:03 +00:00
|
|
|
angular.forEach(self.deferredFns, function(fn, index) {
|
2011-05-28 08:48:14 +00:00
|
|
|
if (fn.id === deferId) fnIndex = index;
|
|
|
|
|
});
|
|
|
|
|
|
2011-09-16 12:18:45 +00:00
|
|
|
if (fnIndex !== undefined) {
|
2011-05-28 08:48:14 +00:00
|
|
|
self.deferredFns.splice(fnIndex, 1);
|
2011-10-22 06:33:24 +00:00
|
|
|
return true;
|
2011-05-28 08:48:14 +00:00
|
|
|
}
|
2011-10-23 04:32:16 +00:00
|
|
|
|
|
|
|
|
return false;
|
2011-07-27 20:24:07 +00:00
|
|
|
};
|
2011-05-28 08:48:14 +00:00
|
|
|
|
|
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#defer.flush
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Flushes all pending requests and executes the defer callbacks.
|
|
|
|
|
*
|
|
|
|
|
* @param {number=} number of miliseconds to flush. See {@link #defer.now}
|
|
|
|
|
*/
|
2011-07-02 00:03:50 +00:00
|
|
|
self.defer.flush = function(delay) {
|
|
|
|
|
if (angular.isDefined(delay)) {
|
|
|
|
|
self.defer.now += delay;
|
|
|
|
|
} else {
|
|
|
|
|
if (self.deferredFns.length) {
|
|
|
|
|
self.defer.now = self.deferredFns[self.deferredFns.length-1].time;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 20:13:16 +00:00
|
|
|
while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
|
|
|
|
|
self.deferredFns.shift().fn();
|
|
|
|
|
}
|
2010-12-05 07:49:26 +00:00
|
|
|
};
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc property
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#defer.now
|
|
|
|
|
* @propertyOf angular.module.ngMock.$browser
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Current milliseconds mock time.
|
|
|
|
|
*/
|
2011-07-19 13:52:27 +00:00
|
|
|
|
|
|
|
|
self.$$baseHref = '';
|
|
|
|
|
self.baseHref = function() {
|
|
|
|
|
return this.$$baseHref;
|
|
|
|
|
};
|
2010-04-05 18:46:53 +00:00
|
|
|
}
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$Browser.prototype = {
|
2010-04-05 18:46:53 +00:00
|
|
|
|
2011-06-24 19:26:44 +00:00
|
|
|
/**
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$browser#poll
|
|
|
|
|
* @methodOf angular.module.ngMock.$browser
|
2011-06-24 19:26:44 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* run all fns in pollFns
|
|
|
|
|
*/
|
2011-10-07 18:27:49 +00:00
|
|
|
poll: function poll() {
|
2011-01-08 06:02:23 +00:00
|
|
|
angular.forEach(this.pollFns, function(pollFn){
|
2010-10-01 23:06:32 +00:00
|
|
|
pollFn();
|
|
|
|
|
});
|
2010-09-22 11:24:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
addPollFn: function(pollFn) {
|
|
|
|
|
this.pollFns.push(pollFn);
|
2010-09-25 22:29:48 +00:00
|
|
|
return pollFn;
|
2010-09-22 11:24:40 +00:00
|
|
|
},
|
|
|
|
|
|
feat($browser): jQuery style url method, onUrlChange event
This is just basic implementation of $browser.url, $browser.onUrlChange methods:
$browser.url() - returns current location.href
$browser.url('/new') - set url to /new
If supported, history.pushState is used, location.href property otherwise.
$browser.url('/new', true) - replace current url with /new
If supported, history.replaceState is used, location.replace otherwise.
$browser.onUrlChange is only fired when url is changed from the browser:
- user types into address bar
- user clicks on back/forward button
- user clicks on link
It's not fired when url is changed using $browser.url()
Breaks Removed $browser.setUrl(), $browser.getUrl(), use $browser.url()
Breaks Removed $browser.onHashChange(), use $browser.onUrlChange()
2011-06-22 17:57:22 +00:00
|
|
|
url: function(url, replace) {
|
|
|
|
|
if (url) {
|
|
|
|
|
this.$$url = url;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2010-04-05 18:46:53 +00:00
|
|
|
|
feat($browser): jQuery style url method, onUrlChange event
This is just basic implementation of $browser.url, $browser.onUrlChange methods:
$browser.url() - returns current location.href
$browser.url('/new') - set url to /new
If supported, history.pushState is used, location.href property otherwise.
$browser.url('/new', true) - replace current url with /new
If supported, history.replaceState is used, location.replace otherwise.
$browser.onUrlChange is only fired when url is changed from the browser:
- user types into address bar
- user clicks on back/forward button
- user clicks on link
It's not fired when url is changed using $browser.url()
Breaks Removed $browser.setUrl(), $browser.getUrl(), use $browser.url()
Breaks Removed $browser.onHashChange(), use $browser.onUrlChange()
2011-06-22 17:57:22 +00:00
|
|
|
return this.$$url;
|
2010-04-05 18:46:53 +00:00
|
|
|
},
|
|
|
|
|
|
2010-09-14 20:51:01 +00:00
|
|
|
cookies: function(name, value) {
|
|
|
|
|
if (name) {
|
|
|
|
|
if (value == undefined) {
|
|
|
|
|
delete this.cookieHash[name];
|
|
|
|
|
} else {
|
2010-10-01 23:06:32 +00:00
|
|
|
if (angular.isString(value) && //strings only
|
|
|
|
|
value.length <= 4096) { //strict cookie storage limits
|
2010-09-22 21:37:17 +00:00
|
|
|
this.cookieHash[name] = value;
|
|
|
|
|
}
|
2010-09-14 20:51:01 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2010-10-01 23:06:32 +00:00
|
|
|
if (!angular.equals(this.cookieHash, this.lastCookieHash)) {
|
2010-10-13 22:23:11 +00:00
|
|
|
this.lastCookieHash = angular.copy(this.cookieHash);
|
|
|
|
|
this.cookieHash = angular.copy(this.cookieHash);
|
2010-09-27 06:45:05 +00:00
|
|
|
}
|
|
|
|
|
return this.cookieHash;
|
2010-09-14 20:51:01 +00:00
|
|
|
}
|
2011-05-31 19:28:42 +00:00
|
|
|
},
|
|
|
|
|
|
2011-11-09 01:40:52 +00:00
|
|
|
notifyWhenNoOutstandingRequests: function(fn) {
|
|
|
|
|
fn();
|
|
|
|
|
}
|
2010-04-05 18:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
2010-10-26 05:05:38 +00:00
|
|
|
|
2011-01-26 04:44:44 +00:00
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc object
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$exceptionHandlerProvider
|
2011-01-26 04:44:44 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
2011-11-12 01:15:22 +00:00
|
|
|
* Configures the mock implementation of {@link angular.module.ng.$exceptionHandler} to rethrow or to log errors passed
|
2011-11-10 05:18:34 +00:00
|
|
|
* into the `$exceptionHandler`.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc object
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$exceptionHandler
|
2011-01-26 04:44:44 +00:00
|
|
|
*
|
2011-11-10 05:18:34 +00:00
|
|
|
* @description
|
2011-11-12 01:15:22 +00:00
|
|
|
* Mock implementation of {@link angular.module.ng.$exceptionHandler} that rethrows or logs errors passed
|
|
|
|
|
* into it. See {@link angular.module.ngMock.$exceptionHandlerProvider $exceptionHandlerProvider} for configuration
|
2011-11-10 05:18:34 +00:00
|
|
|
* information.
|
2011-01-26 04:44:44 +00:00
|
|
|
*/
|
2011-11-10 05:18:34 +00:00
|
|
|
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$ExceptionHandlerProvider = function(){
|
2011-11-02 23:32:46 +00:00
|
|
|
var handler;
|
|
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$exceptionHandlerProvider#mode
|
|
|
|
|
* @methodOf angular.module.ngMock.$exceptionHandlerProvider
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Sets the logging mode.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} mode Mode of operation, defaults to `rethrow`.
|
|
|
|
|
*
|
|
|
|
|
* - `rethrow`: If any errors are are passed into 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.
|
|
|
|
|
* - `log`: Sometimes it is desirable to test that an error is throw, for this case the `log` mode stores the
|
|
|
|
|
* error and allows later assertion of it.
|
2011-11-12 01:15:22 +00:00
|
|
|
* See {@link angular.module.ngMock.$log#assertEmpty assertEmpty()} and
|
|
|
|
|
* {@link angular.module.ngMock.$log#reset reset()}
|
2011-11-10 05:18:34 +00:00
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
this.mode = function(mode){
|
2011-11-04 19:33:01 +00:00
|
|
|
switch(mode) {
|
|
|
|
|
case 'rethrow':
|
|
|
|
|
handler = function(e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'log':
|
|
|
|
|
var errors = [];
|
|
|
|
|
handler = function(e) {
|
|
|
|
|
errors.push(e);
|
|
|
|
|
}
|
|
|
|
|
handler.errors = errors;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw Error("Unknown mode '" + mode + "', only 'log'/'rethrow' modes are allowed!");
|
2011-11-02 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.$get = function(){
|
|
|
|
|
return handler;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.mode('rethrow');
|
|
|
|
|
};
|
2011-01-26 04:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc service
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log
|
2011-01-26 04:44:44 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
2011-11-12 01:15:22 +00:00
|
|
|
* Mock implementation of {@link angular.module.ng.$log} that gathers all logged messages in arrays
|
2011-01-26 04:44:44 +00:00
|
|
|
* (one array per logging level). These arrays are exposed as `logs` property of each of the
|
|
|
|
|
* level-specific log function, e.g. for level `error` the array is exposed as `$log.error.logs`.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.$LogProvider = function(){
|
2011-11-04 19:33:01 +00:00
|
|
|
|
|
|
|
|
function concat(array1, array2, index) {
|
|
|
|
|
return array1.concat(Array.prototype.slice.call(array2, index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-11-02 23:32:46 +00:00
|
|
|
this.$get = function () {
|
|
|
|
|
var $log = {
|
|
|
|
|
log: function() { $log.log.logs.push(concat([], arguments, 0)); },
|
|
|
|
|
warn: function() { $log.warn.logs.push(concat([], arguments, 0)); },
|
|
|
|
|
info: function() { $log.info.logs.push(concat([], arguments, 0)); },
|
|
|
|
|
error: function() { $log.error.logs.push(concat([], arguments, 0)); }
|
|
|
|
|
};
|
2011-01-26 04:44:44 +00:00
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#reset
|
|
|
|
|
* @methodOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Reset all of the logging arrays to empty.
|
|
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
$log.reset = function (){
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc property
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#log.logs
|
|
|
|
|
* @propertyOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Array of logged messages.
|
|
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
$log.log.logs = [];
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc property
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#warn.logs
|
|
|
|
|
* @propertyOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Array of logged messages.
|
|
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
$log.warn.logs = [];
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc property
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#info.logs
|
|
|
|
|
* @propertyOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Array of logged messages.
|
|
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
$log.info.logs = [];
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc property
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#error.logs
|
|
|
|
|
* @propertyOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Array of logged messages.
|
|
|
|
|
*/
|
2011-11-02 23:32:46 +00:00
|
|
|
$log.error.logs = [];
|
|
|
|
|
};
|
2011-01-26 04:44:44 +00:00
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.$log#assertEmpty
|
|
|
|
|
* @methodOf angular.module.ngMock.$log
|
2011-11-10 05:18:34 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Assert that the all of the logging methods have no logged messages. If messages present, an exception is thrown.
|
|
|
|
|
*/
|
|
|
|
|
$log.assertEmpty = function() {
|
2011-11-02 23:32:46 +00:00
|
|
|
var errors = [];
|
|
|
|
|
angular.forEach(['error', 'warn', 'info', 'log'], function(logLevel) {
|
|
|
|
|
angular.forEach($log[logLevel].logs, function(log) {
|
|
|
|
|
angular.forEach(log, function (logItem) {
|
2011-11-04 19:33:01 +00:00
|
|
|
errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' + (logItem.stack || ''));
|
2011-11-02 23:32:46 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
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.push('')
|
|
|
|
|
throw new Error(errors.join('\n---------\n'));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$log.reset();
|
|
|
|
|
return $log;
|
|
|
|
|
};
|
|
|
|
|
};
|
2011-01-26 04:44:44 +00:00
|
|
|
|
|
|
|
|
|
2010-10-26 05:05:38 +00:00
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc object
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.TzDate
|
2011-11-10 05:18:34 +00:00
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* *NOTE*: this is not an injectable instance, just a globally available mock class of `Date`.
|
|
|
|
|
*
|
2010-10-26 05:05:38 +00:00
|
|
|
* Mock of the Date type which has its timezone specified via constroctor arg.
|
|
|
|
|
*
|
|
|
|
|
* The main purpose is to create Date-like instances with timezone fixed to the specified timezone
|
|
|
|
|
* offset, so that we can test code that depends on local timezone settings without dependency on
|
|
|
|
|
* the time zone settings of the machine where the code is running.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} offset Offset of the *desired* timezone in hours (fractions will be honored)
|
|
|
|
|
* @param {(number|string)} timestamp Timestamp representing the desired time in *UTC*
|
|
|
|
|
*
|
|
|
|
|
* @example
|
2011-02-01 00:21:29 +00:00
|
|
|
* !!!! WARNING !!!!!
|
|
|
|
|
* This is not a complete Date object so only methods that were implemented can be called safely.
|
|
|
|
|
* To make matters worse, TzDate instances inherit stuff from Date via a prototype.
|
|
|
|
|
*
|
|
|
|
|
* We do our best to intercept calls to "unimplemented" methods, but since the list of methods is
|
|
|
|
|
* incomplete we might be missing some non-standard methods. This can result in errors like:
|
|
|
|
|
* "Date.prototype.foo called on incompatible Object".
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
2010-10-26 05:05:38 +00:00
|
|
|
* var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00Z');
|
|
|
|
|
* newYearInBratislava.getTimezoneOffset() => -60;
|
|
|
|
|
* newYearInBratislava.getFullYear() => 2010;
|
|
|
|
|
* newYearInBratislava.getMonth() => 0;
|
|
|
|
|
* newYearInBratislava.getDate() => 1;
|
|
|
|
|
* newYearInBratislava.getHours() => 0;
|
|
|
|
|
* newYearInBratislava.getMinutes() => 0;
|
2011-02-01 00:21:29 +00:00
|
|
|
* </pre>
|
2010-10-26 05:05:38 +00:00
|
|
|
*
|
|
|
|
|
*/
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.TzDate = function (offset, timestamp) {
|
2011-11-08 05:54:01 +00:00
|
|
|
var self = new Date(0);
|
2010-10-26 05:05:38 +00:00
|
|
|
if (angular.isString(timestamp)) {
|
|
|
|
|
var tsStr = timestamp;
|
2010-11-07 06:39:56 +00:00
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.origDate = angular.fromJson(angular.toJson({date:timestamp})).date;
|
2010-11-07 06:39:56 +00:00
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
timestamp = self.origDate.getTime();
|
2010-10-26 05:05:38 +00:00
|
|
|
if (isNaN(timestamp))
|
|
|
|
|
throw {
|
|
|
|
|
name: "Illegal Argument",
|
|
|
|
|
message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
|
|
|
|
|
};
|
2010-11-07 06:39:56 +00:00
|
|
|
} else {
|
2011-11-08 05:54:01 +00:00
|
|
|
self.origDate = new Date(timestamp);
|
2010-10-26 05:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var localOffset = new Date(timestamp).getTimezoneOffset();
|
2011-11-08 05:54:01 +00:00
|
|
|
self.offsetDiff = localOffset*60*1000 - offset*1000*60*60;
|
|
|
|
|
self.date = new Date(timestamp + self.offsetDiff);
|
2010-10-26 05:05:38 +00:00
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getTime = function() {
|
|
|
|
|
return self.date.getTime() - self.offsetDiff;
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.toLocaleDateString = function() {
|
|
|
|
|
return self.date.toLocaleDateString();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getFullYear = function() {
|
|
|
|
|
return self.date.getFullYear();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getMonth = function() {
|
|
|
|
|
return self.date.getMonth();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getDate = function() {
|
|
|
|
|
return self.date.getDate();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getHours = function() {
|
|
|
|
|
return self.date.getHours();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getMinutes = function() {
|
|
|
|
|
return self.date.getMinutes();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getSeconds = function() {
|
|
|
|
|
return self.date.getSeconds();
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getTimezoneOffset = function() {
|
2010-10-26 05:05:38 +00:00
|
|
|
return offset * 60;
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCFullYear = function() {
|
|
|
|
|
return self.origDate.getUTCFullYear();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCMonth = function() {
|
|
|
|
|
return self.origDate.getUTCMonth();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCDate = function() {
|
|
|
|
|
return self.origDate.getUTCDate();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCHours = function() {
|
|
|
|
|
return self.origDate.getUTCHours();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCMinutes = function() {
|
|
|
|
|
return self.origDate.getUTCMinutes();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getUTCSeconds = function() {
|
|
|
|
|
return self.origDate.getUTCSeconds();
|
2010-11-07 06:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
2011-11-08 05:54:01 +00:00
|
|
|
self.getDay = function() {
|
|
|
|
|
return self.origDate.getDay();
|
2011-06-22 19:33:31 +00:00
|
|
|
};
|
2010-11-07 06:39:56 +00:00
|
|
|
|
2010-10-26 05:05:38 +00:00
|
|
|
//hide all methods not implemented in this mock that the Date prototype exposes
|
2011-11-08 05:54:01 +00:00
|
|
|
var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
|
2010-11-07 06:39:56 +00:00
|
|
|
'getUTCMilliseconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
|
2010-10-26 05:05:38 +00:00
|
|
|
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
|
|
|
|
|
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
|
|
|
|
|
'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString',
|
|
|
|
|
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
|
|
|
|
|
|
2011-01-08 06:02:23 +00:00
|
|
|
angular.forEach(unimplementedMethods, function(methodName) {
|
2011-07-17 07:47:11 +00:00
|
|
|
self[methodName] = function() {
|
2011-11-08 05:54:01 +00:00
|
|
|
throw Error("Method '" + methodName + "' is not implemented in the TzDate mock");
|
2010-10-26 05:05:38 +00:00
|
|
|
};
|
|
|
|
|
});
|
2011-11-08 05:54:01 +00:00
|
|
|
|
|
|
|
|
return self;
|
2010-10-26 05:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//make "tzDateInstance instanceof Date" return true
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.TzDate.prototype = Date.prototype;
|
2011-11-04 19:33:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-10 05:18:34 +00:00
|
|
|
* @ngdoc function
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.debug
|
2011-11-10 05:18:34 +00:00
|
|
|
* @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.
|
|
|
|
|
*
|
|
|
|
|
* This method is also available on window, where it can be used to display objects on debug console.
|
|
|
|
|
*
|
2011-11-04 19:33:01 +00:00
|
|
|
* @param {*} object - any object to turn into string.
|
|
|
|
|
* @return a serialized string of the argument
|
|
|
|
|
*/
|
2011-11-12 01:15:22 +00:00
|
|
|
angular.module.ngMock.dump = function(object){
|
2011-11-04 19:33:01 +00:00
|
|
|
var out;
|
|
|
|
|
if (angular.isElement(object)) {
|
|
|
|
|
object = angular.element(object);
|
|
|
|
|
out = angular.element('<div></div>')
|
|
|
|
|
angular.forEach(object, function(element){
|
|
|
|
|
out.append(angular.element(element).clone());
|
|
|
|
|
});
|
|
|
|
|
out = out.html();
|
|
|
|
|
} else if (angular.isObject(object)) {
|
|
|
|
|
if (angular.isFunction(object.$eval) && angular.isFunction(object.$apply)) {
|
|
|
|
|
out = serializeScope(object);
|
|
|
|
|
} else {
|
|
|
|
|
out = angular.toJson(object, true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
out = String(object);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
|
|
function serializeScope(scope, offset) {
|
|
|
|
|
offset = offset || ' ';
|
|
|
|
|
var log = [offset + 'Scope(' + scope.$id + '): {'];
|
|
|
|
|
for ( var key in scope ) {
|
|
|
|
|
if (scope.hasOwnProperty(key) && !key.match(/^(\$|this)/)) {
|
|
|
|
|
log.push(' ' + key + ': ' + angular.toJson(scope[key]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var child = scope.$$childHead;
|
|
|
|
|
while(child) {
|
|
|
|
|
log.push(serializeScope(child, offset + ' '));
|
|
|
|
|
child = child.$$nextSibling;
|
|
|
|
|
}
|
|
|
|
|
log.push('}');
|
|
|
|
|
return log.join('\n' + offset);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-08-16 19:24:53 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc object
|
|
|
|
|
* @name angular.module.ngMock.$httpBackend
|
|
|
|
|
*/
|
|
|
|
|
angular.module.ngMock.$HttpBackendProvider = function() {
|
|
|
|
|
this.$get = function() {
|
|
|
|
|
var definitions = [],
|
|
|
|
|
expectations = [],
|
|
|
|
|
responses = [];
|
|
|
|
|
|
|
|
|
|
function createResponse(status, data, headers) {
|
|
|
|
|
return angular.isNumber(status) ? [status, data, headers] : [200, status, data];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(vojta): change params to: method, url, data, headers, callback
|
|
|
|
|
function $httpBackend(method, url, data, callback, headers) {
|
|
|
|
|
var xhr = new MockXhr(),
|
|
|
|
|
expectation = expectations[0],
|
|
|
|
|
wasExpected = false;
|
|
|
|
|
|
|
|
|
|
if (expectation && expectation.match(method, url)) {
|
|
|
|
|
if (!expectation.matchData(data))
|
|
|
|
|
throw Error('Expected ' + method + ' ' + url + ' with different data');
|
|
|
|
|
|
|
|
|
|
if (!expectation.matchHeaders(headers))
|
|
|
|
|
throw Error('Expected ' + method + ' ' + url + ' with different headers');
|
|
|
|
|
|
|
|
|
|
expectations.shift();
|
|
|
|
|
|
|
|
|
|
if (expectation.response) {
|
|
|
|
|
responses.push(function() {
|
|
|
|
|
xhr.$$headers = expectation.response[2];
|
|
|
|
|
callback(expectation.response[0], expectation.response[1]);
|
|
|
|
|
});
|
|
|
|
|
return method == 'JSONP' ? undefined : xhr;
|
|
|
|
|
}
|
|
|
|
|
wasExpected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var i = -1, definition;
|
|
|
|
|
while ((definition = definitions[++i])) {
|
|
|
|
|
if (definition.match(method, url, data, headers || {})) {
|
|
|
|
|
if (!definition.response) throw Error('No response defined !');
|
|
|
|
|
responses.push(function() {
|
|
|
|
|
var response = angular.isFunction(definition.response) ?
|
|
|
|
|
definition.response(method, url, data, headers) : definition.response;
|
|
|
|
|
xhr.$$headers = response[2];
|
|
|
|
|
callback(response[0], response[1]);
|
|
|
|
|
});
|
|
|
|
|
return method == 'JSONP' ? undefined : xhr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw wasExpected ? Error('No response defined !') :
|
|
|
|
|
Error('Unexpected request: ' + method + ' ' + url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$httpBackend.when = function(method, url, data, headers) {
|
|
|
|
|
var definition = new MockHttpExpectation(method, url, data, headers);
|
|
|
|
|
definitions.push(definition);
|
|
|
|
|
return {
|
|
|
|
|
then: function(status, data, headers) {
|
|
|
|
|
definition.response = angular.isFunction(status) ? status : createResponse(status, data, headers);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$httpBackend.expect = function(method, url, data, headers) {
|
|
|
|
|
var expectation = new MockHttpExpectation(method, url, data, headers);
|
|
|
|
|
expectations.push(expectation);
|
|
|
|
|
return {
|
|
|
|
|
respond: function(status, data, headers) {
|
|
|
|
|
expectation.response = createResponse(status, data, headers);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$httpBackend.flush = function(count) {
|
|
|
|
|
count = count || responses.length;
|
|
|
|
|
while (count--) {
|
|
|
|
|
if (!responses.length) throw Error('No more pending requests');
|
|
|
|
|
responses.shift()();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$httpBackend.verifyExpectations = function() {
|
|
|
|
|
if (expectations.length) {
|
|
|
|
|
throw Error('Unsatisfied requests: ' + expectations.join(', '));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$httpBackend.resetExpectations = function() {
|
|
|
|
|
expectations = [];
|
|
|
|
|
responses = [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return $httpBackend;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function MockHttpExpectation(method, url, data, headers) {
|
|
|
|
|
|
|
|
|
|
this.match = function(m, u, d, h) {
|
|
|
|
|
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;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.matchUrl = function(u) {
|
|
|
|
|
if (!url) return true;
|
|
|
|
|
if (angular.isFunction(url.test)) {
|
|
|
|
|
if (!url.test(u)) return false;
|
|
|
|
|
} else if (url != u) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.matchHeaders = function(h) {
|
|
|
|
|
if (angular.isUndefined(headers)) return true;
|
|
|
|
|
if (angular.isFunction(headers)) {
|
|
|
|
|
if (!headers(h)) return false;
|
|
|
|
|
} else if (!angular.equals(headers, h)) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.matchData = function(d) {
|
|
|
|
|
if (angular.isUndefined(data)) return true;
|
|
|
|
|
if (data && angular.isFunction(data.test)) {
|
|
|
|
|
if (!data.test(d)) return false;
|
|
|
|
|
} else if (data != d) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.toString = function() {
|
|
|
|
|
return method + ' ' + url;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function MockXhr() {
|
|
|
|
|
|
|
|
|
|
// hack for testing $http
|
|
|
|
|
MockXhr.$$lastInstance = this;
|
|
|
|
|
|
|
|
|
|
this.getResponseHeader = function(name) {
|
|
|
|
|
return this.$$headers[name];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.getAllResponseHeaders = function() {
|
|
|
|
|
var lines = [];
|
|
|
|
|
|
|
|
|
|
angular.forEach(this.$$headers, function(value, key) {
|
|
|
|
|
lines.push(key + ': ' + value);
|
|
|
|
|
});
|
|
|
|
|
return lines.join('\n');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.abort = noop;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-04 19:33:01 +00:00
|
|
|
window.jstestdriver && (function(window){
|
|
|
|
|
/**
|
|
|
|
|
* Global method to output any number of objects into JSTD console. Useful for debugging.
|
|
|
|
|
*/
|
|
|
|
|
window.dump = function() {
|
|
|
|
|
var args = [];
|
|
|
|
|
angular.forEach(arguments, function(arg){
|
2011-11-12 01:15:22 +00:00
|
|
|
args.push(angular.module.ngMock.dump(arg));
|
2011-11-04 19:33:01 +00:00
|
|
|
});
|
|
|
|
|
jstestdriver.console.log.apply(jstestdriver.console, args);
|
|
|
|
|
};
|
|
|
|
|
})(window);
|
|
|
|
|
|
|
|
|
|
|
2011-11-10 05:18:34 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc function
|
2011-11-12 01:15:22 +00:00
|
|
|
* @name angular.module.ngMock.inject
|
2011-11-10 05:18:34 +00:00
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* *NOTE*: this is not an injectable instance, just a globally available function on window.
|
|
|
|
|
*
|
|
|
|
|
* This function wraps a test method into an injectable method. It create one
|
|
|
|
|
* {@link angular.module.AUTO.$injector $injector} per test, which is then used for testing.
|
|
|
|
|
*
|
|
|
|
|
* Here is an example of what a typical jasmine tests looks like with the inject method.
|
|
|
|
|
* <pre>
|
|
|
|
|
* describe('MyApp', function() {
|
|
|
|
|
*
|
|
|
|
|
* // Argument inference is used to inject the $provide service
|
|
|
|
|
* beforeEach(inject(function($provide, $rootScope) {
|
|
|
|
|
* // $provide service is configured as needed
|
|
|
|
|
* $provide.value('version', 'v1.0.1');
|
|
|
|
|
* $rootScope.value = 123;
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* // Argument inference is used to inject the $rootScope as well as the version
|
|
|
|
|
* it('should provide a version', inject(function($rootScope, version){
|
|
|
|
|
* expect(version).toEqual('v1.0.1');
|
|
|
|
|
* expect($rootScope.value).toEqual(123);
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* // The inject can also chain the methods
|
|
|
|
|
* it('should override a version and test the new version is injected', inject(
|
|
|
|
|
* function($provide) {
|
|
|
|
|
* $provide.value('version', 'overridden'); // override version here
|
|
|
|
|
* },
|
|
|
|
|
* function(version) {
|
|
|
|
|
* expect(version).toEqual('overridden');
|
|
|
|
|
* }
|
|
|
|
|
* ));
|
|
|
|
|
*
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* @param {*} fns... any number of functions which will be injected using the injector.
|
|
|
|
|
* @return a method
|
|
|
|
|
*/
|
2011-11-04 19:33:01 +00:00
|
|
|
window.jasmine && (function(window){
|
|
|
|
|
window.inject = function (){
|
|
|
|
|
var blockFns = Array.prototype.slice.call(arguments, 0);
|
|
|
|
|
return function(){
|
|
|
|
|
var injector = this.$injector;
|
|
|
|
|
if (!injector) {
|
2011-11-12 23:23:30 +00:00
|
|
|
injector = this.$injector = angular.injector('ng', 'ngMock');
|
2011-11-04 19:33:01 +00:00
|
|
|
}
|
|
|
|
|
for(var i = 0, ii = blockFns.length; i < ii; i++) {
|
|
|
|
|
injector.invoke(this, blockFns[i]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
})(window);
|