Skip to content

Commit

Permalink
JSONAPISource#update should always return an array of results when op…
Browse files Browse the repository at this point in the history
…erations is an array
  • Loading branch information
dgeb committed Jul 16, 2021
1 parent 21094b9 commit 89c2ab1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@orbit/jsonapi/src/jsonapi-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export class JSONAPISource<
}

return {
data: responses.length > 1 ? data : data[0],
data: Array.isArray(transform.operations) ? data : data[0],
details,
transforms: [transform, ...transforms]
};
Expand Down
43 changes: 43 additions & 0 deletions packages/@orbit/jsonapi/test/jsonapi-source-updatable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,5 +1217,48 @@ module('JSONAPISource - updatable', function (hooks) {
'fetch called with expected data'
);
});

test('#update - will return an array of results for a transform that contains a single operation', async function (assert) {
assert.expect(5);

const planet1: InitializedRecord = {
type: 'planet',
id: 'p1',
attributes: { name: 'Jupiter' }
};

fetchStub.withArgs('/planets').callsFake(() =>
jsonapiResponse(201, {
data: planet1
})
);

let [planet] = (await source.update((t) => [
t.addRecord(planet1)
])) as InitializedRecord[];

assert.deepEqual(planet, planet1, 'planet matches');

assert.equal(fetchStub.callCount, 1, 'fetch called once');

const firstFetchCall = fetchStub.getCall(0);
assert.equal(
firstFetchCall.args[1].method,
'POST',
'fetch called with expected method'
);
assert.equal(
firstFetchCall.args[1].headers['Content-Type'],
'application/vnd.api+json',
'fetch called with expected content type'
);
assert.deepEqual(
JSON.parse(firstFetchCall.args[1].body),
{
data: planet1
},
'fetch called with expected data'
);
});
});
});

0 comments on commit 89c2ab1

Please sign in to comment.