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

Commit 83155e8

Browse files
committed
style(ResourceSpec): style clean up
1 parent 6d6f875 commit 83155e8

File tree

1 file changed

+100
-79
lines changed

1 file changed

+100
-79
lines changed

test/ResourceSpec.js

+100-79
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
'use strict';
22

33
describe("resource", function() {
4-
var resource, CreditCard, callback;
5-
6-
function nakedExpect(obj) {
7-
return expect(angular.fromJson(angular.toJson(obj)));
8-
}
9-
10-
beforeEach(inject(function($http) {
11-
resource = new ResourceFactory($http);
12-
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
13-
charge:{
14-
method:'POST',
15-
params:{verb:'!charge'}
16-
}
17-
});
18-
callback = jasmine.createSpy();
19-
})
20-
);
4+
var resource, CreditCard, callback, $httpBackend;
5+
6+
beforeEach(inject(function($injector, $http) {
7+
$httpBackend = $injector.get('$httpBackend');
8+
resource = new ResourceFactory($http);
9+
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
10+
charge:{
11+
method:'POST',
12+
params:{verb:'!charge'}
13+
}
14+
});
15+
callback = jasmine.createSpy();
16+
}));
2117

22-
afterEach(inject(function($httpBackend) {
18+
19+
afterEach(function() {
2320
$httpBackend.verifyNoOutstandingExpectation();
24-
}));
21+
});
22+
2523

