Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2a4c326

Browse files
jplhomerrichmolj
authored andcommittedJan 10, 2018
Ensure relationship keys are de-camelized in write payload (#40)
* Ensure relationship payloads are decamelized strings * Clean up test * Use object instead of boolean
1 parent fe19513 commit 2a4c326

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed
 

‎src/util/string.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ const camelize = function(str) {
66
return str.replace(/(\_[a-z])/g, function($1){return $1.toUpperCase().replace('_','');});
77
}
88

9-
export { underscore, camelize };
9+
const decamelize = function(str) {
10+
return str.replace(/([A-Z])/g, function($1){return $1.replace($1,'_' + $1).toLowerCase();});
11+
}
12+
13+
export { underscore, camelize, decamelize };

‎src/util/write-payload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Model from '../model';
22
import IncludeDirective from './include-directive';
33
import * as _snakeCase from './snakecase';
44
import tempId from './temp-id';
5+
import { decamelize } from './string';
56
let snakeCase: any = (<any>_snakeCase).default || _snakeCase;
67
snakeCase = snakeCase['default'] || snakeCase;
78

@@ -87,7 +88,7 @@ export default class WritePayload {
8788
}
8889

8990
if (data) {
90-
_relationships[key] = { data }
91+
_relationships[decamelize(key)] = { data }
9192
}
9293
}
9394
});

‎test/integration/nested-persistence-test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ let expectedCreatePayload = {
4242
method: 'create'
4343
}
4444
]
45+
},
46+
special_books: {
47+
data: [
48+
{
49+
['temp-id']: 'abc3',
50+
type: 'books',
51+
method: 'create'
52+
}
53+
]
4554
}
4655
}
4756
},
@@ -68,6 +77,13 @@ let expectedCreatePayload = {
6877
attributes: {
6978
name: 'Horror'
7079
}
80+
},
81+
{
82+
['temp-id']: 'abc3',
83+
type: 'books',
84+
attributes: {
85+
title: 'The Stand'
86+
}
7187
}
7288
]
7389
};
@@ -122,8 +138,11 @@ const seedPersistedData = function() {
122138
genre.isPersisted(true);
123139
let book = new Book({ id: '10', title: 'The Shining', genre: genre });
124140
book.isPersisted(true);
141+
let specialBook = new Book({ id: '30', title: 'The Stand' });
142+
specialBook.isPersisted(true);
125143
instance.id = '1';
126144
instance.books = [book];
145+
instance.specialBooks = [specialBook];
127146
instance.isPersisted(true);
128147
genre.name = 'Updated Genre Name';
129148
book.title = 'Updated Book Title';
@@ -169,6 +188,12 @@ describe('nested persistence', function() {
169188
id: '20',
170189
type: 'genres',
171190
attributes: { name: 'name from server' }
191+
},
192+
{
193+
['temp-id']: 'abc3',
194+
id: '30',
195+
type: 'books',
196+
attributes: { title: 'another title from server' }
172197
}
173198
]
174199
}
@@ -199,14 +224,16 @@ describe('nested persistence', function() {
199224
beforeEach(function() {
200225
let genre = new Genre({ name: 'Horror' });
201226
let book = new Book({ title: 'The Shining', genre: genre });
227+
let specialBook = new Book({ title: 'The Stand' });
202228
instance.books = [book];
229+
instance.specialBooks = [specialBook];
203230
});
204231

205232
// todo test on the way back - id set, attrs updated, isPersisted
206233
// todo remove #destroy? and just save when markwithpersisted? combo? for ombined payload
207234
// todo test unique includes/circular relationshio
208235
it('sends the correct payload', function(done) {
209-
instance.save({ with: { books: 'genre' } }).then((response) => {
236+
instance.save({ with: { books: 'genre', specialBooks: {} } }).then((response) => {
210237
expect(payloads[0]).to.deep.equal(expectedCreatePayload);
211238
done();
212239
});

0 commit comments

Comments
 (0)
Please sign in to comment.