Skip to content

Commit

Permalink
Add handling case when there is no hotel entity for given hotel id
Browse files Browse the repository at this point in the history
  • Loading branch information
smolak committed Dec 29, 2017
1 parent d1845d7 commit 962aad6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/createHotelPhotosRouteHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export default function createHotelPhotosRouteHandler(dbClient, collectionName)
.collection(collectionName)
.findOne({ hotelId: ctx.params.hotelId })
.then((hotelEntity) => {
ctx.response.status = 200;
ctx.response.body = hotelEntity.photos;
if (hotelEntity) {
ctx.response.status = 200;
ctx.response.body = hotelEntity.photos;
} else {
ctx.response.status = 404; // (2)
}
});
};
}
19 changes: 19 additions & 0 deletions test/unit/src/createHotelPhotosRouteHandlerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ describe('createHotelPhotosRouteHandler', () => {
.to.have.been.calledOnce;
});

context('if hotel entity is not found', () => {
it('should return 404 status', () => {
const connectedClientDoubleWithNoHotelEntity = { // (1)
collection: sinon.stub().returns({
findOne: sinon.stub().resolves(null)
})
};
const routeHandler = createHotelPhotosRouteHandler(
connectedClientDoubleWithNoHotelEntity,
collectionName
);

return routeHandler(ctxDouble)
.then(() => {
expect(ctxDouble.response.status).to.equal(404);
});
});
});

it('should return hotel photos collection', () => {
const routeHandler = createHotelPhotosRouteHandler(connectedClientDouble, collectionName);

Expand Down

0 comments on commit 962aad6

Please sign in to comment.