import { expect } from 'chai'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; proxyquire.noCallThru(); const sandbox = sinon.createSandbox(); describe('mapRedeemTokenResponse', () => { let redeemableTokenData: any; let proxyRedeemToken: any; let mockRedeemableTokenClass: any; beforeEach(() => { redeemableTokenData = { id: 'userId', creationDateTime: '2019-05-30T11:10:55.132Z', redeemableFor: { accessLicenceId: 'accessLicenceId', accessDuration: 'accessDuration', type: 'LICENCE' }, validUntil: '2020-01-01T01:02:03.000Z', status: 'AVAILABLE', redeemability: 'UNLIMITED' }; mockRedeemableTokenClass = sandbox.stub(); proxyRedeemToken = proxyquire('../../../src/mappers/redeem-token-post', { '../models/redeemable-token': { RedeemableToken: mockRedeemableTokenClass }, }); }); context('calls new Licence with', () => { let redeemableTokenArguments: any; beforeEach(() => { proxyRedeemToken.mapRedeemTokenResponse(redeemableTokenData); redeemableTokenArguments = mockRedeemableTokenClass.getCall(0).args; }); it('calls new RedeemableToken', () => { expect(mockRedeemableTokenClass.calledWithNew()).to.be.true; }); it('id', () => { expect(redeemableTokenArguments[0]).to.have.own.property('id'); }); it('creationDateTime', () => { expect(redeemableTokenArguments[0]).to.have.own.property('creationDateTime'); }); it('accessDuration', () => { expect(redeemableTokenArguments[0]).to.have.own.property('accessDuration'); }); it('accessType', () => { expect(redeemableTokenArguments[0]).to.have.own.property('accessType'); }); it('accessLicenceId', () => { expect(redeemableTokenArguments[0]).to.have.own.property('accessLicenceId'); }); it('validUntil', () => { expect(redeemableTokenArguments[0]).to.have.own.property('validUntil'); }); it('status', () => { expect(redeemableTokenArguments[0]).to.have.own.property('status'); }); it('redeemability', () => { expect(redeemableTokenArguments[0]).to.have.own.property('redeemability'); }); }); context('errors', () => { it('throws with mappedTokenData when it contains an undefined value', () => { redeemableTokenData.id = undefined; const expectedResult = { id: redeemableTokenData.id, creationDateTime: redeemableTokenData.creationDateTime, accessDuration: redeemableTokenData.redeemableFor.accessDuration, accessLicenceId: redeemableTokenData.redeemableFor.accessLicenceId, accessType: redeemableTokenData.redeemableFor.type, validUntil: redeemableTokenData.validUntil, redeemability: redeemableTokenData.redeemability, status: redeemableTokenData.status }; try { proxyRedeemToken.mapRedeemTokenResponse(redeemableTokenData); expect.fail('Should have thrown the error!'); } catch (error) { expect(error.data.mappedTokenData).eql(expectedResult); } }); }); });