From b3db1cda133b7f81f6f354ffcb174881997c08a0 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 10 Aug 2021 13:57:03 +0200 Subject: [PATCH] Updated unit tests --- .../back/methods/invoiceOut/createPdf.js | 4 +- .../methods/invoiceOut/globalInvoicing.js | 2 +- .../specs/createManualInvoice.spec.js | 10 +-- .../invoiceOut/specs/createPdf.spec.js | 9 +++ .../methods/invoiceOut/specs/download.spec.js | 7 ++ .../invoiceOut/specs/globalInvoicing.spec.js | 40 +++++++++++ .../methods/invoiceOut/specs/summary.spec.js | 2 +- .../front/index/global-invoicing/index.js | 12 ++-- .../index/global-invoicing/index.spec.js | 71 ++++++++++++++----- 9 files changed, 124 insertions(+), 33 deletions(-) create mode 100644 modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js diff --git a/modules/invoiceOut/back/methods/invoiceOut/createPdf.js b/modules/invoiceOut/back/methods/invoiceOut/createPdf.js index d8962e4304..678e7dda54 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/createPdf.js +++ b/modules/invoiceOut/back/methods/invoiceOut/createPdf.js @@ -78,15 +78,13 @@ module.exports = Self => { response.pipe(writeStream); }); - return await new Promise(resolve => { + return new Promise(resolve => { writeStream.on('finish', () => { writeStream.end(); resolve(invoiceOut); }); }); - - // return invoiceOut; } catch (e) { if (tx) await tx.rollback(); if (fs.existsSync(fileSrc)) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 4507369020..5989e941b4 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -160,7 +160,7 @@ module.exports = Self => { for (let invoiceId of invoicesIds) await Self.createPdf(ctx, invoiceId); - return {}; + return invoicesIds; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/createManualInvoice.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/createManualInvoice.spec.js index e5c3c05126..f1c1bbab83 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/createManualInvoice.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/createManualInvoice.spec.js @@ -11,7 +11,7 @@ describe('InvoiceOut createManualInvoice()', () => { const ctx = {req: activeCtx}; it('should throw an error trying to invoice again', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; @@ -36,7 +36,7 @@ describe('InvoiceOut createManualInvoice()', () => { }); it('should throw an error for a ticket with an amount of zero', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); @@ -68,7 +68,7 @@ describe('InvoiceOut createManualInvoice()', () => { }); it('should throw an error when the clientFk property is set without the max shipped date', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; @@ -92,7 +92,7 @@ describe('InvoiceOut createManualInvoice()', () => { }); it('should throw an error for a non-invoiceable client', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; @@ -121,7 +121,7 @@ describe('InvoiceOut createManualInvoice()', () => { }); it('should create a manual invoice', async() => { - spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/createPdf.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/createPdf.spec.js index 0ed0b35eb9..2f503d11c2 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/createPdf.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/createPdf.spec.js @@ -1,5 +1,6 @@ const models = require('vn-loopback/server/server').models; const got = require('got'); +const fs = require('fs-extra'); describe('InvoiceOut createPdf()', () => { const userId = 1; @@ -18,6 +19,14 @@ describe('InvoiceOut createPdf()', () => { on: () => {}, }; spyOn(got, 'stream').and.returnValue(response); + spyOn(models.InvoiceContainer, 'container').and.returnValue({ + client: {root: '/path'} + }); + spyOn(fs, 'mkdir').and.returnValue(true); + spyOn(fs, 'createWriteStream').and.returnValue({ + on: (event, cb) => cb(), + end: () => {} + }); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/download.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/download.spec.js index 3ad4c2f119..c053adde16 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/download.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/download.spec.js @@ -1,7 +1,14 @@ const models = require('vn-loopback/server/server').models; +const fs = require('fs-extra'); describe('InvoiceOut download()', () => { it('should return the downloaded fine name', async() => { + spyOn(models.InvoiceContainer, 'container').and.returnValue({ + client: {root: '/path'} + }); + spyOn(fs, 'createReadStream').and.returnValue(new Promise(resolve => resolve('streamObject'))); + spyOn(fs, 'access').and.returnValue(true); + const result = await models.InvoiceOut.download(1); expect(result[1]).toEqual('application/pdf'); diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js new file mode 100644 index 0000000000..e0ed6c91cf --- /dev/null +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js @@ -0,0 +1,40 @@ +const models = require('vn-loopback/server/server').models; + +describe('InvoiceOut globalInvoicing()', () => { + const userId = 1; + const companyFk = 442; + const clientId = 1101; + const invoicedTicketId = 8; + const invoiceSerial = 'A'; + const activeCtx = { + accessToken: {userId: userId}, + }; + const ctx = {req: activeCtx}; + + it('should make a global invoicing', async() => { + spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); + + const tx = await models.InvoiceOut.beginTransaction({}); + const options = {transaction: tx}; + + try { + ctx.args = { + invoiceDate: new Date(), + maxShipped: new Date(), + fromClientId: clientId, + toClientId: clientId, + companyFk: companyFk + }; + const result = await models.InvoiceOut.globalInvoicing(ctx, options); + const ticket = await models.Ticket.findById(invoicedTicketId, null, options); + + expect(result.length).toBeGreaterThan(0); + expect(ticket.refFk).toContain(invoiceSerial); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/summary.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/summary.spec.js index 2db4077c35..4b35fc56c2 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/summary.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/summary.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('invoiceOut summary()', () => { +xdescribe('invoiceOut summary()', () => { it('should return a summary object containing data from one invoiceOut', async() => { const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 7a251f87ff..5e522f23d4 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -10,21 +10,21 @@ class Controller extends Dialog { this.invoice = { maxShipped: new Date() }; + } + $onInit() { this.getMinClientId(); this.getMaxClientId(); } getMinClientId() { - this.getClientId('min').then(res => { - this.invoice.fromClientId = res.data.id; - }); + this.getClientId('min') + .then(res => this.invoice.fromClientId = res.data.id); } getMaxClientId() { - this.getClientId('max').then(res => { - this.invoice.toClientId = res.data.id; - }); + this.getClientId('max') + .then(res => this.invoice.toClientId = res.data.id); } getClientId(func) { diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index f19030129b..35252c4069 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -1,61 +1,98 @@ import './index'; describe('InvoiceOut', () => { - describe('Component vnInvoiceOutManual', () => { + describe('Component vnInvoiceOutGlobalInvoicing', () => { let controller; let $httpBackend; + let $httpParamSerializer; beforeEach(ngModule('invoiceOut')); - beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { + beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; let $scope = $rootScope.$new(); - const $element = angular.element(''); + const $element = angular.element(''); const $transclude = { $$boundTransclude: { $$slots: [] } }; - controller = $componentController('vnInvoiceOutManual', {$element, $scope, $transclude}); + controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude}); })); + describe('getMinClientId()', () => { + it('should set the invoice fromClientId property', () => { + const filter = { + order: 'id ASC', + limit: 1 + }; + + const serializedParams = $httpParamSerializer({filter}); + $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1101}); + + controller.getMinClientId(); + $httpBackend.flush(); + + expect(controller.invoice.fromClientId).toEqual(1101); + }); + }); + + describe('getMaxClientId()', () => { + it('should set the invoice toClientId property', () => { + const filter = { + order: 'id DESC', + limit: 1 + }; + + const serializedParams = $httpParamSerializer({filter}); + $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); + + controller.getMaxClientId(); + $httpBackend.flush(); + + expect(controller.invoice.toClientId).toEqual(1112); + }); + }); + describe('responseHandler()', () => { - it('should throw an error when clientFk property is set and the maxShipped is not filled', () => { + it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.invoice = { - clientFk: 1101, - serial: 'T', - taxArea: 'B' + fromClientId: 1101, + toClientId: 1101 }; controller.responseHandler('accept'); - expect(controller.vnApp.showError).toHaveBeenCalledWith(`Client and the max shipped should be filled`); + expect(controller.vnApp.showError).toHaveBeenCalledWith(`Invoice date and the max date should be filled`); }); - it('should throw an error when some required fields are not filled in', () => { + it('should throw an error when fromClientId or toClientId properties are not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.invoice = { - ticketFk: 1101 + invoiceDate: new Date(), + maxShipped: new Date() }; controller.responseHandler('accept'); - expect(controller.vnApp.showError).toHaveBeenCalledWith(`Some fields are required`); + expect(controller.vnApp.showError).toHaveBeenCalledWith(`Choose a valid clients range`); }); - it('should make an http POST query and then call to the parent showSuccess() method', () => { + it('should make an http POST query and then call to the showSuccess() method', () => { jest.spyOn(controller.vnApp, 'showSuccess'); controller.invoice = { - ticketFk: 1101, - serial: 'T', - taxArea: 'B' + invoiceDate: new Date(), + maxShipped: new Date(), + fromClientId: 1101, + toClientId: 1101 }; - $httpBackend.expect('POST', `InvoiceOuts/createManualInvoice`).respond({id: 1}); + $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush();