Permalink
Browse files

Return hotel photos from hotel's entity photos property

This removes returning hardcoded array of file names, finally.
  • Loading branch information...
smolak committed Dec 29, 2017
1 parent 7d0688a commit d1845d72151dd87ed87b2d2532ab4fff8d4001c2
Showing with 23 additions and 18 deletions.
  1. +6 −5 src/createHotelPhotosRouteHandler.js
  2. +17 −13 test/unit/src/createHotelPhotosRouteHandlerSpec.js
@@ -1,10 +1,11 @@
export default function createHotelPhotosRouteHandler(dbClient, collectionName) {
return (ctx) => {
dbClient // (4)
return dbClient
.collection(collectionName)
.findOne({ hotelId: ctx.params.hotelId });
ctx.response.status = 200;
ctx.response.body = [ 'photo-1.jpg', 'photo-2.jpg', 'photo-3.jpg' ];
.findOne({ hotelId: ctx.params.hotelId })
.then((hotelEntity) => {
ctx.response.status = 200;
ctx.response.body = hotelEntity.photos;
});
};
}
@@ -16,16 +16,19 @@ describe('createHotelPhotosRouteHandler', () => {
body: ''
}
};
const findOneSpy = sinon.spy();
const findOneStub = sinon.stub().resolves({ // (1)
photos: [ 'photo-1.jpg', 'photo-2.jpg', 'photo-3.jpg' ]
});
const connectedClientDouble = {
collection: sinon.stub().returns({ // (2)
findOne: findOneSpy
collection: sinon.stub().returns({
findOne: findOneStub
})
};
beforeEach(() => {
connectedClientDouble.collection.resetHistory(); // (3)
findOneSpy.reset();
connectedClientDouble.collection.resetHistory();
findOneStub.resetHistory();
ctxDouble.response.status = 0;
ctxDouble.response.body = '';
@@ -53,20 +56,21 @@ describe('createHotelPhotosRouteHandler', () => {
routeHandler(ctxDouble);
expect(findOneSpy)
expect(findOneStub)
.to.have.been.calledWithExactly({ hotelId: 'hotelId' })
.to.have.been.calledOnce;
});
it('should return hotel photos collection', () => { // (3)
it('should return hotel photos collection', () => {
const routeHandler = createHotelPhotosRouteHandler(connectedClientDouble, collectionName);
routeHandler(ctxDouble); // (4)
expect(ctxDouble.response.status).to.equal(200);
expect(ctxDouble.response.body).to.deep.equal([
'photo-1.jpg', 'photo-2.jpg', 'photo-3.jpg'
]);
return routeHandler(ctxDouble) // (2)
.then(() => {
expect(ctxDouble.response.status).to.equal(200);
expect(ctxDouble.response.body).to.deep.equal([
'photo-1.jpg', 'photo-2.jpg', 'photo-3.jpg'
]);
});
});
});
});

0 comments on commit d1845d7

Please sign in to comment.