angularjs-rails-resource 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "angularjs-rails-resource",
3
- "description" : "A resource factory inspired by $resource from AngularJS",
4
- "version": "2.2.2",
5
- "main" : "angularjs-rails-resource.min.js",
6
- "homepage" : "https://github.com/FineLinePrototyping/angularjs-rails-resource.git",
7
- "author" : "",
3
+ "description": "A resource factory inspired by $resource from AngularJS",
4
+ "version": "2.3.0",
5
+ "main": "angularjs-rails-resource.min.js",
6
+ "homepage": "https://github.com/FineLinePrototyping/angularjs-rails-resource.git",
7
+ "author": "",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/FineLinePrototyping/angularjs-rails-resource.git"
@@ -14,29 +14,31 @@
14
14
  "resources"
15
15
  ],
16
16
  "dependencies": {
17
- "angular": "^1.0"
17
+ "angular": "^1.3"
18
18
  },
19
19
  "devDependencies": {
20
+ "angular-mocks": "^1.3",
20
21
  "grunt": "~0.4.1",
21
- "grunt-contrib-jshint": "~0.6.0",
22
- "grunt-contrib-uglify": "~0.2.2",
22
+ "grunt-bump": "~0.0.13",
23
+ "grunt-contrib-clean": "~0.5.0",
24
+ "grunt-contrib-compress": "~0.5.2",
23
25
  "grunt-contrib-concat": "~0.3.0",
24
26
  "grunt-contrib-copy": "~0.4.1",
25
- "grunt-contrib-clean": "~0.5.0",
27
+ "grunt-contrib-jshint": "~0.6.0",
28
+ "grunt-contrib-uglify": "~0.2.2",
26
29
  "grunt-contrib-watch": "~0.5.1",
27
- "grunt-contrib-compress": "~0.5.2",
28
- "grunt-bump": "~0.0.13",
29
- "karma": "~0.10",
30
+ "karma": "~2.0",
31
+ "karma-chrome-launcher": "~2.2",
30
32
  "karma-jasmine": "~0.1",
31
- "karma-chrome-launcher": "~0.1",
32
- "karma-phantomjs-launcher": "~0.1",
33
- "karma-junit-reporter": "~0.1",
33
+ "karma-junit-reporter": "~1.2",
34
+ "karma-phantomjs-launcher": "~1.0",
35
+ "phantomjs-prebuilt": ">=2",
34
36
  "q": "~0.9.2",
35
37
  "q-io": "~1.10.6",
36
38
  "qq": "~0.3.5"
37
39
  },
38
40
  "scripts": {
39
- "test": "./node_modules/.bin/karma start --single-run --browsers PhantomJS"
41
+ "test": "PHANTOMJS_BIN=./node_modules/.bin/phantomjs ./node_modules/.bin/karma start --single-run --browsers PhantomJS"
40
42
  },
41
43
  "licenses": [
42
44
  {
@@ -44,4 +46,4 @@
44
46
  "url": "https://github.com/FineLinePrototyping/angularjs-rails-resource.js/blob/master/LICENSE"
45
47
  }
46
48
  ]
47
- }
49
+ }
@@ -27,12 +27,12 @@ describe('RailsResource.snapshots', function () {
27
27
  book = new Book(data);
28
28
  expect(book.snapshot()).toBe(0);
29
29
 
30
- expect(book.$snapshots).toBeDefined();
31
- expect(book.$snapshots.length).toBe(1);
32
- expect(book.$snapshots[0].$snapshots).toBeUndefined();
30
+ expect(book.$$snapshots).toBeDefined();
31
+ expect(book.$$snapshots.length).toBe(1);
32
+ expect(book.$$snapshots[0].$$snapshots).toBeUndefined();
33
33
  expect(book).toEqualData(data);
34
- expect(book.$snapshots[0].$key).toBe('1234');
35
- expect(book.$snapshots[0]).toEqualData(data);
34
+ expect(book.$$snapshots[0].$key).toBe('1234');
35
+ expect(book.$$snapshots[0]).toEqualData(data);
36
36
  });
37
37
 
