1
1
'use strict' ;
2
2
3
3
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
+ } ) ) ;
21
17
22
- afterEach ( inject ( function ( $httpBackend ) {
18
+
19
+ afterEach ( function ( ) {
23
20
$httpBackend . verifyNoOutstandingExpectation ( ) ;
24
- } ) ) ;
21
+ } ) ;
22
+
25
23
26
24
it ( "should build resource" , function ( ) {
27
25
expect ( typeof CreditCard ) . toBe ( 'function' ) ;
@@ -32,12 +30,14 @@ describe("resource", function() {
32
30
expect ( typeof CreditCard . query ) . toBe ( 'function' ) ;
33
31
} ) ;
34
32
35
- it ( 'should default to empty parameters' , inject ( function ( $httpBackend ) {
33
+
34
+ it ( 'should default to empty parameters' , function ( ) {
36
35
$httpBackend . expect ( 'GET' , 'URL' ) . respond ( { } ) ;
37
36
resource . route ( 'URL' ) . query ( ) ;
38
- } ) ) ;
37
+ } ) ;
39
38
40
- it ( 'should ignore slashes of undefinend parameters' , inject ( function ( $httpBackend ) {
39
+
40
+ it ( 'should ignore slashes of undefinend parameters' , function ( ) {
41
41
var R = resource . route ( '/Path/:a/:b/:c' ) ;
42
42
43
43
$httpBackend . when ( 'GET' ) . respond ( '{}' ) ;
@@ -50,26 +50,29 @@ describe("resource", function() {
50
50
R . get ( { a :1 } ) ;
51
51
R . get ( { a :2 , b :3 } ) ;
52
52
R . get ( { a :4 , b :5 , c :6 } ) ;
53
- } ) ) ;
53
+ } ) ;
54
54
55
- it ( 'should support escaping collons in url template' , inject ( function ( $httpBackend ) {
55
+
56
+ it ( 'should support escaping collons in url template' , function ( ) {
56
57
var R = resource . route ( 'http://localhost\\:8080/Path/:a/\\:stillPath/:b' ) ;
57
58
58
59
$httpBackend . expect ( 'GET' , 'http://localhost:8080/Path/foo/:stillPath/bar' ) . respond ( ) ;
59
60
R . get ( { a : 'foo' , b : 'bar' } ) ;
60
- } ) ) ;
61
+ } ) ;
62
+
61
63
62
- it ( 'should correctly encode url params' , inject ( function ( $httpBackend ) {
64
+ it ( 'should correctly encode url params' , function ( ) {
63
65
var R = resource . route ( '/Path/:a' ) ;
64
66
65
67
$httpBackend . expect ( 'GET' , '/Path/foo%231' ) . respond ( '{}' ) ;
66
68
$httpBackend . expect ( 'GET' , '/Path/doh!@foo?bar=baz%231' ) . respond ( '{}' ) ;
67
69
68
70
R . get ( { a : 'foo#1' } ) ;
69
71
R . get ( { a : 'doh!@foo' , bar : 'baz#1' } ) ;
70
- } ) ) ;
72
+ } ) ;
71
73
72
- it ( 'should not encode @ in url params' , inject ( function ( $httpBackend ) {
74
+
75
+ it ( 'should not encode @ in url params' , function ( ) {
73
76
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
74
77
//with regards to the character set (pchar) allowed in path segments
75
78
//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() {
78
81
var R = resource . route ( '/Path/:a' ) ;
79
82
$httpBackend . expect ( 'GET' , '/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1' ) . respond ( '{}' ) ;
80
83
R . get ( { a : 'doh@fo o' , ':bar' : '$baz@1' , '!do&h' : 'g=a h' } ) ;
81
- } ) ) ;
84
+ } ) ;
82
85
83
- it ( 'should encode & in url params' , inject ( function ( $httpBackend ) {
86
+
87
+ it ( 'should encode & in url params' , function ( ) {
84
88
var R = resource . route ( '/Path/:a' ) ;
85
89
$httpBackend . expect ( 'GET' , '/Path/doh&foo?bar=baz%261' ) . respond ( '{}' ) ;
86
90
R . get ( { a : 'doh&foo' , bar : 'baz&1' } ) ;
87
- } ) ) ;
91
+ } ) ;
92
+
88
93
89
- it ( 'should build resource with default param' , inject ( function ( $httpBackend ) {
94
+ it ( 'should build resource with default param' , function ( ) {
90
95
$httpBackend . expect ( 'GET' , '/Order/123/Line/456.visa?minimum=0.05' ) . respond ( { id : 'abc' } ) ;
91
96
var LineItem = resource . route ( '/Order/:orderId/Line/:id:verb' ,
92
97
{ orderId : '123' , id : '@id.key' , verb :'.visa' , minimum : 0.05 } ) ;
93
98
var item = LineItem . get ( { id : 456 } ) ;
94
99
$httpBackend . flush ( ) ;
95
- nakedExpect ( item ) . toEqual ( { id :'abc' } ) ;
96
- } ) ) ;
100
+ expect ( item ) . toEqualData ( { id :'abc' } ) ;
101
+ } ) ;
97
102
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 ( ) {
99
105
$httpBackend . expect ( 'GET' , '/Customer/123' ) . respond ( { id : 'abc' } ) ;
100
106
var TypeItem = resource . route ( '/:type/:typeId' , { type : 'Order' } ,
101
107
{ get : { method : 'GET' , params : { type : 'Customer' } } } ) ;
102
108
var item = TypeItem . get ( { typeId : 123 } ) ;
103
109
104
110
$httpBackend . flush ( ) ;
105
- nakedExpect ( item ) . toEqual ( { id : 'abc' } ) ;
106
- } ) ) ;
111
+ expect ( item ) . toEqualData ( { id : 'abc' } ) ;
112
+ } ) ;
107
113
108
- it ( "should create resource" , inject ( function ( $httpBackend ) {
114
+
115
+ it ( "should create resource" , function ( ) {
109
116
$httpBackend . expect ( 'POST' , '/CreditCard' , '{"name":"misko"}' ) . respond ( { id : 123 , name : 'misko' } ) ;
110
117
111
118
var cc = CreditCard . save ( { name : 'misko' } , callback ) ;
112
- nakedExpect ( cc ) . toEqual ( { name : 'misko' } ) ;
119
+ expect ( cc ) . toEqualData ( { name : 'misko' } ) ;
113
120
expect ( callback ) . not . toHaveBeenCalled ( ) ;
114
121
115
122
$httpBackend . flush ( ) ;
116
- nakedExpect ( cc ) . toEqual ( { id : 123 , name : 'misko' } ) ;
123
+ expect ( cc ) . toEqualData ( { id : 123 , name : 'misko' } ) ;
117
124
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
118
125
expect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( cc ) ;
119
126
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
120
- } ) ) ;
127
+ } ) ;
128
+
121
129
122
- it ( "should read resource" , inject ( function ( $httpBackend ) {
130
+ it ( "should read resource" , function ( ) {
123
131
$httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
124
132
var cc = CreditCard . get ( { id : 123 } , callback ) ;
125
133
126
134
expect ( cc instanceof CreditCard ) . toBeTruthy ( ) ;
127
- nakedExpect ( cc ) . toEqual ( { } ) ;
135
+ expect ( cc ) . toEqualData ( { } ) ;
128
136
expect ( callback ) . not . toHaveBeenCalled ( ) ;
129
137
130
138
$httpBackend . flush ( ) ;
131
- nakedExpect ( cc ) . toEqual ( { id : 123 , number : '9876' } ) ;
139
+ expect ( cc ) . toEqualData ( { id : 123 , number : '9876' } ) ;
132
140
expect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( cc ) ;
133
141
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
134
- } ) ) ;
142
+ } ) ;
135
143
136
- it ( "should read partial resource" , inject ( function ( $httpBackend ) {
144
+
145
+ it ( "should read partial resource" , function ( ) {
137
146
$httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id :{ key :123 } } ] ) ;
138
147
var ccs = CreditCard . query ( ) ;
139
148
@@ -150,49 +159,53 @@ describe("resource", function() {
150
159
expect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( cc ) ;
151
160
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
152
161
expect ( cc . number ) . toEqual ( '9876' ) ;
153
- } ) ) ;
162
+ } ) ;
154
163
155
- it ( "should update resource" , inject ( function ( $httpBackend ) {
164
+
165
+ it ( "should update resource" , function ( ) {
156
166
$httpBackend . expect ( 'POST' , '/CreditCard/123' , '{"id":{"key":123},"name":"misko"}' ) .
157
167
respond ( { id : { key : 123 } , name : 'rama' } ) ;
158
168
159
169
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' } ) ;
161
171
expect ( callback ) . not . toHaveBeenCalled ( ) ;
162
172
$httpBackend . flush ( ) ;
163
- } ) ) ;
173
+ } ) ;
174
+
164
175
165
- it ( "should query resource" , inject ( function ( $httpBackend ) {
176
+ it ( "should query resource" , function ( ) {
166
177
$httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
167
178
168
179
var ccs = CreditCard . query ( { key : 'value' } , callback ) ;
169
180
expect ( ccs ) . toEqual ( [ ] ) ;
170
181
expect ( callback ) . not . toHaveBeenCalled ( ) ;
171
182
172
183
$httpBackend . flush ( ) ;
173
- nakedExpect ( ccs ) . toEqual ( [ { id :1 } , { id :2 } ] ) ;
184
+ expect ( ccs ) . toEqualData ( [ { id :1 } , { id :2 } ] ) ;
174
185
expect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( ccs ) ;
175
186
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
176
- } ) ) ;
187
+ } ) ;
177
188
178
- it ( "should have all arguments optional" , inject ( function ( $httpBackend ) {
189
+
190
+ it ( "should have all arguments optional" , function ( ) {
179
191
$httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id :1 } ] ) ;
180
192
181
193
var log = '' ;
182
194
var ccs = CreditCard . query ( function ( ) { log += 'cb;' ; } ) ;
183
195
184
196
$httpBackend . flush ( ) ;
185
- nakedExpect ( ccs ) . toEqual ( [ { id :1 } ] ) ;
197
+ expect ( ccs ) . toEqualData ( [ { id :1 } ] ) ;
186
198
expect ( log ) . toEqual ( 'cb;' ) ;
187
- } ) ) ;
199
+ } ) ;
188
200
189
- it ( 'should delete resource and call callback' , inject ( function ( $httpBackend ) {
201
+
202
+ it ( 'should delete resource and call callback' , function ( ) {
190
203
$httpBackend . expect ( 'DELETE' , '/CreditCard/123' ) . respond ( { } ) ;
191
204
CreditCard . remove ( { id :123 } , callback ) ;
192
205
expect ( callback ) . not . toHaveBeenCalled ( ) ;
193
206
194
207
$httpBackend . flush ( ) ;
195
- nakedExpect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( { } ) ;
208
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toEqualData ( { } ) ;
196
209
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
197
210
198
211
callback . reset ( ) ;
@@ -201,24 +214,27 @@ describe("resource", function() {
201
214
expect ( callback ) . not . toHaveBeenCalled ( ) ;
202
215
203
216
$httpBackend . flush ( ) ;
204
- nakedExpect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( { } ) ;
217
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toEqualData ( { } ) ;
205
218
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { } ) ;
206
- } ) ) ;
219
+ } ) ;
220
+
207
221
208
- it ( 'should post charge verb' , inject ( function ( $httpBackend ) {
222
+ it ( 'should post charge verb' , function ( ) {
209
223
$httpBackend . expect ( 'POST' , '/CreditCard/123!charge?amount=10' , '{"auth":"abc"}' ) . respond ( { success : 'ok' } ) ;
210
224
CreditCard . charge ( { id :123 , amount :10 } , { auth :'abc' } , callback ) ;
211
- } ) ) ;
225
+ } ) ;
212
226
213
- it ( 'should post charge verb on instance' , inject ( function ( $httpBackend ) {
227
+
228
+ it ( 'should post charge verb on instance' , function ( ) {
214
229
$httpBackend . expect ( 'POST' , '/CreditCard/123!charge?amount=10' ,
215
230
'{"id":{"key":123},"name":"misko"}' ) . respond ( { success : 'ok' } ) ;
216
231
217
232
var card = new CreditCard ( { id :{ key :123 } , name :'misko' } ) ;
218
233
card . $charge ( { amount :10 } , callback ) ;
219
- } ) ) ;
234
+ } ) ;
220
235
221
- it ( 'should create on save' , inject ( function ( $httpBackend ) {
236
+
237
+ it ( 'should create on save' , function ( ) {
222
238
$httpBackend . expect ( 'POST' , '/CreditCard' , '{"name":"misko"}' ) . respond ( { id : 123 } , { header1 : 'a' } ) ;
223
239
224
240
var cc = new CreditCard ( ) ;
@@ -229,15 +245,16 @@ describe("resource", function() {
229
245
230
246
cc . name = 'misko' ;
231
247
cc . $save ( callback ) ;
232
- nakedExpect ( cc ) . toEqual ( { name :'misko' } ) ;
248
+ expect ( cc ) . toEqualData ( { name :'misko' } ) ;
233
249
234
250
$httpBackend . flush ( ) ;
235
- nakedExpect ( cc ) . toEqual ( { id :123 } ) ;
251
+ expect ( cc ) . toEqualData ( { id :123 } ) ;
236
252
expect ( callback . mostRecentCall . args [ 0 ] ) . toEqual ( cc ) ;
237
253
expect ( callback . mostRecentCall . args [ 1 ] ( ) ) . toEqual ( { header1 : 'a' } ) ;
238
- } ) ) ;
254
+ } ) ;
255
+
239
256
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 ( ) {
241
258
var data = { id :{ key :123 } , number :'9876' } ;
242
259
$httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( data ) ;
243
260
@@ -251,17 +268,19 @@ describe("resource", function() {
251
268
cc . $save ( ) ;
252
269
$httpBackend . flush ( ) ;
253
270
expect ( idBefore ) . toEqual ( cc . id ) ;
254
- } ) ) ;
271
+ } ) ;
255
272
256
- it ( 'should bind default parameters' , inject ( function ( $httpBackend ) {
273
+
274
+ it ( 'should bind default parameters' , function ( ) {
257
275
$httpBackend . expect ( 'GET' , '/CreditCard/123.visa?minimum=0.05' ) . respond ( { id : 123 } ) ;
258
276
var Visa = CreditCard . bind ( { verb :'.visa' , minimum :0.05 } ) ;
259
277
var visa = Visa . get ( { id :123 } ) ;
260
278
$httpBackend . flush ( ) ;
261
- nakedExpect ( visa ) . toEqual ( { id :123 } ) ;
262
- } ) ) ;
279
+ expect ( visa ) . toEqualData ( { id :123 } ) ;
280
+ } ) ;
263
281
264
- it ( 'should excersize full stack' , inject ( function ( $httpBackend , $resource ) {
282
+
283
+ it ( 'should excersize full stack' , inject ( function ( $resource ) {
265
284
var Person = $resource ( '/Person/:id' ) ;
266
285
267
286
$httpBackend . expect ( 'GET' , '/Person/123' ) . respond ( '\n{\n"name":\n"misko"\n}\n' ) ;
@@ -270,6 +289,7 @@ describe("resource", function() {
270
289
expect ( person . name ) . toEqual ( 'misko' ) ;
271
290
} ) ) ;
272
291
292
+
273
293
describe ( 'failure mode' , function ( ) {
274
294
var ERROR_CODE = 500 ,
275
295
ERROR_RESPONSE = 'Server Error' ,
@@ -282,23 +302,24 @@ describe("resource", function() {
282
302
} ) ;
283
303
} ) ;
284
304
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 ( ) {
287
307
$httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( ERROR_CODE , ERROR_RESPONSE ) ;
288
308
289
309
CreditCard . get ( { id :123 } , callback , errorCB ) ;
290
310
$httpBackend . flush ( ) ;
291
311
expect ( errorCB ) . toHaveBeenCalledOnce ( ) ;
292
312
expect ( callback ) . not . toHaveBeenCalled ( ) ;
293
- } ) ) ;
313
+ } ) ;
294
314
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 ( ) {
296
317
$httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( ERROR_CODE , ERROR_RESPONSE ) ;
297
318
298
319
CreditCard . get ( callback , errorCB ) ;
299
320
$httpBackend . flush ( ) ;
300
321
expect ( errorCB ) . toHaveBeenCalledOnce ( ) ;
301
322
expect ( callback ) . not . toHaveBeenCalled ( ) ;
302
- } ) ) ;
323
+ } ) ;
303
324
} ) ;
304
325
} ) ;
0 commit comments