Skip to content

Commit b75d842

Browse files
Merge pull request #1 from CodingItWrong/patch-options
Patch options
2 parents 295ae84 + b319898 commit b75d842

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"author": "Josh Justice <[email protected]>",
88
"license": "Apache-2.0",
99
"scripts": {
10-
"test": "jest",
10+
"test": "jest --watchAll",
1111
"lint": "eslint \"{src,test}/*.js\"",
1212
"format": "prettier --write \"{src,test}/*.js\"",
1313
"docs:dev": "vuepress dev docs",

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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +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 partialRecord = { id: '1', attributes: { key: 'value' } };
278281
const responseBody = { data: record };
279282
api.patch.mockResolvedValue({ data: responseBody });
280283

281-
const result = resource.update(partialRecord);
284+
const result = resource.update({ id, attributes, relationships });
282285

283-
expect(api.patch).toHaveBeenCalledWith('widgets/1', {
284-
data: { ...partialRecord, type: 'widgets' },
286+
expect(api.patch).toHaveBeenCalledWith('widgets/1?', {
287+
data: { id, type: 'widgets', attributes, relationships },
285288
});
286289
return expect(result).resolves.toEqual(responseBody);
287290
});
288291

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+
289308
it('rejects with the response upon error', () => {
290309
const errorResponse = { dummy: 'data' };
291310
api.patch.mockRejectedValue({ response: errorResponse });

0 commit comments

Comments
 (0)