import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import * as spies from 'chai-spies'; import { async } from 'q'; import * as sinon from 'sinon'; import * as sinonChai from 'sinon-chai'; import {Billing} from '../../../lib/billing'; import { excelObjValue, filledBillingForm, filterBody, itemValue, newBillingForm, newObjValue, proxyItem, requstObj} from '../../data/billing_data'; chai.use(chaiAsPromised); chai.use(spies); chai.use(sinonChai); const expect: any = chai.expect; describe('Billing', () => { const billingTest: any = new Billing('myrtb-ci-billing', 'subramaniamr@nih.gov'); describe('getAll()', () => { it('should call through the getAll api', async () => { sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return 1; }); sinon.stub(billingTest, 'structureRequest').callsFake(() => { return filterBody; }); await billingTest.getAll(); expect(billingTest.billingApiDriver).to.have.been.called; expect(billingTest.structureRequest).to.have.been.called; billingTest.structureRequest.restore(); billingTest.billingApiDriver.restore(); }); }); describe('filter()', () => { it('should call through the filter api', async () => { const getAllrequest = { chargeList: "RTB-Charges-FY18", section: [], endDate: "10/15/2019", status: "readytobill" }; sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return getAllrequest; }); const filterResponse = await billingTest.filter(); expect(filterResponse).to.eql(getAllrequest); expect(billingTest.billingApiDriver).to.have.been.called; billingTest.billingApiDriver.restore(); }); }); describe('getRecords()', () => { it('should should trigger the get all component of the api', async () => { const getAllrequest = { chargeList: "RTB-Charges-FY18", section: [], endDate: "10/15/2019", status: "readytobill" }; sinon.stub(billingTest, 'structureRequest').callsFake(() => { return filterBody; }); sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return [[excelObjValue]]; }); await billingTest.getRecords(getAllrequest); expect(billingTest.structureRequest).to.have.been.called; expect(billingTest.billingApiDriver).to.have.been.called; billingTest.structureRequest.restore(); billingTest.billingApiDriver.restore(); }); it('should should call billingApiDriver api', async () => { sinon.spy(billingTest, 'getRecords'); sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return 1; }); await billingTest.getRecords(filterBody); expect(billingTest.getRecords).to.have.been.calledWith(filterBody); billingTest.billingApiDriver.restore(); }); }); describe('readyToBill()', () => { it('should should trigger the get all component of the api', async () => { const getAllrequest = { chargeList: "RTB-Charges-FY18", section: [], endDate: "10/15/2019", status: "readytobill" }; sinon.stub(billingTest, 'structureRequest').callsFake(() => { return filterBody; }); sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return [[excelObjValue]]; }); sinon.stub(billingTest.xL, 'buildBillingExcel').callsFake(() => { return {}; }); await billingTest.readyToBill(getAllrequest); expect(billingTest.structureRequest).to.have.been.called; expect(billingTest.billingApiDriver).to.have.been.called; billingTest.structureRequest.restore(); billingTest.billingApiDriver.restore(); billingTest.xL.buildBillingExcel.restore(); }); it('should should call billingApiDriver api', async () => { sinon.spy(billingTest, 'readyToBill'); spyOn(billingTest, 'billingApiDriver').and.returnValue([[excelObjValue]]); sinon.stub(billingTest.xL, 'buildBillingExcel').callsFake(() => { return {}; }); await billingTest.readyToBill(filterBody); expect(billingTest.readyToBill).to.have.been.calledWith(filterBody); billingTest.xL.buildBillingExcel.restore(); }); }); describe('generateBilled()', () => { it('should should call billingApiDriver api', async () => { sinon.spy(billingTest, 'generateBilled'); spyOn(billingTest, 'billingApiDriver').and.returnValue([[excelObjValue]]); sinon.stub(billingTest.xL, 'buildBillingExcel').callsFake(() => { return {}; }); await billingTest.generateBilled(filterBody); expect(billingTest.generateBilled).to.have.been.calledWith(filterBody); billingTest.generateBilled.restore(); billingTest.xL.buildBillingExcel.restore(); }); it('should should trigger the get all component of the api', async () => { const getAllrequest = { chargeList: "RTB-Charges-FY18", section: [], endDate: "10/15/2019", status: "readytobill" }; sinon.stub(billingTest, 'structureRequest').callsFake(() => { return filterBody; }); sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return [[excelObjValue]]; }); sinon.stub(billingTest.xL, 'buildBillingExcel').callsFake(() => { return {}; }); await billingTest.generateBilled(getAllrequest); expect(billingTest.structureRequest).to.have.been.called; expect(billingTest.billingApiDriver).to.have.been.called; billingTest.structureRequest.restore(); billingTest.billingApiDriver.restore(); billingTest.xL.buildBillingExcel.restore(); }); }); describe('generateBillingSheet()', () => { it('should should call billingApiDriver api', async () => { sinon.spy(billingTest, 'generateBillingSheet'); spyOn(billingTest, 'billingApiDriver').and.returnValue([[excelObjValue]]); sinon.stub(billingTest.xL, 'buildGenBillingSheet').callsFake(() => { return [{}]; }); await billingTest.generateBillingSheet(filterBody); expect(billingTest.generateBillingSheet).to.have.been.calledWith(filterBody); billingTest.generateBillingSheet.restore(); billingTest.xL.buildGenBillingSheet.restore(); }); it('should should trigger the get all component of the api', async () => { const getAllrequest = { chargeList: "RTB-Charges-FY18", section: [], endDate: "10/15/2019", status: "readytobill" }; sinon.stub(billingTest, 'structureRequest').callsFake(() => { return filterBody; }); sinon.stub(billingTest, 'billingApiDriver').callsFake(() => { return [[excelObjValue]]; }); sinon.stub(billingTest.xL, 'buildGenBillingSheet').callsFake(() => { return {}; }); await billingTest.generateBillingSheet(getAllrequest); expect(billingTest.structureRequest).to.have.been.called; expect(billingTest.billingApiDriver).to.have.been.called; billingTest.structureRequest.restore(); billingTest.billingApiDriver.restore(); billingTest.xL.buildGenBillingSheet.restore(); }); }); describe('buildBillingObj()', () => { it('should build a standard billing object', async () => { const newBilling = await billingTest.buildBillingObj(newBillingForm, newObjValue); expect(newBilling).to.eql(filledBillingForm); }); }); describe('getBillingValue()', () => { it('should find the value from the object', () => { const itemValues = billingTest.getBillingValue('CAN', filledBillingForm); expect(itemValues).to.eql(itemValue); }); it('should set the value to null if it can not find the value', () => { const itemValues = billingTest.getBillingValue('NotRealValue', filledBillingForm); expect(itemValues).to.eql({ value: null }); }); }); describe('updateOriginalList()', () => { it('should behave update the original list item', async () => { const testItem = billingTest.updateOriginalList(proxyItem, filledBillingForm); expect(testItem).to.not.be.undefined; }); it('should not update the bill date if the internal name is not present', async () => { filledBillingForm.Properties = []; const testItem = billingTest.updateOriginalList(proxyItem, filledBillingForm); expect(testItem).to.not.be.undefined; }); }); describe('sleep()', () => { it('return void aftern sleeping', async () => { const testItem = await billingTest.sleep(100); expect(testItem).to.be.undefined; }); }); describe('extractBillingInformation()', () => { it('should create a object with extracted information', () => { spyOn(billingTest, 'getBillingValue').and.returnValue({ value: { Refs: [{ Value: 1 }] }}); spyOn(billingTest, 'getRequestDate').and.returnValue('10/13/2019'); const testItem = billingTest.extractBillingInformation(filledBillingForm, proxyItem); expect(testItem).to.have.property('Title'); }); }); describe('getRequestDate()', () => { it('should return a correctly formatted month/day/year if given a date in miliseconds', () => { const testItem = billingTest.getRequestDate('/Date(1427982649000-0400)/'); expect(testItem).to.eql('4/2/2015'); }); it('should return a correctly formatted month/day/year if given month/day/year', () => { const testItem = billingTest.getRequestDate('4/2/2015'); expect(testItem).to.eql('4/2/2015'); }); }); });