From 666ccbf370fcba2692c3e4b9f07f10b0ce246fba Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Fri, 6 Mar 2020 08:38:03 +0100 Subject: [PATCH] ticket.basicData address --- .../ticket/front/basic-data/step-one/index.js | 16 ++++++--- .../front/basic-data/step-one/index.spec.js | 34 ++++++++++++------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/modules/ticket/front/basic-data/step-one/index.js b/modules/ticket/front/basic-data/step-one/index.js index 820d3d2c6..120140c3d 100644 --- a/modules/ticket/front/basic-data/step-one/index.js +++ b/modules/ticket/front/basic-data/step-one/index.js @@ -22,7 +22,7 @@ class Controller { if (!value || !value.id) return; - this.onChangeClient(value.clientFk); + this.clientAddressesList(value.clientFk); } get clientId() { @@ -33,7 +33,8 @@ class Controller { this.ticket.clientFk = value; this.ticket.addressFk = null; - this.onChangeClient(value); + this.getClientDefaultAddress(value); + this.clientAddressesList(value); } get addressId() { @@ -68,7 +69,6 @@ class Controller { } } - get shipped() { return this.ticket && this.ticket.shipped; } @@ -127,7 +127,7 @@ class Controller { /* * Autocompletes address on client change */ - onChangeClient(value) { + clientAddressesList(value) { let filter = { include: [ { @@ -153,6 +153,14 @@ class Controller { }); } + getClientDefaultAddress(value) { + let query = `Clients/${value}`; + this.$http.get(query).then(res => { + if (res.data) + this.ticket.addressFk = res.data.defaultAddressFk; + }); + } + /* * Gets an agency from an specified zone */ diff --git a/modules/ticket/front/basic-data/step-one/index.spec.js b/modules/ticket/front/basic-data/step-one/index.spec.js index 86977a738..b1931d0bb 100644 --- a/modules/ticket/front/basic-data/step-one/index.spec.js +++ b/modules/ticket/front/basic-data/step-one/index.spec.js @@ -22,18 +22,18 @@ describe('Ticket', () => { })); describe('ticket() setter', () => { - it('should set ticket property and call onChangeClient() method', () => { - jest.spyOn(controller, 'onChangeClient'); + it('should set ticket property and call clientAddressesList() method', () => { + jest.spyOn(controller, 'clientAddressesList'); controller.ticket = {id: 1, clientFk: 101}; - expect(controller.onChangeClient).toHaveBeenCalledWith(101); + expect(controller.clientAddressesList).toHaveBeenCalledWith(101); }); - it(`should not call onChangeClient() method as the ticket doesn't have an ID`, () => { - jest.spyOn(controller, 'onChangeClient'); + it(`should not call clientAddressesList() method as the ticket doesn't have an ID`, () => { + jest.spyOn(controller, 'clientAddressesList'); controller.ticket = {}; - expect(controller.onChangeClient).not.toHaveBeenCalledWith(); + expect(controller.clientAddressesList).not.toHaveBeenCalledWith(); }); }); @@ -46,12 +46,12 @@ describe('Ticket', () => { }); describe('clientId() setter', () => { - it('should set clientId property and call onChangeClient() method ', () => { - jest.spyOn(controller, 'onChangeClient'); + it('should set clientId property and call clientAddressesList() method ', () => { + jest.spyOn(controller, 'clientAddressesList'); controller.ticket = {id: 1, clientId: 101}; controller.clientId = 102; - expect(controller.onChangeClient).toHaveBeenCalledWith(102); + expect(controller.clientAddressesList).toHaveBeenCalledWith(102); }); }); @@ -155,7 +155,6 @@ describe('Ticket', () => { }; controller.landed = landed; - expect(controller.getShipped).toHaveBeenCalledWith(expectedResult); }); }); @@ -230,7 +229,7 @@ describe('Ticket', () => { }); }); - describe('onChangeClient()', () => { + describe('clientAddressesList()', () => { it('should return a list of addresses from choosed client', async() => { const clientId = 102; let filter = { @@ -253,7 +252,18 @@ describe('Ticket', () => { $httpBackend.when('GET', `Clients/${clientId}/addresses?filter=${filter}`).respond(200); $httpBackend.expect('GET', `Clients/${clientId}/addresses?filter=${filter}`); - controller.onChangeClient(clientId); + controller.clientAddressesList(clientId); + $httpBackend.flush(); + }); + }); + + describe('getClientDefaultAddress()', () => { + it('should return the default address from choosed client', async() => { + const clientId = 102; + + $httpBackend.when('GET', `Clients/${clientId}`).respond(200); + $httpBackend.expect('GET', `Clients/${clientId}`); + controller.getClientDefaultAddress(clientId); $httpBackend.flush(); }); });