@@ -12,22 +12,22 @@ import {ListSelectionItem, ListSelection, ListSelectionInputs} from './list-sele
12
12
import { ListNavigation , ListNavigationInputs } from '../list-navigation/list-navigation' ;
13
13
14
14
describe ( 'List Selection' , ( ) => {
15
- interface TestItem extends ListSelectionItem {
15
+ interface TestItem < V > extends ListSelectionItem < V > {
16
16
disabled : WritableSignalLike < boolean > ;
17
17
}
18
18
19
- function getItems ( length : number ) : SignalLike < TestItem [ ] > {
19
+ function getItems < V > ( values : V [ ] ) : SignalLike < TestItem < V > [ ] > {
20
20
return signal (
21
- Array . from ( { length } ) . map ( ( _ , i ) => ( {
21
+ values . map ( ( value , i ) => ( {
22
22
index : signal ( i ) ,
23
- id : signal ( ` ${ i } ` ) ,
23
+ value : signal ( value ) ,
24
24
disabled : signal ( false ) ,
25
25
isAnchor : signal ( false ) ,
26
26
} ) ) ,
27
27
) ;
28
28
}
29
29
30
- function getNavigation < T extends TestItem > (
30
+ function getNavigation < T extends TestItem < V > , V > (
31
31
items : SignalLike < T [ ] > ,
32
32
args : Partial < ListNavigationInputs < T > > = { } ,
33
33
) : ListNavigation < T > {
@@ -42,15 +42,15 @@ describe('List Selection', () => {
42
42
} ) ;
43
43
}
44
44
45
- function getSelection < T extends TestItem > (
45
+ function getSelection < T extends TestItem < V > , V > (
46
46
items : SignalLike < T [ ] > ,
47
47
navigation : ListNavigation < T > ,
48
- args : Partial < ListSelectionInputs < T > > = { } ,
49
- ) : ListSelection < T > {
48
+ args : Partial < ListSelectionInputs < T , V > > = { } ,
49
+ ) : ListSelection < T , V > {
50
50
return new ListSelection ( {
51
51
items,
52
52
navigation,
53
- selectedIds : signal ( [ ] ) ,
53
+ value : signal < V [ ] > ( [ ] ) ,
54
54
multiselectable : signal ( true ) ,
55
55
selectionMode : signal ( 'explicit' ) ,
56
56
...args ,
@@ -59,28 +59,28 @@ describe('List Selection', () => {
59
59
60
60
describe ( '#select' , ( ) => {
61
61
it ( 'should select an item' , ( ) => {
62
- const items = getItems ( 5 ) ;
62
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
63
63
const nav = getNavigation ( items ) ;
64
64
const selection = getSelection ( items , nav ) ;
65
65
66
66
selection . select ( ) ; // [0]
67
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' ] ) ;
67
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
68
68
} ) ;
69
69
70
70
it ( 'should select multiple options' , ( ) => {
71
- const items = getItems ( 5 ) ;
71
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
72
72
const nav = getNavigation ( items ) ;
73
73
const selection = getSelection ( items , nav ) ;
74
74
75
75
selection . select ( ) ; // [0]
76
76
nav . next ( ) ;
77
77
selection . select ( ) ; // [0, 1]
78
78
79
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' , '1' ] ) ;
79
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 ] ) ;
80
80
} ) ;
81
81
82
82
it ( 'should not select multiple options' , ( ) => {
83
- const items = getItems ( 5 ) ;
83
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
84
84
const nav = getNavigation ( items ) ;
85
85
const selection = getSelection ( items , nav , {
86
86
multiselectable : signal ( false ) ,
@@ -90,104 +90,104 @@ describe('List Selection', () => {
90
90
nav . next ( ) ;
91
91
selection . select ( ) ; // [1]
92
92
93
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '1' ] ) ;
93
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 ] ) ;
94
94
} ) ;
95
95
96
96
it ( 'should not select disabled items' , ( ) => {
97
- const items = getItems ( 5 ) ;
97
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
98
98
const nav = getNavigation ( items ) ;
99
99
const selection = getSelection ( items , nav ) ;
100
100
items ( ) [ 0 ] . disabled . set ( true ) ;
101
101
102
102
selection . select ( ) ; // []
103
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ ] ) ;
103
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ ] ) ;
104
104
} ) ;
105
105
106
106
it ( 'should do nothing to already selected items' , ( ) => {
107
- const items = getItems ( 5 ) ;
107
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
108
108
const nav = getNavigation ( items ) ;
109
109
const selection = getSelection ( items , nav ) ;
110
110
111
111
selection . select ( ) ; // [0]
112
112
selection . select ( ) ; // [0]
113
113
114
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' ] ) ;
114
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
115
115
} ) ;
116
116
} ) ;
117
117
118
118
describe ( '#deselect' , ( ) => {
119
119
it ( 'should deselect an item' , ( ) => {
120
- const items = getItems ( 5 ) ;
120
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
121
121
const nav = getNavigation ( items ) ;
122
122
const selection = getSelection ( items , nav ) ;
123
123
selection . deselect ( ) ; // []
124
- expect ( selection . inputs . selectedIds ( ) . length ) . toBe ( 0 ) ;
124
+ expect ( selection . inputs . value ( ) . length ) . toBe ( 0 ) ;
125
125
} ) ;
126
126
127
127
it ( 'should not deselect disabled items' , ( ) => {
128
- const items = getItems ( 5 ) ;
128
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
129
129
const nav = getNavigation ( items ) ;
130
130
const selection = getSelection ( items , nav ) ;
131
131
132
132
selection . select ( ) ; // [0]
133
133
items ( ) [ 0 ] . disabled . set ( true ) ;
134
134
selection . deselect ( ) ; // [0]
135
135
136
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' ] ) ;
136
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
137
137
} ) ;
138
138
} ) ;
139
139
140
140
describe ( '#toggle' , ( ) => {
141
141
it ( 'should select an unselected item' , ( ) => {
142
- const items = getItems ( 5 ) ;
142
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
143
143
const nav = getNavigation ( items ) ;
144
144
const selection = getSelection ( items , nav ) ;
145
145
146
146
selection . toggle ( ) ; // [0]
147
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' ] ) ;
147
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
148
148
} ) ;
149
149
150
150
it ( 'should deselect a selected item' , ( ) => {
151
- const items = getItems ( 5 ) ;
151
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
152
152
const nav = getNavigation ( items ) ;
153
153
const selection = getSelection ( items , nav ) ;
154
154
selection . select ( ) ; // [0]
155
155
selection . toggle ( ) ; // []
156
- expect ( selection . inputs . selectedIds ( ) . length ) . toBe ( 0 ) ;
156
+ expect ( selection . inputs . value ( ) . length ) . toBe ( 0 ) ;
157
157
} ) ;
158
158
} ) ;
159
159
160
160
describe ( '#selectAll' , ( ) => {
161
161
it ( 'should select all items' , ( ) => {
162
- const items = getItems ( 5 ) ;
162
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
163
163
const nav = getNavigation ( items ) ;
164
164
const selection = getSelection ( items , nav ) ;
165
165
selection . selectAll ( ) ;
166
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' , '1' , '2' , '3' , '4' ] ) ;
166
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
167
167
} ) ;
168
168
169
169
it ( 'should do nothing if a list is not multiselectable' , ( ) => {
170
- const items = getItems ( 5 ) ;
170
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
171
171
const nav = getNavigation ( items ) ;
172
172
const selection = getSelection ( items , nav ) ;
173
173
selection . selectAll ( ) ;
174
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' , '1' , '2' , '3' , '4' ] ) ;
174
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
175
175
} ) ;
176
176
} ) ;
177
177
178
178
describe ( '#deselectAll' , ( ) => {
179
179
it ( 'should deselect all items' , ( ) => {
180
- const items = getItems ( 5 ) ;
180
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
181
181
const nav = getNavigation ( items ) ;
182
182
const selection = getSelection ( items , nav ) ;
183
183
selection . deselectAll ( ) ; // []
184
- expect ( selection . inputs . selectedIds ( ) . length ) . toBe ( 0 ) ;
184
+ expect ( selection . inputs . value ( ) . length ) . toBe ( 0 ) ;
185
185
} ) ;
186
186
} ) ;
187
187
188
188
describe ( '#selectFromAnchor' , ( ) => {
189
189
it ( 'should select all items from an anchor at a lower index' , ( ) => {
190
- const items = getItems ( 5 ) ;
190
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
191
191
const nav = getNavigation ( items ) ;
192
192
const selection = getSelection ( items , nav ) ;
193
193
@@ -196,11 +196,11 @@ describe('List Selection', () => {
196
196
nav . next ( ) ;
197
197
selection . selectFromPrevSelectedItem ( ) ; // [0, 1, 2]
198
198
199
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '0' , '1' , '2' ] ) ;
199
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 , 2 ] ) ;
200
200
} ) ;
201
201
202
202
it ( 'should select all items from an anchor at a higher index' , ( ) => {
203
- const items = getItems ( 5 ) ;
203
+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
204
204
const nav = getNavigation ( items , {
205
205
activeIndex : signal ( 3 ) ,
206
206
} ) ;
@@ -211,7 +211,8 @@ describe('List Selection', () => {
211
211
nav . prev ( ) ;
212
212
selection . selectFromPrevSelectedItem ( ) ; // [3, 1, 2]
213
213
214
- expect ( selection . inputs . selectedIds ( ) ) . toEqual ( [ '3' , '1' , '2' ] ) ;
214
+ // TODO(wagnermaciel): Order the values when inserting them.
215
+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 3 , 1 , 2 ] ) ;
215
216
} ) ;
216
217
} ) ;
217
218
} ) ;
0 commit comments