Add parameteres in the url + test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
jorgebl 2021-03-23 16:02:24 +01:00
parent e2db0d5742
commit ea1d6c14e0
2 changed files with 64 additions and 4 deletions

View File

@ -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

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()', () => {
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);
});