// @ts-nocheck import { expect } from 'chai'; import * as sinon from 'sinon'; import { SuspensionService } from '../../src/suspension'; import { TestConfiguration } from '../../src/configuration'; import { Suspension } from '../../src/models/suspension'; import { EmptyResultError } from '../../src/errors'; describe('SuspensionService', () => { let sandbox; let instance; let config; beforeEach(() => { sandbox = sinon.createSandbox(); config = new TestConfiguration({ suspensionApiKey: 'secret', originSystemId: 'systemId' }); instance = new SuspensionService(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 SuspensionService({}); expect.fail('It should throw'); } catch (error) { } }); describe('getSuspensions', () => { const subscriptionNumber = 'subscriptionNumber'; const mockResponse = { items: [ { id: 'id', subscriptionNumber: 'number', startDate: '2019-08-16', endDate: '2020-08-15', credits: 10, editions: 2, originUser: 'originUser', } ] }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/suspensions?subscriptionNumber=${subscriptionNumber}`; await instance.getSuspensions(subscriptionNumber); 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 suspension instances', async () => { const [suspensionItem] = await instance.getSuspensions(subscriptionNumber); expect(suspensionItem).to.be.instanceOf(Suspension); }); it('throws an EmptyResultError when suspensions come back with an empty object', async () => { try { instance.requestGet.resolves({}); await instance.getSuspensions(subscriptionNumber); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.be.instanceOf(EmptyResultError); } }); it('returns an empty array when suspensions come back with an empty items array', async () => { instance.requestGet.resolves({ items: [] }); const response = await instance.getSuspensions(subscriptionNumber); expect(response).to.be.an('array'); expect(response).to.be.empty; }); 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.getSuspensions(subscriptionNumber); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getSuspensionById', () => { const suspensionId = 'suspensionId'; const mockResponse = { suspension: { id: 'id', subscriptionNumber: 'number', startDate: '2019-08-16', endDate: '2020-08-15', credits: 10, editions: 2, originUser: 'originUser', } }; beforeEach(() => { instance.requestGet.resolves(mockResponse); }); it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/suspensions/suspensionId`; await instance.getSuspensionById(suspensionId); 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 suspension instances', async () => { const data = await instance.getSuspensionById(suspensionId); expect(data).to.be.instanceOf(Suspension); }); 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.getSuspensionById(suspensionId); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('createSuspension', () => { const subscriptionNumber = 'subscriptionNumber'; const startDate = '2019-09-26'; const endDate = '2019-10-26'; const mockResponse = { items: [ { id: 'id', subscriptionNumber: 'number', startDate: '2019-08-16', endDate: '2020-08-15', credits: 10, editions: 2, originUser: 'originUser', } ] }; beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('should pass correct parameters to requestPost', async () => { const url = `${instance.membershipApi}/newspaper/suspensions`; await instance.createSuspension(subscriptionNumber, startDate, endDate); 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 array of suspension instances', async () => { const [suspensionItem] = await instance.createSuspension(subscriptionNumber, startDate, endDate); expect(suspensionItem).to.be.instanceOf(Suspension); }); it('throws an EmptyResultError when suspensions come back with an empty object', async () => { try { instance.requestPost.resolves({}); await instance.createSuspension(subscriptionNumber, startDate, endDate); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.be.instanceOf(EmptyResultError); } }); it('throws an EmptyResultError when suspensions come back with an empty items array', async () => { try { instance.requestPost.resolves({ items: [] }); await instance.createSuspension(subscriptionNumber, startDate, endDate); 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.requestPost.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.createSuspension(subscriptionNumber, startDate, endDate); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('endSuspension', () => { const suspensionId = 'suspensionId'; const mockResponse = { items: [ { id: 'id', subscriptionNumber: 'number', startDate: '2019-08-16', endDate: '2020-08-15', credits: 10, editions: 2, originUser: 'originUser', } ] }; beforeEach(() => { instance.requestPost.resolves(mockResponse); }); it('should pass correct parameters to requestPost', async () => { const url = `${instance.membershipApi}/newspaper/suspensions/suspensionId/actions/end`; await instance.endSuspension(suspensionId); const postParams = instance.requestPost.getCall(0).args[0]; // Path checks expect(postParams).to.have.property('url'); expect(postParams.url).to.equal(url); }); it('makes the request with the body set to an empty object when endDateFrom is NOT given', async () => { await instance.endSuspension(suspensionId); const postParams = instance.requestPost.getCall(0).args[0]; expect(postParams.body).to.eql({}); }); it('makes the request with the body containing the endDateFrom when endDateFrom is given', async () => { await instance.endSuspension(suspensionId, '2019-12-12'); const postParams = instance.requestPost.getCall(0).args[0]; expect(postParams.body).to.eql({ endDateFrom: '2019-12-12' }); }); it('returns an array of suspension instances', async () => { const [suspensionItem] = await instance.endSuspension(suspensionId); expect(suspensionItem).to.be.instanceOf(Suspension); }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestPost.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.endSuspension(suspensionId); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('deleteSuspension', () => { const suspensionId = 'suspensionId'; const mockResponse = { id: 'id', subscriptionNumber: 'number', startDate: '2019-08-16', endDate: '2020-08-15', credits: 10, editions: 2, originUser: 'originUser', }; it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/suspensions/${suspensionId}`; await instance.deleteSuspension(suspensionId); const getParams = instance.requestDelete.getCall(0).args[0]; // Path checks expect(getParams).to.have.property('url'); expect(getParams.url).to.equal(url); }); it('returns an array of suspension instances', async () => { const response204 = { status: 204, ok: true }; instance.requestDelete.resolves(response204); const data = await instance.deleteSuspension(suspensionId); expect(data).to.eql(response204); }); context('when an error is thrown', () => { const errorResponse = new Error('Test failure'); beforeEach(() => { instance.requestDelete.rejects(errorResponse); }); it('it throws the same error', async () => { try { await instance.deleteSuspension(suspensionId); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); describe('getSummary', () => { const subscriptionNumber = 'subscriptionNumber'; const mockResponse = { subscriptionNumber: 'number', creditsTotal: 10, creditsUsed: 2, creditsRemaining: 8, periodStartDate: '2019-08-16', periodEndDate: '2020-08-15' }; it('should pass correct parameters to requestGet', async () => { const url = `${instance.membershipApi}/newspaper/suspensions/summary?subscriptionNumber=${subscriptionNumber}`; await instance.getSummary(subscriptionNumber); 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.getSummary(subscriptionNumber); 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.getSummary(subscriptionNumber); expect.fail('Should have thrown the error!'); } catch (error) { expect(error).to.deep.equal(errorResponse); } }); }); }); });