From e6b60d73f1a97ad8bf20644f9e804e79a306c519 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 7 Oct 2021 13:36:23 +0200 Subject: [PATCH] Added unit tests --- modules/invoiceOut/front/descriptor/index.js | 46 ++++---- .../invoiceOut/front/descriptor/index.spec.js | 101 +++++++++++++++--- modules/ticket/front/descriptor-menu/index.js | 2 +- .../front/descriptor-menu/index.spec.js | 94 +++++++++++++--- 4 files changed, 188 insertions(+), 55 deletions(-) diff --git a/modules/invoiceOut/front/descriptor/index.js b/modules/invoiceOut/front/descriptor/index.js index 6685602a06..129fe16d1a 100644 --- a/modules/invoiceOut/front/descriptor/index.js +++ b/modules/invoiceOut/front/descriptor/index.js @@ -14,29 +14,6 @@ class Controller extends Descriptor { return this.aclService.hasAny(['invoicing']); } - deleteInvoiceOut() { - return this.$http.post(`InvoiceOuts/${this.id}/delete`) - .then(() => this.$state.go('invoiceOut.index')) - .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut deleted'))); - } - - bookInvoiceOut() { - return this.$http.post(`InvoiceOuts/${this.invoiceOut.ref}/book`) - .then(() => this.$state.reload()) - .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut booked'))); - } - - createPdfInvoice() { - const invoiceId = this.invoiceOut.id; - return this.$http.post(`InvoiceOuts/${invoiceId}/createPdf`) - .then(() => this.reload()) - .then(() => { - const snackbarMessage = this.$t( - `The invoice PDF document has been regenerated`); - this.vnApp.showSuccess(snackbarMessage); - }); - } - get filter() { if (this.invoiceOut) return JSON.stringify({refFk: this.invoiceOut.ref}); @@ -76,6 +53,29 @@ class Controller extends Descriptor { // Prevents error when not defined } + deleteInvoiceOut() { + return this.$http.post(`InvoiceOuts/${this.id}/delete`) + .then(() => this.$state.go('invoiceOut.index')) + .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut deleted'))); + } + + bookInvoiceOut() { + return this.$http.post(`InvoiceOuts/${this.invoiceOut.ref}/book`) + .then(() => this.$state.reload()) + .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut booked'))); + } + + createPdfInvoice() { + const invoiceId = this.invoiceOut.id; + return this.$http.post(`InvoiceOuts/${invoiceId}/createPdf`) + .then(() => this.reload()) + .then(() => { + const snackbarMessage = this.$t( + `The invoice PDF document has been regenerated`); + this.vnApp.showSuccess(snackbarMessage); + }); + } + showCsvInvoice() { this.vnReport.showCsv('invoice', { recipientId: this.invoiceOut.client.id, diff --git a/modules/invoiceOut/front/descriptor/index.spec.js b/modules/invoiceOut/front/descriptor/index.spec.js index 0a5494b9ab..12430d44db 100644 --- a/modules/invoiceOut/front/descriptor/index.spec.js +++ b/modules/invoiceOut/front/descriptor/index.spec.js @@ -3,30 +3,20 @@ import './index'; describe('vnInvoiceOutDescriptor', () => { let controller; let $httpBackend; - const invoiceOut = {id: 1}; + let $httpParamSerializer; + const invoiceOut = { + id: 1, + client: {id: 1101} + }; beforeEach(ngModule('invoiceOut')); - beforeEach(inject(($componentController, _$httpBackend_) => { + beforeEach(inject(($componentController, _$httpParamSerializer_, _$httpBackend_) => { $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; controller = $componentController('vnInvoiceOutDescriptor', {$element: null}); })); - describe('createInvoicePdf()', () => { - it('should make a query and show a success snackbar', () => { - jest.spyOn(controller.vnApp, 'showSuccess'); - - controller.invoiceOut = invoiceOut; - - $httpBackend.whenGET(`InvoiceOuts/${invoiceOut.id}`).respond(); - $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/createPdf`).respond(); - controller.createInvoicePdf(); - $httpBackend.flush(); - - expect(controller.vnApp.showSuccess).toHaveBeenCalled(); - }); - }); - describe('loadData()', () => { it(`should perform a get query to store the invoice in data into the controller`, () => { const id = 1; @@ -39,4 +29,81 @@ describe('vnInvoiceOutDescriptor', () => { expect(controller.invoiceOut).toEqual(response); }); }); + + describe('createPdfInvoice()', () => { + it('should make a query to the createPdf() endpoint and show a success snackbar', () => { + jest.spyOn(controller.vnApp, 'showSuccess'); + + controller.invoiceOut = invoiceOut; + + $httpBackend.whenGET(`InvoiceOuts/${invoiceOut.id}`).respond(); + $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/createPdf`).respond(); + controller.createPdfInvoice(); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('showCsvInvoice()', () => { + it('should make a query to the csv invoice download endpoint and show a message snackbar', () => { + jest.spyOn(window, 'open').mockReturnThis(); + + controller.invoiceOut = invoiceOut; + + const expectedParams = { + invoiceId: invoiceOut.id, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + const expectedPath = `api/csv/invoice/download?${serializedParams}`; + controller.showCsvInvoice(); + + expect(window.open).toHaveBeenCalledWith(expectedPath); + }); + }); + + describe('sendPdfInvoice()', () => { + it('should make a query to the email invoice endpoint and show a message snackbar', () => { + jest.spyOn(controller.vnApp, 'showMessage'); + + controller.invoiceOut = invoiceOut; + + const $data = {email: 'brucebanner@gothamcity.com'}; + const expectedParams = { + invoiceId: invoiceOut.id, + recipient: $data.email, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + + $httpBackend.expectGET(`email/invoice?${serializedParams}`).respond(); + controller.sendPdfInvoice($data); + $httpBackend.flush(); + + expect(controller.vnApp.showMessage).toHaveBeenCalled(); + }); + }); + + describe('sendCsvInvoice()', () => { + it('should make a query to the csv invoice send endpoint and show a message snackbar', () => { + jest.spyOn(controller.vnApp, 'showMessage'); + + controller.invoiceOut = invoiceOut; + + const $data = {email: 'brucebanner@gothamcity.com'}; + const expectedParams = { + invoiceId: invoiceOut.id, + recipient: $data.email, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + + $httpBackend.expectGET(`csv/invoice/send?${serializedParams}`).respond(); + controller.sendCsvInvoice($data); + $httpBackend.flush(); + + expect(controller.vnApp.showMessage).toHaveBeenCalled(); + }); + }); }); diff --git a/modules/ticket/front/descriptor-menu/index.js b/modules/ticket/front/descriptor-menu/index.js index 6292acbd8d..142f44989c 100644 --- a/modules/ticket/front/descriptor-menu/index.js +++ b/modules/ticket/front/descriptor-menu/index.js @@ -242,7 +242,7 @@ class Controller extends Section { .then(() => this.vnApp.showSuccess(this.$t('Ticket invoiced'))); } - createInvoicePdf() { + createPdfInvoice() { const invoiceId = this.ticket.invoiceOut.id; return this.$http.post(`InvoiceOuts/${invoiceId}/createPdf`) .then(() => this.reload()) diff --git a/modules/ticket/front/descriptor-menu/index.spec.js b/modules/ticket/front/descriptor-menu/index.spec.js index eabb772355..5413545fe7 100644 --- a/modules/ticket/front/descriptor-menu/index.spec.js +++ b/modules/ticket/front/descriptor-menu/index.spec.js @@ -2,6 +2,7 @@ import './index.js'; describe('Ticket Component vnTicketDescriptorMenu', () => { let $httpBackend; + let $httpParamSerializer; let controller; let $state; @@ -25,8 +26,9 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { beforeEach(ngModule('ticket')); - beforeEach(inject(($componentController, _$httpBackend_, _$state_) => { + beforeEach(inject(($componentController, _$httpBackend_, _$httpParamSerializer_, _$state_) => { $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; $state = _$state_; $state.params.id = 16; $state.getCurrentPath = () => [null, {state: {name: 'ticket'}}]; @@ -104,31 +106,33 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { }); }); - describe('showDeliveryNote()', () => { + describe('showPdfDeliveryNote()', () => { it('should open a new window showing a delivery note PDF document', () => { - jest.spyOn(controller.vnReport, 'show'); + jest.spyOn(window, 'open').mockReturnThis(); - window.open = jasmine.createSpy('open'); - const params = { - recipientId: ticket.client.id, - ticketId: ticket.id + const expectedParams = { + ticketId: ticket.id, + recipientId: ticket.client.id }; - controller.showDeliveryNote(); + const serializedParams = $httpParamSerializer(expectedParams); + const expectedPath = `api/report/delivery-note?${serializedParams}`; + controller.showPdfDeliveryNote(); - expect(controller.vnReport.show).toHaveBeenCalledWith('delivery-note', params); + expect(window.open).toHaveBeenCalledWith(expectedPath); }); }); - describe('sendDeliveryNote()', () => { + describe('sendPdfDeliveryNote()', () => { it('should make a query and call vnApp.showMessage()', () => { jest.spyOn(controller.vnEmail, 'send'); + const $data = {email: 'brucebanner@gothamcity.com'}; const params = { - recipient: ticket.client.email, + recipient: $data.email, recipientId: ticket.client.id, ticketId: ticket.id }; - controller.sendDeliveryNote(); + controller.sendPdfDeliveryNote($data); expect(controller.vnEmail.send).toHaveBeenCalledWith('delivery-note', params); }); @@ -149,13 +153,13 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { }); }); - describe('createInvoicePdf()', () => { + describe('createPdfInvoice()', () => { it('should make a query and show a success snackbar', () => { jest.spyOn(controller.vnApp, 'showSuccess'); $httpBackend.whenGET(`Tickets/16`).respond(); $httpBackend.expectPOST(`InvoiceOuts/${ticket.invoiceOut.id}/createPdf`).respond(); - controller.createInvoicePdf(); + controller.createPdfInvoice(); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); @@ -222,4 +226,66 @@ describe('Ticket Component vnTicketDescriptorMenu', () => { }); }); }); + + xdescribe('showCsvInvoice()', () => { + it('should make a query to the csv invoice download endpoint and show a message snackbar', () => { + jest.spyOn(window, 'open').mockReturnThis(); + + controller.invoiceOut = invoiceOut; + + const expectedParams = { + invoiceId: invoiceOut.id, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + const expectedPath = `api/csv/invoice/download?${serializedParams}`; + controller.showCsvInvoice(); + + expect(window.open).toHaveBeenCalledWith(expectedPath); + }); + }); + + xdescribe('sendPdfInvoice()', () => { + it('should make a query to the email invoice endpoint and show a message snackbar', () => { + jest.spyOn(controller.vnApp, 'showMessage'); + + controller.invoiceOut = invoiceOut; + + const $data = {email: 'brucebanner@gothamcity.com'}; + const expectedParams = { + invoiceId: invoiceOut.id, + recipient: $data.email, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + + $httpBackend.expectGET(`email/invoice?${serializedParams}`).respond(); + controller.sendPdfInvoice($data); + $httpBackend.flush(); + + expect(controller.vnApp.showMessage).toHaveBeenCalled(); + }); + }); + + xdescribe('sendCsvInvoice()', () => { + it('should make a query to the csv invoice send endpoint and show a message snackbar', () => { + jest.spyOn(controller.vnApp, 'showMessage'); + + controller.invoiceOut = invoiceOut; + + const $data = {email: 'brucebanner@gothamcity.com'}; + const expectedParams = { + invoiceId: invoiceOut.id, + recipient: $data.email, + recipientId: invoiceOut.client.id + }; + const serializedParams = $httpParamSerializer(expectedParams); + + $httpBackend.expectGET(`csv/invoice/send?${serializedParams}`).respond(); + controller.sendCsvInvoice($data); + $httpBackend.flush(); + + expect(controller.vnApp.showMessage).toHaveBeenCalled(); + }); + }); });