38
38
  it('should store deep copy', function () {
@@ -49,9 +49,9 @@ describe('RailsResource.snapshots', function () {
49
49
  book = new Book(data);
50
50
  book.snapshot();
51
51
 
52
- expect(book.$snapshots[0].author).toBeDefined();
53
- expect(book.$snapshots[0].author.id).toBe(1);
54
- expect(book.$snapshots[0]).toEqualData(data);
52
+ expect(book.$$snapshots[0].author).toBeDefined();
53
+ expect(book.$$snapshots[0].author.id).toBe(1);
54
+ expect(book.$$snapshots[0]).toEqualData(data);
55
55
  });
56
56
 
57
57
  it('should store multiple snapshots', function () {
@@ -63,11 +63,11 @@ describe('RailsResource.snapshots', function () {
63
63
  book.$key = '1236';
64
64
  expect(book.snapshot()).toBe(2);
65
65
 
66
- expect(book.$snapshots).toBeDefined();
67
- expect(book.$snapshots.length).toBe(3);
68
- expect(book.$snapshots[0].$key).toBe('1234');
69
- expect(book.$snapshots[1].$key).toBe('1235');
70
- expect(book.$snapshots[2].$key).toBe('1236');
66
+ expect(book.$$snapshots).toBeDefined();
67
+ expect(book.$$snapshots.length).toBe(3);
68
+ expect(book.$$snapshots[0].$key).toBe('1234');
69
+ expect(book.$$snapshots[1].$key).toBe('1235');
70
+ expect(book.$$snapshots[2].$key).toBe('1236');
71
71
  });
72
72
 
73
73
  it('should rollback single version', function () {
@@ -80,8 +80,8 @@ describe('RailsResource.snapshots', function () {
80
80
  book.rollback();
81
81
 
82
82
  expect(book.$key).toBe('1235');
83
- expect(book.$snapshots).toBeDefined();
84
- expect(book.$snapshots.length).toBe(1);
83
+ expect(book.$$snapshots).toBeDefined();
84
+ expect(book.$$snapshots.length).toBe(1);
85
85
  });
86
86
 
87
87
  it('should rollback deep copy', function () {
@@ -142,13 +142,13 @@ describe('RailsResource.snapshots', function () {
142
142
  book.rollback();
143
143
 
144
144
  expect(book.$key).toBe('1235');
145
- expect(book.$snapshots).toBeDefined();
146
- expect(book.$snapshots.length).toBe(1);
145
+ expect(book.$$snapshots).toBeDefined();
146
+ expect(book.$$snapshots.length).toBe(1);
147
147
  book.rollback();
148
148
 
149
149
  expect(book.$key).toBe('1234');
150
- expect(book.$snapshots).toBeDefined();
151
- expect(book.$snapshots.length).toBe(0);
150
+ expect(book.$$snapshots).toBeDefined();
151
+ expect(book.$$snapshots.length).toBe(0);
152
152
  });
153
153
 
154
154
 
@@ -164,8 +164,8 @@ describe('RailsResource.snapshots', function () {
164
164
  book.rollback(2);
165
165
 
166
166
  expect(book.$key).toBe('1235');
167
- expect(book.$snapshots).toBeDefined();
168
- expect(book.$snapshots.length).toBe(1);
167
+ expect(book.$$snapshots).toBeDefined();
168
+ expect(book.$$snapshots.length).toBe(1);
169
169
  });
170
170
 
171
171
  it('should not change resource on rollback if no snapshots saved', function () {
@@ -188,8 +188,8 @@ describe('RailsResource.snapshots', function () {
188
188
  book.rollback(-1);
189
189
 
190
190
  expect(book.$key).toBe('1234');
191
- expect(book.$snapshots).toBeDefined();
192
- expect(book.$snapshots.length).toBe(0);
191
+ expect(book.$$snapshots).toBeDefined();
192
+ expect(book.$$snapshots.length).toBe(0);
193
193
  });
194
194
 
195
195
  it('should roll back to the first snapshot when versions exceeds available snapshots', function () {
@@ -203,8 +203,8 @@ describe('RailsResource.snapshots', function () {
203
203
  book.rollback(1000);
204
204
 
205
205
  expect(book.$key).toBe('1234');
206
- expect(book.$snapshots).toBeDefined();
207
- expect(book.$snapshots.length).toBe(0);
206
+ expect(book.$$snapshots).toBeDefined();
207
+ expect(book.$$snapshots.length).toBe(0);
208
208
  });
209
209
 
210
210
  it('should roll back to version in middle', function () {
@@ -218,8 +218,8 @@ describe('RailsResource.snapshots', function () {
218
218
  book.rollbackTo(1);
219
219
 
220
220
  expect(book.$key).toBe('1235');
221
- expect(book.$snapshots).toBeDefined();
222
- expect(book.$snapshots.length).toBe(1);
221
+ expect(book.$$snapshots).toBeDefined();
222
+ expect(book.$$snapshots.length).toBe(1);
223
223
  });
224
224
 
225
225
  it('should roll back to first version', function () {
@@ -233,8 +233,8 @@ describe('RailsResource.snapshots', function () {
233
233
  book.rollbackTo(0);
234
234
 
235
235
  expect(book.$key).toBe('1234');
236
- expect(book.$snapshots).toBeDefined();
237
- expect(book.$snapshots.length).toBe(0);
236
+ expect(book.$$snapshots).toBeDefined();
237
+ expect(book.$$snapshots.length).toBe(0);
238
238
  });
239
239
 
240
240
  it('should roll back to last version when version exceeds available versions', function () {
@@ -249,8 +249,8 @@ describe('RailsResource.snapshots', function () {
249
249
  book.rollbackTo(100);
250
250
 
251
251
  expect(book.$key).toBe('1236');
252
- expect(book.$snapshots).toBeDefined();
253
- expect(book.$snapshots.length).toBe(2);
252
+ expect(book.$$snapshots).toBeDefined();
253
+ expect(book.$$snapshots.length).toBe(2);
254
254
  });
255
255
 
256
256
  it('should reset snapshots on create', function () {
@@ -264,8 +264,8 @@ describe('RailsResource.snapshots', function () {
264
264
  $httpBackend.flush();
265
265
 
266
266
  expect(book.$key).toBe('1235');
267
- expect(book.$snapshots).toBeDefined();
268
- expect(book.$snapshots.length).toBe(0);
267
+ expect(book.$$snapshots).toBeDefined();
268
+ expect(book.$$snapshots.length).toBe(0);
269
269
  });
270
270
 
271
271
  it('should be able to save after rollback', function () {
@@ -280,8 +280,28 @@ describe('RailsResource.snapshots', function () {
280
280
  $httpBackend.flush();
281
281
 
282
282
  expect(book.$key).toBe('1234');
283
- expect(book.$snapshots).toBeDefined();
284
- expect(book.$snapshots.length).toBe(0);
283
+ expect(book.$$snapshots).toBeDefined();
284
+ expect(book.$$snapshots.length).toBe(0);
285
+ });
286
+
287
+ it('should not submit $$snapshots', function () {
288
+ var book, data = {id: 1, $key: '1234', name: 'The Winds of Winter'};
289
+ book = new Book(data);
290
+ book.snapshot();
291
+ book.$key = '1235';
292
+
293
+ $httpBackend.whenPUT('/books/1', function(putData) {
294
+ var json = JSON.parse(putData);
295
+ expect(json.book.$$snapshots).toBeUndefined();
296
+ return true;
297
+ }).respond(200, {book: {id: 1}});
298
+
299
+ book.save();
300
+ $httpBackend.flush();
301
+
302
+ expect(book.$key).toBe('1235');
303
+ expect(book.$$snapshots).toBeDefined();
304
+ expect(book.$$snapshots.length).toBe(0);
285
305
  });
286
306
 
287
307
  it('should reset snapshots on update', function () {
@@ -295,8 +315,8 @@ describe('RailsResource.snapshots', function () {
295
315
  $httpBackend.flush();
296
316
 
297
317
  expect(book.$key).toBe('1235');
298
- expect(book.$snapshots).toBeDefined();
299
- expect(book.$snapshots.length).toBe(0);
318
+ expect(book.$$snapshots).toBeDefined();
319
+ expect(book.$$snapshots.length).toBe(0);
300
320
  });
301
321
 
302
322
  it('should reset snapshots on delete', function () {
@@ -310,8 +330,8 @@ describe('RailsResource.snapshots', function () {
310
330
  $httpBackend.flush();
311
331
 
312
332
  expect(book.$key).toBe('1235');
313
- expect(book.$snapshots).toBeDefined();
314
- expect(book.$snapshots.length).toBe(0);
333
+ expect(book.$$snapshots).toBeDefined();
334
+ expect(book.$$snapshots.length).toBe(0);
315
335
  });
316
336
 
317
337
  it('should call rollback callback on rollback', function () {
@@ -397,7 +417,7 @@ describe('RailsResource.snapshots', function () {
397
417
  book.snapshot();
398
418
  book.author.name = 'George Orwell';
399
419
 
400
- expect(book.$snapshots[0].author).not.toBeDefined();
420
+ expect(book.$$snapshots[0].author).not.toBeDefined();
401
421
  expect(book.author).toEqualData({id: 1, name: 'George Orwell'});
402
422
 
403
423
  book.rollback();
@@ -435,8 +455,8 @@ describe('RailsResource.snapshots', function () {
435
455
  book.$key = '1235';
436
456
  book.author.name = 'George Orwell';
437
457
 
438
- expect(book.$snapshots[0].author).toBeDefined();
439
- expect(book.$snapshots[0].$key).not.toBeDefined();
458
+ expect(book.$$snapshots[0].author).toBeDefined();
459
+ expect(book.$$snapshots[0].$key).not.toBeDefined();
440
460
  expect(book.$key).toBe('1235');
441
461
  expect(book.author).toEqualData({id: 1, name: 'George Orwell'});
442
462
 
@@ -15,14 +15,15 @@ describe('railsResourceFactory', function () {
15
15
  });
16
16
 
17
17
  describe('singular', function() {
18
- var $httpBackend, $rootScope, factory, Test, testInterceptor,
18
+ var $httpBackend, $timeout, $rootScope, factory, Test, testInterceptor,
19
19
  config = {
20
20
  url: '/test',
21
21
  name: 'test'
22
22
  };
23
23
 
24
- beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor) {
24
+ beforeEach(inject(function (_$httpBackend_, _$timeout_, _$rootScope_, railsResourceFactory, railsTestInterceptor) {
25
25
  $httpBackend = _$httpBackend_;
26
+ $timeout = _$timeout_;
26
27
  $rootScope = _$rootScope_;
27
28
  factory = railsResourceFactory;
28
29
  Test = railsResourceFactory(config);
@@ -177,22 +178,24 @@ describe('railsResourceFactory', function () {
177
178
  });
178
179
 
179
180
  it('get should call failure callback when 404', function () {
180
- var promise, success = false, failure = false;
181
+ inject(function ($exceptionHandler) {
182
+ var promise, success = false, failure = false;
181
183
 
182
- $httpBackend.expectGET('/test/123').respond(404);
184
+ $httpBackend.expectGET('/test/123').respond(404);
183
185
 
184
- expect(promise = Test.get(123)).toBeDefined();
185
-
186
- promise.then(function () {
187
- success = true;
188
- }, function () {
189
- failure = true;
190
- });
186
+ expect(promise = Test.get(123)).toBeDefined();
191
187
 
192
- $httpBackend.flush();
188
+ promise.then(function () {
189
+ success = true;
190
+ }, function () {
191
+ failure = true;
192
+ });
193
193
 
194
- expect(success).toBe(false);
195
- expect(failure).toBe(true);
194
+ $httpBackend.flush();
195
+ expect($exceptionHandler.errors).toEqual([]);
196
+ expect(success).toBe(false);
197
+ expect(failure).toBe(true);
198
+ });
196
199
  });
197
200
 
198
201
  it('get with default params should add parameter abc=1', function () {
@@ -559,6 +562,164 @@ describe('railsResourceFactory', function () {
559
562
  expect(Resource.configure(config)).toBeInstanceOf(Object);
560
563
  });
561
564
 
565
+ it('get should catch exceptions on failure', function () {
566
+ inject(function ($exceptionHandler) {
567
+ var failure = false;
568
+
569
+ $httpBackend.expectGET('/test/123').respond(500);
570
+
571
+ Test.get(123).catch(function (response) { failure = true});
572
+
573
+ $httpBackend.flush();
574
+ expect($exceptionHandler.errors).toEqual([]);
575
+ expect(failure).toBeTruthy();
576
+ });
577
+ });
578
+
579
+ it('get should timeout after configured time', function () {
580
+ var promise, Resource, success = false, failure = false;
581
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
582
+
583
+ Resource = factory(angular.extend({}, config, {
584
+ httpConfig: {
585
+ timeout: 5000
586
+ }
587
+ }));
588
+ promise = Resource.get(123);
589
+ promise.then(function () {
590
+ success = true;
591
+ }, function () {
592
+ failure = true;
593
+ });
594
+
595
+ $timeout.flush();
596
+ $httpBackend.verifyNoOutstandingExpectation();
597
+ $httpBackend.verifyNoOutstandingRequest();
598
+ expect(success).toBeFalsy();
599
+ expect(failure).toBeTruthy();
600
+ });
601
+
602
+ it('$http should abort after timeout promise resolved', function () {
603
+ inject(function ($q) {
604
+ var promise, success = false, failure = false,
605
+ timeoutDeferred = $q.defer();
606
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
607
+
608
+ promise = Test.$http({method: 'get', url: Test.resourceUrl(123), timeout: timeoutDeferred.promise});
609
+ promise.then(function () {
610
+ success = true;
611
+ }, function () {
612
+ failure = true;
613
+ });
614
+
615
+ timeoutDeferred.resolve();
616
+ $rootScope.$digest();
617
+ $httpBackend.verifyNoOutstandingExpectation();
618
+ $httpBackend.verifyNoOutstandingRequest();
619
+ expect(success).toBeFalsy();
620
+ expect(failure).toBeTruthy();
621
+ });
622
+ });
623
+
624
+ it('get should abort after abort called', function () {
625
+ inject(function ($q) {
626
+ var promise, success = false, failure = false;
627
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
628
+
629
+ promise = Test.get(123);
630
+ promise.then(function () {
631
+ success = true;
632
+ }, function () {
633
+ failure = true;
634
+ });
635
+
636
+ promise.abort();
637
+ $rootScope.$digest();
638
+ $httpBackend.verifyNoOutstandingExpectation();
639
+ $httpBackend.verifyNoOutstandingRequest();
640
+ expect(success).toBeFalsy();
641
+ expect(failure).toBeTruthy();
642
+ });
643
+ });
644
+
645
+ it('should have abort after chaining promise with then', function () {
646
+ inject(function ($q) {
647
+ var promise, success = false, failure = false;
648
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
649
+
650
+ promise = Test.get(123).then(function () {
651
+ success = true;
652
+ }, function () {
653
+ failure = true;
654
+ });
655
+
656
+ promise.abort();
657
+ $rootScope.$digest();
658
+ $httpBackend.verifyNoOutstandingExpectation();
659
+ $httpBackend.verifyNoOutstandingRequest();
660
+ expect(success).toBeFalsy();
661
+ expect(failure).toBeTruthy();
662
+ });
663
+ });
664
+
665
+ it('should have abort after chaining promise with catch', function () {
666
+ inject(function ($q) {
667
+ var promise, caught = false;
668
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
669
+
670
+ promise = Test.get(123).catch(function () {
671
+ caught = true;
672
+ });
673
+
674
+ promise.abort();
675
+ $rootScope.$digest();
676
+ $httpBackend.verifyNoOutstandingExpectation();
677
+ $httpBackend.verifyNoOutstandingRequest();
678
+ expect(caught).toBeTruthy();
679
+ });
680
+ });
681
+
682
+ it('should have abort after chaining promise with finally', function () {
683
+ inject(function ($q) {
684
+ var promise, caught = false, final = false;
685
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
686
+
687
+ promise = Test.get(123).catch(function () {
688
+ caught = true;
689
+ }).finally(function () {
690
+ final = true;
691
+ });
692
+
693
+ promise.abort();
694
+ $rootScope.$digest();
695
+ $httpBackend.verifyNoOutstandingExpectation();
696
+ $httpBackend.verifyNoOutstandingRequest();
697
+ expect(caught).toBeTruthy();
698
+ expect(final).toBeTruthy();
699
+ });
700
+ });
701
+
702
+ it('should have abort after multiple chainings', function () {
703
+ inject(function ($q) {
704
+ var promise, failure = false;
705
+ $httpBackend.expectGET('/test/123').respond(200, {test: {id: 123, abc: 'xyz'}});
706
+
707
+ promise = Test.get(123)
708
+ .then(function () {}, function () {
709
+ return $q.reject(value);
710
+ })
711
+ .then(function () {}, function () {
712
+ failure = true;
713
+ });
714
+
715
+ promise.abort();
716
+ $rootScope.$digest();
717
+ $httpBackend.verifyNoOutstandingExpectation();
718
+ $httpBackend.verifyNoOutstandingRequest();
719
+ expect(failure).toBeTruthy();
720
+ });
721
+ });
722
+
562
723
  describe('overridden idAttribute', function () {
563
724
  beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
564
725
  Test = railsResourceFactory({url: '/test', name: 'test', idAttribute: 'xyz'});