From 86fdb010ae792731c44ce1b972a88bc8cb74c423 Mon Sep 17 00:00:00 2001 From: Carlos Date: Thu, 28 Sep 2017 12:37:45 +0200 Subject: [PATCH] Autocomplete client side unit test for findItems --- client/core/src/autocomplete/autocomplete.js | 2 + .../src/autocomplete/autocomplete.spec.js | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/client/core/src/autocomplete/autocomplete.js b/client/core/src/autocomplete/autocomplete.js index 98ad4675a..b87361fdc 100644 --- a/client/core/src/autocomplete/autocomplete.js +++ b/client/core/src/autocomplete/autocomplete.js @@ -204,6 +204,7 @@ class Autocomplete extends Component { this.getItems(); } } + getItems() { let filter = {}; @@ -233,6 +234,7 @@ class Autocomplete extends Component { } ); } + $onInit() { this.findMore = this.url && this.maxRow; this.mouseFocus = false; diff --git a/client/core/src/autocomplete/autocomplete.spec.js b/client/core/src/autocomplete/autocomplete.spec.js index 67d48c147..8999e25b0 100644 --- a/client/core/src/autocomplete/autocomplete.spec.js +++ b/client/core/src/autocomplete/autocomplete.spec.js @@ -153,4 +153,48 @@ describe('Component vnAutocomplete', () => { }); }); }); + + describe('findItem()', () => { + it(`should return items array if the controller does not provide a url and nither it has items`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.items = ['Batman', 'Bruce Wayne']; + controller.findItems('some search value'); + + expect(controller.items.length).toEqual(2); + }); + + it(`should perform a query with the search value if the finding flag is false`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}); + controller.items = ['Batman', 'Bruce Wayne']; + controller.findItems('Gotham'); + + expect(controller.items.length).toEqual(2); + }); + + it(`should perform a query with the search value if the finding flag is false`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}, {url: 'test.com'}); + let search = 'The Joker'; + let json = JSON.stringify({where: {name: {regexp: search}}}); + $httpBackend.whenGET(`test.com?filter=${json}`).respond([{id: 3, name: 'The Joker'}]); + $httpBackend.expectGET(`test.com?filter=${json}`); + controller.findItems(search); + $httpBackend.flush(); + + expect(controller.items[0]).toEqual({id: 3, name: 'The Joker'}); + }); + + it(`should perform a query with the search value if the finding flag is false`, () => { + let controller = $componentController('vnAutocomplete', {$scope, $element, $httpBackend, $timeout}, {url: 'test.com'}); + let search = 'The Joker'; + let json = JSON.stringify({where: {name: {regexp: search}}}); + $httpBackend.whenGET(`test.com?filter=${json}`).respond([{id: 3, name: 'The Joker'}]); + $httpBackend.expectGET(`test.com?filter=${json}`); + controller.findItems(search); + $httpBackend.flush(); + + expect(controller.items[0]).toEqual({id: 3, name: 'The Joker'}); + }); + + // siguiente test el de Multiple! + }); });