Add matrix-service unit tests. Update angular-mocks.

This commit is contained in:
Kegan Dougal 2014-11-04 17:19:49 +00:00
parent 3068210a93
commit a70765ed90
2 changed files with 262 additions and 112 deletions

View File

@ -1,10 +1,3 @@
/**
* @license AngularJS v1.2.22
* (c) 2010-2014 Google, Inc. http://angularjs.org
* License: MIT
*/
(function(window, angular, undefined) {
'use strict'; 'use strict';
/** /**
@ -63,6 +56,8 @@ angular.mock.$Browser = function() {
return listener; return listener;
}; };
self.$$checkUrlChange = angular.noop;
self.cookieHash = {}; self.cookieHash = {};
self.lastCookieHash = {}; self.lastCookieHash = {};
self.deferredFns = []; self.deferredFns = [];
@ -125,7 +120,7 @@ angular.mock.$Browser = function() {
} }
}; };
self.$$baseHref = ''; self.$$baseHref = '/';
self.baseHref = function() { self.baseHref = function() {
return this.$$baseHref; return this.$$baseHref;
}; };
@ -774,13 +769,22 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
}; };
}); });
$provide.decorator('$animate', function($delegate, $$asyncCallback) { $provide.decorator('$animate', ['$delegate', '$$asyncCallback', '$timeout', '$browser',
function($delegate, $$asyncCallback, $timeout, $browser) {
var animate = { var animate = {
queue : [], queue : [],
cancel : $delegate.cancel,
enabled : $delegate.enabled, enabled : $delegate.enabled,
triggerCallbacks : function() { triggerCallbackEvents : function() {
$$asyncCallback.flush(); $$asyncCallback.flush();
}, },
triggerCallbackPromise : function() {
$timeout.flush(0);
},
triggerCallbacks : function() {
this.triggerCallbackEvents();
this.triggerCallbackPromise();
},
triggerReflow : function() { triggerReflow : function() {
angular.forEach(reflowQueue, function(fn) { angular.forEach(reflowQueue, function(fn) {
fn(); fn();
@ -797,12 +801,12 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
element : arguments[0], element : arguments[0],
args : arguments args : arguments
}); });
$delegate[method].apply($delegate, arguments); return $delegate[method].apply($delegate, arguments);
}; };
}); });
return animate; return animate;
}); }]);
}]); }]);
@ -888,7 +892,7 @@ angular.mock.dump = function(object) {
* development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}. * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
* *
* During unit testing, we want our unit tests to run quickly and have no external dependencies so * 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 * we dont want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or
* [JSONP](http://en.wikipedia.org/wiki/JSONP) requests to a real server. All we really need is * [JSONP](http://en.wikipedia.org/wiki/JSONP) requests to a real server. All we really need is
* to verify whether a certain request has been sent or not, or alternatively just let the * to verify whether a certain request has been sent or not, or alternatively just let the
* application make requests, respond with pre-trained responses and assert that the end result is * application make requests, respond with pre-trained responses and assert that the end result is
@ -1007,13 +1011,14 @@ angular.mock.dump = function(object) {
```js ```js
// testing controller // testing controller
describe('MyController', function() { describe('MyController', function() {
var $httpBackend, $rootScope, createController; var $httpBackend, $rootScope, createController, authRequestHandler;
beforeEach(inject(function($injector) { beforeEach(inject(function($injector) {
// Set up the mock http service responses // Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend'); $httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests // backend definition common for all tests
$httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'}); authRequestHandler = $httpBackend.when('GET', '/auth.py')
.respond({userId: 'userX'}, {'A-Token': 'xxx'});
// Get hold of a scope (i.e. the root scope) // Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope'); $rootScope = $injector.get('$rootScope');
@ -1039,11 +1044,23 @@ angular.mock.dump = function(object) {
}); });
it('should fail authentication', function() {
// Notice how you can change the response even after it was set
authRequestHandler.respond(401, '');
$httpBackend.expectGET('/auth.py');
var controller = createController();
$httpBackend.flush();
expect($rootScope.status).toBe('Failed...');
});
it('should send msg to server', function() { it('should send msg to server', function() {
var controller = createController(); var controller = createController();
$httpBackend.flush(); $httpBackend.flush();
// now you don’t care about the authentication, but // now you dont care about the authentication, but
// the controller will still send the request and // the controller will still send the request and
// $httpBackend will respond without you having to // $httpBackend will respond without you having to
// specify the expectation and response for this request // specify the expectation and response for this request
@ -1186,32 +1203,39 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* Creates a new backend definition. * Creates a new backend definition.
* *
* @param {string} method HTTP method. * @param {string} method HTTP method.
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected. * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current definition. * object and returns true if the headers match the current definition.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
* request is handled. * 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 â * - respond
* `{function([status,] data[, headers, statusText]) * `{function([status,] data[, headers, statusText])
* | function(function(method, url, data, headers)}` * | function(function(method, url, data, headers)}`
* â The respond method takes a set of static data to be returned or a function that can * 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 * return an array containing response status (number), response data (string), response
* headers (Object), and the text for the status (string). * 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) { $httpBackend.when = function(method, url, data, headers) {
var definition = new MockHttpExpectation(method, url, data, headers), var definition = new MockHttpExpectation(method, url, data, headers),
chain = { chain = {
respond: function(status, data, headers, statusText) { respond: function(status, data, headers, statusText) {
definition.passThrough = undefined;
definition.response = createResponse(status, data, headers, statusText); definition.response = createResponse(status, data, headers, statusText);
return chain;
} }
}; };
if ($browser) { if ($browser) {
chain.passThrough = function() { chain.passThrough = function() {
definition.response = undefined;
definition.passThrough = true; definition.passThrough = true;
return chain;
}; };
} }
@ -1225,10 +1249,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for GET requests. For more info see `when()`. * Creates a new backend definition for GET requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1237,10 +1263,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for HEAD requests. For more info see `when()`. * Creates a new backend definition for HEAD requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1249,10 +1277,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for DELETE requests. For more info see `when()`. * Creates a new backend definition for DELETE requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1261,12 +1291,14 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for POST requests. For more info see `when()`. * Creates a new backend definition for POST requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected. * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1275,12 +1307,14 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for PUT requests. For more info see `when()`. * Creates a new backend definition for PUT requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected. * data string and returns true if the data is as expected.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1289,9 +1323,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new backend definition for JSONP requests. For more info see `when()`. * Creates a new backend definition for JSONP requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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'); createShortMethods('when');
@ -1303,30 +1339,36 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* Creates a new request expectation. * Creates a new request expectation.
* *
* @param {string} method HTTP method. * @param {string} method HTTP method.
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * @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 * receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format. * is in JSON format.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current expectation. * object and returns true if the headers match the current expectation.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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 â * - respond
* `{function([status,] data[, headers, statusText]) * `{function([status,] data[, headers, statusText])
* | function(function(method, url, data, headers)}` * | function(function(method, url, data, headers)}`
* â The respond method takes a set of static data to be returned or a function that can * 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 * return an array containing response status (number), response data (string), response
* headers (Object), and the text for the status (string). * 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) { $httpBackend.expect = function(method, url, data, headers) {
var expectation = new MockHttpExpectation(method, url, data, headers); var expectation = new MockHttpExpectation(method, url, data, headers),
expectations.push(expectation); chain = {
return {
respond: function (status, data, headers, statusText) { respond: function (status, data, headers, statusText) {
expectation.response = createResponse(status, data, headers, statusText); expectation.response = createResponse(status, data, headers, statusText);
return chain;
} }
}; };
expectations.push(expectation);
return chain;
}; };
@ -1336,10 +1378,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for GET requests. For more info see `expect()`. * Creates a new request expectation for GET requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. See #expect for more info. * 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.
*/ */
/** /**
@ -1348,10 +1392,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for HEAD requests. For more info see `expect()`. * Creates a new request expectation for HEAD requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1360,10 +1406,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for DELETE requests. For more info see `expect()`. * Creates a new request expectation for DELETE requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1372,13 +1420,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for POST requests. For more info see `expect()`. * Creates a new request expectation for POST requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * @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 * receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format. * is in JSON format.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1387,13 +1437,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for PUT requests. For more info see `expect()`. * Creates a new request expectation for PUT requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * @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 * receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format. * is in JSON format.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1402,13 +1454,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for PATCH requests. For more info see `expect()`. * Creates a new request expectation for PATCH requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * @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 * receives data string and returns true if the data is as expected, or Object if request body
* is in JSON format. * is in JSON format.
* @param {Object=} headers HTTP headers. * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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.
*/ */
/** /**
@ -1417,9 +1471,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* @description * @description
* Creates a new request expectation for JSONP requests. For more info see `expect()`. * Creates a new request expectation for JSONP requests. For more info see `expect()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched * @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. * 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'); createShortMethods('expect');
@ -1434,11 +1490,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* all pending requests will be flushed. If there are no pending requests when the flush method * 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). * is called an exception is thrown (as this typically a sign of programming error).
*/ */
$httpBackend.flush = function(count) { $httpBackend.flush = function(count, digest) {
$rootScope.$digest(); if (digest !== false) $rootScope.$digest();
if (!responses.length) throw new Error('No pending request to flush !'); if (!responses.length) throw new Error('No pending request to flush !');
if (angular.isDefined(count)) { if (angular.isDefined(count) && count !== null) {
while (count--) { while (count--) {
if (!responses.length) throw new Error('No more pending request to flush !'); if (!responses.length) throw new Error('No more pending request to flush !');
responses.shift()(); responses.shift()();
@ -1448,7 +1504,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
responses.shift()(); responses.shift()();
} }
} }
$httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingExpectation(digest);
}; };
@ -1466,8 +1522,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
* afterEach($httpBackend.verifyNoOutstandingExpectation); * afterEach($httpBackend.verifyNoOutstandingExpectation);
* ``` * ```
*/ */
$httpBackend.verifyNoOutstandingExpectation = function() { $httpBackend.verifyNoOutstandingExpectation = function(digest) {
$rootScope.$digest(); if (digest !== false) $rootScope.$digest();
if (expectations.length) { if (expectations.length) {
throw new Error('Unsatisfied requests: ' + expectations.join(', ')); throw new Error('Unsatisfied requests: ' + expectations.join(', '));
} }
@ -1511,7 +1567,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
function createShortMethods(prefix) { function createShortMethods(prefix) {
angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) { angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) {
$httpBackend[prefix + method] = function(url, headers) { $httpBackend[prefix + method] = function(url, headers) {
return $httpBackend[prefix](method, url, undefined, headers); return $httpBackend[prefix](method, url, undefined, headers);
}; };
@ -1541,6 +1597,7 @@ function MockHttpExpectation(method, url, data, headers) {
this.matchUrl = function(u) { this.matchUrl = function(u) {
if (!url) return true; if (!url) return true;
if (angular.isFunction(url.test)) return url.test(u); if (angular.isFunction(url.test)) return url.test(u);
if (angular.isFunction(url)) return url(u);
return url == u; return url == u;
}; };
@ -1627,7 +1684,7 @@ function MockXhr() {
* that adds a "flush" and "verifyNoPendingTasks" methods. * that adds a "flush" and "verifyNoPendingTasks" methods.
*/ */
angular.mock.$TimeoutDecorator = function($delegate, $browser) { angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function ($delegate, $browser) {
/** /**
* @ngdoc method * @ngdoc method
@ -1666,9 +1723,9 @@ angular.mock.$TimeoutDecorator = function($delegate, $browser) {
} }
return $delegate; return $delegate;
}; }];
angular.mock.$RAFDecorator = function($delegate) { angular.mock.$RAFDecorator = ['$delegate', function($delegate) {
var queue = []; var queue = [];
var rafFn = function(fn) { var rafFn = function(fn) {
var index = queue.length; var index = queue.length;
@ -1694,9 +1751,9 @@ angular.mock.$RAFDecorator = function($delegate) {
}; };
return rafFn; return rafFn;
}; }];
angular.mock.$AsyncCallbackDecorator = function($delegate) { angular.mock.$AsyncCallbackDecorator = ['$delegate', function($delegate) {
var callbacks = []; var callbacks = [];
var addFn = function(fn) { var addFn = function(fn) {
callbacks.push(fn); callbacks.push(fn);
@ -1708,7 +1765,7 @@ angular.mock.$AsyncCallbackDecorator = function($delegate) {
callbacks = []; callbacks = [];
}; };
return addFn; return addFn;
}; }];
/** /**
* *
@ -1822,22 +1879,25 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* Creates a new backend definition. * Creates a new backend definition.
* *
* @param {string} method HTTP method. * @param {string} method HTTP method.
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp)=} data HTTP request body. * @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
* object and returns true if the headers match the current definition. * object and returns true if the headers match the current definition.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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 â * - respond
* `{function([status,] data[, headers, statusText]) * `{function([status,] data[, headers, statusText])
* | function(function(method, url, data, headers)}` * | function(function(method, url, data, headers)}`
* â The respond method takes a set of static data to be returned or a function that can return * 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 * an array containing response status (number), response data (string), response headers
* (Object), and the text for the status (string). * (Object), and the text for the status (string).
* - passThrough â `{function()}` â Any request matching a backend definition with * - 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 * `passThrough` handler will be passed through to the real backend (an XHR request will be made
* to the server.) * to the server.)
* - Both methods return the `requestHandler` object for possible overrides.
*/ */
/** /**
@ -1847,10 +1907,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for GET requests. For more info see `when()`. * Creates a new backend definition for GET requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1860,10 +1922,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for HEAD requests. For more info see `when()`. * Creates a new backend definition for HEAD requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1873,10 +1937,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for DELETE requests. For more info see `when()`. * Creates a new backend definition for DELETE requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1886,11 +1952,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for POST requests. For more info see `when()`. * Creates a new backend definition for POST requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp)=} data HTTP request body. * @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1900,11 +1968,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for PUT requests. For more info see `when()`. * Creates a new backend definition for PUT requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp)=} data HTTP request body. * @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1914,11 +1984,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for PATCH requests. For more info see `when()`. * Creates a new backend definition for PATCH requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @param {(string|RegExp)=} data HTTP request body. * @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers. * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
/** /**
@ -1928,30 +2000,17 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description * @description
* Creates a new backend definition for JSONP requests. For more info see `when()`. * Creates a new backend definition for JSONP requests. For more info see `when()`.
* *
* @param {string|RegExp} url HTTP url. * @param {string|RegExp|function(string)} url HTTP url or function that receives the url
* and returns true if the url match the current definition.
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
* control how a matched request is handled. * 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.
*/ */
angular.mock.e2e = {}; angular.mock.e2e = {};
angular.mock.e2e.$httpBackendDecorator = angular.mock.e2e.$httpBackendDecorator =
['$rootScope', '$delegate', '$browser', createHttpBackendMock]; ['$rootScope', '$delegate', '$browser', createHttpBackendMock];
angular.mock.clearDataCache = function() {
var key,
cache = angular.element.cache;
for(key in cache) {
if (Object.prototype.hasOwnProperty.call(cache,key)) {
var handle = cache[key].handle;
handle && angular.element(handle.elem).off();
delete cache[key];
}
}
};
if(window.jasmine || window.mocha) { if(window.jasmine || window.mocha) {
var currentSpec = null, var currentSpec = null,
@ -1982,8 +2041,6 @@ if(window.jasmine || window.mocha) {
injector.get('$browser').pollFns.length = 0; injector.get('$browser').pollFns.length = 0;
} }
angular.mock.clearDataCache();
// clean up jquery's fragment cache // clean up jquery's fragment cache
angular.forEach(angular.element.fragments, function(val, key) { angular.forEach(angular.element.fragments, function(val, key) {
delete angular.element.fragments[key]; delete angular.element.fragments[key];
@ -2003,6 +2060,7 @@ if(window.jasmine || window.mocha) {
* @description * @description
* *
* *NOTE*: This function is also published on window for easy access.<br> * *NOTE*: This function is also published on window for easy access.<br>
* *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
* *
* This function registers a module configuration code. It collects the configuration information * This function registers a module configuration code. It collects the configuration information
* which will be used when the injector is created by {@link angular.mock.inject inject}. * which will be used when the injector is created by {@link angular.mock.inject inject}.
@ -2045,6 +2103,7 @@ if(window.jasmine || window.mocha) {
* @description * @description
* *
* *NOTE*: This function is also published on window for easy access.<br> * *NOTE*: This function is also published on window for easy access.<br>
* *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
* *
* The inject function wraps a function into an injectable function. The inject() creates new * The inject function wraps a function into an injectable function. The inject() creates new
* instance of {@link auto.$injector $injector} per test, which is then used for * instance of {@link auto.$injector $injector} per test, which is then used for
@ -2144,14 +2203,28 @@ if(window.jasmine || window.mocha) {
///////////////////// /////////////////////
function workFn() { function workFn() {
var modules = currentSpec.$modules || []; var modules = currentSpec.$modules || [];
var strictDi = !!currentSpec.$injectorStrict;
modules.unshift('ngMock'); modules.unshift('ngMock');
modules.unshift('ng'); modules.unshift('ng');
var injector = currentSpec.$injector; var injector = currentSpec.$injector;
if (!injector) { if (!injector) {
injector = currentSpec.$injector = angular.injector(modules); if (strictDi) {
// If strictDi is enabled, annotate the providerInjector blocks
angular.forEach(modules, function(moduleFn) {
if (typeof moduleFn === "function") {
angular.injector.$$annotate(moduleFn);
}
});
}
injector = currentSpec.$injector = angular.injector(modules, strictDi);
currentSpec.$injectorStrict = strictDi;
} }
for(var i = 0, ii = blockFns.length; i < ii; i++) { for(var i = 0, ii = blockFns.length; i < ii; i++) {
if (currentSpec.$injectorStrict) {
// If the injector is strict / strictDi, and the spec wants to inject using automatic
// annotation, then annotate the function here.
injector.annotate(blockFns[i]);
}
try { try {
/* jshint -W040 *//* Jasmine explicitly provides a `this` object when calling functions */ /* jshint -W040 *//* Jasmine explicitly provides a `this` object when calling functions */
injector.invoke(blockFns[i] || angular.noop, this); injector.invoke(blockFns[i] || angular.noop, this);
@ -2167,7 +2240,20 @@ if(window.jasmine || window.mocha) {
} }
} }
}; };
angular.mock.inject.strictDi = function(value) {
value = arguments.length ? !!value : true;
return isSpecRunning() ? workFn() : workFn;
function workFn() {
if (value !== currentSpec.$injectorStrict) {
if (currentSpec.$injector) {
throw new Error('Injector already created, can not modify strict annotations');
} else {
currentSpec.$injectorStrict = value;
}
}
}
};
} }
})(window, window.angular);

View File

@ -0,0 +1,64 @@
describe('MatrixService', function() {
var scope, httpBackend, createController;
var BASE = "http://example.com";
var PREFIX = "/_matrix/client/api/v1";
var URL = BASE + PREFIX;
var roomId = "!wejigf387t34:matrix.org";
beforeEach(module('matrixService'));
beforeEach(inject(function($rootScope, $httpBackend, $controller) {
httpBackend = $httpBackend;
scope = $rootScope;
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should be able to GET /rooms/$roomid/state', inject(function(matrixService) {
matrixService.setConfig({
access_token: "foobar",
homeserver: "http://example.com"
});
matrixService.roomState(roomId).then(function(response) {
expect(response.data).toEqual([]);
});
httpBackend.expect('GET',
URL + "/rooms/" + roomId + "/state?access_token=foobar")
.respond([]);
httpBackend.flush();
}));
it('should be able to POST /join', inject(function(matrixService) {
matrixService.setConfig({
access_token: "foobar",
homeserver: "http://example.com"
});
matrixService.joinAlias(roomId).then(function(response) {
expect(response.data).toEqual({});
});
httpBackend.expect('POST',
URL + "/join/" + encodeURIComponent(roomId) + "?access_token=foobar")
.respond({});
httpBackend.flush();
}));
it('should be able to POST /rooms/$roomid/join', inject(function(matrixService) {
matrixService.setConfig({
access_token: "foobar",
homeserver: "http://example.com"
});
matrixService.join(roomId).then(function(response) {
expect(response.data).toEqual({});
});
httpBackend.expect('POST',
URL + "/rooms/" + encodeURIComponent(roomId) + "/join?access_token=foobar")
.respond({});
httpBackend.flush();
}));
});