angularjs-rails-resource 1.0.0.pre.2 → 1.0.0.pre.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,4 +40,24 @@ describe('resource provider factory config', function () {
40
40
  expect(railsResourceFactory({name: 'test', url: '/test'}).config.defaultParams).toEqualData({'test': '1'});
41
41
  });
42
42
  });
43
+
44
+ it('should allow setting default extensions globally', function () {
45
+ module('rails', function (RailsResourceProvider) {
46
+ expect(RailsResourceProvider.extensions('snapshots')).toBe(RailsResourceProvider);
47
+ });
48
+
49
+ inject(function (railsResourceFactory) {
50
+ expect(railsResourceFactory({name: 'test', url: '/test'}).prototype.snapshot).toBeDefined();
51
+ });
52
+ });
53
+
54
+ it('should allow setting default extensions using an array', function () {
55
+ module('rails', function (RailsResourceProvider) {
56
+ expect(RailsResourceProvider.extensions(['snapshots'])).toBe(RailsResourceProvider);
57
+ });
58
+
59
+ inject(function (railsResourceFactory) {
60
+ expect(railsResourceFactory({name: 'test', url: '/test'}).prototype.snapshot).toBeDefined();
61
+ });
62
+ });
43
63
  });
@@ -34,7 +34,7 @@ describe('railsResourceFactory', function () {
34
34
  $httpBackend.verifyNoOutstandingRequest();
35
35
  });
36
36
 
