angularjs-rails-resource 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,486 @@
1
+ describe("js.rails", function () {
2
+ 'use strict';
3
+
4
+ beforeEach(module('rails'));
5
+
6
+ describe('railsRootWrapping', function() {
7
+ var q, rootScope,
8
+ transformer, interceptor,
9
+ config = {rootName: 'test', rootPluralName: 'tests'};
10
+
11
+
12
+ function testTransform(wrappedData, unwrappedData) {
13
+ var result, resultPromise,
14
+ deferred = q.defer();
15
+
16
+ expect(transformer(unwrappedData, config)).toEqualData(wrappedData);
17
+ deferred.promise.resource = config;
18
+ expect(resultPromise = interceptor(deferred.promise)).toBeDefined();
19
+
20
+ resultPromise.then(function (response) {
21
+ result = response;
22
+ });
23
+
24
+ deferred.resolve({data: wrappedData});
25
+ rootScope.$digest(); // needed for $q to actually run callbacks
26
+ expect(result).toEqualData({data: unwrappedData});
27
+ }
28
+
29
+ beforeEach(inject(function ($rootScope, $q, railsRootWrappingTransformer, railsRootWrappingInterceptor) {
30
+ q = $q;
31
+ rootScope = $rootScope;
32
+ transformer = railsRootWrappingTransformer;
33
+ interceptor = railsRootWrappingInterceptor;
34
+ }));
35
+
36
+ it('should handle null root', function() {
37
+ testTransform({test: null}, null);
38
+ });
39
+
40
+ it('should transform arrays', function() {
41
+ testTransform({tests: [1, 2, 3]}, [1, 2, 3]);
42
+ });
43
+
44
+ it('should transform object', function() {
45
+ testTransform({test: {abc: 'xyz', def: 'abc'}}, {abc: 'xyz', def: 'abc'});
46
+ });
47
+ });
48
+
49
+ describe('railsFieldRenaming', function() {
50
+ var q, rootScope,
51
+ transformer, interceptor;
52
+
53
+ function testTransform(underscoreData, camelizeData) {
54
+ var result, resultPromise,
55
+ deferred = q.defer();
56
+
57
+ expect(transformer(angular.copy(camelizeData, {}))).toEqualData(underscoreData);
58
+ expect(resultPromise = interceptor(deferred.promise)).toBeDefined();
59
+
60
+ resultPromise.then(function (response) {
61
+ result = response;
62
+ });
63
+
64
+ deferred.resolve(angular.copy(underscoreData, {}));
65
+ rootScope.$digest(); // needed for $q to actually run callbacks
66
+ expect(result).toEqualData(camelizeData);
67
+ }
68
+
69
+ beforeEach(inject(function ($rootScope, $q, railsFieldRenamingTransformer, railsFieldRenamingInterceptor) {
70
+ q = $q;
71
+ rootScope = $rootScope;
72
+ transformer = railsFieldRenamingTransformer;
73
+ interceptor = railsFieldRenamingInterceptor;
74
+ }));
75
+
76
+ it('should ignore empty response', function() {
77
+ testTransform({}, {});
78
+ });
79
+
80
+ it('should ignore null response data', function() {
81
+ testTransform({data: null}, {data: null});
82
+ });
83
+
84
+ it('should leave non-data response fields untouched', function() {
85
+ testTransform({data: null, test_value: 'xyz'}, {data: null, test_value: 'xyz'});
86
+ });
87
+
88
+ it('should transform abc_def <-> abcDef', function() {
89
+ testTransform({data: {abc_def: 'xyz'}}, {data: {abcDef: 'xyz'}});
90
+ });
91
+
92
+ it('should transform abc_def, ghi_jkl <-> abcDef, ghiJkl', function() {
93
+ testTransform({data: {abc_def: 'xyz', ghi_jkl: 'abc'}}, {data: {abcDef: 'xyz', ghiJkl: 'abc'}});
94
+ });
95
+
96
+ it('should transform abc <-> abc', function() {
97
+ testTransform({data: {abc: 'xyz'}}, {data: {abc: 'xyz'}});
98
+ });
99
+
100
+ it('should transform _abc <-> _abc', function() {
101
+ testTransform({data: {_abc: 'xyz'}}, {data: {_abc: 'xyz'}});
102
+ });
103
+
104
+ it('should transform abc_ <-> abc_', function() {
105
+ testTransform({data: {abc_: 'xyz'}}, {data: {abc_: 'xyz'}});
106
+ });
107
+
108
+ it('should transform nested abc_def.abc_def <-> abcDef.abcDef', function() {
109
+ testTransform({data: {abc_def: {abc_def: 'xyz'}}}, {data: {abcDef: {abcDef: 'xyz'}}});
110
+ });
111
+
112
+ it('should transform nested abc_def.abc_def, abc_def.ghi_jkl <-> abcDef.abcDef, abcDef.ghiJkl', function() {
113
+ testTransform({data: {abc_def: {abc_def: 'xyz', ghi_jkl: 'abc'}}}, {data: {abcDef: {abcDef: 'xyz', ghiJkl: 'abc'}}});
114
+ });
115
+
116
+ it('should transform nested abc.abc_def <-> abc.abcDef', function() {
117
+ testTransform({data: {abc: {abc_def: 'xyz'}}}, {data: {abc: {abcDef: 'xyz'}}});
118
+ });
119
+
120
+ it('should handle empty root array', function() {
121
+ testTransform({data: []}, {data: []});
122
+ });
123
+
124
+ it('should camelize array of objects', function() {
125
+ testTransform({data: [{abc_def: 'xyz'}, {ghi_jkl: 'abc'}]}, {data: [{abcDef: 'xyz'}, {ghiJkl: 'abc'}]});
126
+ });
127
+
128
+ it('should handle array of strings', function() {
129
+ testTransform({data: ['abc', 'def']}, {data: ['abc', 'def']});
130
+ });
131
+
132
+ it('should handle array of numbers', function() {
133
+ testTransform({data: [1, 2, 3]}, {data: [1, 2, 3]});
134
+ });
135
+ });
136
+
137
+ describe('railsResourceFactory', function() {
138
+ var $httpBackend, $rootScope, factory, Test, PluralTest,
139
+ config = {
140
+ url: '/test',
141
+ name: 'test'
142
+ }, pluralConfig = {
143
+ url: '/pluralTest',
144
+ name: 'singular',
145
+ pluralName: 'plural'
146
+ };
147
+
148
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
149
+ $httpBackend = _$httpBackend_;
150
+ $rootScope = _$rootScope_;
151
+ factory = railsResourceFactory;
152
+ Test = railsResourceFactory(config);
153
+ PluralTest = railsResourceFactory(pluralConfig);
154
+ }));
155
+
156
+ afterEach(function() {
157
+ $httpBackend.verifyNoOutstandingExpectation();
158
+ $httpBackend.verifyNoOutstandingRequest();
159
+ });
160
+
161
+ it('query should return resource object when response is single object', inject(function($httpBackend) {
162
+ var promise, result;
163
+
164
+ $httpBackend.expectGET('/test').respond(200, {test: {abc: 'xyz'}});
165
+
166
+ expect(promise = Test.query()).toBeDefined();
167
+
168
+ promise.then(function (response) {
169
+ result = response;
170
+ });
171
+
172
+ $httpBackend.flush();
173
+
174
+ expect(result).toBeInstanceOf(Test);
175
+ expect(result).toEqualData({abc: 'xyz'});
176
+ }));
177
+
178
+ it('query should return no data on 204', inject(function($httpBackend) {
179
+ var promise, result;
180
+
181
+ $httpBackend.expectGET('/test').respond(204);
182
+ expect(promise = Test.query()).toBeDefined();
183
+
184
+ promise.then(function (response) {
185
+ result = response;
186
+ });
187
+
188
+ $httpBackend.flush();
189
+
190
+ expect(result).toBeUndefined();
191
+ }));
192
+
193
+ it('query should add parameter abc=1', inject(function($httpBackend) {
194
+ var promise;
195
+
196
+ $httpBackend.expectGET('/test?abc=1').respond(200, {test: {abc: 'xyz'}});
197
+
198
+ expect(promise = Test.query({abc: '1'})).toBeDefined();
199
+ $httpBackend.flush();
200
+ }));
201
+
202
+ it('query should add parameters abc=1 & xyz=2', inject(function($httpBackend) {
203
+ var promise;
204
+
205
+ $httpBackend.expectGET('/test?abc=1&xyz=2').respond(200, {test: {abc: 'xyz'}});
206
+
207
+ expect(promise = Test.query({abc: '1', xyz: 2})).toBeDefined();
208
+ $httpBackend.flush();
209
+ }));
210
+
211
+ it('query with default params should add parameter abc=1', inject(function($httpBackend) {
212
+ var promise, resource, defaultParamsConfig = {};
213
+
214
+ $httpBackend.expectGET('/test?abc=1').respond(200, {test: {abc: 'xyz'}});
215
+
216
+ angular.copy(config, defaultParamsConfig);
217
+ defaultParamsConfig.defaultParams = {abc: '1'};
218
+
219
+ resource = factory(defaultParamsConfig);
220
+ expect(promise = resource.query()).toBeDefined();
221
+
222
+ $httpBackend.flush();
223
+ }));
224
+
225
+ it('query should return array of resource objects when result is an array', inject(function($httpBackend) {
226
+ var promise, result;
227
+
228
+ $httpBackend.expectGET('/pluralTest').respond(200, {plural: [{abc: 'xyz'}, {xyz: 'abc'}]});
229
+
230
+ expect(promise = PluralTest.query()).toBeDefined();
231
+
232
+ promise.then(function (response) {
233
+ result = response;
234
+ });
235
+
236
+ $httpBackend.flush();
237
+
238
+ expect(angular.isArray(result)).toBe(true);
239
+ angular.forEach(result, function (value) {
240
+ expect(value).toBeInstanceOf(PluralTest);
241
+ });
242
+ expect(result[0]).toEqualData({abc: 'xyz'});
243
+ expect(result[1]).toEqualData({xyz: 'abc'});
244
+
245
+ }));
246
+
247
+ it('query should return empty array when result is empty array', inject(function($httpBackend) {
248
+ var promise, result;
249
+
250
+ $httpBackend.expectGET('/pluralTest').respond(200, {plural: []});
251
+
252
+ expect(promise = PluralTest.query()).toBeDefined();
253
+
254
+ promise.then(function (response) {
255
+ result = response;
256
+ });
257
+
258
+ $httpBackend.flush();
259
+
260
+ expect(angular.isArray(result)).toBe(true);
261
+ expect(result.length).toBe(0);
262
+ }));
263
+
264
+
265
+ it('get should return resource object when response is 200', inject(function($httpBackend) {
266
+ var promise, result;
267
+
268
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
269
+
270
+ expect(promise = Test.get(123)).toBeDefined();
271
+
272
+ promise.then(function (response) {
273
+ result = response;
274
+ });
275
+
276
+ $httpBackend.flush();
277
+
278
+ expect(result).toBeInstanceOf(Test);
279
+ expect(result).toEqualData({id: 123, abc: 'xyz'});
280
+ }));
281
+
282
+ it('get should call failure callback when 404', inject(function($httpBackend) {
283
+ var promise, success = false, failure = false;
284
+
285
+ $httpBackend.expectGET('/test/123').respond(404);
286
+
287
+ expect(promise = Test.get(123)).toBeDefined();
288
+
289
+ promise.then(function () {
290
+ success = true;
291
+ }, function () {
292
+ failure = true;
293
+ });
294
+
295
+ $httpBackend.flush();
296
+
297
+ expect(success).toBe(false);
298
+ expect(failure).toBe(true);
299
+ }));
300
+
301
+ it('get with default params should add parameter abc=1', inject(function($httpBackend) {
302
+ var promise, resource, defaultParamsConfig = {};
303
+
304
+ $httpBackend.expectGET('/test/123?abc=1').respond(200, {test: {abc: 'xyz'}});
305
+
306
+ angular.copy(config, defaultParamsConfig);
307
+ defaultParamsConfig.defaultParams = {abc: '1'};
308
+
309
+ resource = factory(defaultParamsConfig);
310
+ expect(promise = resource.get(123)).toBeDefined();
311
+
312
+ $httpBackend.flush();
313
+ }));
314
+
315
+ it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) {
316
+ var promise, result, resource;
317
+
318
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
319
+
320
+ resource = factory(config);
321
+ resource.responseInterceptors = [];
322
+ resource.requestTransformers = [];
323
+ expect(promise = resource.get(123)).toBeDefined();
324
+
325
+ promise.then(function (response) {
326
+ result = response;
327
+ });
328
+
329
+ $httpBackend.flush();
330
+
331
+ expect(result).toBeInstanceOf(resource);
332
+ expect(result).toEqualData({id: 123, abc_def: 'xyz'});
333
+ }));
334
+
335
+ it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) {
336
+ var promise, result, resource, testConfig = {};
337
+
338
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
339
+
340
+ angular.copy(config, testConfig);
341
+ testConfig.requestTransformers = [];
342
+ testConfig.responseInterceptors = ['railsFieldRenamingInterceptor'];
343
+ resource = factory(config);
344
+
345
+ expect(promise = resource.get(123)).toBeDefined();
346
+
347
+ promise.then(function (response) {
348
+ result = response;
349
+ });
350
+
351
+ $httpBackend.flush();
352
+
353
+ expect(result).toBeInstanceOf(resource);
354
+ expect(result).toEqualData({id: 123, abcDef: 'xyz'});
355
+ }));
356
+
357
+ it('should be able to create new instance and save it', inject(function($httpBackend) {
358
+ var data = new Test({abcDef: 'xyz'});
359
+
360
+ $httpBackend.expectPOST('/test').respond(200, {test: {id: 123, abc_def: 'xyz'}});
361
+ data.create();
362
+ $httpBackend.flush();
363
+
364
+ expect(data).toEqualData({id: 123, abcDef: 'xyz'});
365
+ }));
366
+
367
+ it('should be able to create new instance and update it', inject(function($httpBackend) {
368
+ var data = new Test({abcDef: 'xyz'});
369
+
370
+ $httpBackend.expectPOST('/test').respond(200, {test: {id: 123, abc_def: 'xyz'}});
371
+ data.create();
372
+ $httpBackend.flush(1);
373
+
374
+ expect(data).toEqualData({id: 123, abcDef: 'xyz'});
375
+
376
+ $httpBackend.expectPUT('/test/123').respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
377
+ data.xyz = 'abc';
378
+ data.update();
379
+ $httpBackend.flush();
380
+
381
+ expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
382
+ }));
383
+
384
+ it('create with default params should add parameter abc=1', inject(function($httpBackend) {
385
+ var promise, Resource, data, defaultParamsConfig = {};
386
+
387
+ $httpBackend.expectPOST('/test?abc=1').respond(200, {test: {abc: 'xyz'}});
388
+
389
+ angular.copy(config, defaultParamsConfig);
390
+ defaultParamsConfig.defaultParams = {abc: '1'};
391
+
392
+ Resource = factory(defaultParamsConfig);
393
+ data = new Resource();
394
+ data.create();
395
+
396
+ $httpBackend.flush();
397
+ }));
398
+
399
+ it('should be able to get resource and update it', inject(function($httpBackend) {
400
+ var promise, result;
401
+
402
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
403
+
404
+ expect(promise = Test.get(123)).toBeDefined();
405
+
406
+ promise.then(function (response) {
407
+ result = response;
408
+ });
409
+
410
+ $httpBackend.flush();
411
+
412
+ expect(result).toBeInstanceOf(Test);
413
+ expect(result).toEqualData({id: 123, abc: 'xyz'});
414
+
415
+ $httpBackend.expectPUT('/test/123').respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
416
+ result.xyz = 'abc';
417
+ result.update();
418
+ $httpBackend.flush();
419
+
420
+ // abc was originally set on the object so it should still be there after the update
421
+ expect(result).toEqualData({id: 123, abc: 'xyz', abcDef: 'xyz', xyz: 'abc', extra: 'test'});
422
+ }));
423
+
424
+ it('update should handle 204 response', inject(function($httpBackend) {
425
+ var promise, result;
426
+
427
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
428
+
429
+ expect(promise = Test.get(123)).toBeDefined();
430
+
431
+ promise.then(function (response) {
432
+ result = response;
433
+ });
434
+
435
+ $httpBackend.flush();
436
+
437
+ expect(result).toBeInstanceOf(Test);
438
+ expect(result).toEqualData({id: 123, abc: 'xyz'});
439
+
440
+ $httpBackend.expectPUT('/test/123').respond(204);
441
+ result.xyz = 'abc';
442
+ result.update();
443
+ $httpBackend.flush();
444
+
445
+ expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc'});
446
+ }));
447
+
448
+ it('should be able to delete instance returned from get', inject(function($httpBackend) {
449
+ var promise, result;
450
+
451
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
452
+
453
+ expect(promise = Test.get(123)).toBeDefined();
454
+
455
+ promise.then(function (response) {
456
+ result = response;
457
+ });
458
+
459
+ $httpBackend.flush();
460
+
461
+ expect(result).toBeInstanceOf(Test);
462
+ expect(result).toEqualData({id: 123, abc: 'xyz'});
463
+
464
+ $httpBackend.expectDELETE('/test/123').respond(204);
465
+ result.remove();
466
+ $httpBackend.flush();
467
+ }));
468
+
469
+ it('delete with default params should add parameter abc=1', inject(function($httpBackend) {
470
+ var promise, Resource, data, defaultParamsConfig = {};
471
+
472
+ $httpBackend.expectDELETE('/test/123?abc=1').respond(204);
473
+
474
+ angular.copy(config, defaultParamsConfig);
475
+ defaultParamsConfig.defaultParams = {abc: '1'};
476
+
477
+ Resource = factory(defaultParamsConfig);
478
+ data = new Resource();
479
+ data.id = 123;
480
+ data.remove();
481
+
482
+ $httpBackend.flush();
483
+ }));
484
+ });
485
+
486
+ });