@@ -157,6 +157,36 @@ describe('List Selection', () => {
157157 } ) ;
158158 } ) ;
159159
160+ describe ( '#toggleOne' , ( ) => {
161+ it ( 'should select an unselected item' , ( ) => {
162+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
163+ const nav = getNavigation ( items ) ;
164+ const selection = getSelection ( items , nav ) ;
165+
166+ selection . toggleOne ( ) ; // [0]
167+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
168+ } ) ;
169+
170+ it ( 'should deselect a selected item' , ( ) => {
171+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
172+ const nav = getNavigation ( items ) ;
173+ const selection = getSelection ( items , nav ) ;
174+ selection . select ( ) ; // [0]
175+ selection . toggleOne ( ) ; // []
176+ expect ( selection . inputs . value ( ) . length ) . toBe ( 0 ) ;
177+ } ) ;
178+
179+ it ( 'should only leave one item selected' , ( ) => {
180+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
181+ const nav = getNavigation ( items ) ;
182+ const selection = getSelection ( items , nav ) ;
183+ selection . select ( ) ; // [0]
184+ nav . next ( ) ;
185+ selection . toggleOne ( ) ; // [1]
186+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 ] ) ;
187+ } ) ;
188+ } ) ;
189+
160190 describe ( '#selectAll' , ( ) => {
161191 it ( 'should select all items' , ( ) => {
162192 const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
@@ -185,7 +215,71 @@ describe('List Selection', () => {
185215 } ) ;
186216 } ) ;
187217
188- describe ( '#selectFromAnchor' , ( ) => {
218+ describe ( '#toggleAll' , ( ) => {
219+ it ( 'should select all items' , ( ) => {
220+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
221+ const nav = getNavigation ( items ) ;
222+ const selection = getSelection ( items , nav ) ;
223+ selection . toggleAll ( ) ;
224+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
225+ } ) ;
226+
227+ it ( 'should deselect all if all items are selected' , ( ) => {
228+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
229+ const nav = getNavigation ( items ) ;
230+ const selection = getSelection ( items , nav ) ;
231+ selection . selectAll ( ) ;
232+ selection . toggleAll ( ) ;
233+ expect ( selection . inputs . value ( ) ) . toEqual ( [ ] ) ;
234+ } ) ;
235+
236+ it ( 'should ignore disabled items when determining if all items are selected' , ( ) => {
237+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
238+ const nav = getNavigation ( items ) ;
239+ const selection = getSelection ( items , nav ) ;
240+ items ( ) [ 0 ] . disabled . set ( true ) ;
241+ selection . toggleAll ( ) ;
242+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
243+ selection . toggleAll ( ) ;
244+ expect ( selection . inputs . value ( ) ) . toEqual ( [ ] ) ;
245+ } ) ;
246+ } ) ;
247+
248+ describe ( '#selectOne' , ( ) => {
249+ it ( 'should select a single item' , ( ) => {
250+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
251+ const nav = getNavigation ( items ) ;
252+ const selection = getSelection ( items , nav ) ;
253+
254+ selection . selectOne ( ) ; // [0]
255+ nav . next ( ) ;
256+ selection . selectOne ( ) ; // [1]
257+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 ] ) ;
258+ } ) ;
259+
260+ it ( 'should not select disabled items' , ( ) => {
261+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
262+ const nav = getNavigation ( items ) ;
263+ const selection = getSelection ( items , nav ) ;
264+ items ( ) [ 0 ] . disabled . set ( true ) ;
265+
266+ selection . select ( ) ; // []
267+ expect ( selection . inputs . value ( ) ) . toEqual ( [ ] ) ;
268+ } ) ;
269+
270+ it ( 'should do nothing to already selected items' , ( ) => {
271+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
272+ const nav = getNavigation ( items ) ;
273+ const selection = getSelection ( items , nav ) ;
274+
275+ selection . selectOne ( ) ; // [0]
276+ selection . selectOne ( ) ; // [0]
277+
278+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
279+ } ) ;
280+ } ) ;
281+
282+ describe ( '#selectRange' , ( ) => {
189283 it ( 'should select all items from an anchor at a lower index' , ( ) => {
190284 const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
191285 const nav = getNavigation ( items ) ;
@@ -194,7 +288,7 @@ describe('List Selection', () => {
194288 selection . select ( ) ; // [0]
195289 nav . next ( ) ;
196290 nav . next ( ) ;
197- selection . selectFromPrevSelectedItem ( ) ; // [0, 1, 2]
291+ selection . selectRange ( ) ; // [0, 1, 2]
198292
199293 expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 1 , 2 ] ) ;
200294 } ) ;
@@ -209,10 +303,98 @@ describe('List Selection', () => {
209303 selection . select ( ) ; // [3]
210304 nav . prev ( ) ;
211305 nav . prev ( ) ;
212- selection . selectFromPrevSelectedItem ( ) ; // [3, 1, 2]
306+ selection . selectRange ( ) ; // [3, 2, 1]
307+
308+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 3 , 2 , 1 ] ) ;
309+ } ) ;
310+
311+ it ( 'should deselect items within the range when the range is changed' , ( ) => {
312+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
313+ const nav = getNavigation ( items ) ;
314+ const selection = getSelection ( items , nav ) ;
315+
316+ nav . next ( ) ;
317+ nav . next ( ) ;
318+ selection . select ( ) ; // [2]
319+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 2 ] ) ;
213320
214- // TODO(wagnermaciel): Order the values when inserting them.
215- expect ( selection . inputs . value ( ) ) . toEqual ( [ 3 , 1 , 2 ] ) ;
321+ nav . next ( ) ;
322+ nav . next ( ) ;
323+ selection . selectRange ( ) ; // [2, 3, 4]
324+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 2 , 3 , 4 ] ) ;
325+
326+ nav . first ( ) ;
327+ selection . selectRange ( ) ; // [2, 1, 0]
328+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 2 , 1 , 0 ] ) ;
329+ } ) ;
330+
331+ it ( 'should not select a disabled item' , ( ) => {
332+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
333+ const nav = getNavigation ( items ) ;
334+ const selection = getSelection ( items , nav ) ;
335+ items ( ) [ 1 ] . disabled . set ( true ) ;
336+
337+ selection . select ( ) ; // [0]
338+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
339+
340+ nav . next ( ) ;
341+ selection . selectRange ( ) ; // [0]
342+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 ] ) ;
343+
344+ nav . next ( ) ;
345+ selection . selectRange ( ) ; // [0, 2]
346+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 0 , 2 ] ) ;
347+ } ) ;
348+
349+ it ( 'should not deselect a disabled item' , ( ) => {
350+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
351+ const nav = getNavigation ( items ) ;
352+ const selection = getSelection ( items , nav ) ;
353+
354+ selection . select ( items ( ) [ 1 ] ) ;
355+ items ( ) [ 1 ] . disabled . set ( true ) ;
356+
357+ selection . select ( ) ; // [0]
358+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 , 0 ] ) ;
359+
360+ nav . next ( ) ;
361+ nav . next ( ) ;
362+ selection . selectRange ( ) ; // [0, 1, 2]
363+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 , 0 , 2 ] ) ;
364+
365+ nav . prev ( ) ;
366+ nav . prev ( ) ;
367+ selection . selectRange ( ) ; // [0]
368+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 , 0 ] ) ;
369+ } ) ;
370+ } ) ;
371+
372+ describe ( '#beginRangeSelection' , ( ) => {
373+ it ( 'should set where a range is starting from' , ( ) => {
374+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
375+ const nav = getNavigation ( items ) ;
376+ const selection = getSelection ( items , nav ) ;
377+
378+ nav . next ( ) ;
379+ nav . next ( ) ;
380+ selection . beginRangeSelection ( ) ;
381+ expect ( selection . inputs . value ( ) ) . toEqual ( [ ] ) ;
382+ nav . next ( ) ;
383+ nav . next ( ) ;
384+ selection . selectRange ( ) ; // [2, 3, 4]
385+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 2 , 3 , 4 ] ) ;
386+ } ) ;
387+
388+ it ( 'should be able to select a range starting on a disabled item' , ( ) => {
389+ const items = getItems ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
390+ const nav = getNavigation ( items ) ;
391+ const selection = getSelection ( items , nav ) ;
392+ items ( ) [ 0 ] . disabled . set ( true ) ;
393+ selection . beginRangeSelection ( 0 ) ;
394+ nav . next ( ) ;
395+ nav . next ( ) ;
396+ selection . selectRange ( ) ;
397+ expect ( selection . inputs . value ( ) ) . toEqual ( [ 1 , 2 ] ) ;
216398 } ) ;
217399 } ) ;
218400} ) ;
0 commit comments