37
- it('query should return resource object when response is single object', inject(function($httpBackend) {
37
+ it('query should return resource object when response is single object', function () {
38
38
  var promise, result;
39
39
 
40
40
  $httpBackend.expectGET('/test').respond(200, {test: {abc: 'xyz'}});
@@ -49,9 +49,9 @@ describe('railsResourceFactory', function () {
49
49
 
50
50
  expect(result).toBeInstanceOf(Test);
51
51
  expect(result).toEqualData({abc: 'xyz'});
52
- }));
52
+ });
53
53
 
54
- it('query should return no data on 204', inject(function($httpBackend) {
54
+ it('query should return no data on 204', function () {
55
55
  var promise, result;
56
56
 
57
57
  $httpBackend.expectGET('/test').respond(204);
@@ -64,27 +64,56 @@ describe('railsResourceFactory', function () {
64
64
  $httpBackend.flush();
65
65
 
66
66
  expect(result).toBeUndefined();
67
- }));
67
+ });
68
68
 
69
- it('query should add parameter abc=1', inject(function($httpBackend) {
69
+ it('query should add parameter abc=1', function () {
70
70
  var promise;
71
71
 
72
72
  $httpBackend.expectGET('/test?abc=1').respond(200, {test: {abc: 'xyz'}});
73
73
 
74
74
  expect(promise = Test.query({abc: '1'})).toBeDefined();
75
75
  $httpBackend.flush();
76
- }));
76
+ });
77
77
 
78
- it('query should add parameters abc=1 & xyz=2', inject(function($httpBackend) {
78
+ it('query should add parameters abc=1 & xyz=2', function () {
79
79
  var promise;
80
80
 
81
81
  $httpBackend.expectGET('/test?abc=1&xyz=2').respond(200, {test: {abc: 'xyz'}});
82
82
 
83
83
  expect(promise = Test.query({abc: '1', xyz: 2})).toBeDefined();
84
84
  $httpBackend.flush();
85
- }));
85
+ });
86
+
87
+ it('query should underscore parameters abc_xyz=1 & test=2', function () {
88
+ var promise;
89
+
90
+ $httpBackend.expectGET('/test?abc_xyz=1&test=2').respond(200, {test: {abc: 'xyz'}});
91
+
92
+ expect(promise = Test.query({abcXyz: '1', test: 2})).toBeDefined();
93
+ $httpBackend.flush();
94
+ });
95
+
96
+ it('query should not underscore parameters abcXyz=1 & test=2', function () {
97
+ var promise;
86
98
 
87
- it('query with default params should add parameter abc=1', inject(function($httpBackend) {
99
+ $httpBackend.expectGET('/test?abcXyz=1&test=2').respond(200, {test: {abc: 'xyz'}});
100
+ Test = factory(angular.extend({underscoreParams: false}, config));
101
+ expect(promise = Test.query({abcXyz: '1', test: 2})).toBeDefined();
102
+ $httpBackend.flush();
103
+ });
104
+
105
+ it('Parameter underscoring should not modify the defaultParams.', function () {
106
+ var promise;
107
+
108
+ $httpBackend.expectGET('/test?abc_xyz=1').respond(200, {test: {abc: 'xyz'}});
109
+ Test = factory(angular.extend({defaultParams: {abcXyz: 1}}, config));
110
+ expect(promise = Test.query()).toBeDefined();
111
+ $httpBackend.flush();
112
+ expect(Test.config.defaultParams.abcXyz).toBeDefined();
113
+ expect(Test.config.defaultParams.abc_xyz).not.toBeDefined();
114
+ });
115
+
116
+ it('query with default params should add parameter abc=1', function () {
88
117
  var promise, resource, defaultParamsConfig = {};
89
118
 
90
119
  $httpBackend.expectGET('/test?abc=1').respond(200, {test: {abc: 'xyz'}});
@@ -96,9 +125,24 @@ describe('railsResourceFactory', function () {
96
125
  expect(promise = resource.query()).toBeDefined();
97
126
 
98
127
  $httpBackend.flush();
99
- }));
128
+ });
129
+
130
+ it('query with default params and additional parameters should not modify default params', function () {
131
+ var promise, resource, defaultParamsConfig = {};
132
+
133
+ $httpBackend.expectGET('/test?abc=1&xyz=2').respond(200, {test: {abc: 'xyz'}});
134
+
135
+ angular.copy(config, defaultParamsConfig);
136
+ defaultParamsConfig.defaultParams = {abc: '1'};
137
+
138
+ resource = factory(defaultParamsConfig);
139
+ expect(promise = resource.query({xyz: '2'})).toBeDefined();
140
+
141
+ $httpBackend.flush();
142
+ expect(resource.config.defaultParams).toEqualData({abc: '1'});
143
+ });
100
144
 
101
- it('get should return resource object when response is 200', inject(function($httpBackend) {
145
+ it('get should return resource object when response is 200', function () {
102
146
  var promise, result;
103
147
 
104
148
  $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
@@ -113,9 +157,9 @@ describe('railsResourceFactory', function () {
113
157
 
114
158
  expect(result).toBeInstanceOf(Test);
115
159
  expect(result).toEqualData({id: 123, abc: 'xyz'});
116
- }));
160
+ });
117
161
 
118
- it('get should work with id as string as well', inject(function($httpBackend) {
162
+ it('get should work with id as string as well', function () {
119
163
  var promise, result;
120
164
 
121
165
  $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
@@ -130,9 +174,9 @@ describe('railsResourceFactory', function () {
130
174
 
131
175
  expect(result).toBeInstanceOf(Test);
132
176
  expect(result).toEqualData({id: 123, abc: 'xyz'});
133
- }));
177
+ });
134
178
 
135
- it('get should call failure callback when 404', inject(function($httpBackend) {
179
+ it('get should call failure callback when 404', function () {
136
180
  var promise, success = false, failure = false;
137
181
 
138
182
  $httpBackend.expectGET('/test/123').respond(404);
@@ -149,9 +193,9 @@ describe('railsResourceFactory', function () {
149
193
 
150
194
  expect(success).toBe(false);
151
195
  expect(failure).toBe(true);
152
- }));
196
+ });
153
197
 
154
- it('get with default params should add parameter abc=1', inject(function($httpBackend) {
198
+ it('get with default params should add parameter abc=1', function () {
155
199
  var promise, resource, defaultParamsConfig = {};
156
200
 
157
201
  $httpBackend.expectGET('/test/123?abc=1').respond(200, {test: {abc: 'xyz'}});
@@ -163,9 +207,9 @@ describe('railsResourceFactory', function () {
163
207
  expect(promise = resource.get(123)).toBeDefined();
164
208
 
165
209
  $httpBackend.flush();
166
- }));
210
+ });
167
211
 
168
- it('should be able to create new instance and save it', inject(function($httpBackend) {
212
+ it('should be able to create new instance and save it', function () {
169
213
  var data = new Test({abcDef: 'xyz'});
170
214
 
171
215
  $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}});
@@ -173,18 +217,18 @@ describe('railsResourceFactory', function () {
173
217
  $httpBackend.flush();
174
218
 
175
219
  expect(data).toEqualData({id: 123, abcDef: 'xyz'});
176
- }));
220
+ });
177
221
 
178
- it("should return a promise when calling save", inject(function($httpBackend) {
222
+ it("should return a promise when calling save", function () {
179
223
  var promise, data;
180
224
 
181
225
  data = new Test({abc_def: 'xyz'});
182
226
  $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}});
183
227
  expect(promise = data.save()).toBeDefined();
184
228
  $httpBackend.flush()
185
- }));
229
+ });
186
230
 
187
- it('should be able to create new instance and save it using save', inject(function($httpBackend) {
231
+ it('should be able to create new instance and save it using save', function () {
188
232
  var data = new Test({abcDef: 'xyz'});
189
233
 
190
234
  $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}});
@@ -192,9 +236,9 @@ describe('railsResourceFactory', function () {
192
236
  $httpBackend.flush();
193
237
 
194
238
  expect(data).toEqualData({id: 123, abcDef: 'xyz'});
195
- }));
239
+ });
196
240
 
197
- it('should be able to create new instance and update it', inject(function($httpBackend) {
241
+ it('should be able to create new instance and update it', function () {
198
242
  var data = new Test({abcDef: 'xyz'});
199
243
 
200
244
  $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}});
@@ -209,9 +253,9 @@ describe('railsResourceFactory', function () {
209
253
  $httpBackend.flush();
210
254
 
211
255
  expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
212
- }));
256
+ });
213
257
 
214
- it('should be able to create new instance and update it using save', inject(function($httpBackend) {
258
+ it('should be able to create new instance and update it using save', function () {
215
259
  var data = new Test({abcDef: 'xyz'});
216
260
 
217
261
  $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}});
@@ -226,9 +270,9 @@ describe('railsResourceFactory', function () {
226
270
  $httpBackend.flush();
227
271
 
228
272
  expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
229
- }));
273
+ });
230
274
 
231
- it('should be able to create new instance and update it using PATCH', inject(function($httpBackend) {
275
+ it('should be able to create new instance and update it using PATCH', function () {
232
276
  var promise, Resource, data, defaultParamsConfig = {};
233
277
 
234
278
  angular.copy(config, defaultParamsConfig);
@@ -249,9 +293,9 @@ describe('railsResourceFactory', function () {
249
293
  $httpBackend.flush();
250
294
 
251
295
  expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
252
- }));
296
+ });
253
297
 
254
- it('should be able to create new instance and update it using save using PATCH', inject(function($httpBackend) {
298
+ it('should be able to create new instance and update it using save using PATCH', function () {
255
299
  var promise, Resource, data, defaultParamsConfig = {};
256
300
 
257
301
  angular.copy(config, defaultParamsConfig);
@@ -272,10 +316,10 @@ describe('railsResourceFactory', function () {
272
316
  $httpBackend.flush();
273
317
 
274
318
  expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
275
- }));
319
+ });
276
320
 
277
321
 
278
- it('create with default params should add parameter abc=1', inject(function($httpBackend) {
322
+ it('create with default params should add parameter abc=1', function () {
279
323
  var promise, Resource, data, defaultParamsConfig = {};
280
324
 
281
325
  $httpBackend.expectPOST('/test?abc=1', {test: {}}).respond(200, {test: {abc: 'xyz'}});
@@ -288,9 +332,9 @@ describe('railsResourceFactory', function () {
288
332
  data.create();
289
333
 
290
334
  $httpBackend.flush();
291
- }));
335
+ });
292
336
 
293
- it('should be able to get resource and update it', inject(function($httpBackend) {
337
+ it('should be able to get resource and update it', function () {
294
338
  var promise, result;
295
339
 
296
340
  $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abcd'}});
@@ -313,9 +357,9 @@ describe('railsResourceFactory', function () {
313
357
 
314
358
  // abc was originally set on the object so it should still be there after the update
315
359
  expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
316
- }));
360
+ });
317
361
 
318
- it('update should handle 204 response', inject(function($httpBackend) {
362
+ it('update should handle 204 response', function () {
319
363
  var promise, result;
320
364
 
321
365
  $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
@@ -337,9 +381,9 @@ describe('railsResourceFactory', function () {
337
381
  $httpBackend.flush();
338
382
 
339
383
  expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc'});
340
- }));
384
+ });
341
385
 
342
- it('should be able to delete instance returned from get', inject(function($httpBackend) {
386
+ it('should be able to delete instance returned from get', function () {
343
387
  var promise, result;
344
388
 
345
389
  $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
@@ -358,9 +402,9 @@ describe('railsResourceFactory', function () {
358
402
  $httpBackend.expectDELETE('/test/123').respond(204);
359
403
  result.remove();
360
404
  $httpBackend.flush();
361
- }));
405
+ });
362
406
 
363
- it('delete with default params should add parameter abc=1', inject(function($httpBackend) {
407
+ it('delete with default params should add parameter abc=1', function () {
364
408
  var promise, Resource, data, defaultParamsConfig = {};
365
409
 
366
410
  $httpBackend.expectDELETE('/test/123?abc=1').respond(204);
@@ -374,7 +418,7 @@ describe('railsResourceFactory', function () {
374
418
  data.remove();
375
419
 
376
420
  $httpBackend.flush();
377
- }));
421
+ });
378
422
 
379
423
  it('should transform attributes on build', function() {
380
424
  var test = new Test({id: 123, abc_def: "T"});
@@ -400,7 +444,7 @@ describe('railsResourceFactory', function () {
400
444
  });
401
445
 
402
446
  angular.forEach(['post', 'put', 'patch'], function (method) {
403
- it('should be able to ' + method + ' to arbitrary url', inject(function($httpBackend) {
447
+ it('should be able to ' + method + ' to arbitrary url', function () {
404
448
  var promise, result = {};
405
449
 
406
450
  promise = Test['$' + method]('/xyz', {id: 123, abc: 'xyz', xyz: 'abc'});
@@ -414,9 +458,9 @@ describe('railsResourceFactory', function () {
414
458
 
415
459
  // abc was originally set on the object so it should still be there after the update
416
460
  expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
417
- }));
461
+ });
418
462
 
419
- it('should be able to ' + method + ' instance to arbitrary url', inject(function($httpBackend) {
463
+ it('should be able to ' + method + ' instance to arbitrary url', function () {
420
464
  var test = new Test({id: 123, abc: 'xyz', xyz: 'abc'});
421
465
  $httpBackend['expect' + angular.uppercase(method)]('/xyz', {test: {id: 123, abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}});
422
466
  test['$' + method]('/xyz');
@@ -424,7 +468,7 @@ describe('railsResourceFactory', function () {
424
468
 
425
469
  // abc was originally set on the object so it should still be there after the update
426
470
  expect(test).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
427
- }));
471
+ });
428
472
  });
429
473
 
430
474
  it('should be able to $post an array of resources', function () {
@@ -456,7 +500,7 @@ describe('railsResourceFactory', function () {
456
500
  $httpBackend.verifyNoOutstandingRequest();
457
501
  });
458
502
 
459
- it('query should return array of resource objects when result is an array', inject(function($httpBackend) {
503
+ it('query should return array of resource objects when result is an array', function () {
460
504
  var promise, result;
461
505
 
462
506
  $httpBackend.expectGET('/pluralTest').respond(200, {plural: [{abc: 'xyz'}, {xyz: 'abc'}]});
@@ -476,9 +520,9 @@ describe('railsResourceFactory', function () {
476
520
  expect(result[0]).toEqualData({abc: 'xyz'});
477
521
  expect(result[1]).toEqualData({xyz: 'abc'});
478
522
 
479
- }));
523
+ });
480
524
 
481
- it('query should return empty array when result is empty array', inject(function($httpBackend) {
525
+ it('query should return empty array when result is empty array', function () {
482
526
  var promise, result;
483
527
 
484
528
  $httpBackend.expectGET('/pluralTest').respond(200, {plural: []});
@@ -493,7 +537,7 @@ describe('railsResourceFactory', function () {
493
537
 
494
538
  expect(angular.isArray(result)).toBe(true);
495
539
  expect(result.length).toBe(0);
496
- }));
540
+ });
497
541
  });
498
542
 
499
543
  describe('subclassing', function() {
@@ -513,6 +557,8 @@ describe('railsResourceFactory', function () {
513
557
 
514
558
  // @configure url: '/books', name: 'book'
515
559
  Book.configure({ url: '/books', name: 'book' });
560
+ Book.extend('RailsResourceSnapshotsMixin');
561
+ Book.extend({ bookProperty: 1});
516
562
 
517
563
  function Book() {
518
564
  Book.__super__.constructor.apply(this, arguments);
@@ -582,5 +628,82 @@ describe('railsResourceFactory', function () {
582
628
  expect(carManual.id).toBe(1);
583
629
  expect(carManual.name).toBe('Honda CR-V');
584
630
  });
631
+
632
+ it('should have included properties on subclass', function () {
633
+ expect(CarManual.bookProperty).toBe(1);
634
+ expect(CarManual.prototype.snapshot).toBeDefined();
635
+ });
636
+ });
637
+
638
+ describe('mixins', function () {
639
+ var railsResourceFactory;
640
+
641
+ beforeEach(inject(function (_railsResourceFactory_) {
642
+ railsResourceFactory = _railsResourceFactory_;
643
+ }));
644
+
645
+ it('should include extensions as part of initial configure', function () {
646
+ var Resource = railsResourceFactory({name: 'test', url: '/test', extensions: ['snapshots']});
647
+ expect(Resource.prototype.snapshot).toBeDefined();
648
+ });
649
+
650
+ it('should only include extensions once', function () {
651
+ var Resource = railsResourceFactory({name: 'test', url: '/test', extensions: ['snapshots', 'snapshots']});
652
+ expect(Resource.$mixins.length).toBe(2); // Snapshots extension includes additional mixin
653
+ expect(Resource.prototype.snapshot).toBeDefined();
654
+ });
655
+
656
+ it('should only include module once', function () {
657
+ var mixin = {test: 1}, Resource = railsResourceFactory({name: 'test', url: '/test'});
658
+ Resource.include(mixin);
659
+ Resource.include(mixin);
660
+ expect(Resource.$mixins.length).toBe(1);
661
+ });
662
+
663
+ it('should include extensions as part of second configure call', function () {
664
+ var Resource = railsResourceFactory({name: 'test', url: '/test'});
665
+ Resource.configure({extensions: ['snapshots']});
666
+ expect(Resource.prototype.snapshot).toBeDefined();
667
+ });
668
+
669
+ it('should throw an error if extension is not valid', function () {
670
+ var Resource = railsResourceFactory({name: 'test', url: '/test'});
671
+ expect(function () {
672
+ Resource.configure({extensions: ['invalid']});
673
+ }).toThrow();
674
+ });
675
+
676
+ it('should include object properties as class properties', function () {
677
+ var Resource = railsResourceFactory({name: 'test', url: '/test'});
678
+ Resource.extend({
679
+ classMethod: function () {},
680
+ classProperty: 1
681
+ });
682
+ expect(Resource.classMethod).toBeDefined();
683
+ expect(Resource.classProperty).toBe(1);
684
+ });
685
+
686
+ it('should include class properties', function () {
687
+ function Mixin() {}
688
+ Mixin.classMethod = function () {};
689
+ Mixin.classProperty = 1;
690
+
691
+ var Resource = railsResourceFactory({name: 'test', url: '/test'});
692
+ Resource.extend(Mixin);
693
+ expect(Resource.classMethod).toBe(Mixin.classMethod);
694
+ expect(Resource.classProperty).toBe(Mixin.classProperty);
695
+ });
696
+
697
+ it('should include instance properties', function () {
698
+ function Mixin() {}
699
+ Mixin.instanceMethod = function () {};
700
+ Mixin.instanceProperty = 1;
701
+
702
+ var Resource = railsResourceFactory({name: 'test', url: '/test'});
703
+ Resource.include(Mixin);
704
+ expect(Resource.prototype.instanceMethod).toBe(Mixin.instanceMethod);
705
+ expect(Resource.prototype.instanceProperty).toBe(Mixin.instanceProperty);
706
+ });
707
+
585
708
  });
586
709
  });