angular.js/test/angular-mocks.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

2010-04-15 21:17:33 +00:00
/**
* The MIT License
*
* Copyright (c) 2010 Adam Abrons and Misko Hevery http://getangular.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2010-04-05 18:46:53 +00:00
function MockBrowser() {
2010-04-27 18:18:08 +00:00
var self = this,
expectations = {},
requests = [];
2010-05-07 19:09:14 +00:00
this.isMock = true;
2010-04-05 21:09:25 +00:00
self.url = "http://server";
self.pollFns = [];
2010-04-05 21:09:25 +00:00
2010-04-27 18:18:08 +00:00
self.xhr = function(method, url, data, callback) {
2010-05-07 19:09:14 +00:00
if (angular.isFunction(data)) {
2010-04-27 18:18:08 +00:00
callback = data;
data = null;
}
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] || {};
var response = expect[url];
if (!response) {
2010-04-06 03:53:33 +00:00
throw "Unexepected request for method '" + method + "' and url '" + url + "'.";
2010-04-05 21:09:25 +00:00
}
requests.push(function(){
2010-05-19 18:51:17 +00:00
callback(response.code, response.response);
2010-04-05 21:09:25 +00:00
});
};
self.xhr.expectations = expectations;
self.xhr.requests = requests;
2010-04-27 18:18:08 +00:00
self.xhr.expect = function(method, url, data) {
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) {
if (!angular.isNumber(code)) {
2010-05-19 18:51:17 +00:00
response = code;
code = 200;
}
expect[url] = {code:code, response:response};
2010-04-05 21:09:25 +00:00
}
};
};
2010-04-27 18:18:08 +00:00
self.xhr.expectGET = angular.bind(self, self.xhr.expect, 'GET');
self.xhr.expectPOST = angular.bind(self, self.xhr.expect, 'POST');
self.xhr.expectDELETE = angular.bind(self, self.xhr.expect, 'DELETE');
self.xhr.expectPUT = angular.bind(self, self.xhr.expect, 'PUT');
2010-07-22 18:18:32 +00:00
self.xhr.expectJSON = angular.bind(self, self.xhr.expect, 'JSON');
2010-04-05 21:09:25 +00:00
self.xhr.flush = function() {
while(requests.length) {
requests.pop()();
}
};
self.cookieHash = {};
self.lastCookieHash = {};
2010-04-05 18:46:53 +00:00
}
MockBrowser.prototype = {
poll: function poll(){
foreach(this.pollFns, function(pollFn){ pollFn(); });
},
addPollFn: function(pollFn) {
this.pollFns.push(pollFn);
return pollFn;
},
2010-04-06 03:53:33 +00:00
hover: function(onHover) {
},
2010-04-05 18:46:53 +00:00
getUrl: function(){
return this.url;
},
setUrl: function(url){
this.url = url;
},
cookies: function(name, value) {
if (name) {
if (value == undefined) {
delete this.cookieHash[name];
} else {
if (isString(value) && //strings only
value.length <= 4096) { //strict cookie storage limits
this.cookieHash[name] = value;
}
}
} else {
if (!equals(this.cookieHash, this.lastCookieHash)) {
this.lastCookieHash = copy(this.cookieHash);
this.cookieHash = copy(this.cookieHash);
}
return this.cookieHash;
}
2010-04-05 18:46:53 +00:00
}
2010-04-05 18:46:53 +00:00
};
angular.service('$browser', function(){
return new MockBrowser();
});