angularjs-rails-resource 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +63 -6
- data/lib/angularjs-rails-resource/version.rb +1 -1
- data/test/lib/angular/angular-bootstrap-prettify.js +13 -8
- data/test/lib/angular/angular-bootstrap.js +3 -2
- data/test/lib/angular/angular-cookies.js +14 -1
- data/test/lib/angular/angular-loader.js +33 -5
- data/test/lib/angular/angular-locale_en-us.js +4 -0
- data/test/lib/angular/angular-mobile.js +267 -0
- data/test/lib/angular/angular-mocks.js +157 -52
- data/test/lib/angular/angular-resource.js +195 -102
- data/test/lib/angular/angular-sanitize.js +28 -5
- data/test/lib/angular/angular-scenario.js +27862 -0
- data/test/lib/angular/angular.js +2820 -907
- data/test/unit/angularjs/rails/fieldRenamingSpec.js +91 -0
- data/test/unit/angularjs/rails/httpSettingsSpec.js +219 -0
- data/test/unit/angularjs/rails/nestedUrlsSpec.js +327 -0
- data/test/unit/angularjs/rails/resourceSpec.js +37 -662
- data/test/unit/angularjs/rails/rootWrappingSpec.js +46 -0
- data/vendor/assets/javascripts/angularjs/rails/resource.js +62 -16
- metadata +16 -2
@@ -1,140 +1,9 @@
|
|
1
|
-
describe("
|
1
|
+
describe("railsResourceFactory", function () {
|
2
2
|
'use strict';
|
3
3
|
|
4
4
|
beforeEach(module('rails'));
|
5
5
|
|
6
|
-
describe('
|
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('singular railsResourceFactory', function() {
|
6
|
+
describe('singular', function() {
|
138
7
|
var $httpBackend, $rootScope, factory, Test,
|
139
8
|
config = {
|
140
9
|
url: '/test',
|
@@ -453,9 +322,43 @@ describe("js.rails", function () {
|
|
453
322
|
|
454
323
|
$httpBackend.flush();
|
455
324
|
}));
|
325
|
+
|
326
|
+
it('should transform attributes on build', function() {
|
327
|
+
var test = new Test({id: 123, abc_def: "T"});
|
328
|
+
expect(test).toEqualData({id: 123, abcDef: "T"});
|
329
|
+
});
|
330
|
+
|
331
|
+
angular.forEach(['post', 'put', 'patch'], function (method) {
|
332
|
+
it('should be able to ' + method + ' to arbitrary url', inject(function($httpBackend) {
|
333
|
+
var promise, result = {};
|
334
|
+
|
335
|
+
promise = Test['$' + method]('/xyz', {id: 123, abc: 'xyz', xyz: 'abc'});
|
336
|
+
$httpBackend['expect' + angular.uppercase(method)]('/xyz', {test: {id: 123, abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}});
|
337
|
+
|
338
|
+
promise.then(function (response) {
|
339
|
+
result = response;
|
340
|
+
});
|
341
|
+
|
342
|
+
$httpBackend.flush();
|
343
|
+
|
344
|
+
// abc was originally set on the object so it should still be there after the update
|
345
|
+
expect(result).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
|
346
|
+
}));
|
347
|
+
|
348
|
+
it('should be able to ' + method + ' instance to arbitrary url', inject(function($httpBackend) {
|
349
|
+
var test = new Test({id: 123, abc: 'xyz', xyz: 'abc'});
|
350
|
+
$httpBackend['expect' + angular.uppercase(method)]('/xyz', {test: {id: 123, abc: 'xyz', xyz: 'abc'}}).respond(200, {test: {id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'}});
|
351
|
+
test['$' + method]('/xyz');
|
352
|
+
$httpBackend.flush();
|
353
|
+
|
354
|
+
// abc was originally set on the object so it should still be there after the update
|
355
|
+
expect(test).toEqualData({id: 123, abc: 'xyz', xyz: 'abc', extra: 'test'});
|
356
|
+
}));
|
357
|
+
});
|
358
|
+
|
456
359
|
});
|
457
360
|
|
458
|
-
describe('plural
|
361
|
+
describe('plural', function() {
|
459
362
|
var $httpBackend, $rootScope, factory, PluralTest,
|
460
363
|
pluralConfig = {
|
461
364
|
url: '/pluralTest',
|
@@ -514,532 +417,4 @@ describe("js.rails", function () {
|
|
514
417
|
expect(result.length).toBe(0);
|
515
418
|
}));
|
516
419
|
});
|
517
|
-
|
518
|
-
describe('nested railsResourceFactory', function() {
|
519
|
-
var $httpBackend, $rootScope, factory, NestedTest,
|
520
|
-
nestedConfig = {
|
521
|
-
url: '/nested/{{nestedId}}/test/{{id}}',
|
522
|
-
name: 'nestedTest'
|
523
|
-
};
|
524
|
-
|
525
|
-
beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
|
526
|
-
$httpBackend = _$httpBackend_;
|
527
|
-
$rootScope = _$rootScope_;
|
528
|
-
factory = railsResourceFactory;
|
529
|
-
NestedTest = railsResourceFactory(nestedConfig);
|
530
|
-
}));
|
531
|
-
|
532
|
-
afterEach(function() {
|
533
|
-
$httpBackend.verifyNoOutstandingExpectation();
|
534
|
-
$httpBackend.verifyNoOutstandingRequest();
|
535
|
-
});
|
536
|
-
|
537
|
-
it('query should return resource object when response is single object', inject(function($httpBackend) {
|
538
|
-
var promise, result;
|
539
|
-
|
540
|
-
$httpBackend.expectGET('/nested/1234/test').respond(200, {nested_test: {abc: 'xyz'}});
|
541
|
-
|
542
|
-
expect(promise = NestedTest.query(null, {nestedId: 1234})).toBeDefined();
|
543
|
-
|
544
|
-
promise.then(function (response) {
|
545
|
-
result = response;
|
546
|
-
});
|
547
|
-
|
548
|
-
$httpBackend.flush();
|
549
|
-
|
550
|
-
expect(result).toBeInstanceOf(NestedTest);
|
551
|
-
expect(result).toEqualData({abc: 'xyz'});
|
552
|
-
}));
|
553
|
-
|
554
|
-
it('query should return no data on 204', inject(function($httpBackend) {
|
555
|
-
var promise, result;
|
556
|
-
|
557
|
-
$httpBackend.expectGET('/nested/1234/test').respond(204);
|
558
|
-
expect(promise = NestedTest.query(null, {nestedId: 1234})).toBeDefined();
|
559
|
-
|
560
|
-
promise.then(function (response) {
|
561
|
-
result = response;
|
562
|
-
});
|
563
|
-
|
564
|
-
$httpBackend.flush();
|
565
|
-
|
566
|
-
expect(result).toBeUndefined();
|
567
|
-
}));
|
568
|
-
|
569
|
-
it('query should add parameter abc=1', inject(function($httpBackend) {
|
570
|
-
var promise;
|
571
|
-
|
572
|
-
$httpBackend.expectGET('/nested/1234/test?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
|
573
|
-
|
574
|
-
expect(promise = NestedTest.query({abc: '1'}, {nestedId: 1234})).toBeDefined();
|
575
|
-
$httpBackend.flush();
|
576
|
-
}));
|
577
|
-
|
578
|
-
it('query should add parameters abc=1 & xyz=2', inject(function($httpBackend) {
|
579
|
-
var promise;
|
580
|
-
|
581
|
-
$httpBackend.expectGET('/nested/1234/test?abc=1&xyz=2').respond(200, {nested_test: {abc: 'xyz'}});
|
582
|
-
|
583
|
-
expect(promise = NestedTest.query({abc: '1', xyz: 2}, {nestedId: 1234})).toBeDefined();
|
584
|
-
$httpBackend.flush();
|
585
|
-
}));
|
586
|
-
|
587
|
-
it('query with default params should add parameter abc=1', inject(function($httpBackend) {
|
588
|
-
var promise, resource, defaultParamsConfig = {};
|
589
|
-
|
590
|
-
$httpBackend.expectGET('/nested/1234/test?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
|
591
|
-
|
592
|
-
angular.copy(nestedConfig, defaultParamsConfig);
|
593
|
-
defaultParamsConfig.defaultParams = {abc: '1'};
|
594
|
-
|
595
|
-
resource = factory(defaultParamsConfig);
|
596
|
-
expect(promise = resource.query(null, {nestedId: 1234})).toBeDefined();
|
597
|
-
|
598
|
-
$httpBackend.flush();
|
599
|
-
}));
|
600
|
-
|
601
|
-
it('get should return resource object when response is 200', inject(function($httpBackend) {
|
602
|
-
var promise, result;
|
603
|
-
|
604
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, abc: 'xyz'}});
|
605
|
-
|
606
|
-
expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
|
607
|
-
|
608
|
-
promise.then(function (response) {
|
609
|
-
result = response;
|
610
|
-
});
|
611
|
-
|
612
|
-
$httpBackend.flush();
|
613
|
-
|
614
|
-
expect(result).toBeInstanceOf(NestedTest);
|
615
|
-
expect(result).toEqualData({id: 123, abc: 'xyz'});
|
616
|
-
}));
|
617
|
-
|
618
|
-
it('get should call failure callback when 404', inject(function($httpBackend) {
|
619
|
-
var promise, success = false, failure = false;
|
620
|
-
|
621
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(404);
|
622
|
-
|
623
|
-
expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
|
624
|
-
|
625
|
-
promise.then(function () {
|
626
|
-
success = true;
|
627
|
-
}, function () {
|
628
|
-
failure = true;
|
629
|
-
});
|
630
|
-
|
631
|
-
$httpBackend.flush();
|
632
|
-
|
633
|
-
expect(success).toBe(false);
|
634
|
-
expect(failure).toBe(true);
|
635
|
-
}));
|
636
|
-
|
637
|
-
it('get with default params should add parameter abc=1', inject(function($httpBackend) {
|
638
|
-
var promise, resource, defaultParamsConfig = {};
|
639
|
-
|
640
|
-
$httpBackend.expectGET('/nested/1234/test/123?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
|
641
|
-
|
642
|
-
angular.copy(nestedConfig, defaultParamsConfig);
|
643
|
-
defaultParamsConfig.defaultParams = {abc: '1'};
|
644
|
-
|
645
|
-
resource = factory(defaultParamsConfig);
|
646
|
-
expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
|
647
|
-
|
648
|
-
$httpBackend.flush();
|
649
|
-
}));
|
650
|
-
|
651
|
-
it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) {
|
652
|
-
var promise, result, resource;
|
653
|
-
|
654
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
|
655
|
-
|
656
|
-
resource = factory(nestedConfig);
|
657
|
-
resource.responseInterceptors = [];
|
658
|
-
resource.requestTransformers = [];
|
659
|
-
expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
|
660
|
-
|
661
|
-
promise.then(function (response) {
|
662
|
-
result = response;
|
663
|
-
});
|
664
|
-
|
665
|
-
$httpBackend.flush();
|
666
|
-
|
667
|
-
expect(result).toBeInstanceOf(resource);
|
668
|
-
expect(result).toEqualData({id: 123, nested_id: 1234, abc_def: 'xyz'});
|
669
|
-
}));
|
670
|
-
|
671
|
-
it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) {
|
672
|
-
var promise, result, resource, testConfig = {};
|
673
|
-
|
674
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
|
675
|
-
|
676
|
-
angular.copy(nestedConfig, testConfig);
|
677
|
-
testConfig.requestTransformers = [];
|
678
|
-
testConfig.responseInterceptors = ['railsFieldRenamingInterceptor'];
|
679
|
-
resource = factory(testConfig);
|
680
|
-
|
681
|
-
expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
|
682
|
-
|
683
|
-
promise.then(function (response) {
|
684
|
-
result = response;
|
685
|
-
});
|
686
|
-
|
687
|
-
$httpBackend.flush();
|
688
|
-
|
689
|
-
expect(result).toBeInstanceOf(resource);
|
690
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
|
691
|
-
}));
|
692
|
-
|
693
|
-
it('should be able to create new instance and save it', inject(function($httpBackend) {
|
694
|
-
var data = new NestedTest({nestedId: 1234, abcDef: 'xyz'});
|
695
|
-
|
696
|
-
$httpBackend.expectPOST('/nested/1234/test', {nested_test: {nested_id: 1234, abc_def: 'xyz'}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz'}});
|
697
|
-
data.nestedId = 1234;
|
698
|
-
data.create();
|
699
|
-
$httpBackend.flush();
|
700
|
-
|
701
|
-
expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
|
702
|
-
}));
|
703
|
-
|
704
|
-
it('should be able to create new instance and update it', inject(function($httpBackend) {
|
705
|
-
var data = new NestedTest({abcDef: 'xyz'});
|
706
|
-
|
707
|
-
$httpBackend.expectPOST('/nested/1234/test', {nested_test: {abc_def: 'xyz', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz'}});
|
708
|
-
data.nestedId = 1234;
|
709
|
-
data.create();
|
710
|
-
$httpBackend.flush(1);
|
711
|
-
|
712
|
-
expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
|
713
|
-
|
714
|
-
$httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, xyz: 'abc', abc_def: 'xyz', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
|
715
|
-
data.xyz = 'abc';
|
716
|
-
data.update();
|
717
|
-
$httpBackend.flush();
|
718
|
-
|
719
|
-
expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
|
720
|
-
}));
|
721
|
-
|
722
|
-
it('create with default params should add parameter abc=1', inject(function($httpBackend) {
|
723
|
-
var promise, Resource, data, defaultParamsConfig = {};
|
724
|
-
|
725
|
-
$httpBackend.expectPOST('/nested/1234/test?abc=1', {nested_test: {nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234}});
|
726
|
-
|
727
|
-
angular.copy(nestedConfig, defaultParamsConfig);
|
728
|
-
defaultParamsConfig.defaultParams = {abc: '1'};
|
729
|
-
|
730
|
-
Resource = factory(defaultParamsConfig);
|
731
|
-
data = new Resource();
|
732
|
-
data.nestedId = 1234;
|
733
|
-
data.create();
|
734
|
-
|
735
|
-
$httpBackend.flush();
|
736
|
-
}));
|
737
|
-
|
738
|
-
it('should be able to get resource and update it', inject(function($httpBackend) {
|
739
|
-
var promise, result;
|
740
|
-
|
741
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nested_id: 1234, abc: 'xyz'}});
|
742
|
-
|
743
|
-
expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
|
744
|
-
|
745
|
-
promise.then(function (response) {
|
746
|
-
result = response;
|
747
|
-
});
|
748
|
-
|
749
|
-
$httpBackend.flush();
|
750
|
-
|
751
|
-
expect(result).toBeInstanceOf(NestedTest);
|
752
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
|
753
|
-
|
754
|
-
$httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, abc: 'xyz', xyz: 'abc', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz', xyz: 'abc', extra: 'test'}});
|
755
|
-
result.xyz = 'abc';
|
756
|
-
result.update();
|
757
|
-
$httpBackend.flush();
|
758
|
-
|
759
|
-
// abc was originally set on the object so it should still be there after the update
|
760
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz', abcDef: 'xyz', xyz: 'abc', extra: 'test'});
|
761
|
-
}));
|
762
|
-
|
763
|
-
it('update should handle 204 response', inject(function($httpBackend) {
|
764
|
-
var promise, result;
|
765
|
-
|
766
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nested_id: 1234, abc: 'xyz'}});
|
767
|
-
|
768
|
-
expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
|
769
|
-
|
770
|
-
promise.then(function (response) {
|
771
|
-
result = response;
|
772
|
-
});
|
773
|
-
|
774
|
-
$httpBackend.flush();
|
775
|
-
|
776
|
-
expect(result).toBeInstanceOf(NestedTest);
|
777
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
|
778
|
-
|
779
|
-
$httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, abc: 'xyz', xyz: 'abc', nested_id: 1234}}).respond(204);
|
780
|
-
result.xyz = 'abc';
|
781
|
-
result.update();
|
782
|
-
$httpBackend.flush();
|
783
|
-
|
784
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz', xyz: 'abc'});
|
785
|
-
}));
|
786
|
-
|
787
|
-
it('should be able to delete instance returned from get', inject(function($httpBackend) {
|
788
|
-
var promise, result;
|
789
|
-
|
790
|
-
$httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nestedId: 1234, abc: 'xyz'}});
|
791
|
-
|
792
|
-
expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
|
793
|
-
|
794
|
-
promise.then(function (response) {
|
795
|
-
result = response;
|
796
|
-
});
|
797
|
-
|
798
|
-
$httpBackend.flush();
|
799
|
-
|
800
|
-
expect(result).toBeInstanceOf(NestedTest);
|
801
|
-
expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
|
802
|
-
|
803
|
-
$httpBackend.expectDELETE('/nested/1234/test/123').respond(204);
|
804
|
-
result.remove();
|
805
|
-
$httpBackend.flush();
|
806
|
-
}));
|
807
|
-
|
808
|
-
it('should be able to create new instance and update it', inject(function($httpBackend) {
|
809
|
-
var data = new NestedTest({abcDef: 'xyz'});
|
810
|
-
|
811
|
-
$httpBackend.expectPOST('/nested/1234/test', {nested_test: {abc_def: 'xyz', nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234, abc_def: 'xyz'}});
|
812
|
-
data.nestedId = 1234;
|
813
|
-
data.create();
|
814
|
-
$httpBackend.flush(1);
|
815
|
-
|
816
|
-
expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
|
817
|
-
expect(data).toBeInstanceOf(NestedTest);
|
818
|
-
|
819
|
-
$httpBackend.expectDELETE('/nested/1234/test/123').respond(204);
|
820
|
-
data.remove();
|
821
|
-
$httpBackend.flush();
|
822
|
-
}));
|
823
|
-
|
824
|
-
it('delete with default params should add parameter abc=1', inject(function($httpBackend) {
|
825
|
-
var Resource, data, defaultParamsConfig = {};
|
826
|
-
|
827
|
-
$httpBackend.expectDELETE('/nested/1234/test/123?abc=1').respond(204);
|
828
|
-
|
829
|
-
angular.copy(nestedConfig, defaultParamsConfig);
|
830
|
-
defaultParamsConfig.defaultParams = {abc: '1'};
|
831
|
-
|
832
|
-
Resource = factory(defaultParamsConfig);
|
833
|
-
data = new Resource();
|
834
|
-
data.id = 123;
|
835
|
-
data.nestedId = 1234;
|
836
|
-
data.remove();
|
837
|
-
|
838
|
-
$httpBackend.flush();
|
839
|
-
}));
|
840
|
-
});
|
841
|
-
|
842
|
-
describe('http settings', function() {
|
843
|
-
var $httpBackend, $rootScope, factory,
|
844
|
-
config = {
|
845
|
-
url: '/test',
|
846
|
-
name: 'test'
|
847
|
-
};
|
848
|
-
|
849
|
-
beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
|
850
|
-
$httpBackend = _$httpBackend_;
|
851
|
-
$rootScope = _$rootScope_;
|
852
|
-
factory = railsResourceFactory;
|
853
|
-
}));
|
854
|
-
|
855
|
-
afterEach(function() {
|
856
|
-
$httpBackend.verifyNoOutstandingExpectation();
|
857
|
-
$httpBackend.verifyNoOutstandingRequest();
|
858
|
-
});
|
859
|
-
|
860
|
-
function headerComparison(expectedHeaders) {
|
861
|
-
return function(headers) {
|
862
|
-
var matches = true;
|
863
|
-
|
864
|
-
angular.forEach(expectedHeaders, function (value, key) {
|
865
|
-
if (headers[key] !== value) {
|
866
|
-
matches = false;
|
867
|
-
}
|
868
|
-
});
|
869
|
-
|
870
|
-
return matches;
|
871
|
-
};
|
872
|
-
}
|
873
|
-
|
874
|
-
it('query should pass default $http options', inject(function($httpBackend) {
|
875
|
-
var promise, result, Test;
|
876
|
-
|
877
|
-
$httpBackend.expectGET('/test', headerComparison({'Accept': 'application/json'})).respond(200, {test: {abc: 'xyz'}});
|
878
|
-
|
879
|
-
Test = factory(config);
|
880
|
-
expect(promise = Test.query()).toBeDefined();
|
881
|
-
|
882
|
-
promise.then(function (response) {
|
883
|
-
result = response;
|
884
|
-
});
|
885
|
-
|
886
|
-
$httpBackend.flush();
|
887
|
-
}));
|
888
|
-
|
889
|
-
it('query should allow custom Accept', inject(function($httpBackend) {
|
890
|
-
var promise, result, Test;
|
891
|
-
|
892
|
-
$httpBackend.expectGET('/test', headerComparison({'Accept': 'text/plain'})).respond(200, {test: {abc: 'xyz'}});
|
893
|
-
|
894
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
|
895
|
-
expect(promise = Test.query()).toBeDefined();
|
896
|
-
|
897
|
-
promise.then(function (response) {
|
898
|
-
result = response;
|
899
|
-
});
|
900
|
-
|
901
|
-
$httpBackend.flush();
|
902
|
-
}));
|
903
|
-
|
904
|
-
it('query should allow custom header', inject(function($httpBackend) {
|
905
|
-
var promise, result, Test;
|
906
|
-
|
907
|
-
$httpBackend.expectGET('/test', headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {abc: 'xyz'}});
|
908
|
-
|
909
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
|
910
|
-
expect(promise = Test.query()).toBeDefined();
|
911
|
-
|
912
|
-
promise.then(function (response) {
|
913
|
-
result = response;
|
914
|
-
});
|
915
|
-
|
916
|
-
$httpBackend.flush();
|
917
|
-
}));
|
918
|
-
|
919
|
-
it('get should pass default $http options', inject(function($httpBackend) {
|
920
|
-
var promise, result, Test;
|
921
|
-
|
922
|
-
$httpBackend.expectGET('/test/123', headerComparison({'Accept': 'application/json'})).respond(200, {test: {abc: 'xyz'}});
|
923
|
-
|
924
|
-
Test = factory(config);
|
925
|
-
expect(promise = Test.get(123)).toBeDefined();
|
926
|
-
|
927
|
-
promise.then(function (response) {
|
928
|
-
result = response;
|
929
|
-
});
|
930
|
-
|
931
|
-
$httpBackend.flush();
|
932
|
-
}));
|
933
|
-
|
934
|
-
it('get should allow custom Accept', inject(function($httpBackend) {
|
935
|
-
var promise, result, Test;
|
936
|
-
|
937
|
-
$httpBackend.expectGET('/test/123', headerComparison({'Accept': 'text/plain'})).respond(200, {test: {abc: 'xyz'}});
|
938
|
-
|
939
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
|
940
|
-
expect(promise = Test.get(123)).toBeDefined();
|
941
|
-
|
942
|
-
promise.then(function (response) {
|
943
|
-
result = response;
|
944
|
-
});
|
945
|
-
|
946
|
-
$httpBackend.flush();
|
947
|
-
}));
|
948
|
-
|
949
|
-
it('get should allow custom header', inject(function($httpBackend) {
|
950
|
-
var promise, result, Test;
|
951
|
-
|
952
|
-
$httpBackend.expectGET('/test/123', headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {abc: 'xyz'}});
|
953
|
-
|
954
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
|
955
|
-
expect(promise = Test.get(123)).toBeDefined();
|
956
|
-
|
957
|
-
promise.then(function (response) {
|
958
|
-
result = response;
|
959
|
-
});
|
960
|
-
|
961
|
-
$httpBackend.flush();
|
962
|
-
}));
|
963
|
-
|
964
|
-
it('create should pass default $http options', inject(function($httpBackend) {
|
965
|
-
var Test;
|
966
|
-
|
967
|
-
$httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'application/json'})).respond(200, {test: {id: 123, xyz: '123'}});
|
968
|
-
|
969
|
-
Test = factory(config);
|
970
|
-
var test = new Test();
|
971
|
-
test.xyz = '123';
|
972
|
-
test.create();
|
973
|
-
|
974
|
-
$httpBackend.flush();
|
975
|
-
}));
|
976
|
-
|
977
|
-
it('create should allow custom Accept', inject(function($httpBackend) {
|
978
|
-
var Test;
|
979
|
-
|
980
|
-
$httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'text/plain'})).respond(200, {test: {id: 123, xyz: '123'}});
|
981
|
-
|
982
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
|
983
|
-
var test = new Test();
|
984
|
-
test.xyz = '123';
|
985
|
-
test.create();
|
986
|
-
|
987
|
-
$httpBackend.flush();
|
988
|
-
}));
|
989
|
-
|
990
|
-
it('create should allow custom header', inject(function($httpBackend) {
|
991
|
-
var Test;
|
992
|
-
|
993
|
-
$httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {id: 123, xyz: '123'}});
|
994
|
-
|
995
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
|
996
|
-
var test = new Test();
|
997
|
-
test.xyz = '123';
|
998
|
-
test.create();
|
999
|
-
|
1000
|
-
$httpBackend.flush();
|
1001
|
-
}));
|
1002
|
-
|
1003
|
-
it('update should pass default $http options', inject(function($httpBackend) {
|
1004
|
-
var Test;
|
1005
|
-
|
1006
|
-
$httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'application/json'})).respond(200, {test: {id: 123, xyz: '123'}});
|
1007
|
-
|
1008
|
-
Test = factory(config);
|
1009
|
-
var test = new Test();
|
1010
|
-
test.id = 123;
|
1011
|
-
test.xyz = '123';
|
1012
|
-
test.update();
|
1013
|
-
|
1014
|
-
$httpBackend.flush();
|
1015
|
-
}));
|
1016
|
-
|
1017
|
-
it('update should allow custom Accept', inject(function($httpBackend) {
|
1018
|
-
var Test;
|
1019
|
-
|
1020
|
-
$httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'text/plain'})).respond(200, {test: {id: 123, xyz: '123'}});
|
1021
|
-
|
1022
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
|
1023
|
-
var test = new Test();
|
1024
|
-
test.id = 123;
|
1025
|
-
test.xyz = '123';
|
1026
|
-
test.update();
|
1027
|
-
|
1028
|
-
$httpBackend.flush();
|
1029
|
-
}));
|
1030
|
-
|
1031
|
-
it('update should allow custom header', inject(function($httpBackend) {
|
1032
|
-
var Test;
|
1033
|
-
|
1034
|
-
$httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {id: 123, xyz: '123'}});
|
1035
|
-
|
1036
|
-
Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
|
1037
|
-
var test = new Test();
|
1038
|
-
test.id = 123;
|
1039
|
-
test.xyz = '123';
|
1040
|
-
test.update();
|
1041
|
-
|
1042
|
-
$httpBackend.flush();
|
1043
|
-
}));
|
1044
|
-
});
|
1045
420
|
});
|