Skip to content

Commit 2c4bf70

Browse files
authored
Merge pull request #867 from orbitjs/fix-jsonapi
Ensure JSONAPISource query and update return arrays to match array requests
2 parents 22674a0 + 89c2ab1 commit 2c4bf70

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

packages/@orbit/jsonapi/src/jsonapi-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export class JSONAPISource<
363363
}
364364

365365
return {
366-
data: responses.length > 1 ? data : data[0],
366+
data: Array.isArray(query.expressions) ? data : data[0],
367367
details,
368368
transforms
369369
};
@@ -398,7 +398,7 @@ export class JSONAPISource<
398398
}
399399

400400
return {
401-
data: responses.length > 1 ? data : data[0],
401+
data: Array.isArray(transform.operations) ? data : data[0],
402402
details,
403403
transforms: [transform, ...transforms]
404404
};

packages/@orbit/jsonapi/test/jsonapi-source-queryable-test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,5 +2051,38 @@ module('JSONAPISource - queryable', function (hooks) {
20512051

20522052
assert.equal(fetchStub.callCount, 1, 'fetch called once');
20532053
});
2054+
2055+
test('#query - will return an array of results for a query that contains an array with a single expression', async function (assert) {
2056+
assert.expect(4);
2057+
2058+
const data1: Resource = {
2059+
type: 'planet',
2060+
id: '12345',
2061+
attributes: { name: 'Jupiter' }
2062+
};
2063+
2064+
const planet1 = resourceSerializer.deserialize({
2065+
type: 'planet',
2066+
id: '12345'
2067+
}) as InitializedRecord;
2068+
2069+
fetchStub
2070+
.withArgs('/planets/12345')
2071+
.returns(jsonapiResponse(200, { data: data1 }));
2072+
2073+
let records = (await source.query((q) => [
2074+
q.findRecord({ type: 'planet', id: planet1.id })
2075+
])) as InitializedRecord[];
2076+
2077+
assert.ok(Array.isArray(records), 'multiple primary records returned');
2078+
assert.equal(records[0].attributes?.name, 'Jupiter');
2079+
2080+
assert.equal(fetchStub.callCount, 1, 'fetch called once');
2081+
assert.equal(
2082+
fetchStub.getCall(0).args[1].method,
2083+
undefined,
2084+
'fetch called with no method (equivalent to GET)'
2085+
);
2086+
});
20542087
});
20552088
});

packages/@orbit/jsonapi/test/jsonapi-source-updatable-test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,5 +1217,48 @@ module('JSONAPISource - updatable', function (hooks) {
12171217
'fetch called with expected data'
12181218
);
12191219
});
1220+
1221+
test('#update - will return an array of results for a transform that contains a single operation', async function (assert) {
1222+
assert.expect(5);
1223+
1224+
const planet1: InitializedRecord = {
1225+
type: 'planet',
1226+
id: 'p1',
1227+
attributes: { name: 'Jupiter' }
1228+
};
1229+
1230+
fetchStub.withArgs('/planets').callsFake(() =>
1231+
jsonapiResponse(201, {
1232+
data: planet1
1233+
})
1234+
);
1235+
1236+
let [planet] = (await source.update((t) => [
1237+
t.addRecord(planet1)
1238+
])) as InitializedRecord[];
1239+
1240+
assert.deepEqual(planet, planet1, 'planet matches');
1241+
1242+
assert.equal(fetchStub.callCount, 1, 'fetch called once');
1243+
1244+
const firstFetchCall = fetchStub.getCall(0);
1245+
assert.equal(
1246+
firstFetchCall.args[1].method,
1247+
'POST',
1248+
'fetch called with expected method'
1249+
);
1250+
assert.equal(
1251+
firstFetchCall.args[1].headers['Content-Type'],
1252+
'application/vnd.api+json',
1253+
'fetch called with expected content type'
1254+
);
1255+
assert.deepEqual(
1256+
JSON.parse(firstFetchCall.args[1].body),
1257+
{
1258+
data: planet1
1259+
},
1260+
'fetch called with expected data'
1261+
);
1262+
});
12201263
});
12211264
});

0 commit comments

Comments
 (0)