@@ -4,30 +4,29 @@ import { SelectOption } from './select.types';
4
4
import { render } from '@solidjs/testing-library' ;
5
5
6
6
describe ( 'Select' , ( ) => {
7
- const fruits : SelectOption [ ] = [
8
- { value : 'apple' , label : 'Apple' } ,
9
- { value : 'banana' , label : 'Banana' } ,
10
- { value : 'cherry' , label : 'Cherry' } ,
11
- ] ;
12
-
13
- interface Car {
14
- type : string ;
15
- color : string ;
7
+ interface Fruit {
8
+ name : string ;
9
+ description : string ;
16
10
}
17
11
18
- const cars : SelectOption < Car > [ ] = [
19
- {
20
- value : { type : 'limousine' , color : 'black' } ,
21
- label : 'Black Limousine' ,
22
- } ,
12
+ const fruits : SelectOption < Fruit > [ ] = [
23
13
{
24
- value : { type : 'sedan' , color : 'red' } ,
25
- label : 'Red Sedan' ,
14
+ key : 'apple' ,
15
+ value : {
16
+ name : 'Apple' ,
17
+ description : 'A red apple.' ,
18
+ } ,
19
+ label : 'Apple' ,
26
20
} ,
27
21
{
28
- value : { type : 'coupe' , color : 'blue' } ,
29
- label : 'Blue Coupe' ,
22
+ key : 'banana' ,
23
+ value : {
24
+ name : 'Banana' ,
25
+ description : 'A tasty banana.' ,
26
+ } ,
27
+ label : 'Banana' ,
30
28
} ,
29
+ { key : 'cherry' , value : { name : 'Cherry' , description : 'A sweet cherry.' } , label : 'Cherry' } ,
31
30
] ;
32
31
33
32
it ( 'should render with default values' , ( ) => {
@@ -43,20 +42,21 @@ describe('Select', () => {
43
42
const label = selectContainer ?. querySelector ( 'label' ) ;
44
43
expect ( label ) . toBeInTheDocument ( ) ;
45
44
expect ( label ) . toHaveAttribute ( 'for' , select ! . id ) ;
45
+ const icon = label ! . querySelector ( 'iconify-icon' ) ;
46
+ expect ( icon ) . not . toBeInTheDocument ( ) ;
46
47
const options = selectContainer ?. querySelectorAll ( 'option' ) ;
47
48
expect ( options ) . toHaveLength ( 3 ) ;
48
- expect ( options ! [ 0 ] ) . toHaveAttribute ( 'value' , 'apple' ) ;
49
- expect ( options ! [ 0 ] ) . toHaveAttribute ( 'label' , 'Apple' ) ;
50
- expect ( options ! [ 1 ] ) . toHaveAttribute ( 'value' , 'banana' ) ;
51
- expect ( options ! [ 1 ] ) . toHaveAttribute ( 'label' , 'Banana' ) ;
52
- expect ( options ! [ 2 ] ) . toHaveAttribute ( 'value' , 'cherry' ) ;
53
- expect ( options ! [ 2 ] ) . toHaveAttribute ( 'label' , 'Cherry' ) ;
49
+ fruits . forEach ( ( fruit , index ) => {
50
+ expect ( options ! [ index ] ) . toHaveAttribute ( 'value' , fruit . key ) ;
51
+ expect ( options ! [ index ] ) . toHaveAttribute ( 'label' , fruit . label ) ;
52
+ } ) ;
54
53
} ) ;
55
54
56
55
it ( 'should render with custom values' , ( ) => {
57
56
const { container } = render ( ( ) => (
58
57
< Select
59
58
label = "Fruits"
59
+ icon = "mdi:fruit-cherries"
60
60
options = { fruits }
61
61
variant = "outlined"
62
62
size = "small"
@@ -71,9 +71,13 @@ describe('Select', () => {
71
71
expect ( selectContainer ) . toHaveAttribute ( 'spx-size' , 'small' ) ;
72
72
expect ( selectContainer ) . toHaveClass ( 'spx' , 'spx-select' , 'my-class' ) ;
73
73
expect ( selectContainer ) . toHaveStyle ( { color : 'rgb(255, 0, 0)' } ) ;
74
- const select = selectContainer ? .querySelector ( 'select' ) ;
74
+ const select = selectContainer ! . querySelector ( 'select' ) ;
75
75
expect ( select ) . toBeInTheDocument ( ) ;
76
76
expect ( select ) . toHaveAttribute ( 'disabled' ) ;
77
+ const label = selectContainer ! . querySelector ( 'label' ) ;
78
+ expect ( label ) . toBeInTheDocument ( ) ;
79
+ const icon = label ! . querySelector ( 'iconify-icon' ) ;
80
+ expect ( icon ) . toBeInTheDocument ( ) ;
77
81
} ) ;
78
82
79
83
it ( 'should call the onChange callback' , ( ) => {
@@ -86,17 +90,47 @@ describe('Select', () => {
86
90
select ! . value = 'banana' ;
87
91
select ! . dispatchEvent ( new Event ( 'change' ) ) ;
88
92
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
89
- expect ( onChange ) . toHaveBeenCalledWith ( fruits [ 1 ] . value , expect . any ( Event ) ) ;
93
+ expect ( onChange ) . toHaveBeenCalledWith ( fruits [ 1 ] , expect . any ( Event ) ) ;
90
94
} ) ;
91
95
92
- it ( 'should be able to handle complex values' , ( ) => {
93
- const onChange = vi . fn ( ) ;
94
- const { container } = render ( ( ) => < Select label = "Cars" options = { cars } onChange = { onChange } /> ) ;
96
+ it ( 'it should generate random UUIDs if no option keys are provided' , ( ) => {
97
+ const fruitsWithoutKeys = fruits . map ( ( fruit ) => ( { ...fruit , key : undefined } ) ) ;
98
+ const { container } = render ( ( ) => < Select label = "Fruits" options = { fruitsWithoutKeys } /> ) ;
99
+ const selectContainer = container . querySelector ( '.spx-select' ) ;
100
+ const options = selectContainer ?. querySelectorAll ( 'option' ) ;
101
+ expect ( options ) . toHaveLength ( 3 ) ;
102
+ fruits . forEach ( ( _fruit , index ) => {
103
+ expect ( options ! [ index ] ) . toHaveAttribute ( 'value' , expect . stringContaining ( '-' ) ) ;
104
+ } ) ;
105
+ } ) ;
106
+
107
+ it ( 'should set a default option by value' , ( ) => {
108
+ const { container } = render ( ( ) => (
109
+ < Select label = "Fruits" options = { fruits } default = { fruits [ 1 ] } />
110
+ ) ) ;
95
111
const select = container . querySelector ( 'select' ) ;
96
- expect ( select ) . toBeInTheDocument ( ) ;
97
- select ! . value = '1' ;
98
- select ! . dispatchEvent ( new Event ( 'change' ) ) ;
99
- expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
100
- expect ( onChange ) . toHaveBeenCalledWith ( cars [ 1 ] . value , expect . any ( Event ) ) ;
112
+ expect ( select ) . toHaveValue ( fruits [ 1 ] . key ) ;
113
+ } ) ;
114
+
115
+ it ( 'should set a default option by key' , ( ) => {
116
+ const { container } = render ( ( ) => (
117
+ < Select label = "Fruits" options = { fruits } default = { fruits [ 1 ] . key } />
118
+ ) ) ;
119
+ const select = container . querySelector ( 'select' ) ;
120
+ expect ( select ) . toHaveValue ( fruits [ 1 ] . key ) ;
121
+ } ) ;
122
+
123
+ it ( 'should set a default option by index' , ( ) => {
124
+ const { container } = render ( ( ) => < Select label = "Fruits" options = { fruits } default = { 1 } /> ) ;
125
+ const select = container . querySelector ( 'select' ) ;
126
+ expect ( select ) . toHaveValue ( fruits [ 1 ] . key ) ;
127
+ } ) ;
128
+
129
+ it ( 'should use the provided id instead of generating a random one' , ( ) => {
130
+ const { container } = render ( ( ) => (
131
+ < Select label = "Fruits" options = { fruits } attrs = { { id : 'my-select' } } />
132
+ ) ) ;
133
+ const select = container . querySelector ( 'select' ) ;
134
+ expect ( select ) . toHaveAttribute ( 'id' , 'my-select' ) ;
101
135
} ) ;
102
136
} ) ;
0 commit comments