From ea1d6c14e0d4890a5f0ba9d051e290330b2b1b7e Mon Sep 17 00:00:00 2001 From: jorgebl Date: Tue, 23 Mar 2021 16:02:24 +0100 Subject: [PATCH] Add parameteres in the url + test --- modules/zone/front/delivery-days/index.js | 25 +++++++++-- .../zone/front/delivery-days/index.spec.js | 43 ++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/modules/zone/front/delivery-days/index.js b/modules/zone/front/delivery-days/index.js index 408c77be2..ff369217e 100644 --- a/modules/zone/front/delivery-days/index.js +++ b/modules/zone/front/delivery-days/index.js @@ -3,8 +3,23 @@ import Section from 'salix/components/section'; import './style.scss'; class Controller extends Section { + constructor($element, $scope, $location) { + super($element, $scope); + this.$location = $location; + } $onInit() { - this.$.params = {}; + this.$.params = this.$location.search(); + if (!this.$.params) + this.$.params = {}; + this.$.data = null; + this.$http.get(`Zones/getEvents`, {params: this.$.params}) + .then(res => { + const data = res.data; + this.$.data = data; + const agencyModeFk = this.$.params.agencyModeFk; + const geoFk = this.$.params.geoFk; + this.$location.path('/zone/delivery-days').search({agencyModeFk, geoFk}); + }); } $postLink() { @@ -14,8 +29,11 @@ class Controller extends Section { this.$.data = null; this.$http.get(`Zones/getEvents`, {params: this.$.params}) .then(res => { - let data = res.data; + const data = res.data; this.$.data = data; + const agencyModeFk = this.$.params.agencyModeFk; + const geoFk = this.$.params.geoFk; + this.$location.path('/zone/delivery-days').search({agencyModeFk, geoFk}); if (!data.events.length) this.vnApp.showMessage(this.$t('No service for the specified zone')); }); @@ -27,7 +45,6 @@ class Controller extends Section { set deliveryMethodFk(value) { this._deliveryMethodFk = value; - this.$.params.agencyModeFk = null; let filter; if (value === 'pickUp') { filter = {where: {code: 'PICKUP'}}; @@ -67,6 +84,8 @@ class Controller extends Section { } } +Controller.$inject = ['$element', '$scope', '$location']; + ngModule.vnComponent('vnZoneDeliveryDays', { template: require('./index.html'), controller: Controller diff --git a/modules/zone/front/delivery-days/index.spec.js b/modules/zone/front/delivery-days/index.spec.js index c39b34296..10c2037bd 100644 --- a/modules/zone/front/delivery-days/index.spec.js +++ b/modules/zone/front/delivery-days/index.spec.js @@ -37,15 +37,50 @@ describe('Zone Component vnZoneDeliveryDays', () => { }); }); + describe('$onInit()', () => { + it('should set params empty by default', () => { + const expectedData = {events: []}; + const expectedUrl = '/zone/delivery-days'; + const params = {}; + + $httpBackend.when('GET', 'Zones/getEvents').respond({events: []}); + controller.$onInit(); + $httpBackend.flush(); + + expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl); + expect(controller.$.data).toBeDefined(); + expect(controller.$.data).toEqual(expectedData); + }); + + it('should do load the params that has been saved in the URL when page open', () => { + const expectedData = {events: [{zoneFk: 1}]}; + const expectedUrl = '/zone/delivery-days?agencyModeFk=1&geoFk=1'; + const params = { + agencyModeFk: 1, + geoFk: 1}; + + $httpBackend.when('GET', 'Zones/getEvents').respond({events: [{zoneFk: 1}]}); + controller.$onInit(); + $httpBackend.flush(); + + expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl); + expect(controller.$.data).toBeDefined(); + expect(controller.$.data).toEqual(expectedData); + }); + }); + describe('onSubmit()', () => { it('should make an HTTP GET query and then call the showMessage() method', () => { jest.spyOn(controller.vnApp, 'showMessage'); - const expectedData = {events: []}; + const expectedUrl = '/zone/delivery-days'; + const params = {}; + $httpBackend.when('GET', 'Zones/getEvents').respond({events: []}); controller.onSubmit(); $httpBackend.flush(); + expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl); expect(controller.$.data).toBeDefined(); expect(controller.$.data).toEqual(expectedData); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('No service for the specified zone'); @@ -53,10 +88,16 @@ describe('Zone Component vnZoneDeliveryDays', () => { it('should make an HTTP GET query and then set the data property', () => { const expectedData = {events: [{zoneFk: 1}]}; + const expectedUrl = '/zone/delivery-days?agencyModeFk=1&geoFk=1'; + const params = { + agencyModeFk: 1, + geoFk: 1}; + $httpBackend.when('GET', 'Zones/getEvents').respond({events: [{zoneFk: 1}]}); controller.onSubmit(); $httpBackend.flush(); + expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl); expect(controller.$.data).toBeDefined(); expect(controller.$.data).toEqual(expectedData); });