From 7db4945a6d53b062b46488e8c70dcf8218d343dd Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Tue, 8 Oct 2019 17:27:27 +0200 Subject: [PATCH] #1702 Autocomplete data refresh and #1754 watcher --- e2e/helpers/selectors.js | 1 + e2e/paths/04-item-module/03_tax.spec.js | 27 ++++++-- .../components/autocomplete/autocomplete.js | 4 +- front/core/components/watcher/watcher.js | 7 +- modules/item/front/tax/index.html | 5 +- modules/item/front/tax/index.js | 21 +++--- modules/item/front/tax/index.spec.js | 66 +++++++++---------- 7 files changed, 72 insertions(+), 59 deletions(-) diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index 351053a02..a1aac1adf 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -260,6 +260,7 @@ export default { submitItemTagsButton: `vn-item-tags ${components.vnSubmit}` }, itemTax: { + undoChangesButton: 'vn-item-tax vn-button-bar > vn-button[label="Undo changes"]', firstClassAutocomplete: 'vn-item-tax vn-horizontal:nth-child(1) > vn-autocomplete[field="tax.taxClassFk"]', secondClassAutocomplete: 'vn-item-tax vn-horizontal:nth-child(2) > vn-autocomplete[field="tax.taxClassFk"]', thirdClassAutocomplete: 'vn-item-tax vn-horizontal:nth-child(3) > vn-autocomplete[field="tax.taxClassFk"]', diff --git a/e2e/paths/04-item-module/03_tax.spec.js b/e2e/paths/04-item-module/03_tax.spec.js index 38861f1bf..903d05f10 100644 --- a/e2e/paths/04-item-module/03_tax.spec.js +++ b/e2e/paths/04-item-module/03_tax.spec.js @@ -1,8 +1,7 @@ import selectors from '../../helpers/selectors.js'; import createNightmare from '../../helpers/nightmare'; -// #1702 Autocomplete no siempre refresca al cancelar formulario -xdescribe('Item edit tax path', () => { +describe('Item edit tax path', () => { const nightmare = createNightmare(); beforeAll(() => { @@ -14,9 +13,9 @@ xdescribe('Item edit tax path', () => { it(`should add the item tax to all countries`, async() => { const result = await nightmare - .autocompleteSearch(selectors.itemTax.firstClassAutocomplete, 'Reduced VAT') + .autocompleteSearch(selectors.itemTax.firstClassAutocomplete, 'General VAT') .autocompleteSearch(selectors.itemTax.secondClassAutocomplete, 'General VAT') - .autocompleteSearch(selectors.itemTax.thirdClassAutocomplete, 'Reduced VAT') + .autocompleteSearch(selectors.itemTax.thirdClassAutocomplete, 'General VAT') .waitToClick(selectors.itemTax.submitTaxButton) .waitForLastSnackbar(); @@ -28,7 +27,7 @@ xdescribe('Item edit tax path', () => { .reloadSection('item.card.tax') .waitToGetProperty(`${selectors.itemTax.firstClassAutocomplete} input`, 'value'); - expect(firstVatType).toEqual('Reduced VAT'); + expect(firstVatType).toEqual('General VAT'); }); it(`should confirm the second item tax class was edited`, async() => { @@ -42,6 +41,22 @@ xdescribe('Item edit tax path', () => { const thirdVatType = await nightmare .waitToGetProperty(`${selectors.itemTax.thirdClassAutocomplete} input`, 'value'); - expect(thirdVatType).toEqual('Reduced VAT'); + expect(thirdVatType).toEqual('General VAT'); + }); + + it(`should edit the first class without saving the form`, async() => { + const firstVatType = await nightmare + .autocompleteSearch(selectors.itemTax.firstClassAutocomplete, 'Reduced VAT') + .waitToGetProperty(`${selectors.itemTax.firstClassAutocomplete} input`, 'value'); + + expect(firstVatType).toEqual('Reduced VAT'); + }); + + it(`should now click the undo changes button and see the changes works`, async() => { + const firstVatType = await nightmare + .waitToClick(selectors.itemTax.undoChangesButton) + .waitToGetProperty(`${selectors.itemTax.firstClassAutocomplete} input`, 'value'); + + expect(firstVatType).toEqual('General VAT'); }); }); diff --git a/front/core/components/autocomplete/autocomplete.js b/front/core/components/autocomplete/autocomplete.js index 469bb9e93..ce081b58a 100755 --- a/front/core/components/autocomplete/autocomplete.js +++ b/front/core/components/autocomplete/autocomplete.js @@ -136,8 +136,8 @@ export default class Autocomplete extends Input { return; const selection = this.fetchSelection(); - if (!this.selection) - this.selection = selection; + + this.selection = selection; } fetchSelection() { diff --git a/front/core/components/watcher/watcher.js b/front/core/components/watcher/watcher.js index a085ee45f..02b92dda6 100644 --- a/front/core/components/watcher/watcher.js +++ b/front/core/components/watcher/watcher.js @@ -223,11 +223,8 @@ export default class Watcher extends Component { } loadOriginalData() { - Object.keys(this.data).forEach(key => { - delete this.data[key]; - }); - - this.data = Object.assign(this.data, this.orgData); + const orgData = JSON.parse(JSON.stringify(this.orgData)); + this.data = Object.assign(this.data, orgData); this.setPristine(); } diff --git a/modules/item/front/tax/index.html b/modules/item/front/tax/index.html index e0d58ba1a..62c4e305c 100644 --- a/modules/item/front/tax/index.html +++ b/modules/item/front/tax/index.html @@ -1,5 +1,5 @@ @@ -11,7 +11,7 @@
- + diff --git a/modules/item/front/tax/index.js b/modules/item/front/tax/index.js index f4cc6f73e..84a847e05 100644 --- a/modules/item/front/tax/index.js +++ b/modules/item/front/tax/index.js @@ -1,11 +1,11 @@ import ngModule from '../module'; export default class Controller { - constructor($stateParams, $http, $translate, vnApp) { + constructor($stateParams, $http, $translate, $scope) { + this.$ = $scope; this.$http = $http; this.$stateParams = $stateParams; this._ = $translate; - this.vnApp = vnApp; } $onInit() { @@ -21,9 +21,8 @@ export default class Controller { }] }; - let urlFilter = encodeURIComponent(JSON.stringify(filter)); - let url = `/item/api/Items/${this.$stateParams.id}/taxes?filter=${urlFilter}`; - this.$http.get(url).then(json => { + let url = `api/Items/${this.$stateParams.id}/taxes`; + this.$http.get(url, {params: {filter}}).then(json => { this.taxes = json.data; }); } @@ -33,14 +32,16 @@ export default class Controller { for (let tax of this.taxes) data.push({id: tax.id, taxClassFk: tax.taxClassFk}); - let url = `/item/api/Items/updateTaxes`; - this.$http.post(url, data).then( - () => this.vnApp.showSuccess(this._.instant('Data saved!')) - ); + this.$.watcher.check(); + let url = `api/Items/updateTaxes`; + this.$http.post(url, data).then(() => { + this.$.watcher.notifySaved(); + this.$.watcher.updateOriginalData(); + }); } } -Controller.$inject = ['$stateParams', '$http', '$translate', 'vnApp']; +Controller.$inject = ['$stateParams', '$http', '$translate', '$scope']; ngModule.component('vnItemTax', { template: require('./index.html'), diff --git a/modules/item/front/tax/index.spec.js b/modules/item/front/tax/index.spec.js index 56c3b9306..59d8ad892 100644 --- a/modules/item/front/tax/index.spec.js +++ b/modules/item/front/tax/index.spec.js @@ -2,65 +2,65 @@ import './index.js'; describe('Item', () => { describe('Component vnItemTax', () => { + let $element; let $stateParams; let controller; let $httpBackend; - let vnApp; beforeEach(angular.mock.module('item', $translateProvider => { $translateProvider.translations('en', {}); })); - beforeEach(angular.mock.inject(($componentController, _$httpBackend_, _$stateParams_, _vnApp_) => { + beforeEach(angular.mock.inject((_$httpBackend_, $rootScope, _$stateParams_, $compile) => { $stateParams = _$stateParams_; $stateParams.id = 1; $httpBackend = _$httpBackend_; - vnApp = _vnApp_; - spyOn(vnApp, 'showSuccess'); - controller = $componentController('vnItemTax', {$state: $stateParams}); + + $httpBackend.whenGET(url => url.startsWith(`api/TaxClasses`)) + .respond([ + {id: 1, description: 'Reduced VAT', code: 'R'}, + {id: 2, description: 'General VAT', code: 'G'} + ]); + + $httpBackend.whenGET(url => url.startsWith(`api/Items/${$stateParams.id}/taxes`)) + .respond([ + {id: 1, taxClassFk: 1} + ]); + + $element = $compile(` { + $element.remove(); + }); + describe('getTaxes()', () => { it('should perform a query to set the array of taxes into the controller', () => { - let filter = { - fields: ['id', 'countryFk', 'taxClassFk'], - include: [{ - relation: 'country', - scope: {fields: ['country']} - }] - }; - let response = [{id: 1, taxClassFk: 1}]; - filter = encodeURIComponent(JSON.stringify(filter)); - $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response); - $httpBackend.expect('GET', `/item/api/Items/1/taxes?filter=${filter}`); - controller.$onInit(); $httpBackend.flush(); - expect(controller.taxes).toEqual(response); + expect(controller.taxes[0].id).toEqual(1); + expect(controller.taxes[0].taxClassFk).toEqual(1); }); }); describe('submit()', () => { it('should perform a post to update taxes', () => { - let filter = { - fields: ['id', 'countryFk', 'taxClassFk'], - include: [{ - relation: 'country', - scope: {fields: ['country']} - }] - }; - let response = [{id: 1, taxClassFk: 1}]; - filter = encodeURIComponent(JSON.stringify(filter)); - $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response); - controller.$onInit(); - $httpBackend.flush(); + spyOn(controller.$.watcher, 'notifySaved'); + spyOn(controller.$.watcher, 'updateOriginalData'); + controller.taxes = [ + {id: 37, countryFk: 1, taxClassFk: 1, country: {id: 1, country: 'España'}} + ]; + controller.$.watcher.data = [ + {id: 37, countryFk: 1, taxClassFk: 2, country: {id: 1, country: 'España'}} + ]; - $httpBackend.when('POST', `/item/api/Items/updateTaxes`).respond('ok'); - $httpBackend.expect('POST', `/item/api/Items/updateTaxes`); + $httpBackend.whenPOST(`api/Items/updateTaxes`).respond('oki doki'); controller.submit(); $httpBackend.flush(); - expect(vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + expect(controller.$.watcher.notifySaved).toHaveBeenCalledWith(); + expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); });