import { expect } from 'chai'; import * as sinon from 'sinon'; import { DeliveryAddressService } from '../../src/delivery-address'; import { TestConfiguration } from '../../src/configuration'; import { EmptyResultError } from '../../src/errors'; describe('DeliveryAddressService', () => { let sandbox: any; let instance: any; let config: any; beforeEach(() => { sandbox = sinon.createSandbox(); config = new TestConfiguration({ deliveryAddressApiKey: 'secret', originSystemId: 'systemId' }); instance = new DeliveryAddressService(config); sandbox.stub(instance, 'requestGet').resolves({}); sandbox.stub(instance, 'requestPost').resolves({}); sandbox.stub(instance, 'requestDelete').resolves({}); }); afterEach(() => { sandbox.restore(); }); it('throws an error when host and key configuration properties do not exist', () => { try { new DeliveryAddressService({} as any); expect.fail('It should throw'); } catch (error) { } }); describe('getDeliveryAddresses', () => { const subscription = { number: 'number', startDate: '2019-08-01T00:00:00.000Z' }; const mockResponse = { items: [ { id: 'id', userId: 'userId', subscriptionNumber: 'number', line1: '1 Friday Street', line2: 'Bracken House', line3: 'St Pauls', townCity: 'London', county: 'London', country: 'GBR', postcode: 'SE19HL', validDateFrom: '2017-08-01', validDateTo: null, deliveryNotes: 'Use the red door', securityNotes: 'code 212', addressType: 'default', isCurrentDefaultAddress: true } ] }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/delivery-addresses?subscriptionNumber=${subscription.number}`; await instance.getDeliveryAddresses(subscription); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('returns an array of delivery addresses', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[0]).to.include(mockResponse.items[0]); }); describe('Current address', () => { const mockResponse = { items: [ { validDateFrom: '2017-08-01', addressType: 'default', isCurrentDefaultAddress: false, isCurrentWeekendAddress: false }, { validDateFrom: '2019-08-01', addressType: 'default', isCurrentDefaultAddress: false, isCurrentWeekendAddress: false }, { validDateFrom: '2018-08-01', addressType: 'default', isCurrentDefaultAddress: false, isCurrentWeekendAddress: false }, { validDateFrom: '2019-10-05', addressType: 'weekend', isCurrentDefaultAddress: false, isCurrentWeekendAddress: false }, { validDateFrom: '2019-10-12', addressType: 'weekend', isCurrentDefaultAddress: false, isCurrentWeekendAddress: false } ] }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); describe('sets isCurrentDefaultAddress', () => { it('returns false when the validDateFrom of the first default address in the array is not the largest', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[0].isCurrentDefaultAddress).to.be.false; }); it('returns true when the validDateFrom of this address is the largest of the past default addresses', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[1].isCurrentDefaultAddress).to.be.true; }); it('returns false when the validDateFrom of the last default address in the array is not the largest', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[2].isCurrentDefaultAddress).to.be.false; }); }); describe('sets isCurrentWeekendAddress', () => { it('returns false when the validDateFrom of the first weekend address in the array is not the largest', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[3].isCurrentWeekendAddress).to.be.false; }); it('returns true when the validDateFrom of this address is the largest of the past weekend addresses', async () => { const data = await instance.getDeliveryAddresses(subscription); expect(data[4].isCurrentWeekendAddress).to.be.true; }); }); }); it('throws an EmptyResultError when the response come back with an empty object', async () => { try { instance.requestGet.resolves({}); await instance.getDeliveryAddresses(subscription); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.be.instanceOf(EmptyResultError); } }); it('throws an EmptyResultError when response come back with an empty items array', async () => { try { instance.requestGet.resolves({ items: [] }); await instance.getDeliveryAddresses(subscription); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.be.instanceOf(EmptyResultError); } }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestGet.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.getDeliveryAddresses(subscription); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getDeliveryAddressById', () => { const addressId = 'addressId'; const mockResponse = { deliveryAddress: { id: 'id', userId: 'userId', subscriptionNumber: 'number', line1: '1 Friday Street', line2: 'Bracken House', line3: 'St Pauls', townCity: 'London', county: 'London', validDateFrom: '2017-08-01', validDateTo: null, deliveryNotes: 'Use the red door', securityNotes: 'code 212', addressType: 'default' } }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/delivery-addresses/${addressId}`; await instance.getDeliveryAddressById(addressId); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('returns a single delivery address', async () => { const data = await instance.getDeliveryAddressById(addressId); expect(data).to.eql(mockResponse.deliveryAddress); }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestGet.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.getDeliveryAddressById(addressId); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('createDeliveryAddress', () => { const deliveryAddress = { userId: 'userId', subscriptionNumber: 'A-S00381148', line1: '1 Southwark Bridge', line2: 'Just there', line3: 'Under the bridge', townCity: 'LONDON', county: 'Kent, not really.', postcode: 'SE19HL', country: 'GBR', validDateFrom: '2017-01-16', validDateTo: '2017-02-27', deliveryNotes: 'Leave in the porch', securityNotes: 'Do not leave it in the porch', addressType: 'holiday', }; const mockResponse = { addressId: '123456789' }; beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('should pass correct parameters to requestPost', async () => { const url = `${instance.membershipApi}/newspaper/delivery-addresses`; await instance.createDeliveryAddress(deliveryAddress); const postParams = instance.requestPost.getCall(0).args[0]; // Path checks expect(postParams).to.have.property('url'); expect(postParams.url).to.equal(url); }); }); describe('updateFutureDeliveryAddress', () => { const deliveryAddress = { userId: 'userId', subscriptionNumber: 'A-S00381148', line1: '1 Southwark Bridge', line2: 'Just there', line3: 'Under the bridge', townCity: 'LONDON', county: 'Kent, not really.', postcode: 'SE19HL', country: 'GBR', validDateFrom: '2017-01-16', validDateTo: '2017-02-27', deliveryNotes: 'Leave in the porch', securityNotes: 'Do not leave it in the porch', addressType: 'holiday', }; const mockResponse = { addressId: '123456789' }; beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('should pass correct parameters to requestPost', async () => { const addressId = '123456789'; const url = `${instance.membershipApi}/newspaper/delivery-addresses/${addressId}/actions/update-future-address`; await instance.updateFutureDeliveryAddress(addressId, deliveryAddress); const postParams = instance.requestPost.getCall(0).args[0]; // Path checks expect(postParams).to.have.property('url'); expect(postParams.url).to.equal(url); }); it('returns an address id', async () => { const data = await instance.updateFutureDeliveryAddress(deliveryAddress); expect(data.addressId).to.equal('123456789'); }); }); describe('updateDeliveryAddress', () => { const deliveryAddressUpdate = { line1: 'Change where I live', postcode: 'SE20HL', deliveryNotes: 'Leave in the back garden', securityNotes: 'Do not leave it in the back garden', }; const addressId = '123456789'; const mockResponse = { deliveryAddress: deliveryAddressUpdate }; beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('should pass correct parameters to requestPost', async () => { const url = `${instance.membershipApi}/newspaper/delivery-addresses/${addressId}`; await instance.updateDeliveryAddress(addressId, deliveryAddressUpdate); const postParams = instance.requestPost.getCall(0).args[0]; // Path checks expect(postParams).to.have.property('url'); expect(postParams.url).to.equal(url); }); it('returns the updated address', async () => { const data = await instance.createDeliveryAddress(addressId, deliveryAddressUpdate); expect(data.deliveryAddress).to.equal(deliveryAddressUpdate); }); }); describe('deleteDeliveryAddress', () => { const addressId = '1234'; it('should pass correct parametes to requestDelete', async () => { const url = `${instance.membershipApi}/newspaper/delivery-addresses/${addressId}`; await instance.deleteDeliveryAddress(addressId); const getParams = instance.requestDelete.getCall(0).args[0]; expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('returns a 204 http status code when deletion succeed', async () => { const response204 = { status: 204, ok: true }; instance.requestDelete.resolves(response204); const data = await instance.deleteDeliveryAddress(addressId); expect(data).to.eql(response204); }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestDelete.rejects(errorResponse); }); it('throws the same error', async () => { try { await instance.deleteDeliveryAddress(addressId); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); });