Skip to content

Commit

Permalink
Merge pull request #247 from aljachimiak/issues_245_246
Browse files Browse the repository at this point in the history
Issues #245 and #246
  • Loading branch information
kixxauth authored Oct 27, 2016
2 parents 76edd72 + 2437948 commit c96e101
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/middleware/response-json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ module.exports = function (options) {
// make a single object with all of the relationships of the requested entities
const toInclude = req.query.include.split(',');
let allRelationships = [];
const relationships = data.relationships || {};
toInclude.map(type => {
allRelationships = allRelationships.concat((data.relationships[type] || {}).data);
const typeData = (relationships[type] || {}).data;
allRelationships = allRelationships.concat(typeData || []);
return type;
});

Expand Down
18 changes: 18 additions & 0 deletions spec/fixtures/collections/collection-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "collection-2",
"type": "collection",
"channel": "channel-id",
"title": "This collection has no relationships on purpose",
"images": [
{
"url": "http://www.oddnetworks.com.s3.amazonaws.com/assets/img/hero-odd-logo.png",
"mimeType": "image/png",
"width": 422,
"height": 467,
"label": "logo"
}
],
"meta": {
"extra": "info"
}
}
71 changes: 69 additions & 2 deletions spec/middleware/response-json-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const jsonApiSchema = require('../helpers/json-api-schema.json');

const COLLECTION = require('../fixtures/collections/collection-0.json');
const COLLECTION_1 = require('../fixtures/collections/collection-1.json');
const COLLECTION_2 = require('../fixtures/collections/collection-2.json');
const REL_0 = require('../fixtures/videos/video-0.json');
const REL_1 = require('../fixtures/videos/video-1.json');
const REL_2 = require('../fixtures/videos/video-2.json');
Expand Down Expand Up @@ -90,7 +91,8 @@ describe('Middleware Response JSON API', function () {
bus.sendCommand({role, cmd, type: 'video'}, REL_B),
bus.sendCommand({role, cmd, type: 'video'}, REL_C),
bus.sendCommand({role, cmd, type: 'collection'}, COLLECTION),
bus.sendCommand({role, cmd, type: 'collection'}, COLLECTION_1)
bus.sendCommand({role, cmd, type: 'collection'}, COLLECTION_1),
bus.sendCommand({role, cmd, type: 'collection'}, COLLECTION_2)
]);
})
.then(_.noop)
Expand Down Expand Up @@ -261,6 +263,7 @@ describe('Middleware Response JSON API', function () {
it('has an included array', function () {
expect(Array.isArray(res.body.included)).toBe(true);
expect(res.body.included.length).toBe(3);
// console.log(JSON.stringify(res.body, null, 2));
});

it('has included resources with self links', function () {
Expand Down Expand Up @@ -454,7 +457,7 @@ describe('Middleware Response JSON API', function () {
type,
id: COLLECTION.id,
platform: 'platform-id',
include: ['entities']
include: ['entities', 'video']
};

return bus.query({role, cmd, type}, args).then(result => {
Expand Down Expand Up @@ -736,4 +739,68 @@ describe('Middleware Response JSON API', function () {
expect(Object.keys(data[1]).length).toBe(2);
});
});

describe('with a collection that has no relationships', function () {
const req = _.cloneDeep(REQ);
let res = null;
let middleware = null;

const role = 'store';
const cmd = 'get';
const type = 'collection';

const RESPONSES = {
NO_RELATIONSHIPS: null
};

beforeAll(function (done) {
req.query = {include: 'foobar'};
res = mockExpressResponse();
res.body = _.cloneDeep(COLLECTION_2);

middleware = responseJsonApi({bus});

const args = {
channel: 'channel-id',
type,
id: COLLECTION_2.id,
platform: 'platform-id',
include: ['foobar']
};

req.url = `/${type}s/${COLLECTION_2.id}?include=foobar`;

return Promise.resolve(null)
.then(() => {
return bus.query({role, cmd, type}, args).then(result => {
res.body = result;
return res;
});
})
.then(res => {
return middleware(req, res, err => {
if (err) {
return done.fail(err);
}
RESPONSES.NO_RELATIONSHIPS = res;
});
})
.then(_.noop)
.then(done)
.catch(done.fail);
});

it('returns a valid api response', function (done) {
const v = Validator.validate(RESPONSES.NO_RELATIONSHIPS.body, jsonApiSchema);
expect(v.valid).toBe(true);
done();
});

it('returns with the expected members in the data array', function (done) {
const data = (RESPONSES.NO_RELATIONSHIPS.body || {}).data;
expect(Object.keys(data.relationships).length).toBe(0);
expect(RESPONSES.NO_RELATIONSHIPS.body.included.length).toBe(0);
done();
});
});
});

0 comments on commit c96e101

Please sign in to comment.