From 34038b885c6f291b8cde321ffb0653e11769cc27 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Wed, 15 Sep 2021 17:58:58 +0200 Subject: [PATCH] refactor(newBankEntity): client.billingData and supplier.account refactor to use newBankEntity Dialog --- front/salix/components/bank-entity/index.js | 2 +- .../components/bank-entity/index.spec.js | 45 ++++++----------- modules/client/front/billing-data/index.html | 4 +- modules/client/front/billing-data/index.js | 12 +---- .../client/front/billing-data/index.spec.js | 4 +- modules/supplier/front/account/index.html | 7 +-- modules/supplier/front/account/index.js | 21 ++------ modules/supplier/front/account/index.spec.js | 50 ++++--------------- 8 files changed, 39 insertions(+), 106 deletions(-) diff --git a/front/salix/components/bank-entity/index.js b/front/salix/components/bank-entity/index.js index 154e39fc4..d4ac8030a 100644 --- a/front/salix/components/bank-entity/index.js +++ b/front/salix/components/bank-entity/index.js @@ -11,7 +11,7 @@ class Controller extends Dialog { throw new Error(`The country can't be empty`); return this.$http.post(`bankEntities`, this.data) - .then(res => super.responseHandler(response)) + .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))); } } diff --git a/front/salix/components/bank-entity/index.spec.js b/front/salix/components/bank-entity/index.spec.js index c288c3052..3c3de4c40 100644 --- a/front/salix/components/bank-entity/index.spec.js +++ b/front/salix/components/bank-entity/index.spec.js @@ -5,49 +5,34 @@ describe('Salix Component vnNewBankEntity', () => { let $httpBackend; let $scope; let $element; - let vnApp; beforeEach(ngModule('salix')); - beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _vnApp_) => { + beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; - vnApp = _vnApp_; - jest.spyOn(vnApp, 'showError'); $scope = $rootScope.$new(); - $element = angular.element(''); - controller = $componentController('vnNewBankEntity', {$element, $scope}); + $element = angular.element(''); + const $transclude = { + $$boundTransclude: { + $$slots: [] + } + }; + controller = $componentController('vnNewBankEntity', {$element, $scope, $transclude}); + controller.vnApp = {showSuccess: jest.fn()}; })); - describe('resetData()', () => { - it('should reset the location in the controller', () => { - expect(controller.data).toBeUndefined(); - - controller.resetData(); - - expect(controller.data).toEqual({}); - }); - }); - - describe('onAccept()', () => { - it('should throw an error if there is no country id in the location', () => { - jest.spyOn(controller.vnApp, 'showMessage'); - - controller.data = {}; - - controller.onAccept(); - - expect(controller.vnApp.showError).toHaveBeenCalledWith(`The country can't be empty`); - }); - - it('should do add the new bank entity', () => { + describe('responseHandler()', () => { + it('should show a success message after the query to bankEntities', () => { controller.data = { countryFk: 1 }; - $httpBackend.expectPOST('bankEntities', controller.data).respond(200, controller.data); + $httpBackend.expectPOST('bankEntities', controller.data).respond(200); - controller.onAccept(); + controller.responseHandler('accept'); $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); }); }); }); diff --git a/modules/client/front/billing-data/index.html b/modules/client/front/billing-data/index.html index ae1ad85a5..ff2e2f157 100644 --- a/modules/client/front/billing-data/index.html +++ b/modules/client/front/billing-data/index.html @@ -66,7 +66,7 @@ @@ -111,5 +111,5 @@ + on-accept="$ctrl.onAccept($data)"> \ No newline at end of file diff --git a/modules/client/front/billing-data/index.js b/modules/client/front/billing-data/index.js index dd43a661e..7056fa566 100644 --- a/modules/client/front/billing-data/index.js +++ b/modules/client/front/billing-data/index.js @@ -37,16 +37,8 @@ export default class Controller extends Section { return payMethod || iban || dueDay; } - onAddEntityClick(event) { - event.preventDefault(); - this.newBankEntity = { - countryFk: Number.parseInt(this.client.countryFk) - }; - this.$.bankEntity.open(); - } - - onResponse(response) { - this.client.bankEntityFk = response.id; + onAccept(data) { + this.client.bankEntityFk = data.id; } get ibanCountry() { diff --git a/modules/client/front/billing-data/index.spec.js b/modules/client/front/billing-data/index.spec.js index 60cc4025c..2e9e8ba97 100644 --- a/modules/client/front/billing-data/index.spec.js +++ b/modules/client/front/billing-data/index.spec.js @@ -35,10 +35,10 @@ describe('Client', () => { }); }); - describe('onResponse()', () => { + describe('onAccept()', () => { it('should assign the response id to the client bankEntityFk', () => { const expectedResponse = {id: 999}; - controller.onResponse(expectedResponse); + controller.onAccept(expectedResponse); expect(controller.client.bankEntityFk).toEqual(expectedResponse.id); }); diff --git a/modules/supplier/front/account/index.html b/modules/supplier/front/account/index.html index c8ec8ba31..cdaf79e8c 100644 --- a/modules/supplier/front/account/index.html +++ b/modules/supplier/front/account/index.html @@ -30,9 +30,10 @@ + vn-click-stop="bankEntity.show({index: $index})" + vn-tooltip="New bank entity"> + on-accept="$ctrl.onAccept($data)"> \ No newline at end of file diff --git a/modules/supplier/front/account/index.js b/modules/supplier/front/account/index.js index 26f4af988..1e21b983e 100644 --- a/modules/supplier/front/account/index.js +++ b/modules/supplier/front/account/index.js @@ -18,23 +18,10 @@ class Controller extends Section { }); } - onResponse(response) { - const data = this.$.model.data; - const supplierAccount = data[this.currentRowIndex]; - supplierAccount.bankEntityFk = response.id; - } - - showBankEntity(event, $index) { - if (event.defaultPrevented) return; - event.preventDefault(); - this.currentRowIndex = $index; - this.$.bankEntity.open(); - } - - onBankEntityAccept() { - const query = `SupplierAccounts/${this.$params.id}/createBankEntity`; - return this.$http.patch(query, this.newBankEntity) - .then(res => this.supplierAccount.bankEntityFk = res.data.id); + onAccept(data) { + const accounts = this.supplierAccounts; + const targetAccount = accounts[data.index]; + targetAccount.bankEntityFk = data.id; } onSubmit() { diff --git a/modules/supplier/front/account/index.spec.js b/modules/supplier/front/account/index.spec.js index cb206429b..77d135f24 100644 --- a/modules/supplier/front/account/index.spec.js +++ b/modules/supplier/front/account/index.spec.js @@ -4,11 +4,9 @@ describe('Supplier Component vnSupplierAccount', () => { let $scope; let $element; let controller; - let $httpBackend; beforeEach(ngModule('supplier')); beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { - $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $scope.bankEntity = { open: () => {} @@ -21,50 +19,20 @@ describe('Supplier Component vnSupplierAccount', () => { }; })); - describe('showBankEntity()', () => { - it('should do nothing if it default is prevented', () => { - const event = { - defaultPrevented: true, - preventDefault: () => {} - }; - jest.spyOn(event, 'preventDefault'); - jest.spyOn(controller.$.bankEntity, 'open'); + describe('onAccept()', () => { + it('should set the created bank entity id into the target account', () => { + controller.supplierAccounts = [{}, {}, {}]; - controller.showBankEntity(event); - - expect(event.preventDefault).not.toHaveBeenCalledWith(); - expect(controller.$.bankEntity.open).not.toHaveBeenCalledWith(); - }); - - it('should call preventDefault() and open() when the default is not prevented', () => { - const event = { - defaultPrevented: false, - preventDefault: () => {} + const data = { + id: 999, + index: 1 }; - jest.spyOn(event, 'preventDefault'); - jest.spyOn(controller.$.bankEntity, 'open'); + controller.onAccept(data); - controller.showBankEntity(event); + const targetAccount = controller.supplierAccounts[data.index]; - expect(event.preventDefault).toHaveBeenCalledWith(); - expect(controller.$.bankEntity.open).toHaveBeenCalledWith(); - }); - - it('should now request to create a new bank entity', () => { - controller.bankEntity = { - name: 'My new bank entity', - bic: 'ES1234', - countryFk: 1, - id: 2200 - }; - - const query = `SupplierAccounts/${controller.$.bankEntity.id}/createBankEntity`; - $httpBackend.expectPATCH(query).respond({id: 2200}); - controller.onBankEntityAccept(); - $httpBackend.flush(); - - expect(controller.supplierAccount.bankEntityFk).toEqual(controller.bankEntity.id); + expect(targetAccount.bankEntityFk).toEqual(data.id); }); }); });