Permalink
Browse files

Add handling case when there is no hotel entity for given hotel id

  • Loading branch information...
smolak committed Dec 29, 2017
1 parent d1845d7 commit 962aad608e0063b4adb0e96551443385c7ada7f0
Showing with 25 additions and 2 deletions.
  1. +6 −2 src/createHotelPhotosRouteHandler.js
  2. +19 −0 test/unit/src/createHotelPhotosRouteHandlerSpec.js
@@ -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)
}
});
};
}
@@ -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);

0 comments on commit 962aad6

Please sign in to comment.