From f34130cbd774c6cd9b28024ad1e61d81e0cd4527 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 10 Nov 2021 14:48:17 +0100 Subject: [PATCH] feat(invoiceOut): separate index.js and jest test --- .../invoiceOut/front/descriptor-menu/index.js | 24 +++--- .../front/descriptor-menu/index.spec.js | 17 +--- .../invoiceOut/front/descriptor/index.html | 2 +- .../invoiceOut/front/descriptor/index.spec.js | 85 +------------------ 4 files changed, 18 insertions(+), 110 deletions(-) diff --git a/modules/invoiceOut/front/descriptor-menu/index.js b/modules/invoiceOut/front/descriptor-menu/index.js index 864e21ab2..6f764a3d0 100644 --- a/modules/invoiceOut/front/descriptor-menu/index.js +++ b/modules/invoiceOut/front/descriptor-menu/index.js @@ -3,8 +3,10 @@ import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { - constructor($element, $) { + constructor($element, $, vnReport, vnEmail) { super($element, $); + this.vnReport = vnReport; + this.vnEmail = vnEmail; } get invoiceOut() { @@ -13,6 +15,8 @@ class Controller extends Section { set invoiceOut(value) { this._invoiceOut = value; + if (value) + this.id = value.id; } loadData() { @@ -31,8 +35,8 @@ class Controller extends Section { } ] }; - return this.getData(`InvoiceOuts/${this.invoiceOut.id}`, {filter}) - .then(res => this.invoice = res.data); + return this.$http.get(`InvoiceOuts/${this.invoiceOut.id}`, {filter}) + .then(res => this.invoiceOut = res.data); } reload() { return this.loadData().then(() => { @@ -48,8 +52,8 @@ class Controller extends Section { deleteInvoiceOut() { return this.$http.post(`InvoiceOuts/${this.invoiceOut.id}/delete`) .then(() => this.$state.go('invoiceOut.index')) - .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut deleted'))) - .then(() => this.close()); + .then(() => this.$state.reload()) + .then(() => this.vnApp.showSuccess(this.$t('InvoiceOut deleted'))); } bookInvoiceOut() { @@ -59,8 +63,7 @@ class Controller extends Section { } createPdfInvoice() { - const invoiceId = this.invoiceOut.id; - return this.$http.post(`InvoiceOuts/${invoiceId}/createPdf`) + return this.$http.post(`InvoiceOuts/${this.id}/createPdf`) .then(() => this.reload()) .then(() => { const snackbarMessage = this.$t( @@ -72,7 +75,7 @@ class Controller extends Section { showCsvInvoice() { this.vnReport.showCsv('invoice', { recipientId: this.invoiceOut.client.id, - invoiceId: this.id, + invoiceId: this.id }); } @@ -95,17 +98,18 @@ class Controller extends Section { showExportationLetter() { this.vnReport.show('exportation', { recipientId: this.invoiceOut.client.id, - invoiceId: this.id, + invoiceId: this.id }); } } -Controller.$inject = ['$element', '$scope']; +Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail']; ngModule.vnComponent('vnInvoiceOutDescriptorMenu', { template: require('./index.html'), controller: Controller, bindings: { invoiceOut: '<', + parentReload: '&' } }); diff --git a/modules/invoiceOut/front/descriptor-menu/index.spec.js b/modules/invoiceOut/front/descriptor-menu/index.spec.js index 12430d44d..19822f094 100644 --- a/modules/invoiceOut/front/descriptor-menu/index.spec.js +++ b/modules/invoiceOut/front/descriptor-menu/index.spec.js @@ -1,6 +1,6 @@ import './index'; -describe('vnInvoiceOutDescriptor', () => { +describe('vnInvoiceOutDescriptorMenu', () => { let controller; let $httpBackend; let $httpParamSerializer; @@ -14,22 +14,9 @@ describe('vnInvoiceOutDescriptor', () => { beforeEach(inject(($componentController, _$httpParamSerializer_, _$httpBackend_) => { $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; - controller = $componentController('vnInvoiceOutDescriptor', {$element: null}); + controller = $componentController('vnInvoiceOutDescriptorMenu', {$element: null}); })); - describe('loadData()', () => { - it(`should perform a get query to store the invoice in data into the controller`, () => { - const id = 1; - const response = {id: 1}; - - $httpBackend.expectGET(`InvoiceOuts/${id}`).respond(response); - controller.id = id; - $httpBackend.flush(); - - 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'); diff --git a/modules/invoiceOut/front/descriptor/index.html b/modules/invoiceOut/front/descriptor/index.html index 9c3685338..135eae0e1 100644 --- a/modules/invoiceOut/front/descriptor/index.html +++ b/modules/invoiceOut/front/descriptor/index.html @@ -4,7 +4,7 @@ diff --git a/modules/invoiceOut/front/descriptor/index.spec.js b/modules/invoiceOut/front/descriptor/index.spec.js index 12430d44d..987763b0a 100644 --- a/modules/invoiceOut/front/descriptor/index.spec.js +++ b/modules/invoiceOut/front/descriptor/index.spec.js @@ -3,17 +3,11 @@ import './index'; describe('vnInvoiceOutDescriptor', () => { let controller; let $httpBackend; - let $httpParamSerializer; - const invoiceOut = { - id: 1, - client: {id: 1101} - }; beforeEach(ngModule('invoiceOut')); - beforeEach(inject(($componentController, _$httpParamSerializer_, _$httpBackend_) => { + beforeEach(inject(($componentController, _$httpBackend_) => { $httpBackend = _$httpBackend_; - $httpParamSerializer = _$httpParamSerializer_; controller = $componentController('vnInvoiceOutDescriptor', {$element: null}); })); @@ -29,81 +23,4 @@ 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(); - }); - }); });