@@ -232,7 +232,7 @@ describe('ListView', function () {
232
232
expect ( document . activeElement ) . toBe ( start ) ;
233
233
} ) ;
234
234
235
- it ( 'should mov focus to above row' , function ( ) {
235
+ it ( 'should move focus to above row' , function ( ) {
236
236
let tree = renderListWithFocusables ( ) ;
237
237
let start = getCell ( tree , 'Bar' ) ;
238
238
let end = getCell ( tree , 'Foo' ) ;
@@ -372,4 +372,71 @@ describe('ListView', function () {
372
372
} ) ;
373
373
} ) ;
374
374
} ) ;
375
+
376
+ describe ( 'scrolling' , function ( ) {
377
+ beforeAll ( ( ) => {
378
+ jest . spyOn ( window . HTMLElement . prototype , 'scrollHeight' , 'get' )
379
+ . mockImplementation ( function ( ) {
380
+ return 40 ;
381
+ } ) ;
382
+ } ) ;
383
+
384
+ let moveFocus = ( key , opts = { } ) => {
385
+ fireEvent . keyDown ( document . activeElement , { key, ...opts } ) ;
386
+ fireEvent . keyUp ( document . activeElement , { key, ...opts } ) ;
387
+ } ;
388
+
389
+ it ( 'should scroll to a cell when it is focused' , function ( ) {
390
+ let onSelectionChange = jest . fn ( ) ;
391
+
392
+ let tree = render (
393
+ < ListView
394
+ width = "250px"
395
+ height = "60px"
396
+ aria-label = "List"
397
+ data-testid = "test"
398
+ selectionStyle = "highlight"
399
+ selectionMode = "multiple"
400
+ onSelectionChange = { onSelectionChange }
401
+ items = { [ ...Array ( 20 ) . keys ( ) ] . map ( k => ( { key : k , name : `Item ${ k } ` } ) ) } >
402
+ { item => < Item > { item . name } </ Item > }
403
+ </ ListView >
404
+ ) ;
405
+ let grid = tree . getByRole ( 'grid' ) ;
406
+ Object . defineProperty ( grid , 'clientHeight' , {
407
+ get ( ) {
408
+ return 60 ;
409
+ }
410
+ } ) ;
411
+ // fire resize so the new clientHeight is requested
412
+ act ( ( ) => {
413
+ fireEvent ( window , new Event ( 'resize' ) ) ;
414
+ } ) ;
415
+ userEvent . tab ( ) ;
416
+ expect ( grid . scrollTop ) . toBe ( 0 ) ;
417
+
418
+ let rows = tree . getAllByRole ( 'row' ) ;
419
+ let rowWrappers = rows . map ( item => item . parentElement ) ;
420
+
421
+ expect ( rowWrappers [ 0 ] . style . top ) . toBe ( '0px' ) ;
422
+ expect ( rowWrappers [ 0 ] . style . height ) . toBe ( '40px' ) ;
423
+ expect ( rowWrappers [ 1 ] . style . top ) . toBe ( '40px' ) ;
424
+ expect ( rowWrappers [ 1 ] . style . height ) . toBe ( '40px' ) ;
425
+ expect ( rowWrappers [ 2 ] . style . top ) . toBe ( '80px' ) ;
426
+ expect ( rowWrappers [ 2 ] . style . height ) . toBe ( '40px' ) ;
427
+
428
+ // scroll us down far enough that item 0 isn't in the view
429
+ moveFocus ( 'ArrowDown' ) ;
430
+ moveFocus ( 'ArrowDown' ) ;
431
+ moveFocus ( 'ArrowDown' ) ;
432
+ expect ( document . activeElement ) . toBe ( getCell ( tree , 'Item 3' ) ) ;
433
+ expect ( grid . scrollTop ) . toBe ( 100 ) ;
434
+
435
+ moveFocus ( 'ArrowUp' ) ;
436
+ moveFocus ( 'ArrowUp' ) ;
437
+ moveFocus ( 'ArrowUp' ) ;
438
+ expect ( document . activeElement ) . toBe ( getCell ( tree , 'Item 0' ) ) ;
439
+ expect ( grid . scrollTop ) . toBe ( 0 ) ;
440
+ } ) ;
441
+ } ) ;
375
442
} ) ;
0 commit comments