import { expect } from 'chai'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; proxyquire.noCallThru(); const sandbox = sinon.createSandbox(); describe('mapToMembershipResponse', () => { let licenceData: any; let proxyLicence: any; let mockLicenceClass: any; const acquisitionContextResponse = { displayName: 'Display Name' }; beforeEach(() => { licenceData = { id: 'licenceId', details: { links: [{ id: 'echo:ABC123', rel: 'trial' }], isB2c: undefined, isB2b: true, isSso: true, seatLimit: 100, status: 'active', issueReason: 'Signed With Google', products: [{ name: 'FT.com Premium', code: 'P2' }] }, seats: { allocated: 0 }, acqContexts: [acquisitionContextResponse], administrators: [{ userId: 'userId', licenceId: 'licenceId' }] }; const fakeResponse = sandbox.stub().returns(acquisitionContextResponse); mockLicenceClass = sandbox.stub(); proxyLicence = proxyquire('../../../src/mappers/licence', { '../models/licence': { Licence: mockLicenceClass }, './acquisition-context': { mapAcquisitionContextResponse: fakeResponse } }); }); context('calls new Licence with', () => { let licenceArguments: any; beforeEach(() => { proxyLicence.mapLicenceResponse(licenceData); licenceArguments = mockLicenceClass.getCall(0).args; }); it('calls new Licence', () => { expect(mockLicenceClass.calledWithNew()).to.be.true; }); it('id', () => { expect(licenceArguments[0]).to.have.own.property('id'); }); it('allocateSeats', () => { expect(licenceArguments[0]).to.have.own.property('allocatedSeats'); }); it('isB2c', () => { expect(licenceArguments[0]).to.have.own.property('isB2c'); }); it('isB2b', () => { expect(licenceArguments[0]).to.have.own.property('isB2b'); }); it('isSso', () => { expect(licenceArguments[0]).to.have.own.property('isSso'); }); it('seatLimit', () => { expect(licenceArguments[0]).to.have.own.property('seatLimit'); }); it('status', () => { expect(licenceArguments[0]).to.have.own.property('status'); }); it('types', () => { expect(licenceArguments[0]).to.have.own.property('types'); }); it('acquisitionContext', () => { expect(licenceArguments[0]).to.have.own.property('acquisitionContext'); }); it('linkedContractId', () => { expect(licenceArguments[0]).to.have.own.property('linkedContractId'); }); it('links', () => { expect(licenceArguments[0]).to.have.own.property('links'); }); it('products', () => { expect(licenceArguments[0]).to.have.own.property('products'); }); it('administrators', () => { expect(licenceArguments[0]).to.have.own.property('administrators'); }); it('issueReason', () => { expect(licenceArguments[0]).to.have.own.property('issueReason'); }); }); context('when the licence data contains a contract link', () => { let mappedLicenceData: any; beforeEach(() => { licenceData.details.links.push({ rel: 'contract', id: 'FT-00000001' }); proxyLicence.mapLicenceResponse(licenceData); mappedLicenceData = mockLicenceClass.getCall(0).args[0]; }); it('should map the link id to the licence linkedContractId property', () => { expect(mappedLicenceData.linkedContractId).to.equal('FT-00000001'); }); }); context('when the licence data doesn\'t contain a contract link', () => { let mappedLicenceData: any; beforeEach(() => { proxyLicence.mapLicenceResponse(licenceData); mappedLicenceData = mockLicenceClass.getCall(0).args[0]; }); it('should set the linkedContractId property to null', () => { expect(mappedLicenceData.linkedContractId).to.equal(null); }); }); context('when the licence data contains an undefined value for a required propery', () => { it('throws with an error containing the mappedLicenceData ', () => { licenceData.id = undefined; const expectedResult = { id: licenceData.id, allocatedSeats: licenceData.seats.allocated, seatLimit: licenceData.details.seatLimit, status: licenceData.details.status, isB2c: false, // there are valid models missing the property causing code to throw isB2b: true, isSso: true, types: [licenceData.details.links[0].rel], acquisitionContext: [acquisitionContextResponse], linkedContractId: null, links: licenceData.details.links, products: licenceData.details.products, administrators: licenceData.administrators, issueReason: licenceData.details.issueReason }; try { proxyLicence.mapLicenceResponse(licenceData); expect.fail('Should have thrown the error!'); } catch (error) { expect(error.data.mappedLicenceData).eql(expectedResult); } }); }); });