resources now use browser mock

This commit is contained in:
Misko Hevery 2010-04-27 11:18:08 -07:00
parent b275403465
commit fce48eb60a
7 changed files with 50 additions and 17 deletions

View file

@ -1,4 +1,6 @@
<div>
<div ng-controller="AccountController">
account page goes here!
<input type="text" name="name" value="misko"/>
<button ng-click="hello()">hello</button>
</div>

View file

@ -3,6 +3,18 @@
<head>
<link rel="stylesheet" type="text/css" href="style.css"></link>
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
<script>
function AccountController(){
}
AccountController.prototype = {
hello: function(){
alert(this.name);
}
};
</script>
</head>
<body ng-init="$window.$scope = this">
[ <a href="#login">login</a>
@ -11,9 +23,12 @@
<ng:switch on="$location.hashPath">
<div ng-switch-when="login">login screen</div>
<ng:include ng-switch-when="account" src="application-account.html"></ng:include>
<ng:include ng-switch-when="account" src="'application-account.html'"></ng:include>
</ng:switch>
(( input name ))
<pre>$location={{$location}}</pre>
</body>
</html>

View file

@ -1,4 +1,4 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>

View file

@ -83,7 +83,7 @@ ResourceFactory.prototype = {
}
var value = action.isArray ? [] : new Resource(data);
self.xhr(action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, function(response) {
self.xhr(action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, function(status, response) {
if (action.isArray) {
foreach(response, function(item){
value.push(new Resource(item));

View file

@ -60,8 +60,9 @@ describe("resource", function() {
var xhr, resource, CreditCard, callback;
beforeEach(function(){
xhr = new MockXHR();
resource = new ResourceFactory(bind(xhr, xhr.method));
var browser = new MockBrowser();
xhr = browser.xhr;
resource = new ResourceFactory(xhr);
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
charge:{
method:'POST',
@ -95,7 +96,7 @@ describe("resource", function() {
});
it("should create resource", function(){
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'});
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'});
var cc = CreditCard.save({name:'misko'}, callback);
nakedExpect(cc).toEqual({name:'misko'});
@ -117,7 +118,7 @@ describe("resource", function() {
});
it("should update resource", function(){
xhr.expectPOST('/CreditCard/123').data({id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
@ -148,13 +149,13 @@ describe("resource", function() {
});
it('should post charge verb', function(){
xhr.expectPOST('/CreditCard/123!charge?amount=10').data({auth:'abc'}).respond({success:'ok'});
xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'});
CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
});
it('should create on save', function(){
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123});
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123});
var cc = new CreditCard();
expect(cc.$get).not.toBeDefined();
expect(cc.$query).not.toBeDefined();

20
test/angular-mocks.js vendored
View file

@ -23,11 +23,19 @@
*/
function MockBrowser() {
var self = this, expectations = {}, requests = [];
var self = this,
expectations = {},
requests = [];
self.url = "http://server";
self.watches = [];
self.xhr = function(method, url, callback) {
self.xhr = function(method, url, data, callback) {
if (isFunction(data)) {
callback = data;
data = null;
}
if (data && isObject(data)) data = angular.toJson(data);
if (data && isString(data)) url += "|" + data;
var expect = expectations[method] || {};
var response = expect[url];
if (!response) {
@ -39,7 +47,9 @@ function MockBrowser() {
};
self.xhr.expectations = expectations;
self.xhr.requests = requests;
self.xhr.expect = function(method, url) {
self.xhr.expect = function(method, url, data) {
if (data && isObject(data)) data = angular.toJson(data);
if (data && isString(data)) url += "|" + data;
var expect = expectations[method] || (expectations[method] = {});
return {
respond: function(response) {
@ -47,6 +57,10 @@ function MockBrowser() {
}
};
};
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');
self.xhr.flush = function() {
while(requests.length) {
requests.pop()();

View file

@ -10,20 +10,21 @@ function childNode(element, index) {
}
extend(angular, {
'element': jqLite,
'bind': bind,
'compile': compile,
'scope': createScope,
'copy': copy,
'element': jqLite,
'extend': extend,
'foreach': foreach,
'noop':noop,
'identity':identity,
'isUndefined': isUndefined,
'isDefined': isDefined,
'isString': isString,
'isFunction': isFunction,
'isNumber': isNumber,
'isArray': isArray
'isArray': isArray,
'noop':noop,
'scope': createScope
});