@@ -837,6 +837,130 @@ describe('Select.vue', () => {
837
837
} )
838
838
} )
839
839
840
+ describe ( 'When index prop is defined' , ( ) => {
841
+ it ( 'can accept an array of objects and pre-selected value (single)' , ( ) => {
842
+ const vm = new Vue ( {
843
+ template : '<div><v-select :index="index" :options="options" :value="value"></v-select></div>' ,
844
+ components : { vSelect} ,
845
+ data : {
846
+ index : 'value' ,
847
+ value : 'foo' ,
848
+ options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
849
+ }
850
+ } ) . $mount ( )
851
+ expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( vm . value )
852
+ } )
853
+
854
+ it ( 'can accept an array of objects and pre-selected values (multiple)' , ( ) => {
855
+ const vm = new Vue ( {
856
+ template : '<div><v-select :index="index" :options="options" :value="value" :multiple="true"></v-select></div>' ,
857
+ components : { vSelect} ,
858
+ data : {
859
+ index : 'value' ,
860
+ value : [ 'foo' , 'bar' ] ,
861
+ options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
862
+ }
863
+ } ) . $mount ( )
864
+ expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( vm . value )
865
+ } )
866
+
867
+ it ( 'can deselect a pre-selected object' , ( ) => {
868
+ const vm = new Vue ( {
869
+ template : '<div><v-select :index="index" :options="options" :value="value" :multiple="true"></v-select></div>' ,
870
+ data : {
871
+ index : 'value' ,
872
+ value : [ 'foo' , 'bar' ] ,
873
+ options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
874
+ }
875
+ } ) . $mount ( )
876
+ vm . $children [ 0 ] . select ( 'foo' )
877
+ expect ( vm . $children [ 0 ] . mutableValue . length ) . toEqual ( 1 )
878
+ } )
879
+
880
+ it ( 'can deselect an option when multiple is false' , ( ) => {
881
+ const vm = new Vue ( {
882
+ template : `<div><v-select :index="index" :options="options" :value="value"></v-select></div>` ,
883
+ data : {
884
+ index : 'value' ,
885
+ value : 'foo' ,
886
+ options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
887
+ }
888
+ } ) . $mount ( )
889
+ vm . $children [ 0 ] . select ( 'foo' )
890
+ expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( null )
891
+ } )
892
+
893
+ it ( 'can use v-model syntax for a two way binding to a parent component' , ( done ) => {
894
+ const vm = new Vue ( {
895
+ template : '<div><v-select :index="index" :options="options" v-model="value"></v-select></div>' ,
896
+ components : { vSelect} ,
897
+ data : {
898
+ index : 'value' ,
899
+ value : 'foo' ,
900
+ options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } , { label : 'This is Baz' , value : 'baz' } ]
901
+ }
902
+ } ) . $mount ( )
903
+
904
+ expect ( vm . $children [ 0 ] . value ) . toEqual ( 'foo' )
905
+ expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( 'foo' )
906
+
907
+ vm . $children [ 0 ] . mutableValue = 'bar'
908
+
909
+ Vue . nextTick ( ( ) => {
910
+ expect ( vm . value ) . toEqual ( 'bar' )
911
+ done ( )
912
+ } )
913
+ } ) ,
914
+
915
+ it ( 'can generate labels using a custom label key' , ( ) => {
916
+ const vm = new Vue ( {
917
+ template : '<div><v-select :index="index" label="name" :options="options" v-model="value" :multiple="true"></v-select></div>' ,
918
+ components : { vSelect} ,
919
+ data : {
920
+ index : 'value' ,
921
+ value : [ 'baz' ] ,
922
+ options : [ { value : 'foo' , name : 'Foo' } , { value : 'baz' , name : 'Baz' } ]
923
+ }
924
+ } ) . $mount ( )
925
+ expect ( vm . $children [ 0 ] . $refs . toggle . querySelector ( '.selected-tag' ) . textContent ) . toContain ( 'Baz' )
926
+ } )
927
+
928
+ it ( 'will console.warn when options contain objects without a valid index key' , ( done ) => {
929
+ spyOn ( console , 'warn' )
930
+ const vm = new Vue ( {
931
+ template : '<div><v-select :index="index" :options="options"></v-select></div>' ,
932
+ data : {
933
+ index : 'value' ,
934
+ options : [ { label : 'Foo' } ]
935
+ }
936
+ } ) . $mount ( )
937
+ vm . $children [ 0 ] . open = true
938
+ Vue . nextTick ( ( ) => {
939
+ expect ( console . warn ) . toHaveBeenCalledWith (
940
+ `[vue-select warn]: Index key "option.value" does not exist in options object {"label":"Foo"}.`
941
+ )
942
+ done ( )
943
+ } )
944
+ } )
945
+
946
+ it ( 'will not console.warn when options contain objects without an index key' , ( done ) => {
947
+ spyOn ( console , 'warn' )
948
+ const vm = new Vue ( {
949
+ template : '<div><v-select :options="options"></v-select></div>' ,
950
+ data : {
951
+ options : [ { label : 'Foo' } ]
952
+ }
953
+ } ) . $mount ( )
954
+ vm . $children [ 0 ] . open = true
955
+ Vue . nextTick ( ( ) => {
956
+ expect ( console . warn ) . not . toHaveBeenCalledWith (
957
+ `[vue-select warn]: Index key "option.value" does not exist in options object {"label":"Foo"}.`
958
+ )
959
+ done ( )
960
+ } )
961
+ } )
962
+ } )
963
+
840
964
describe ( 'When Tagging Is Enabled' , ( ) => {
841
965
it ( 'can determine if a given option string already exists' , ( ) => {
842
966
const vm = new Vue ( {
0 commit comments