angularjs-rails-resource 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,333 @@
1
+ describe('railsUrlSerializer', function () {
2
+ 'use strict';
3
+ var factory, railsInjector;
4
+
5
+ function createSerializer(options, customizer) {
6
+ return railsInjector.createService(factory(options, customizer));
7
+ }
8
+
9
+ beforeEach(module('rails'));
10
+
11
+ beforeEach(inject(function (railsSerializer, RailsResourceInjector) {
12
+ factory = railsSerializer;
13
+ railsInjector = RailsResourceInjector;
14
+ }));
15
+
16
+ it('should support customizer being first parameter', function () {
17
+ var called = false;
18
+
19
+ createSerializer(function () {
20
+ called = true;
21
+ });
22
+
23
+ expect(called).toBeTruthy();
24
+ });
25
+
26
+ it('should support customizer with options as first parameter', function () {
27
+ var called = false;
28
+
29
+ createSerializer({}, function () {
30
+ called = true;
31
+ });
32
+
33
+ expect(called).toBeTruthy();
34
+
35
+ });
36
+
37
+ describe('default config', function () {
38
+ var serializer;
39
+
40
+ beforeEach(function () {
41
+ serializer = createSerializer();
42
+ });
43
+
44
+ it('should underscore attributes on single object', function () {
45
+ var orig = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'},
46
+ result = serializer.serialize(orig);
47
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'});
48
+ result = serializer.deserialize(result);
49
+ expect(result).toEqualData(orig);
50
+ });
51
+
52
+ it('should underscore attributes on nested objects', function () {
53
+ var orig = {id: 6, title: 'Winds of Winter', pages: 1105, publicationDate: '2020-05-25', author: {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'}},
54
+ result = serializer.serialize(orig);
55
+ expect(result).toEqualData({id: 6, title: 'Winds of Winter', pages: 1105, publication_date: '2020-05-25', author: {id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'}});
56
+ result = serializer.deserialize(result);
57
+ expect(result).toEqualData(orig);
58
+ });
59
+
60
+ it('should underscore attribute inside array objects', function () {
61
+ var orig = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', books: [
62
+ {id: 1, title: 'A Game of Thrones', publicationDate: '1996-08-06'}
63
+ ]},
64
+ result = serializer.serialize(orig);
65
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', books: [
66
+ {id: 1, title: 'A Game of Thrones', publication_date: '1996-08-06'}
67
+ ]});
68
+ result = serializer.deserialize(result);
69
+ expect(result).toEqualData(orig);
70
+ });
71
+
72
+ it('should support primitive arrays', function () {
73
+ var orig = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', books: [1, 2, 3]},
74
+ result = serializer.serialize(orig);
75
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', books: [1, 2, 3]});
76
+ result = serializer.deserialize(result);
77
+ expect(result).toEqualData(orig);
78
+ });
79
+
80
+ it('should exclude attributes that start with $', function () {
81
+ var result = serializer.serialize({id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'});
82
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'});
83
+ result = serializer.deserialize({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'});
84
+ expect(result).toEqualData({id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'});
85
+ });
86
+
87
+ it('should exclude functions', function () {
88
+ var result = serializer.serialize({id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $books: [], getNumBooks: function () {
89
+ this.$books.length
90
+ }});
91
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'});
92
+ });
93
+
94
+ });
95
+
96
+ describe('custom options', function () {
97
+ it('should allow overriding attribute transformation functions with undefined', function () {
98
+ var test,
99
+ serializer = createSerializer({underscore: undefined, camelize: undefined});
100
+
101
+ test = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'};
102
+ expect(serializer.serialize(test)).toEqualData(test);
103
+ test = {id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'};
104
+ expect(serializer.deserialize(test)).toEqualData(test);
105
+ });
106
+
107
+ it('should allow overriding attribute transformation with custom function', function () {
108
+ var result,
109
+ serializer = createSerializer({
110
+ underscore: function (attribute) {
111
+ return 'x' + attribute
112
+ },
113
+ camelize: function (attribute) {
114
+ return 'y' + attribute
115
+ }
116
+ });
117
+
118
+ result = serializer.serialize({id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'});
119
+ expect(result).toEqualData({xid: 1, xfirstName: 'George', xmiddleName: 'R. R.', xlastName: 'Martin'});
120
+ result = serializer.deserialize({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'});
121
+ expect(result).toEqualData({yid: 1, yfirst_name: 'George', ymiddle_name: 'R. R.', ylast_name: 'Martin'});
122
+ });
123
+
124
+ it('should allow safely ignore null excludePrefixes', function () {
125
+ var result, underscored, camelized,
126
+ serializer = createSerializer({exclusionMatchers: null});
127
+
128
+ camelized = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'};
129
+ underscored = {id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'};
130
+ result = serializer.serialize(camelized);
131
+ expect(result).toEqualData(underscored);
132
+ result = serializer.deserialize(underscored);
133
+ expect(result).toEqualData(camelized);
134
+ });
135
+
136
+ it('should allow safely ignore undefined excludePrefixes', function () {
137
+ var result, underscored, camelized,
138
+ serializer = createSerializer({exclusionMatchers: undefined});
139
+
140
+ camelized = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'};
141
+ underscored = {id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'};
142
+ result = serializer.serialize(camelized);
143
+ expect(result).toEqualData(underscored);
144
+ result = serializer.deserialize(underscored);
145
+ expect(result).toEqualData(camelized);
146
+ });
147
+
148
+ it('should allow empty excludePrefixes', function () {
149
+ var result, underscored, camelized,
150
+ serializer = createSerializer({exclusionMatchers: []});
151
+
152
+ camelized = {id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'};
153
+ underscored = {id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'};
154
+ result = serializer.serialize(camelized);
155
+ expect(result).toEqualData(underscored);
156
+ result = serializer.deserialize(underscored);
157
+ expect(result).toEqualData(camelized);
158
+ });
159
+
160
+ it('should treat exclusionMatcher strings as prefix exclusions', function () {
161
+ var result,
162
+ serializer = createSerializer({exclusionMatchers: ['x']});
163
+
164
+ result = serializer.serialize({xid: 1, firstNamex: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'});
165
+ expect(result).toEqualData({first_namex: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'});
166
+ result = serializer.deserialize({xid: 1, first_namex: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'});
167
+ expect(result).toEqualData({xid: 1, firstNamex: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'});
168
+ });
169
+
170
+ it('should use combination of string prefix, function, and regexp for exclusions', function () {
171
+ var result,
172
+ serializer = createSerializer({exclusionMatchers: ['x', /^$/, function (key) {
173
+ return key === 'middleName';
174
+ }]});
175
+
176
+ result = serializer.serialize({xid: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birthDate: '1948-09-20'});
177
+ expect(result).toEqualData({first_name: 'George', last_name: 'Martin'});
178
+ result = serializer.deserialize({xid: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', $birth_date: '1948-09-20'});
179
+ expect(result).toEqualData({xid: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin', $birth_date: '1948-09-20'});
180
+ });
181
+ });
182
+
183
+ describe('customized serialization', function () {
184
+ var camelizedAuthor = {
185
+ id: 1,
186
+ firstName: 'George',
187
+ middleName: 'R. R.',
188
+ lastName: 'Martin',
189
+ birthDate: '1948-09-20',
190
+ books: [
191
+ {id: 1, title: 'A Game of Thrones', pages: 694, series: 'A Song of Ice and Fire', publicationDate: '1996-08-06', authorId: 1},
192
+ {id: 2, title: 'A Clash of Kings', pages: 768, series: 'A Song of Ice and Fire', publicationDate: '1999-03-01', authorId: 1},
193
+ ]
194
+ },
195
+ underscoredAuthor = {
196
+ id: 1,
197
+ first_name: 'George',
198
+ middle_name: 'R. R.',
199
+ last_name: 'Martin',
200
+ birth_date: '1948-09-20',
201
+ books: [
202
+ {id: 1, title: 'A Game of Thrones', pages: 694, series: 'A Song of Ice and Fire', publication_date: '1996-08-06', author_id: 1},
203
+ {id: 2, title: 'A Clash of Kings', pages: 768, series: 'A Song of Ice and Fire', publication_date: '1999-03-01', author_id: 1},
204
+ ]
205
+ };
206
+
207
+ it('should allow single exclusion', function () {
208
+ var result,
209
+ serializer = createSerializer(function (config) {
210
+ config.exclude('books');
211
+ });
212
+
213
+ result = serializer.serialize(camelizedAuthor);
214
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin', birth_date: '1948-09-20'});
215
+ result = serializer.deserialize(underscoredAuthor);
216
+ expect(result).toEqualData(camelizedAuthor);
217
+ });
218
+
219
+ it('should allow variable exclusions', function () {
220
+ var result,
221
+ serializer = createSerializer(function (config) {
222
+ config.exclude('books', 'birthDate');
223
+ });
224
+
225
+ result = serializer.serialize(camelizedAuthor);
226
+ expect(result).toEqualData({id: 1, first_name: 'George', middle_name: 'R. R.', last_name: 'Martin'});
227
+ result = serializer.deserialize(underscoredAuthor);
228
+ expect(result).toEqualData(camelizedAuthor);
229
+ });
230
+
231
+ it('should allow renaming attributes', function () {
232
+ var result,
233
+ serializer = createSerializer(function (config) {
234
+ // this & config should be interchangeable
235
+ this.exclude('books', 'birthDate');
236
+ this.rename('id', 'authorId');
237
+ this.rename('firstName', 'first');
238
+ this.rename('middleName', 'middle');
239
+ config.rename('lastName', 'last');
240
+ });
241
+
242
+ result = serializer.serialize(camelizedAuthor);
243
+ expect(result).toEqualData({author_id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
244
+ result = serializer.deserialize({author_id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
245
+ expect(result).toEqualData({id: 1, firstName: 'George', middleName: 'R. R.', lastName: 'Martin'});
246
+ });
247
+
248
+ it('should allow nested attributes', function () {
249
+ var result,
250
+ serializer = createSerializer(function () {
251
+ this.nestedAttribute('books')
252
+ });
253
+
254
+ result = serializer.serialize(camelizedAuthor);
255
+ expect(result['books_attributes']).toEqualData([
256
+ {id: 1, title: 'A Game of Thrones', pages: 694, series: 'A Song of Ice and Fire', publication_date: '1996-08-06', author_id: 1},
257
+ {id: 2, title: 'A Clash of Kings', pages: 768, series: 'A Song of Ice and Fire', publication_date: '1999-03-01', author_id: 1}
258
+ ]);
259
+ result = serializer.deserialize(underscoredAuthor);
260
+ expect(result).toEqualData(camelizedAuthor);
261
+ });
262
+
263
+ it('should add custom attribute from function', function () {
264
+ var result,
265
+ serializer = createSerializer(function () {
266
+ this.add('numBooks', function (author) {
267
+ return author.books.length;
268
+ });
269
+ });
270
+
271
+ result = serializer.serialize(camelizedAuthor);
272
+ expect(result['num_books']).toBe(2);
273
+ });
274
+
275
+ it('should add custom attribute from constant value', function () {
276
+ var result,
277
+ serializer = createSerializer(function () {
278
+ this.add('numBooks', 2);
279
+ });
280
+
281
+ result = serializer.serialize(camelizedAuthor);
282
+ expect(result['num_books']).toBe(2);
283
+ });
284
+
285
+ it('should use custom serializer for books', function () {
286
+ var result, serializedBooks, underscored,
287
+ serializer = createSerializer(function () {
288
+ this.serializeWith('books', factory(function () {
289
+ this.rename('publicationDate', 'published');
290
+ }));
291
+ });
292
+
293
+ result = serializer.serialize(camelizedAuthor);
294
+ serializedBooks = [
295
+ {id: 1, title: 'A Game of Thrones', pages: 694, series: 'A Song of Ice and Fire', published: '1996-08-06', author_id: 1},
296
+ {id: 2, title: 'A Clash of Kings', pages: 768, series: 'A Song of Ice and Fire', published: '1999-03-01', author_id: 1}
297
+ ];
298
+
299
+ expect(result['books']).toEqualData(serializedBooks);
300
+ underscored = angular.copy(underscoredAuthor);
301
+ underscored['books'] = serializedBooks;
302
+ result = serializer.deserialize(underscored);
303
+ expect(result).toEqualData(camelizedAuthor);
304
+ });
305
+ });
306
+
307
+ describe('default exclusion serialization', function () {
308
+ it('should only serialize id', function () {
309
+ var result,
310
+ serializer = createSerializer(function (config) {
311
+ // this & config should be interchangeable
312
+ this.only('id');
313
+ });
314
+
315
+ result = serializer.serialize({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
316
+ expect(result).toEqualData({id: 1});
317
+ result = serializer.deserialize({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
318
+ expect(result).toEqualData({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
319
+ });
320
+
321
+ it('should only serialize id and last', function () {
322
+ var result,
323
+ serializer = createSerializer(function (config) {
324
+ this.only('id', 'last');
325
+ });
326
+
327
+ result = serializer.serialize({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
328
+ expect(result).toEqualData({id: 1, last: 'Martin'});
329
+ result = serializer.deserialize({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
330
+ expect(result).toEqualData({id: 1, first: 'George', middle: 'R. R.', last: 'Martin'});
331
+ });
332
+ });
333
+ });
@@ -0,0 +1,96 @@
1
+ describe('transformers', function () {
2
+ 'use strict';
3
+ var $httpBackend, $rootScope, factory, Test, testTransformer,
4
+ config = {
5
+ url: '/test',
6
+ name: 'test'
7
+ };
8
+
9
+ beforeEach(function() {
10
+ module('rails');
11
+
12
+ angular.module('rails').factory('railsTestTransformer', function () {
13
+ return function (data, resource) {
14
+ data.transformer_called = true;
15
+ return data;
16
+ }
17
+ });
18
+ });
19
+
20
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestTransformer) {
21
+ $httpBackend = _$httpBackend_;
22
+ $rootScope = _$rootScope_;
23
+ factory = railsResourceFactory;
24
+ Test = railsResourceFactory(config);
25
+ testTransformer = railsTestTransformer;
26
+ }));
27
+
28
+ afterEach(function() {
29
+ $httpBackend.verifyNoOutstandingExpectation();
30
+ $httpBackend.verifyNoOutstandingRequest();
31
+ });
32
+
33
+
34
+ it('should be able to add transformer using name', function() {
35
+ var Resource, testConfig = {};
36
+
37
+ $httpBackend.expectPOST('/test/123', {test: {id: 123, transformer_called: true}}).respond(200, {id: 123, abc_def: 'xyz'});
38
+
39
+ angular.copy(config, testConfig);
40
+ testConfig.requestTransformers = ['railsTestTransformer'];
41
+ Resource = factory(testConfig);
42
+ new Resource({id: 123}).create();
43
+
44
+ $httpBackend.flush();
45
+ });
46
+
47
+ it('should be able to add transformer using reference', function() {
48
+ var Resource, testConfig = {};
49
+
50
+ $httpBackend.expectPOST('/test/123', {test: {id: 123, transformer_called: true}}).respond(200, {id: 123, abc_def: 'xyz'});
51
+
52
+ angular.copy(config, testConfig);
53
+ testConfig.requestTransformers = [testTransformer];
54
+ Resource = factory(testConfig);
55
+ new Resource({id: 123}).create();
56
+
57
+ $httpBackend.flush();
58
+ });
59
+
60
+ it('should call transformer function with beforeRequest', function () {
61
+ var Resource, transformerCalled = false;
62
+
63
+ $httpBackend.expectPOST('/test/123', {test: {id: 123}}).respond(200, {id: 123, abc_def: 'xyz'});
64
+
65
+ Resource = factory(config);
66
+ Resource.beforeRequest(function (data, constructor) {
67
+ expect(data).toEqualData({id: 123});
68
+ expect(constructor).toEqual(Resource);
69
+ transformerCalled = true;
70
+ });
71
+
72
+ new Resource({id: 123}).create();
73
+
74
+ $httpBackend.flush();
75
+ expect(transformerCalled).toBeTruthy();
76
+ });
77
+
78
+ it('should be able to return new data from beforeRequest function', function () {
79
+ var Resource, transformerCalled = false;
80
+
81
+ $httpBackend.expectPOST('/test/123', {test: {id: 1}}).respond(200, {id: 123, abc_def: 'xyz'});
82
+
83
+ Resource = factory(config);
84
+ Resource.beforeRequest(function (data, resource) {
85
+ expect(data).toEqualData({id: 123});
86
+ expect(resource).toEqual(Resource);
87
+ transformerCalled = true;
88
+ return {id: 1};
89
+ });
90
+
91
+ new Resource({id: 123}).create();
92
+
93
+ $httpBackend.flush();
94
+ expect(transformerCalled).toBeTruthy();
95
+ });
96
+ });
@@ -0,0 +1,40 @@
1
+ describe("railsUrlBuilder", function () {
2
+ 'use strict';
3
+
4
+ beforeEach(module('rails'));
5
+
6
+ it('should return custom function', inject(function (railsUrlBuilder) {
7
+ expect(railsUrlBuilder(function () { return 'test' })()).toEqualData('test')
8
+ }));
9
+
10
+ it('should return base url when no context object', inject(function (railsUrlBuilder) {
11
+ expect(railsUrlBuilder('/books')()).toEqualData('/books');
12
+ }));
13
+
14
+ it('should append id', inject(function (railsUrlBuilder) {
15
+ expect(railsUrlBuilder('/books')({id: 1})).toEqualData('/books/1');
16
+ }));
17
+
18
+ it('should use author id for book list', inject(function (railsUrlBuilder) {
19
+ expect(railsUrlBuilder('/authors/{{authorId}}/books/{{id}}')({authorId: 1})).toEqualData('/authors/1/books');
20
+ }));
21
+
22
+ it('should use author id and book id', inject(function (railsUrlBuilder) {
23
+ expect(railsUrlBuilder('/authors/{{authorId}}/books/{{id}}')({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
24
+ }));
25
+
26
+ describe('custom interpolation symbols', function() {
27
+ beforeEach(module(function($interpolateProvider) {
28
+ $interpolateProvider.startSymbol('--');
29
+ $interpolateProvider.endSymbol('--');
30
+ }));
31
+
32
+ it('should append id', inject(function (railsUrlBuilder) {
33
+ expect(railsUrlBuilder('/books')({id: 1})).toEqualData('/books/1');
34
+ }));
35
+
36
+ it('should use author id and book id', inject(function (railsUrlBuilder) {
37
+ expect(railsUrlBuilder('/authors/--authorId--/books/--id--')({authorId: 1, id: 2})).toEqualData('/authors/1/books/2');
38
+ }));
39
+ });
40
+ });