mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-04-20 23:00:59 +00:00
resources now use browser mock
This commit is contained in:
parent
b275403465
commit
fce48eb60a
7 changed files with 50 additions and 17 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
<div>
|
<div ng-controller="AccountController">
|
||||||
account page goes here!
|
account page goes here!
|
||||||
|
<input type="text" name="name" value="misko"/>
|
||||||
|
<button ng-click="hello()">hello</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,18 @@
|
||||||
<head>
|
<head>
|
||||||
<link rel="stylesheet" type="text/css" href="style.css"></link>
|
<link rel="stylesheet" type="text/css" href="style.css"></link>
|
||||||
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
|
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
|
||||||
|
<script>
|
||||||
|
function AccountController(){
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountController.prototype = {
|
||||||
|
hello: function(){
|
||||||
|
alert(this.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body ng-init="$window.$scope = this">
|
<body ng-init="$window.$scope = this">
|
||||||
[ <a href="#login">login</a>
|
[ <a href="#login">login</a>
|
||||||
|
|
@ -11,9 +23,12 @@
|
||||||
|
|
||||||
<ng:switch on="$location.hashPath">
|
<ng:switch on="$location.hashPath">
|
||||||
<div ng-switch-when="login">login screen</div>
|
<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>
|
</ng:switch>
|
||||||
|
|
||||||
|
|
||||||
|
(( input name ))
|
||||||
|
|
||||||
<pre>$location={{$location}}</pre>
|
<pre>$location={{$location}}</pre>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -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">
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<link rel="stylesheet" type="text/css" href="style.css"/>
|
<link rel="stylesheet" type="text/css" href="style.css"/>
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ ResourceFactory.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
var value = action.isArray ? [] : new Resource(data);
|
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) {
|
if (action.isArray) {
|
||||||
foreach(response, function(item){
|
foreach(response, function(item){
|
||||||
value.push(new Resource(item));
|
value.push(new Resource(item));
|
||||||
|
|
|
||||||
|
|
@ -60,8 +60,9 @@ describe("resource", function() {
|
||||||
var xhr, resource, CreditCard, callback;
|
var xhr, resource, CreditCard, callback;
|
||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
xhr = new MockXHR();
|
var browser = new MockBrowser();
|
||||||
resource = new ResourceFactory(bind(xhr, xhr.method));
|
xhr = browser.xhr;
|
||||||
|
resource = new ResourceFactory(xhr);
|
||||||
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
|
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
|
||||||
charge:{
|
charge:{
|
||||||
method:'POST',
|
method:'POST',
|
||||||
|
|
@ -95,7 +96,7 @@ describe("resource", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create 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);
|
var cc = CreditCard.save({name:'misko'}, callback);
|
||||||
nakedExpect(cc).toEqual({name:'misko'});
|
nakedExpect(cc).toEqual({name:'misko'});
|
||||||
|
|
@ -117,7 +118,7 @@ describe("resource", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should update 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);
|
var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);
|
||||||
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
|
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
|
||||||
|
|
@ -148,13 +149,13 @@ describe("resource", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should post charge verb', 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);
|
CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create on save', function(){
|
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();
|
var cc = new CreditCard();
|
||||||
expect(cc.$get).not.toBeDefined();
|
expect(cc.$get).not.toBeDefined();
|
||||||
expect(cc.$query).not.toBeDefined();
|
expect(cc.$query).not.toBeDefined();
|
||||||
|
|
|
||||||
20
test/angular-mocks.js
vendored
20
test/angular-mocks.js
vendored
|
|
@ -23,11 +23,19 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function MockBrowser() {
|
function MockBrowser() {
|
||||||
var self = this, expectations = {}, requests = [];
|
var self = this,
|
||||||
|
expectations = {},
|
||||||
|
requests = [];
|
||||||
self.url = "http://server";
|
self.url = "http://server";
|
||||||
self.watches = [];
|
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 expect = expectations[method] || {};
|
||||||
var response = expect[url];
|
var response = expect[url];
|
||||||
if (!response) {
|
if (!response) {
|
||||||
|
|
@ -39,7 +47,9 @@ function MockBrowser() {
|
||||||
};
|
};
|
||||||
self.xhr.expectations = expectations;
|
self.xhr.expectations = expectations;
|
||||||
self.xhr.requests = requests;
|
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] = {});
|
var expect = expectations[method] || (expectations[method] = {});
|
||||||
return {
|
return {
|
||||||
respond: function(response) {
|
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() {
|
self.xhr.flush = function() {
|
||||||
while(requests.length) {
|
while(requests.length) {
|
||||||
requests.pop()();
|
requests.pop()();
|
||||||
|
|
|
||||||
|
|
@ -10,20 +10,21 @@ function childNode(element, index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
extend(angular, {
|
extend(angular, {
|
||||||
'element': jqLite,
|
'bind': bind,
|
||||||
'compile': compile,
|
'compile': compile,
|
||||||
'scope': createScope,
|
|
||||||
'copy': copy,
|
'copy': copy,
|
||||||
|
'element': jqLite,
|
||||||
'extend': extend,
|
'extend': extend,
|
||||||
'foreach': foreach,
|
'foreach': foreach,
|
||||||
'noop':noop,
|
|
||||||
'identity':identity,
|
'identity':identity,
|
||||||
'isUndefined': isUndefined,
|
'isUndefined': isUndefined,
|
||||||
'isDefined': isDefined,
|
'isDefined': isDefined,
|
||||||
'isString': isString,
|
'isString': isString,
|
||||||
'isFunction': isFunction,
|
'isFunction': isFunction,
|
||||||
'isNumber': isNumber,
|
'isNumber': isNumber,
|
||||||
'isArray': isArray
|
'isArray': isArray,
|
||||||
|
'noop':noop,
|
||||||
|
'scope': createScope
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue