@@ -851,6 +851,45 @@ describe('Select.vue', () => {
851
851
expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( vm . value )
852
852
} )
853
853
854
+ it ( 'can determine if an object is pre-selected' , ( ) => {
855
+ const vm = new Vue ( {
856
+ template : '<div><v-select :options="options" v-model="value" index="id"></v-select></div>' ,
857
+ components : { vSelect} ,
858
+ data : {
859
+ value : 'foo' ,
860
+ options : [ {
861
+ id : 'foo' ,
862
+ label : 'This is Foo'
863
+ } ]
864
+ }
865
+ } ) . $mount ( )
866
+
867
+ expect ( vm . $children [ 0 ] . isOptionSelected ( {
868
+ id : 'foo' ,
869
+ label : 'This is Foo'
870
+ } ) ) . toEqual ( true )
871
+ } )
872
+
873
+ it ( 'can determine if an object is selected after it has been chosen' , ( ) => {
874
+ const vm = new Vue ( {
875
+ template : '<div><v-select :options="options" index="id"></v-select></div>' ,
876
+ components : { vSelect} ,
877
+ data : {
878
+ options : [ { id : 'foo' , label : 'FooBar' } ]
879
+ }
880
+ } ) . $mount ( )
881
+
882
+ vm . $children [ 0 ] . select ( { id : 'foo' , label : 'FooBar' } ) ;
883
+
884
+ // Vue.nextTick(() => {
885
+ expect ( vm . $children [ 0 ] . isOptionSelected ( {
886
+ id : 'foo' ,
887
+ label : 'This is Foo'
888
+ } ) ) . toEqual ( true )
889
+ // done()
890
+ // })
891
+ } )
892
+
854
893
it ( 'can accept an array of objects and pre-selected values (multiple)' , ( ) => {
855
894
const vm = new Vue ( {
856
895
template : '<div><v-select :index="index" :options="options" :value="value" :multiple="true"></v-select></div>' ,
@@ -873,8 +912,9 @@ describe('Select.vue', () => {
873
912
options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
874
913
}
875
914
} ) . $mount ( )
876
- vm . $children [ 0 ] . select ( 'foo' )
915
+ vm . $children [ 0 ] . deselect ( 'foo' )
877
916
expect ( vm . $children [ 0 ] . mutableValue . length ) . toEqual ( 1 )
917
+ expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( [ 'bar' ] )
878
918
} )
879
919
880
920
it ( 'can deselect an option when multiple is false' , ( ) => {
@@ -886,7 +926,7 @@ describe('Select.vue', () => {
886
926
options : [ { label : 'This is Foo' , value : 'foo' } , { label : 'This is Bar' , value : 'bar' } ]
887
927
}
888
928
} ) . $mount ( )
889
- vm . $children [ 0 ] . select ( 'foo' )
929
+ vm . $children [ 0 ] . deselect ( 'foo' )
890
930
expect ( vm . $children [ 0 ] . mutableValue ) . toEqual ( null )
891
931
} )
892
932
@@ -925,39 +965,97 @@ describe('Select.vue', () => {
925
965
expect ( vm . $children [ 0 ] . $refs . toggle . querySelector ( '.selected-tag' ) . textContent ) . toContain ( 'Baz' )
926
966
} )
927
967
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 ( )
968
+ it ( 'will console.warn when attempting to select an option with an undefined index' , ( ) => {
969
+ spyOn ( console , 'warn' )
970
+
971
+ const vm = new Vue ( {
972
+ template : '<div><v-select index="value" :options="options"></v-select></div>' ,
973
+ data : {
974
+ options : [ { label : 'Foo' } ]
975
+ }
976
+ } ) . $mount ( )
977
+ vm . $children [ 0 ] . select ( { label : 'Foo' } )
978
+ expect ( console . warn ) . toHaveBeenCalledWith (
979
+ `[vue-select warn]: Index key "option.value" does not exist in options object {"label":"Foo"}.`
980
+ )
981
+ } )
982
+
983
+ it ( 'can find the original option within this.options' , ( ) => {
984
+ const vm = new Vue ( {
985
+ template : '<div><v-select index="id" :options="options"></v-select></div>' ,
986
+ data : {
987
+ options : [ { id : 1 , label : 'Foo' } , { id :2 , label : 'Bar' } ]
988
+ }
989
+ } ) . $mount ( )
990
+
991
+ expect ( vm . $children [ 0 ] . findOptionByIndexValue ( 1 ) ) . toEqual ( { id : 1 , label : 'Foo' } )
992
+ expect ( vm . $children [ 0 ] . findOptionByIndexValue ( { id : 1 , label : 'Foo' } ) ) . toEqual ( { id : 1 , label : 'Foo' } )
993
+ } )
994
+
995
+ describe ( 'And when option[index] is a nested object' , ( ) => {
996
+ it ( 'can determine if an object is pre-selected' , ( ) => {
997
+ const nestedOption = {
998
+ value : {
999
+ nested : true
1000
+ } ,
1001
+ label : 'foo'
1002
+ } ;
1003
+ const vm = new Vue ( {
1004
+ template : '<div><v-select index="value" :options="options" :value="value"></v-select></div>' ,
1005
+ components : { vSelect} ,
1006
+ data : {
1007
+ value : {
1008
+ nested : true
1009
+ } ,
1010
+ options : [ nestedOption ]
1011
+ }
1012
+ } ) . $mount ( )
1013
+ expect ( vm . $children [ 0 ] . isOptionSelected ( {
1014
+ nested : true
1015
+ } ) ) . toEqual ( true )
943
1016
} )
944
- } )
945
1017
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 ( )
1018
+ it ( 'can determine if an object is selected after it is chosen' , ( ) => {
1019
+ const nestedOption = {
1020
+ value : {
1021
+ nested : true
1022
+ } ,
1023
+ label : 'foo'
1024
+ } ;
1025
+ const vm = new Vue ( {
1026
+ template : '<div><v-select index="value" :options="options"></v-select></div>' ,
1027
+ components : { vSelect} ,
1028
+ data : {
1029
+ options : [ nestedOption ]
1030
+ }
1031
+ } ) . $mount ( )
1032
+ vm . $children [ 0 ] . select ( nestedOption )
1033
+ expect ( vm . $children [ 0 ] . isOptionSelected ( nestedOption ) ) . toEqual ( true )
960
1034
} )
1035
+
1036
+ it ( 'can determine a selected values label' , ( ) => {
1037
+ const nestedOption = {
1038
+ value : {
1039
+ nested : true
1040
+ } ,
1041
+ label : 'foo'
1042
+ } ;
1043
+ const vm = new Vue ( {
1044
+ template : '<div><v-select index="value" :options="options" :value="value"></v-select></div>' ,
1045
+ components : { vSelect} ,
1046
+ data : {
1047
+ value : {
1048
+ nested : true
1049
+ } ,
1050
+ options : [ nestedOption ]
1051
+ }
1052
+ } ) . $mount ( )
1053
+
1054
+ expect ( vm . $children [ 0 ] . getOptionLabel ( {
1055
+ nested : true
1056
+ } ) ) . toEqual ( 'foo' )
1057
+ } )
1058
+
961
1059
} )
962
1060
} )
963
1061
@@ -1501,7 +1599,7 @@ describe('Select.vue', () => {
1501
1599
value : 'foo'
1502
1600
}
1503
1601
} ) . $mount ( )
1504
-
1602
+
1505
1603
expect ( vm . mutableValue ) . toEqual ( 'foo' )
1506
1604
vm . $el . querySelector ( 'button.clear' ) . click ( )
1507
1605
expect ( vm . mutableValue ) . toEqual ( null )
@@ -1520,6 +1618,6 @@ describe('Select.vue', () => {
1520
1618
const buttonEl = vm . $el . querySelector ( 'button.clear' )
1521
1619
expect ( buttonEl . disabled ) . toEqual ( true ) ;
1522
1620
} )
1523
-
1621
+
1524
1622
} ) ;
1525
1623
} )
0 commit comments