Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fce48eb

Browse files
committed
resources now use browser mock
1 parent b275403 commit fce48eb

File tree

7 files changed

+50
-17
lines changed

7 files changed

+50
-17
lines changed

scenario/application-account.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<div>
1+
<div ng-controller="AccountController">
22
account page goes here!
3+
<input type="text" name="name" value="misko"/>
4+
<button ng-click="hello()">hello</button>
35
</div>
46

scenario/application.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
<head>
44
<link rel="stylesheet" type="text/css" href="style.css"></link>
55
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
6+
<script>
7+
function AccountController(){
8+
}
9+
10+
AccountController.prototype = {
11+
hello: function(){
12+
alert(this.name);
13+
}
14+
15+
};
16+
17+
</script>
618
</head>
719
<body ng-init="$window.$scope = this">
820
[ <a href="#login">login</a>
@@ -11,9 +23,12 @@
1123

1224
<ng:switch on="$location.hashPath">
1325
<div ng-switch-when="login">login screen</div>
14-
<ng:include ng-switch-when="account" src="application-account.html"></ng:include>
26+
<ng:include ng-switch-when="account" src="'application-account.html'"></ng:include>
1527
</ng:switch>
1628

29+
30+
(( input name ))
31+
1732
<pre>$location={{$location}}</pre>
1833
</body>
1934
</html>

scenario/widgets.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
33
<head>
44
<link rel="stylesheet" type="text/css" href="style.css"/>

src/Resource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ ResourceFactory.prototype = {
8383
}
8484

8585
var value = action.isArray ? [] : new Resource(data);
86-
self.xhr(action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, function(response) {
86+
self.xhr(action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, function(status, response) {
8787
if (action.isArray) {
8888
foreach(response, function(item){
8989
value.push(new Resource(item));

test/ResourceSpec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ describe("resource", function() {
6060
var xhr, resource, CreditCard, callback;
6161

6262
beforeEach(function(){
63-
xhr = new MockXHR();
64-
resource = new ResourceFactory(bind(xhr, xhr.method));
63+
var browser = new MockBrowser();
64+
xhr = browser.xhr;
65+
resource = new ResourceFactory(xhr);
6566
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
6667
charge:{
6768
method:'POST',
@@ -95,7 +96,7 @@ describe("resource", function() {
9596
});
9697

9798
it("should create resource", function(){
98-
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'});
99+
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'});
99100

100101
var cc = CreditCard.save({name:'misko'}, callback);
101102
nakedExpect(cc).toEqual({name:'misko'});
@@ -117,7 +118,7 @@ describe("resource", function() {
117118
});
118119

119120
it("should update resource", function(){
120-
xhr.expectPOST('/CreditCard/123').data({id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
121+
xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
121122

122123
var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);
123124
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
@@ -148,13 +149,13 @@ describe("resource", function() {
148149
});
149150

150151
it('should post charge verb', function(){
151-
xhr.expectPOST('/CreditCard/123!charge?amount=10').data({auth:'abc'}).respond({success:'ok'});
152+
xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'});
152153

153154
CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
154155
});
155156

156157
it('should create on save', function(){
157-
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123});
158+
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123});
158159
var cc = new CreditCard();
159160
expect(cc.$get).not.toBeDefined();
160161
expect(cc.$query).not.toBeDefined();

test/angular-mocks.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@
2323
*/
2424

2525
function MockBrowser() {
26-
var self = this, expectations = {}, requests = [];
26+
var self = this,
27+
expectations = {},
28+
requests = [];
2729
self.url = "http://server";
2830
self.watches = [];
2931

30-
self.xhr = function(method, url, callback) {
32+
self.xhr = function(method, url, data, callback) {
33+
if (isFunction(data)) {
34+
callback = data;
35+
data = null;
36+
}
37+
if (data && isObject(data)) data = angular.toJson(data);
38+
if (data && isString(data)) url += "|" + data;
3139
var expect = expectations[method] || {};
3240
var response = expect[url];
3341
if (!response) {
@@ -39,14 +47,20 @@ function MockBrowser() {
3947
};
4048
self.xhr.expectations = expectations;
4149
self.xhr.requests = requests;
42-
self.xhr.expect = function(method, url) {
50+
self.xhr.expect = function(method, url, data) {
51+
if (data && isObject(data)) data = angular.toJson(data);
52+
if (data && isString(data)) url += "|" + data;
4353
var expect = expectations[method] || (expectations[method] = {});
4454
return {
4555
respond: function(response) {
4656
expect[url] = response;
4757
}
4858
};
4959
};
60+
self.xhr.expectGET = angular.bind(self, self.xhr.expect, 'GET');
61+
self.xhr.expectPOST = angular.bind(self, self.xhr.expect, 'POST');
62+
self.xhr.expectDELETE = angular.bind(self, self.xhr.expect, 'DELETE');
63+
self.xhr.expectPUT = angular.bind(self, self.xhr.expect, 'PUT');
5064
self.xhr.flush = function() {
5165
while(requests.length) {
5266
requests.pop()();

test/testabilityPatch.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ function childNode(element, index) {
1010
}
1111

1212
extend(angular, {
13-
'element': jqLite,
13+
'bind': bind,
1414
'compile': compile,
15-
'scope': createScope,
1615
'copy': copy,
16+
'element': jqLite,
1717
'extend': extend,
1818
'foreach': foreach,
19-
'noop':noop,
2019
'identity':identity,
2120
'isUndefined': isUndefined,
2221
'isDefined': isDefined,
2322
'isString': isString,
2423
'isFunction': isFunction,
2524
'isNumber': isNumber,
26-
'isArray': isArray
25+
'isArray': isArray,
26+
'noop':noop,
27+
'scope': createScope
2728
});
2829

2930

0 commit comments

Comments
 (0)