Skip to content

Commit b319898

Browse files
committed
Add options to patch
1 parent 5937b9f commit b319898

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Resource.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,21 @@ class Resource {
8383
.catch(extractErrorResponse);
8484
}
8585

86-
update(partialRecord) {
86+
update({ id, attributes, relationships, options }) {
8787
// http://jsonapi.org/faq/#wheres-put
88-
const record = Object.assign({}, partialRecord, { type: this.name });
88+
const record = { type: this.name, id };
89+
if (attributes) {
90+
record.attributes = attributes;
91+
}
92+
if (relationships) {
93+
record.relationships = relationships;
94+
}
8995
const requestData = { data: record };
9096
return this.api
91-
.patch(`${this.name}/${record.id}`, requestData)
97+
.patch(
98+
`${this.name}/${record.id}?${getOptionsQuery(options)}`,
99+
requestData,
100+
)
92101
.then(extractData)
93102
.catch(extractErrorResponse);
94103
}

test/Resource.spec.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,21 +273,38 @@ describe('Resource', () => {
273273
});
274274

275275
describe('update', () => {
276+
const id = '1';
277+
const attributes = { key: 'value' };
278+
const relationships = { key: 'value' };
279+
276280
it('can update a record', () => {
277-
const id = '1';
278-
const attributes = { key: 'value' };
279-
const relationships = { key: 'value' };
280281
const responseBody = { data: record };
281282
api.patch.mockResolvedValue({ data: responseBody });
282283

283284
const result = resource.update({ id, attributes, relationships });
284285

285-
expect(api.patch).toHaveBeenCalledWith('widgets/1', {
286+
expect(api.patch).toHaveBeenCalledWith('widgets/1?', {
286287
data: { id, type: 'widgets', attributes, relationships },
287288
});
288289
return expect(result).resolves.toEqual(responseBody);
289290
});
290291

292+
it('passes options', () => {
293+
const responseBody = { data: record };
294+
api.patch.mockResolvedValue({ data: responseBody });
295+
296+
const result = resource.update({
297+
id,
298+
attributes,
299+
relationships,
300+
options: optionsWithInclude,
301+
});
302+
303+
expect(api.patch).toHaveBeenCalledWith('widgets/1?include=comments', {
304+
data: { id, type: 'widgets', attributes, relationships },
305+
});
306+
});
307+
291308
it('rejects with the response upon error', () => {
292309
const errorResponse = { dummy: 'data' };
293310
api.patch.mockRejectedValue({ response: errorResponse });

0 commit comments

Comments
 (0)