2624
it("should build resource", function() {
2725
expect(typeof CreditCard).toBe('function');
@@ -32,12 +30,14 @@ describe("resource", function() {
3230
expect(typeof CreditCard.query).toBe('function');
3331
});
3432

35-
it('should default to empty parameters', inject(function($httpBackend) {
33+
34+
it('should default to empty parameters', function() {
3635
$httpBackend.expect('GET', 'URL').respond({});
3736
resource.route('URL').query();
38-
}));
37+
});
3938

40-
it('should ignore slashes of undefinend parameters', inject(function($httpBackend) {
39+
40+
it('should ignore slashes of undefinend parameters', function() {
4141
var R = resource.route('/Path/:a/:b/:c');
4242

4343
$httpBackend.when('GET').respond('{}');
@@ -50,26 +50,29 @@ describe("resource", function() {
5050
R.get({a:1});
5151
R.get({a:2, b:3});
5252
R.get({a:4, b:5, c:6});
53-
}));
53+
});
5454

55-
it('should support escaping collons in url template', inject(function($httpBackend) {
55+
56+
it('should support escaping collons in url template', function() {
5657
var R = resource.route('http://localhost\\:8080/Path/:a/\\:stillPath/:b');
5758

5859
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo/:stillPath/bar').respond();
5960
R.get({a: 'foo', b: 'bar'});
60-
}));
61+
});
62+
6163

62-
it('should correctly encode url params', inject(function($httpBackend) {
64+
it('should correctly encode url params', function() {
6365
var R = resource.route('/Path/:a');
6466

6567
$httpBackend.expect('GET', '/Path/foo%231').respond('{}');
6668
$httpBackend.expect('GET', '/Path/doh!@foo?bar=baz%231').respond('{}');
6769

6870
R.get({a: 'foo#1'});
6971
R.get({a: 'doh!@foo', bar: 'baz#1'});
70-
}));
72+
});
7173

72-
it('should not encode @ in url params', inject(function($httpBackend) {
74+
75+
it('should not encode @ in url params', function() {
7376
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
7477
//with regards to the character set (pchar) allowed in path segments
7578
//so we need this test to make sure that we don't over-encode the params and break stuff like
@@ -78,62 +81,68 @@ describe("resource", function() {
7881
var R = resource.route('/Path/:a');
7982
$httpBackend.expect('GET', '/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond('{}');
8083
R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'});
81-
}));
84+
});
8285

83-
it('should encode & in url params', inject(function($httpBackend) {
86+
87+
it('should encode & in url params', function() {
8488
var R = resource.route('/Path/:a');
8589
$httpBackend.expect('GET', '/Path/doh&foo?bar=baz%261').respond('{}');
8690
R.get({a: 'doh&foo', bar: 'baz&1'});
87-
}));
91+
});
92+
8893

89-
it('should build resource with default param', inject(function($httpBackend) {
94+
it('should build resource with default param', function() {
9095
$httpBackend.expect('GET', '/Order/123/Line/456.visa?minimum=0.05').respond({id: 'abc'});
9196
var LineItem = resource.route('/Order/:orderId/Line/:id:verb',
9297
{orderId: '123', id: '@id.key', verb:'.visa', minimum: 0.05});
9398
var item = LineItem.get({id: 456});
9499
$httpBackend.flush();
95-
nakedExpect(item).toEqual({id:'abc'});
96-
}));
100+
expect(item).toEqualData({id:'abc'});
101+
});
97102

98-
it("should build resource with action default param overriding default param", inject(function($httpBackend) {
103+
104+
it("should build resource with action default param overriding default param", function() {
99105
$httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'});
100106
var TypeItem = resource.route('/:type/:typeId', {type: 'Order'},
101107
{get: {method: 'GET', params: {type: 'Customer'}}});
102108
var item = TypeItem.get({typeId: 123});
103109

104110
$httpBackend.flush();
105-
nakedExpect(item).toEqual({id: 'abc'});
106-
}));
111+
expect(item).toEqualData({id: 'abc'});
112+
});
107113

108-
it("should create resource", inject(function($httpBackend) {
114+
115+
it("should create resource", function() {
109116
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
110117

111118
var cc = CreditCard.save({name: 'misko'}, callback);
112-
nakedExpect(cc).toEqual({name: 'misko'});
119+
expect(cc).toEqualData({name: 'misko'});
113120
expect(callback).not.toHaveBeenCalled();
114121

115122
$httpBackend.flush();
116-
nakedExpect(cc).toEqual({id: 123, name: 'misko'});
123+
expect(cc).toEqualData({id: 123, name: 'misko'});
117124
expect(callback).toHaveBeenCalledOnce();
118125
expect(callback.mostRecentCall.args[0]).toEqual(cc);
119126
expect(callback.mostRecentCall.args[1]()).toEqual({});
120-
}));
127+
});
128+
121129

122-
it("should read resource", inject(function($httpBackend) {
130+
it("should read resource", function() {
123131
$httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
124132
var cc = CreditCard.get({id: 123}, callback);
125133

126134
expect(cc instanceof CreditCard).toBeTruthy();
127-
nakedExpect(cc).toEqual({});
135+
expect(cc).toEqualData({});
128136
expect(callback).not.toHaveBeenCalled();
129137

130138
$httpBackend.flush();
131-
nakedExpect(cc).toEqual({id: 123, number: '9876'});
139+
expect(cc).toEqualData({id: 123, number: '9876'});
132140
expect(callback.mostRecentCall.args[0]).toEqual(cc);
133141
expect(callback.mostRecentCall.args[1]()).toEqual({});
134-
}));
142+
});
135143

136-
it("should read partial resource", inject(function($httpBackend) {
144+
145+
it("should read partial resource", function() {
137146
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
138147
var ccs = CreditCard.query();
139148

@@ -150,49 +159,53 @@ describe("resource", function() {
150159
expect(callback.mostRecentCall.args[0]).toEqual(cc);
151160
expect(callback.mostRecentCall.args[1]()).toEqual({});
152161
expect(cc.number).toEqual('9876');
153-
}));
162+
});
154163

155-
it("should update resource", inject(function($httpBackend) {
164+
165+
it("should update resource", function() {
156166
$httpBackend.expect('POST', '/CreditCard/123', '{"id":{"key":123},"name":"misko"}').
157167
respond({id: {key: 123}, name: 'rama'});
158168

159169
var cc = CreditCard.save({id: {key: 123}, name: 'misko'}, callback);
160-
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
170+
expect(cc).toEqualData({id:{key:123}, name:'misko'});
161171
expect(callback).not.toHaveBeenCalled();
162172
$httpBackend.flush();
163-
}));
173+
});
174+
164175

165-
it("should query resource", inject(function($httpBackend) {
176+
it("should query resource", function() {
166177
$httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);
167178

168179
var ccs = CreditCard.query({key: 'value'}, callback);
169180
expect(ccs).toEqual([]);
170181
expect(callback).not.toHaveBeenCalled();
171182

172183
$httpBackend.flush();
173-
nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
184+
expect(ccs).toEqualData([{id:1}, {id:2}]);
174185
expect(callback.mostRecentCall.args[0]).toEqual(ccs);
175186
expect(callback.mostRecentCall.args[1]()).toEqual({});
176-
}));
187+
});
177188

178-
it("should have all arguments optional", inject(function($httpBackend) {
189+
190+
it("should have all arguments optional", function() {
179191
$httpBackend.expect('GET', '/CreditCard').respond([{id:1}]);
180192

181193
var log = '';
182194
var ccs = CreditCard.query(function() { log += 'cb;'; });
183195

184196
$httpBackend.flush();
185-
nakedExpect(ccs).toEqual([{id:1}]);
197+
expect(ccs).toEqualData([{id:1}]);
186198
expect(log).toEqual('cb;');
187-
}));
199+
});
188200

189-
it('should delete resource and call callback', inject(function($httpBackend) {
201+
202+
it('should delete resource and call callback', function() {
190203
$httpBackend.expect('DELETE', '/CreditCard/123').respond({});
191204
CreditCard.remove({id:123}, callback);
192205
expect(callback).not.toHaveBeenCalled();
193206

194207
$httpBackend.flush();
195-
nakedExpect(callback.mostRecentCall.args[0]).toEqual({});
208+
expect(callback.mostRecentCall.args[0]).toEqualData({});
196209
expect(callback.mostRecentCall.args[1]()).toEqual({});
197210

198211
callback.reset();
@@ -201,24 +214,27 @@ describe("resource", function() {
201214
expect(callback).not.toHaveBeenCalled();
202215

203216
$httpBackend.flush();
204-
nakedExpect(callback.mostRecentCall.args[0]).toEqual({});
217+
expect(callback.mostRecentCall.args[0]).toEqualData({});
205218
expect(callback.mostRecentCall.args[1]()).toEqual({});
206-
}));
219+
});
220+
207221

208-
it('should post charge verb', inject(function($httpBackend) {
222+
it('should post charge verb', function() {
209223
$httpBackend.expect('POST', '/CreditCard/123!charge?amount=10', '{"auth":"abc"}').respond({success: 'ok'});
210224
CreditCard.charge({id:123, amount:10}, {auth:'abc'}, callback);
211-
}));
225+
});
212226

213-
it('should post charge verb on instance', inject(function($httpBackend) {
227+
228+
it('should post charge verb on instance', function() {
214229
$httpBackend.expect('POST', '/CreditCard/123!charge?amount=10',
215230
'{"id":{"key":123},"name":"misko"}').respond({success: 'ok'});
216231

217232
var card = new CreditCard({id:{key:123}, name:'misko'});
218233
card.$charge({amount:10}, callback);
219-
}));
234+
});
220235

221-
it('should create on save', inject(function($httpBackend) {
236+
237+
it('should create on save', function() {
222238
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});
223239

224240
var cc = new CreditCard();
@@ -229,15 +245,16 @@ describe("resource", function() {
229245

230246
cc.name = 'misko';
231247
cc.$save(callback);
232-
nakedExpect(cc).toEqual({name:'misko'});
248+
expect(cc).toEqualData({name:'misko'});
233249

234250
$httpBackend.flush();
235-
nakedExpect(cc).toEqual({id:123});
251+
expect(cc).toEqualData({id:123});
236252
expect(callback.mostRecentCall.args[0]).toEqual(cc);
237253
expect(callback.mostRecentCall.args[1]()).toEqual({header1: 'a'});
238-
}));
254+
});
255+
239256

240-
it('should not mutate the resource object if response contains no body', inject(function($httpBackend) {
257+
it('should not mutate the resource object if response contains no body', function() {
241258
var data = {id:{key:123}, number:'9876'};
242259
$httpBackend.expect('GET', '/CreditCard/123').respond(data);
243260

@@ -251,17 +268,19 @@ describe("resource", function() {
251268
cc.$save();
252269
$httpBackend.flush();
253270
expect(idBefore).toEqual(cc.id);
254-
}));
271+
});
255272

256-
it('should bind default parameters', inject(function($httpBackend) {
273+
274+
it('should bind default parameters', function() {
257275
$httpBackend.expect('GET', '/CreditCard/123.visa?minimum=0.05').respond({id: 123});
258276
var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});
259277
var visa = Visa.get({id:123});
260278
$httpBackend.flush();
261-
nakedExpect(visa).toEqual({id:123});
262-
}));
279+
expect(visa).toEqualData({id:123});
280+
});
263281

264-
it('should excersize full stack', inject(function($httpBackend, $resource) {
282+
283+
it('should excersize full stack', inject(function($resource) {
265284
var Person = $resource('/Person/:id');
266285

267286
$httpBackend.expect('GET', '/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
@@ -270,6 +289,7 @@ describe("resource", function() {
270289
expect(person.name).toEqual('misko');
271290
}));
272291

292+
273293
describe('failure mode', function() {
274294
var ERROR_CODE = 500,
275295
ERROR_RESPONSE = 'Server Error',
@@ -282,23 +302,24 @@ describe("resource", function() {
282302
});
283303
});
284304

285-
it('should call the error callback if provided on non 2xx response',
286-
inject(function($httpBackend, $rootScope) {
305+
306+
it('should call the error callback if provided on non 2xx response', function() {
287307
$httpBackend.expect('GET', '/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
288308

289309
CreditCard.get({id:123}, callback, errorCB);
290310
$httpBackend.flush();
291311
expect(errorCB).toHaveBeenCalledOnce();
292312
expect(callback).not.toHaveBeenCalled();
293-
}));
313+
});
294314

295-
it('should call the error callback if provided on non 2xx response', inject(function($httpBackend) {
315+
316+
it('should call the error callback if provided on non 2xx response', function() {
296317
$httpBackend.expect('GET', '/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);
297318

298319
CreditCard.get(callback, errorCB);
299320
$httpBackend.flush();
300321
expect(errorCB).toHaveBeenCalledOnce();
301322
expect(callback).not.toHaveBeenCalled();
302-
}));
323+
});
303324
});
304325
});

0 commit comments

Comments
 (0)