From 568579f46170736aeeaffa6fb1eb4d53aa9fd50f Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 12 May 2022 08:27:04 +0200 Subject: [PATCH] refactor: pull request changes --- modules/invoiceIn/front/tax/index.html | 17 +++++----- modules/invoiceIn/front/tax/index.js | 35 ++++++++++--------- modules/invoiceIn/front/tax/index.spec.js | 41 +++++++++++++++++++---- modules/invoiceIn/front/tax/locale/es.yml | 7 ++-- 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/modules/invoiceIn/front/tax/index.html b/modules/invoiceIn/front/tax/index.html index 44ceef8074..acc9cf4924 100644 --- a/modules/invoiceIn/front/tax/index.html +++ b/modules/invoiceIn/front/tax/index.html @@ -35,7 +35,7 @@ {{id}}: {{name}} @@ -105,31 +105,32 @@ - + -
-
{{$ctrl.$t('New expence')}}
+
+
{{$ctrl.$t('New expense')}}
+ ng-model="$ctrl.expense.code" + required="true" + vn-focus> + ng-model="$ctrl.expense.isWithheld"> diff --git a/modules/invoiceIn/front/tax/index.js b/modules/invoiceIn/front/tax/index.js index 48fb98d4ea..d05a77f29f 100644 --- a/modules/invoiceIn/front/tax/index.js +++ b/modules/invoiceIn/front/tax/index.js @@ -3,6 +3,10 @@ import Section from 'salix/components/section'; import UserError from 'core/lib/user-error'; class Controller extends Section { + constructor($element, $, vnWeekDays) { + super($element, $); + this.expense = {}; + } taxRate(invoiceInTax, taxRateSelection) { const taxTypeSage = taxRateSelection && taxRateSelection.rate; const taxableBase = invoiceInTax && invoiceInTax.taxableBase; @@ -29,23 +33,24 @@ class Controller extends Section { } onResponse() { - if (!this.expence) - throw new UserError(`The fields can't be empty`); - else if (!this.expence.code) - throw new UserError(`The code can't be empty`); - else if (!this.expence.description) - throw new UserError(`The description can't be empty`); + try { + if (!this.expense.code) + throw new Error(`The code can't be empty`); + if (!this.expense.description) + throw new UserError(`The description can't be empty`); - const params = []; - params.push({ - id: this.expence.code, - isWithheld: this.expence.isWithheld, - name: this.expence.description - }); + const data = [{ + id: this.expense.code, + isWithheld: this.expense.isWithheld, + name: this.expense.description + }]; - this.$http.post(`Expenses`, params) .then(() => { - this.vnApp.showSuccess(this.$t('Expence saved!')); - }); + this.$http.post(`Expenses`, data) .then(() => { + this.vnApp.showSuccess(this.$t('Expense saved!')); + }); + } catch (e) { + this.vnApp.showError(this.$t(e.message)); + } } } diff --git a/modules/invoiceIn/front/tax/index.spec.js b/modules/invoiceIn/front/tax/index.spec.js index bc79638994..c62ada9ca8 100644 --- a/modules/invoiceIn/front/tax/index.spec.js +++ b/modules/invoiceIn/front/tax/index.spec.js @@ -1,6 +1,7 @@ import './index.js'; import watcher from 'core/mocks/watcher'; import crudModel from 'core/mocks/crud-model'; +const UserError = require('vn-loopback/util/user-error'); describe('InvoiceIn', () => { describe('Component tax', () => { @@ -61,25 +62,51 @@ describe('InvoiceIn', () => { describe('onResponse()', () => { it('should return success message', () => { - controller.expence = { + controller.expense = { code: 7050000005, isWithheld: 0, description: 'Test' }; - const params = [{ - id: controller.expence.code, - isWithheld: controller.expence.isWithheld, - name: controller.expence.description + const data = [{ + id: controller.expense.code, + isWithheld: controller.expense.isWithheld, + name: controller.expense.description }]; jest.spyOn(controller.vnApp, 'showSuccess'); - $httpBackend.expect('POST', `Expenses`, params).respond(); + $httpBackend.expect('POST', `Expenses`, data).respond(); controller.onResponse(); $httpBackend.flush(); - expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Expence saved!'); + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Expense saved!'); + }); + + it('should return an error if code is empty', () => { + controller.expense = { + code: null, + isWithheld: 0, + description: 'Test' + }; + + jest.spyOn(controller.vnApp, 'showError'); + controller.onResponse(); + + expect(controller.vnApp.showError).toHaveBeenCalledWith(`The code can't be empty`); + }); + + it('should return an error if description is empty', () => { + controller.expense = { + code: 7050000005, + isWithheld: 0, + description: null + }; + + jest.spyOn(controller.vnApp, 'showError'); + controller.onResponse(); + + expect(controller.vnApp.showError).toHaveBeenCalledWith(`The description can't be empty`); }); }); }); diff --git a/modules/invoiceIn/front/tax/locale/es.yml b/modules/invoiceIn/front/tax/locale/es.yml index 220802d9af..3ff68ea402 100644 --- a/modules/invoiceIn/front/tax/locale/es.yml +++ b/modules/invoiceIn/front/tax/locale/es.yml @@ -1,6 +1,7 @@ -Create expence: Crear gasto -New expence: Nuevo gasto +Create expense: Crear gasto +New expense: Nuevo gasto It's a withholding: Es una retención The fields can't be empty: Los campos no pueden estar vacíos The code can't be empty: El código no puede estar vacío -The description can't be empty: La descripción no puede estar vacía \ No newline at end of file +The description can't be empty: La descripción no puede estar vacía +Expense saved!: Gasto guardado! \ No newline at end of file