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.
@@ -0,0 +1,91 @@
1
+ describe("field renaming", function () {
2
+ 'use strict';
3
+
4
+ beforeEach(module('rails'));
5
+
6
+ var q, rootScope,
7
+ transformer, interceptor;
8
+
9
+ function testTransform(underscoreData, camelizeData) {
10
+ var result, resultPromise,
11
+ deferred = q.defer();
12
+
13
+ expect(transformer(angular.copy(camelizeData, {}))).toEqualData(underscoreData);
14
+ expect(resultPromise = interceptor(deferred.promise)).toBeDefined();
15
+
16
+ resultPromise.then(function (response) {
17
+ result = response;
18
+ });
19
+
20
+ deferred.resolve(angular.copy(underscoreData, {}));
21
+ rootScope.$digest(); // needed for $q to actually run callbacks
22
+ expect(result).toEqualData(camelizeData);
23
+ }
24
+
25
+ beforeEach(inject(function ($rootScope, $q, railsFieldRenamingTransformer, railsFieldRenamingInterceptor) {
26
+ q = $q;
27
+ rootScope = $rootScope;
28
+ transformer = railsFieldRenamingTransformer;
29
+ interceptor = railsFieldRenamingInterceptor;
30
+ }));
31
+
32
+ it('should ignore empty response', function() {
33
+ testTransform({}, {});
34
+ });
35
+
36
+ it('should ignore null response data', function() {
37
+ testTransform({data: null}, {data: null});
38
+ });
39
+
40
+ it('should leave non-data response fields untouched', function() {
41
+ testTransform({data: null, test_value: 'xyz'}, {data: null, test_value: 'xyz'});
42
+ });
43
+
44
+ it('should transform abc_def <-> abcDef', function() {
45
+ testTransform({data: {abc_def: 'xyz'}}, {data: {abcDef: 'xyz'}});
46
+ });
47
+
48
+ it('should transform abc_def, ghi_jkl <-> abcDef, ghiJkl', function() {
49
+ testTransform({data: {abc_def: 'xyz', ghi_jkl: 'abc'}}, {data: {abcDef: 'xyz', ghiJkl: 'abc'}});
50
+ });
51
+
52
+ it('should transform abc <-> abc', function() {
53
+ testTransform({data: {abc: 'xyz'}}, {data: {abc: 'xyz'}});
54
+ });
55
+
56
+ it('should transform _abc <-> _abc', function() {
57
+ testTransform({data: {_abc: 'xyz'}}, {data: {_abc: 'xyz'}});
58
+ });
59
+
60
+ it('should transform abc_ <-> abc_', function() {
61
+ testTransform({data: {abc_: 'xyz'}}, {data: {abc_: 'xyz'}});
62
+ });
63
+
64
+ it('should transform nested abc_def.abc_def <-> abcDef.abcDef', function() {
65
+ testTransform({data: {abc_def: {abc_def: 'xyz'}}}, {data: {abcDef: {abcDef: 'xyz'}}});
66
+ });
67
+
68
+ it('should transform nested abc_def.abc_def, abc_def.ghi_jkl <-> abcDef.abcDef, abcDef.ghiJkl', function() {
69
+ testTransform({data: {abc_def: {abc_def: 'xyz', ghi_jkl: 'abc'}}}, {data: {abcDef: {abcDef: 'xyz', ghiJkl: 'abc'}}});
70
+ });
71
+
72
+ it('should transform nested abc.abc_def <-> abc.abcDef', function() {
73
+ testTransform({data: {abc: {abc_def: 'xyz'}}}, {data: {abc: {abcDef: 'xyz'}}});
74
+ });
75
+
76
+ it('should handle empty root array', function() {
77
+ testTransform({data: []}, {data: []});
78
+ });
79
+
80
+ it('should camelize array of objects', function() {
81
+ testTransform({data: [{abc_def: 'xyz'}, {ghi_jkl: 'abc'}]}, {data: [{abcDef: 'xyz'}, {ghiJkl: 'abc'}]});
82
+ });
83
+
84
+ it('should handle array of strings', function() {
85
+ testTransform({data: ['abc', 'def']}, {data: ['abc', 'def']});
86
+ });
87
+
88
+ it('should handle array of numbers', function() {
89
+ testTransform({data: [1, 2, 3]}, {data: [1, 2, 3]});
90
+ });
91
+ });
@@ -0,0 +1,219 @@
1
+ describe("http setting", function () {
2
+ 'use strict';
3
+
4
+ beforeEach(module('rails'));
5
+
6
+ var $httpBackend, $rootScope, factory,
7
+ config = {
8
+ url: '/test',
9
+ name: 'test'
10
+ };
11
+
12
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
13
+ $httpBackend = _$httpBackend_;
14
+ $rootScope = _$rootScope_;
15
+ factory = railsResourceFactory;
16
+ }));
17
+
18
+ afterEach(function() {
19
+ $httpBackend.verifyNoOutstandingExpectation();
20
+ $httpBackend.verifyNoOutstandingRequest();
21
+ });
22
+
23
+ function headerComparison(expectedHeaders) {
24
+ return function(headers) {
25
+ var matches = true;
26
+
27
+ angular.forEach(expectedHeaders, function (value, key) {
28
+ if (headers[key] !== value) {
29
+ matches = false;
30
+ }
31
+ });
32
+
33
+ return matches;
34
+ };
35
+ }
36
+
37
+ it('query should pass default $http options', inject(function($httpBackend) {
38
+ var promise, result, Test;
39
+
40
+ $httpBackend.expectGET('/test', headerComparison({'Accept': 'application/json'})).respond(200, {test: {abc: 'xyz'}});
41
+
42
+ Test = factory(config);
43
+ expect(promise = Test.query()).toBeDefined();
44
+
45
+ promise.then(function (response) {
46
+ result = response;
47
+ });
48
+
49
+ $httpBackend.flush();
50
+ }));
51
+
52
+ it('query should allow custom Accept', inject(function($httpBackend) {
53
+ var promise, result, Test;
54
+
55
+ $httpBackend.expectGET('/test', headerComparison({'Accept': 'text/plain'})).respond(200, {test: {abc: 'xyz'}});
56
+
57
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
58
+ expect(promise = Test.query()).toBeDefined();
59
+
60
+ promise.then(function (response) {
61
+ result = response;
62
+ });
63
+
64
+ $httpBackend.flush();
65
+ }));
66
+
67
+ it('query should allow custom header', inject(function($httpBackend) {
68
+ var promise, result, Test;
69
+
70
+ $httpBackend.expectGET('/test', headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {abc: 'xyz'}});
71
+
72
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
73
+ expect(promise = Test.query()).toBeDefined();
74
+
75
+ promise.then(function (response) {
76
+ result = response;
77
+ });
78
+
79
+ $httpBackend.flush();
80
+ }));
81
+
82
+ it('get should pass default $http options', inject(function($httpBackend) {
83
+ var promise, result, Test;
84
+
85
+ $httpBackend.expectGET('/test/123', headerComparison({'Accept': 'application/json'})).respond(200, {test: {abc: 'xyz'}});
86
+
87
+ Test = factory(config);
88
+ expect(promise = Test.get(123)).toBeDefined();
89
+
90
+ promise.then(function (response) {
91
+ result = response;
92
+ });
93
+
94
+ $httpBackend.flush();
95
+ }));
96
+
97
+ it('get should allow custom Accept', inject(function($httpBackend) {
98
+ var promise, result, Test;
99
+
100
+ $httpBackend.expectGET('/test/123', headerComparison({'Accept': 'text/plain'})).respond(200, {test: {abc: 'xyz'}});
101
+
102
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
103
+ expect(promise = Test.get(123)).toBeDefined();
104
+
105
+ promise.then(function (response) {
106
+ result = response;
107
+ });
108
+
109
+ $httpBackend.flush();
110
+ }));
111
+
112
+ it('get should allow custom header', inject(function($httpBackend) {
113
+ var promise, result, Test;
114
+
115
+ $httpBackend.expectGET('/test/123', headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {abc: 'xyz'}});
116
+
117
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
118
+ expect(promise = Test.get(123)).toBeDefined();
119
+
120
+ promise.then(function (response) {
121
+ result = response;
122
+ });
123
+
124
+ $httpBackend.flush();
125
+ }));
126
+
127
+ it('create should pass default $http options', inject(function($httpBackend) {
128
+ var Test;
129
+
130
+ $httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'application/json', 'Content-Type': 'application/json'})).respond(200, {test: {id: 123, xyz: '123'}});
131
+
132
+ Test = factory(config);
133
+ var test = new Test();
134
+ test.xyz = '123';
135
+ test.create();
136
+
137
+ $httpBackend.flush();
138
+ }));
139
+
140
+ it('create should allow custom Accept', inject(function($httpBackend) {
141
+ var Test;
142
+
143
+ $httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'text/plain'})).respond(200, {test: {id: 123, xyz: '123'}});
144
+
145
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
146
+ var test = new Test();
147
+ test.xyz = '123';
148
+ test.create();
149
+
150
+ $httpBackend.flush();
151
+ }));
152
+
153
+ it('create should allow custom header', inject(function($httpBackend) {
154
+ var Test;
155
+
156
+ $httpBackend.expectPOST('/test', {test: {xyz: '123'}}, headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {id: 123, xyz: '123'}});
157
+
158
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
159
+ var test = new Test();
160
+ test.xyz = '123';
161
+ test.create();
162
+
163
+ $httpBackend.flush();
164
+ }));
165
+
166
+ it('update should pass default $http options', inject(function($httpBackend) {
167
+ var Test;
168
+
169
+ $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'application/json', 'Content-Type': 'application/json'})).respond(200, {test: {id: 123, xyz: '123'}});
170
+
171
+ Test = factory(config);
172
+ var test = new Test();
173
+ test.id = 123;
174
+ test.xyz = '123';
175
+ test.update();
176
+
177
+ $httpBackend.flush();
178
+ }));
179
+
180
+ it('update should allow custom Accept', inject(function($httpBackend) {
181
+ var Test;
182
+
183
+ $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'text/plain'})).respond(200, {test: {id: 123, xyz: '123'}});
184
+
185
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'Accept': 'text/plain'}}}));
186
+ var test = new Test();
187
+ test.id = 123;
188
+ test.xyz = '123';
189
+ test.update();
190
+
191
+ $httpBackend.flush();
192
+ }));
193
+
194
+ it('update should allow custom header', inject(function($httpBackend) {
195
+ var Test;
196
+
197
+ $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'application/json', 'X-Test': 'test'})).respond(200, {test: {id: 123, xyz: '123'}});
198
+
199
+ Test = factory(angular.extend(angular.copy(config), {httpConfig: {headers: {'X-Test': 'test'}}}));
200
+ var test = new Test();
201
+ test.id = 123;
202
+ test.xyz = '123';
203
+ test.update();
204
+
205
+ $httpBackend.flush();
206
+ }));
207
+
208
+ it('$patch should pass default $http options', inject(function($httpBackend) {
209
+ var Test;
210
+
211
+ $httpBackend.expectPATCH('/test/123', {test: {id: 123, xyz: '123'}}, headerComparison({'Accept': 'application/json', 'Content-Type': 'application/json'})).respond(200, {test: {id: 123, xyz: '123'}});
212
+
213
+ Test = factory(config);
214
+ Test.$patch('/test/123', {id: 123, xyz: '123'});
215
+
216
+ $httpBackend.flush();
217
+ }));
218
+
219
+ });
@@ -0,0 +1,327 @@
1
+ describe("nested urls", function () {
2
+ 'use strict';
3
+
4
+ beforeEach(module('rails'));
5
+
6
+ var $httpBackend, $rootScope, factory, NestedTest,
7
+ nestedConfig = {
8
+ url: '/nested/{{nestedId}}/test/{{id}}',
9
+ name: 'nestedTest'
10
+ };
11
+
12
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) {
13
+ $httpBackend = _$httpBackend_;
14
+ $rootScope = _$rootScope_;
15
+ factory = railsResourceFactory;
16
+ NestedTest = railsResourceFactory(nestedConfig);
17
+ }));
18
+
19
+ afterEach(function() {
20
+ $httpBackend.verifyNoOutstandingExpectation();
21
+ $httpBackend.verifyNoOutstandingRequest();
22
+ });
23
+
24
+ it('query should return resource object when response is single object', inject(function($httpBackend) {
25
+ var promise, result;
26
+
27
+ $httpBackend.expectGET('/nested/1234/test').respond(200, {nested_test: {abc: 'xyz'}});
28
+
29
+ expect(promise = NestedTest.query(null, {nestedId: 1234})).toBeDefined();
30
+
31
+ promise.then(function (response) {
32
+ result = response;
33
+ });
34
+
35
+ $httpBackend.flush();
36
+
37
+ expect(result).toBeInstanceOf(NestedTest);
38
+ expect(result).toEqualData({abc: 'xyz'});
39
+ }));
40
+
41
+ it('query should return no data on 204', inject(function($httpBackend) {
42
+ var promise, result;
43
+
44
+ $httpBackend.expectGET('/nested/1234/test').respond(204);
45
+ expect(promise = NestedTest.query(null, {nestedId: 1234})).toBeDefined();
46
+
47
+ promise.then(function (response) {
48
+ result = response;
49
+ });
50
+
51
+ $httpBackend.flush();
52
+
53
+ expect(result).toBeUndefined();
54
+ }));
55
+
56
+ it('query should add parameter abc=1', inject(function($httpBackend) {
57
+ var promise;
58
+
59
+ $httpBackend.expectGET('/nested/1234/test?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
60
+
61
+ expect(promise = NestedTest.query({abc: '1'}, {nestedId: 1234})).toBeDefined();
62
+ $httpBackend.flush();
63
+ }));
64
+
65
+ it('query should add parameters abc=1 & xyz=2', inject(function($httpBackend) {
66
+ var promise;
67
+
68
+ $httpBackend.expectGET('/nested/1234/test?abc=1&xyz=2').respond(200, {nested_test: {abc: 'xyz'}});
69
+
70
+ expect(promise = NestedTest.query({abc: '1', xyz: 2}, {nestedId: 1234})).toBeDefined();
71
+ $httpBackend.flush();
72
+ }));
73
+
74
+ it('query with default params should add parameter abc=1', inject(function($httpBackend) {
75
+ var promise, resource, defaultParamsConfig = {};
76
+
77
+ $httpBackend.expectGET('/nested/1234/test?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
78
+
79
+ angular.copy(nestedConfig, defaultParamsConfig);
80
+ defaultParamsConfig.defaultParams = {abc: '1'};
81
+
82
+ resource = factory(defaultParamsConfig);
83
+ expect(promise = resource.query(null, {nestedId: 1234})).toBeDefined();
84
+
85
+ $httpBackend.flush();
86
+ }));
87
+
88
+ it('get should return resource object when response is 200', inject(function($httpBackend) {
89
+ var promise, result;
90
+
91
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, abc: 'xyz'}});
92
+
93
+ expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
94
+
95
+ promise.then(function (response) {
96
+ result = response;
97
+ });
98
+
99
+ $httpBackend.flush();
100
+
101
+ expect(result).toBeInstanceOf(NestedTest);
102
+ expect(result).toEqualData({id: 123, abc: 'xyz'});
103
+ }));
104
+
105
+ it('get should call failure callback when 404', inject(function($httpBackend) {
106
+ var promise, success = false, failure = false;
107
+
108
+ $httpBackend.expectGET('/nested/1234/test/123').respond(404);
109
+
110
+ expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
111
+
112
+ promise.then(function () {
113
+ success = true;
114
+ }, function () {
115
+ failure = true;
116
+ });
117
+
118
+ $httpBackend.flush();
119
+
120
+ expect(success).toBe(false);
121
+ expect(failure).toBe(true);
122
+ }));
123
+
124
+ it('get with default params should add parameter abc=1', inject(function($httpBackend) {
125
+ var promise, resource, defaultParamsConfig = {};
126
+
127
+ $httpBackend.expectGET('/nested/1234/test/123?abc=1').respond(200, {nested_test: {abc: 'xyz'}});
128
+
129
+ angular.copy(nestedConfig, defaultParamsConfig);
130
+ defaultParamsConfig.defaultParams = {abc: '1'};
131
+
132
+ resource = factory(defaultParamsConfig);
133
+ expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
134
+
135
+ $httpBackend.flush();
136
+ }));
137
+
138
+ it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) {
139
+ var promise, result, resource;
140
+
141
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
142
+
143
+ resource = factory(nestedConfig);
144
+ resource.responseInterceptors = [];
145
+ resource.requestTransformers = [];
146
+ expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
147
+
148
+ promise.then(function (response) {
149
+ result = response;
150
+ });
151
+
152
+ $httpBackend.flush();
153
+
154
+ expect(result).toBeInstanceOf(resource);
155
+ expect(result).toEqualData({id: 123, nested_id: 1234, abc_def: 'xyz'});
156
+ }));
157
+
158
+ it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) {
159
+ var promise, result, resource, testConfig = {};
160
+
161
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {id: 123, nested_id: 1234, abc_def: 'xyz'});
162
+
163
+ angular.copy(nestedConfig, testConfig);
164
+ testConfig.requestTransformers = [];
165
+ testConfig.responseInterceptors = ['railsFieldRenamingInterceptor'];
166
+ resource = factory(testConfig);
167
+
168
+ expect(promise = resource.get({nestedId: 1234, id: 123})).toBeDefined();
169
+
170
+ promise.then(function (response) {
171
+ result = response;
172
+ });
173
+
174
+ $httpBackend.flush();
175
+
176
+ expect(result).toBeInstanceOf(resource);
177
+ expect(result).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
178
+ }));
179
+
180
+ it('should be able to create new instance and save it', inject(function($httpBackend) {
181
+ var data = new NestedTest({nestedId: 1234, abcDef: 'xyz'});
182
+
183
+ $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'}});
184
+ data.nestedId = 1234;
185
+ data.create();
186
+ $httpBackend.flush();
187
+
188
+ expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
189
+ }));
190
+
191
+ it('should be able to create new instance and update it', inject(function($httpBackend) {
192
+ var data = new NestedTest({abcDef: 'xyz'});
193
+
194
+ $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'}});
195
+ data.nestedId = 1234;
196
+ data.create();
197
+ $httpBackend.flush(1);
198
+
199
+ expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
200
+
201
+ $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'}});
202
+ data.xyz = 'abc';
203
+ data.update();
204
+ $httpBackend.flush();
205
+
206
+ expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz', xyz: 'abc', extra: 'test'});
207
+ }));
208
+
209
+ it('create with default params should add parameter abc=1', inject(function($httpBackend) {
210
+ var promise, Resource, data, defaultParamsConfig = {};
211
+
212
+ $httpBackend.expectPOST('/nested/1234/test?abc=1', {nested_test: {nested_id: 1234}}).respond(200, {nested_test: {id: 123, nested_id: 1234}});
213
+
214
+ angular.copy(nestedConfig, defaultParamsConfig);
215
+ defaultParamsConfig.defaultParams = {abc: '1'};
216
+
217
+ Resource = factory(defaultParamsConfig);
218
+ data = new Resource();
219
+ data.nestedId = 1234;
220
+ data.create();
221
+
222
+ $httpBackend.flush();
223
+ }));
224
+
225
+ it('should be able to get resource and update it', inject(function($httpBackend) {
226
+ var promise, result;
227
+
228
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nested_id: 1234, abc: 'xyz'}});
229
+
230
+ expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
231
+
232
+ promise.then(function (response) {
233
+ result = response;
234
+ });
235
+
236
+ $httpBackend.flush();
237
+
238
+ expect(result).toBeInstanceOf(NestedTest);
239
+ expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
240
+
241
+ $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'}});
242
+ result.xyz = 'abc';
243
+ result.update();
244
+ $httpBackend.flush();
245
+
246
+ // abc was originally set on the object so it should still be there after the update
247
+ expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz', abcDef: 'xyz', xyz: 'abc', extra: 'test'});
248
+ }));
249
+
250
+ it('update should handle 204 response', inject(function($httpBackend) {
251
+ var promise, result;
252
+
253
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nested_id: 1234, abc: 'xyz'}});
254
+
255
+ expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
256
+
257
+ promise.then(function (response) {
258
+ result = response;
259
+ });
260
+
261
+ $httpBackend.flush();
262
+
263
+ expect(result).toBeInstanceOf(NestedTest);
264
+ expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
265
+
266
+ $httpBackend.expectPUT('/nested/1234/test/123', {nested_test: {id: 123, abc: 'xyz', xyz: 'abc', nested_id: 1234}}).respond(204);
267
+ result.xyz = 'abc';
268
+ result.update();
269
+ $httpBackend.flush();
270
+
271
+ expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz', xyz: 'abc'});
272
+ }));
273
+
274
+ it('should be able to delete instance returned from get', inject(function($httpBackend) {
275
+ var promise, result;
276
+
277
+ $httpBackend.expectGET('/nested/1234/test/123').respond(200, {nested_test: {id: 123, nestedId: 1234, abc: 'xyz'}});
278
+
279
+ expect(promise = NestedTest.get({nestedId: 1234, id: 123})).toBeDefined();
280
+
281
+ promise.then(function (response) {
282
+ result = response;
283
+ });
284
+
285
+ $httpBackend.flush();
286
+
287
+ expect(result).toBeInstanceOf(NestedTest);
288
+ expect(result).toEqualData({id: 123, nestedId: 1234, abc: 'xyz'});
289
+
290
+ $httpBackend.expectDELETE('/nested/1234/test/123').respond(204);
291
+ result.remove();
292
+ $httpBackend.flush();
293
+ }));
294
+
295
+ it('should be able to create new instance and update it', inject(function($httpBackend) {
296
+ var data = new NestedTest({abcDef: 'xyz'});
297
+
298
+ $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'}});
299
+ data.nestedId = 1234;
300
+ data.create();
301
+ $httpBackend.flush(1);
302
+
303
+ expect(data).toEqualData({id: 123, nestedId: 1234, abcDef: 'xyz'});
304
+ expect(data).toBeInstanceOf(NestedTest);
305
+
306
+ $httpBackend.expectDELETE('/nested/1234/test/123').respond(204);
307
+ data.remove();
308
+ $httpBackend.flush();
309
+ }));
310
+
311
+ it('delete with default params should add parameter abc=1', inject(function($httpBackend) {
312
+ var Resource, data, defaultParamsConfig = {};
313
+
314
+ $httpBackend.expectDELETE('/nested/1234/test/123?abc=1').respond(204);
315
+
316
+ angular.copy(nestedConfig, defaultParamsConfig);
317
+ defaultParamsConfig.defaultParams = {abc: '1'};
318
+
319
+ Resource = factory(defaultParamsConfig);
320
+ data = new Resource();
321
+ data.id = 123;
322
+ data.nestedId = 1234;
323
+ data.remove();
324
+
325
+ $httpBackend.flush();
326
+ }));
327
+ });