// @ts-nocheck import { expect } from 'chai'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; import { format } from 'date-fns'; import { PrintInfo as PrintSdk } from '../../src/print-info'; import { TestConfiguration } from '../../src/configuration'; describe('Print info SDK', () => { const countryCode = 'GBR'; const postcode = 'SE19HL'; const productCode = 'NWE'; let sandbox; let instance; let config; let mockDeliverySchedule; beforeEach(() => { sandbox = sinon.createSandbox(); config = new TestConfiguration({ printInfoApiKey: 'secret', originSystemId: 'n-membership-sdk test', }); mockDeliverySchedule = sandbox.stub(); const PrintSdkMock = proxyquire('../../src/print-info', { './models/delivery-voucher-dates': { DeliveryVoucherDates: mockDeliverySchedule } }).PrintInfo; instance = new PrintSdkMock(config); sandbox.stub(instance, 'requestGet').resolves({}); }); afterEach(() => { sandbox.restore(); }); it('throws an error when host and key configuration properties do not exist', () => { try { new PrintSdk({}); expect.fail('It should throw'); } catch (error) { } }); describe('getFirstDeliveryDate', () => { const option = 'HD'; const startDate = '2017-07-22'; const mockResponse = { productCode: productCode, countryCode: countryCode, postcode: postcode, firstDeliveryDate: startDate }; context('should pass correct parameters to requestGet', () => { it('Sets a url with the startDate when startDate is set', async () => { const url = `${instance.membershipApi}/newspaper/info/first-delivery-date?productCode=${productCode}&countryCode=${countryCode}&postCode=${postcode}&option=${option}&startDate=${startDate}`; await instance.getFirstDeliveryDate(productCode, countryCode, postcode, option, startDate); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('Sets url without the startDate when this value is not given', async () => { const url = `${instance.membershipApi}/newspaper/info/first-delivery-date?productCode=${productCode}&countryCode=${countryCode}&postCode=${postcode}&option=${option}`; await instance.getFirstDeliveryDate(productCode, countryCode, postcode, option); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); }); context('when no errors are returned', () => { beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('returns the result', async () => { const data = await instance.getFirstDeliveryDate(productCode, countryCode, postcode, option, startDate); expect(data).to.deep.equal(mockResponse); }); }); 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.getFirstDeliveryDate(productCode, countryCode, postcode, option, startDate); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getFirstSuspensionDate', () => { const countryCode = 'GBR'; const fromDate = '2019-09-26'; const mockResponse = { firstSuspensionDate: '2019-09-28' }; it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/info/first-suspension-date?countryCode=${countryCode}&fromDate=${fromDate}`; await instance.getFirstSuspensionDate(countryCode, fromDate); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); context('when no errors are returned', () => { beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('returns the result', async () => { const data = await instance.getFirstSuspensionDate(countryCode, fromDate); expect(data).to.deep.equal(mockResponse); }); }); 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.getFirstSuspensionDate(countryCode, fromDate); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getVoucherDeliverySchedule', () => { const term = 'P1Y'; const startDate = '2019-10-31'; const mockResponse = { schedule: [ { voucherStartDate: '2020-01-30', voucherEndDate: '2020-04-29', fileDeliveryDate: '2020-01-10' }, { voucherStartDate: '2019-10-31', voucherEndDate: '2020-01-29', fileDeliveryDate: '2019-10-18' } ] }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/info/vouchers/schedules?startDate=${startDate}&term=${term}`; await instance.getVoucherDeliverySchedule(term, startDate); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('defaults startDate to todays date', async () => { await instance.getVoucherDeliverySchedule(term); const getParams = instance.requestGet.getCall(0).args[0]; expect(getParams.url).to.include(format(new Date(), 'yyyy-MM-dd')); }); context('when no errors are returned', () => { it('calls DeliveryVoucherDates model', async () => { await instance.getVoucherDeliverySchedule(term, startDate); expect(mockDeliverySchedule.calledWith(mockResponse.schedule[0])).to.be.true; expect(mockDeliverySchedule.calledWith(mockResponse.schedule[1])).to.be.true; }); }); 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.getVoucherDeliverySchedule(startDate, term); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getLastDeliveryDateGenerated', () => { const region = 'UK'; const checkDate = '2019-09-26'; const mockResponse = { region: 'UK', lastDeliveryDate: '2019-09-27' }; it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/info/delivery-files/last-date?date=${checkDate}®ion=${region}`; await instance.getLastDeliveryDateGenerated(checkDate, region); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); context('when no errors are returned', () => { beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('returns the result', async () => { const data = await instance.getLastDeliveryDateGenerated(checkDate, region); expect(data).to.deep.equal(mockResponse); }); }); 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.getLastDeliveryDateGenerated(checkDate, region); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getLastDeliveryDateGeneratedByCountry', () => { const countryCode = 'GBR'; const checkDate = '2019-09-26'; const mockResponse = { region: 'UK', lastDeliveryDate: '2019-09-27' }; it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/info/delivery-files/last-date?date=${checkDate}&countryCode=${countryCode}`; await instance.getLastDeliveryDateGeneratedByCountry(checkDate, countryCode); const getParams = instance.requestGet.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); context('when no errors are returned', () => { beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('returns the result', async () => { const data = await instance.getLastDeliveryDateGeneratedByCountry(checkDate, countryCode); expect(data).to.deep.equal(mockResponse); }); }); 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.getLastDeliveryDateGeneratedByCountry(checkDate, countryCode); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); });