/* Unit tests for the REST API handler for the /offer endpoints. */ // Public npm libraries const assert = require('chai').assert const sinon = require('sinon') // Local support libraries const adapters = require('../../../mocks/adapters') const UseCasesMock = require('../../../mocks/use-cases') // const app = require('../../../mocks/app-mock') const OfferRESTController = require('../../../../../src/controllers/rest-api/offer/controller') let uut let sandbox let ctx const mockContext = require('../../../../unit/mocks/ctx-mock').context describe('#Offer-REST-Router', () => { // const testUser = {} beforeEach(() => { const useCases = new UseCasesMock() uut = new OfferRESTController({ adapters, useCases }) sandbox = sinon.createSandbox() // Mock the context object. ctx = mockContext() }) afterEach(() => sandbox.restore()) describe('#constructor', () => { it('should throw an error if adapters are not passed in', () => { try { uut = new OfferRESTController() assert.fail('Unexpected code path') } catch (err) { assert.include( err.message, 'Instance of Adapters library required when instantiating /offer REST Controller.' ) } }) it('should throw an error if useCases are not passed in', () => { try { uut = new OfferRESTController({ adapters }) assert.fail('Unexpected code path') } catch (err) { assert.include( err.message, 'Instance of Use Cases library required when instantiating /offer REST Controller.' ) } }) }) describe('#createOffer', () => { it('should create a new offer', async () => { ctx.request.body = { offer: {} } // Mock dependencies sandbox.stub(uut.useCases.offer, 'createOffer').resolves('testHash') await uut.createOffer(ctx) assert.equal(ctx.body.hash, 'testHash') }) it('should catch and throw an error', async () => { try { ctx.request.body = { offer: {} } // Force an error sandbox .stub(uut.useCases.offer, 'createOffer') .rejects(new Error('test error')) await uut.createOffer(ctx) assert.fail('Unexpected code path') } catch (err) { // console.log('err: ', err) assert.include(err.message, 'test error') } }) }) describe('#handleError', () => { it('should still throw error if there is no message', () => { try { const err = { status: 404 } uut.handleError(ctx, err) } catch (err) { assert.include(err.message, 'Not Found') } }) it('should catch error if message is provided', () => { try { const err = { status: 422, message: 'Unprocessable Entity' } uut.handleError(ctx, err) } catch (err) { assert.include(err.message, 'Unprocessable Entity') } }) }) })