2017-09-26 08:26:21 +00:00
import './autocomplete.js' ;
describe ( 'Component vnAutocomplete' , ( ) => {
let $componentController ;
let $scope ;
let $httpBackend ;
let $timeout ;
let $element ;
beforeEach ( ( ) => {
angular . mock . module ( 'client' ) ;
} ) ;
beforeEach ( angular . mock . inject ( ( _$componentController _ , $rootScope , _$httpBackend _ , _$timeout _ ) => {
$componentController = _$componentController _ ;
$scope = $rootScope . $new ( ) ;
$httpBackend = _$httpBackend _ ;
$timeout = _$timeout _ ;
$element = angular . element ( '<div></div>' ) ;
} ) ) ;
describe ( 'showDropDown() setter' , ( ) => {
it ( ` should set _showDropDown value ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
controller . _showDropDown = '' ;
controller . showDropDown = 'some value' ;
expect ( controller . _showDropDown ) . toEqual ( 'some value' ) ;
} ) ;
2017-09-26 12:10:55 +00:00
it ( ` should set _showDropDown value ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
controller . _showDropDown = '' ;
controller . showDropDown = 'some value' ;
expect ( controller . _showDropDown ) . toEqual ( 'some value' ) ;
} ) ;
} ) ;
describe ( 'displayValue() setter' , ( ) => {
it ( ` should display value in a formated way ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
let value = 'some value' ;
controller . displayValue = value ;
expect ( controller . _value ) . toEqual ( value ) ;
} ) ;
describe ( 'when the autocomeplete is multiple' , ( ) => {
it ( ` should display values separated with commas ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
controller . multiple = true ;
controller . displayValue = 'some value' ;
controller . displayValue = 'another value' ;
expect ( controller . _value ) . toEqual ( 'some value, another value' ) ;
} ) ;
} ) ;
2017-09-26 08:26:21 +00:00
} ) ;
2017-09-26 16:21:11 +00:00
describe ( 'field() setter' , ( ) => {
2017-09-27 12:10:03 +00:00
describe ( 'when value is an object' , ( ) => {
it ( ` should set _field controllers property ` , ( ) => {
2017-09-26 16:21:11 +00:00
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
2017-09-27 12:10:03 +00:00
controller . field = { id : 1 , name : 'Bruce Wayne' } ;
2017-09-26 16:21:11 +00:00
2017-09-27 12:10:03 +00:00
expect ( controller . _field ) . toEqual ( 1 ) ;
} ) ;
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 ( 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 ) ;
2017-09-26 16:21:11 +00:00
2017-09-27 12:10:03 +00:00
controller . field = { id : 1 , name : 'Bruce Wayne' } ;
2017-09-26 16:21:11 +00:00
expect ( controller . _multiField ) . toEqual ( [ ] ) ;
2017-09-27 12:10:03 +00:00
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' ) ;
2017-09-26 16:21:11 +00:00
} ) ;
} ) ;
2017-09-27 06:09:10 +00:00
2017-09-27 09:51:25 +00:00
describe ( 'when value is a number' , ( ) => {
2017-09-27 12:10:03 +00:00
it ( ` should set _field controller property finding an existing item in the controller.items property ` , ( ) => {
2017-09-27 06:09:10 +00:00
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } ) ;
2017-09-27 12:10:03 +00:00
controller . items = [ { id : 1 , name : 'Batman' } , { id : 2 , name : 'Bruce Wayne' } ] ;
2017-09-27 07:42:59 +00:00
controller . field = 2 ;
2017-09-27 06:09:10 +00:00
2017-09-27 07:42:59 +00:00
expect ( controller . _field ) . toEqual ( 2 ) ;
2017-09-27 12:10:03 +00:00
} ) ;
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 ;
2017-09-27 07:42:59 +00:00
expect ( controller . _multiField ) . toEqual ( [ ] ) ;
2017-09-27 12:10:03 +00:00
} ) ;
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 ( ) ;
} ) ;
2017-09-27 06:09:10 +00:00
2017-09-27 12:10:03 +00:00
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 ;
2017-09-27 06:09:10 +00:00
2017-09-27 12:10:03 +00:00
expect ( controller . displayValue ) . toEqual ( 'Bruce Wayne' ) ;
2017-09-27 06:09:10 +00:00
} ) ;
2017-09-27 09:51:25 +00:00
it ( ` should set field performing a query as the item id isn't present in the controller.items property ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } , { url : 'test.com' } ) ;
2017-09-27 09:58:35 +00:00
$httpBackend . whenGET ( 'test.com?filter={"fields":{"id":true,"name":true},"where":{"id":3}}' ) . respond ( ) ;
2017-09-27 09:51:25 +00:00
$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 ( ) ;
} ) ;
2017-09-27 06:09:10 +00:00
} ) ;
2017-09-26 16:21:11 +00:00
} ) ;
2017-09-28 10:37:45 +00:00
2017-10-16 13:20:40 +00:00
describe ( 'findItems()' , ( ) => {
2017-10-04 06:47:16 +00:00
it ( ` should return items empty array if the controller does not provide a url and have no items defined ` , ( ) => {
let controller = $componentController ( 'vnIconMenu' , { $scope , $element , $httpBackend , $timeout } ) ;
2017-09-28 10:37:45 +00:00
controller . findItems ( 'some search value' ) ;
2017-10-04 06:47:16 +00:00
expect ( controller . items ) . not . toBeDefined ( ) ;
2017-09-28 10:37:45 +00:00
} ) ;
2017-10-04 06:47:16 +00:00
it ( ` should return items array if the controller does not provide a url ` , ( ) => {
let controller = $componentController ( 'vnIconMenu' , { $scope , $element , $httpBackend , $timeout } ) ;
2017-09-28 10:37:45 +00:00
controller . items = [ 'Batman' , 'Bruce Wayne' ] ;
2017-10-04 06:47:16 +00:00
controller . findItems ( 'some search value' ) ;
2017-09-28 10:37:45 +00:00
expect ( controller . items . length ) . toEqual ( 2 ) ;
} ) ;
2017-10-03 07:18:12 +00:00
it ( ` should perform a search and store the result in controller items ` , ( ) => {
2017-09-28 10:37:45 +00:00
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' } ) ;
} ) ;
2017-10-03 07:18:12 +00:00
it ( ` should perform a search with multiple true and store the result in controller items with the checked property defined ` , ( ) => {
2017-09-28 10:37:45 +00:00
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } , { url : 'test.com' } ) ;
2017-10-03 07:18:12 +00:00
let search = 'Joker' ;
controller . multiple = true ;
2017-09-28 10:37:45 +00:00
let json = JSON . stringify ( { where : { name : { regexp : search } } } ) ;
2017-10-03 07:18:12 +00:00
$httpBackend . whenGET ( ` test.com?filter= ${ json } ` ) . respond ( [ { id : 3 , name : 'The Joker' } , { id : 4 , name : 'Joker' } ] ) ;
2017-09-28 10:37:45 +00:00
$httpBackend . expectGET ( ` test.com?filter= ${ json } ` ) ;
controller . findItems ( search ) ;
$httpBackend . flush ( ) ;
2017-10-03 07:18:12 +00:00
expect ( controller . items ) . toEqual ( [ { id : 3 , name : 'The Joker' , checked : false } , { id : 4 , name : 'Joker' , checked : false } ] ) ;
2017-09-28 10:37:45 +00:00
} ) ;
2017-10-03 09:32:25 +00:00
it ( ` should call getItems function if there's no search value ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } , { url : 'test.com' } ) ;
spyOn ( controller , 'getItems' ) ;
controller . findItems ( ) ;
expect ( controller . getItems ) . toHaveBeenCalledWith ( ) ;
} ) ;
} ) ;
2017-10-03 12:41:35 +00:00
2017-10-04 06:47:16 +00:00
describe ( 'getItems()' , ( ) => {
2017-10-03 09:32:25 +00:00
it ( ` should perfom a query to fill the items without filter ` , ( ) => {
let controller = $componentController ( 'vnAutocomplete' , { $scope , $element , $httpBackend , $timeout } , { url : 'test.com' } ) ;
$httpBackend . whenGET ( ` test.com?filter={"skip":0,"limit":10,"order":"name ASC"} ` ) . respond ( [ { id : 1 , name : 'Batman' } , { id : 2 , name : 'Bruce Wayne' } ] ) ;
$httpBackend . expectGET ( ` test.com?filter={"skip":0,"limit":10,"order":"name ASC"} ` ) ;
controller . getItems ( ) ;
$httpBackend . flush ( ) ;
expect ( controller . items ) . toEqual ( [ { id : 1 , name : 'Batman' } , { id : 2 , name : 'Bruce Wayne' } ] ) ;
} ) ;
2017-09-28 10:37:45 +00:00
} ) ;
2017-09-26 08:26:21 +00:00
} ) ;