2747 Zone delivery days save parameters in the url #581

Closed
Ghost wants to merge 2 commits from 2747-zone_delivery_days_url_parameters into dev
2 changed files with 70 additions and 4 deletions

View File

@ -3,19 +3,43 @@ import Section from 'salix/components/section';
import './style.scss'; import './style.scss';
class Controller extends Section { class Controller extends Section {
constructor($element, $scope, $location) {
super($element, $scope);
this.$location = $location;
}
$onInit() { $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() { $postLink() {
this.deliveryMethodFk = 'delivery'; this.deliveryMethodFk = 'delivery';
} }
onSubmit() { onSubmit() {
this.$.data = null; this.$.data = null;
this.$http.get(`Zones/getEvents`, {params: this.$.params}) this.$http.get(`Zones/getEvents`, {params: this.$.params})
.then(res => { .then(res => {
let data = res.data; const data = res.data;
this.$.data = 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) if (!data.events.length)
this.vnApp.showMessage(this.$t('No service for the specified zone')); this.vnApp.showMessage(this.$t('No service for the specified zone'));
}); });
@ -27,7 +51,6 @@ class Controller extends Section {
set deliveryMethodFk(value) { set deliveryMethodFk(value) {
this._deliveryMethodFk = value; this._deliveryMethodFk = value;
this.$.params.agencyModeFk = null;
let filter; let filter;
if (value === 'pickUp') { if (value === 'pickUp') {
filter = {where: {code: 'PICKUP'}}; filter = {where: {code: 'PICKUP'}};
@ -67,6 +90,8 @@ class Controller extends Section {
} }
} }
Controller.$inject = ['$element', '$scope', '$location'];
ngModule.vnComponent('vnZoneDeliveryDays', { ngModule.vnComponent('vnZoneDeliveryDays', {
template: require('./index.html'), template: require('./index.html'),
controller: Controller controller: Controller

View File

@ -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()', () => { describe('onSubmit()', () => {
it('should make an HTTP GET query and then call the showMessage() method', () => { it('should make an HTTP GET query and then call the showMessage() method', () => {
jest.spyOn(controller.vnApp, 'showMessage'); jest.spyOn(controller.vnApp, 'showMessage');
const expectedData = {events: []}; const expectedData = {events: []};
const expectedUrl = '/zone/delivery-days';
const params = {};
$httpBackend.when('GET', 'Zones/getEvents').respond({events: []}); $httpBackend.when('GET', 'Zones/getEvents').respond({events: []});
controller.onSubmit(); controller.onSubmit();
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl);
expect(controller.$.data).toBeDefined(); expect(controller.$.data).toBeDefined();
expect(controller.$.data).toEqual(expectedData); expect(controller.$.data).toEqual(expectedData);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('No service for the specified zone'); 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', () => { it('should make an HTTP GET query and then set the data property', () => {
const expectedData = {events: [{zoneFk: 1}]}; 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}]}); $httpBackend.when('GET', 'Zones/getEvents').respond({events: [{zoneFk: 1}]});
controller.onSubmit(); controller.onSubmit();
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$location.path('/zone/delivery-days').search(params).$$url).toEqual(expectedUrl);
expect(controller.$.data).toBeDefined(); expect(controller.$.data).toBeDefined();
expect(controller.$.data).toEqual(expectedData); expect(controller.$.data).toEqual(expectedData);
}); });