diff --git a/client/core/src/autocomplete/autocomplete.js b/client/core/src/autocomplete/autocomplete.js index 3f0bb68df..98ad4675a 100644 --- a/client/core/src/autocomplete/autocomplete.js +++ b/client/core/src/autocomplete/autocomplete.js @@ -73,7 +73,9 @@ class Autocomplete extends Component { this.finding = true; if (value && value.hasOwnProperty(this.valueField)) { this._field = value[this.valueField]; - this.setMultiField(value[this.valueField]); + if (this.multiple) { + this.setMultiField(value[this.valueField]); + } } else { this.setValue(value); } @@ -156,7 +158,6 @@ class Autocomplete extends Component { showItem(item) { this.displayValue = item ? item[this.showField] : ''; this.field = item; - this.setMultiField(item); } getRequestFields() { diff --git a/client/core/src/autocomplete/autocomplete.spec.js b/client/core/src/autocomplete/autocomplete.spec.js index 21dbcec9b..67d48c147 100644 --- a/client/core/src/autocomplete/autocomplete.spec.js +++ b/client/core/src/autocomplete/autocomplete.spec.js @@ -59,34 +59,88 @@ describe('Component vnAutocomplete', () => { }); describe('field() setter', () => { - describe('when value is an object with valueField property', () => { - it(`should set _field and _multifield values and remove it from _multifield if called again`, () => { + describe('when value is an object', () => { + it(`should set _field controllers property`, () => { let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); - controller.valueField = 'name'; - controller.field = {name: 'Bruce Wayne'}; + controller.field = {id: 1, name: 'Bruce Wayne'}; - expect(controller._field).toEqual('Bruce Wayne'); - expect(controller._multiField[0]).toEqual('Bruce Wayne'); + expect(controller._field).toEqual(1); + }); - controller.field = {name: 'Bruce Wayne'}; + it(`should set _multifield controllers property `, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.multiple = true; + controller.field = {id: 1, name: 'Bruce Wayne'}; + + expect(controller._field).toEqual(1); + expect(controller._multiField[0]).toEqual(1); + + controller.field = {id: 1, name: 'Bruce Wayne'}; expect(controller._multiField).toEqual([]); - expect(controller._field).toEqual('Bruce Wayne'); + expect(controller._field).toEqual(1); + }); + + it(`should set _multifield value and remove it if called a second type with same value`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.multiple = true; + controller.field = {id: 1, name: 'Bruce Wayne'}; + + expect(controller._field).toEqual(1); + expect(controller._multiField[0]).toEqual(1); + + controller.field = {id: 1, name: 'Bruce Wayne'}; + + expect(controller._multiField).toEqual([]); + expect(controller._field).toEqual(1); + }); + + it(`should set displayValue finding an existing item in the controller.items property`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.items = [{id: 1, name: 'test1'}, {id: 2, name: 'Bruce Wayne'}]; + controller.field = {id: 2, name: 'Bruce Wayne'}; + + expect(controller.displayValue).toEqual('Bruce Wayne'); }); }); describe('when value is a number', () => { - it(`should set field find an existing item in the controller.items property`, () => { + it(`should set _field controller property finding an existing item in the controller.items property`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.items = [{id: 1, name: 'Batman'}, {id: 2, name: 'Bruce Wayne'}]; + controller.field = 2; + + expect(controller._field).toEqual(2); + }); + + it(`should set _multifield value and remove it if called a second type with same value finding an existing item in the controller.items property`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.items = [{id: 1, name: 'Batman'}, {id: 2, name: 'Bruce Wayne'}]; + controller.multiple = true; + controller.field = 2; + + expect(controller._multiField[0]).toEqual(2); + + controller.field = 2; + + expect(controller._multiField).toEqual([]); + }); + + it(`should perform a query if the item id isn't present in the controller.items property`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}, {url: 'test.com'}); + $httpBackend.whenGET('test.com?filter={"fields":{"id":true,"name":true},"where":{"id":3}}').respond(); + $httpBackend.expectGET('test.com?filter={"fields":{"id":true,"name":true},"where":{"id":3}}'); + controller.items = [{id: 1, name: 'test1'}, {id: 2, name: 'Bruce Wayne'}]; + controller.field = 3; + $httpBackend.flush(); + }); + + it(`should set displayValue finding an existing item in the controller.items property`, () => { let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); controller.items = [{id: 1, name: 'test1'}, {id: 2, name: 'Bruce Wayne'}]; controller.field = 2; - expect(controller._field).toEqual(2); - expect(controller._multiField).toEqual([]); - - controller.field = {id: 3, name: 'The Joker'}; - - expect(controller._field).toEqual(3); + expect(controller.displayValue).toEqual('Bruce Wayne'); }); it(`should set field performing a query as the item id isn't present in the controller.items property`, () => {