|
6 | 6 | * Side Public License, v 1. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { getParentCellContent } from './focus'; |
| 9 | +import { testCustomHook } from '../../../test'; |
| 10 | + |
| 11 | +import { |
| 12 | + getParentCellContent, |
| 13 | + useKeyboardFocusScrollWorkaround, |
| 14 | +} from './focus'; |
10 | 15 |
|
11 | 16 | describe('getParentCellContent', () => { |
12 | 17 | const doc = document.createDocumentFragment(); |
@@ -40,3 +45,75 @@ describe('getParentCellContent', () => { |
40 | 45 | expect(getParentCellContent(body)).toBeNull(); |
41 | 46 | }); |
42 | 47 | }); |
| 48 | + |
| 49 | +describe('useKeyboardFocusScrollWorkaround', () => { |
| 50 | + const gridRef = { |
| 51 | + current: { |
| 52 | + scrollToItem: jest.fn(), |
| 53 | + scrollTo: jest.fn(), |
| 54 | + }, |
| 55 | + } as any; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + jest.clearAllMocks(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('calls scrollToItem whenever a new cell is focused via keyboard navigation', () => { |
| 62 | + testCustomHook(() => |
| 63 | + useKeyboardFocusScrollWorkaround({ |
| 64 | + focusedCell: [1, 0], // previous cell is [0, 0], so this is navigating to the right |
| 65 | + visibleRowCount: 3, |
| 66 | + gridRef, |
| 67 | + }) |
| 68 | + ); |
| 69 | + |
| 70 | + expect(gridRef.current.scrollToItem).toHaveBeenCalledWith({ |
| 71 | + columnIndex: 1, |
| 72 | + rowIndex: 0, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('does not call scrollToItem when keyboard navigating downwards', () => { |
| 77 | + testCustomHook(() => |
| 78 | + useKeyboardFocusScrollWorkaround({ |
| 79 | + focusedCell: [0, 1], // previous cell is [0, 0] |
| 80 | + visibleRowCount: 3, |
| 81 | + gridRef, |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + expect(gridRef.current.scrollToItem).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('calls scrollTo with a manual height calculation when navigating on the last row', () => { |
| 89 | + testCustomHook(() => |
| 90 | + useKeyboardFocusScrollWorkaround({ |
| 91 | + focusedCell: [2, 2], |
| 92 | + visibleRowCount: 3, |
| 93 | + gridRef: { |
| 94 | + current: { |
| 95 | + ...gridRef.current, |
| 96 | + _outerRef: { clientHeight: 200, firstChild: { clientHeight: 500 } }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + expect(gridRef.current.scrollToItem).toHaveBeenCalledWith({ |
| 103 | + columnIndex: 2, |
| 104 | + }); |
| 105 | + expect(gridRef.current.scrollTo).toHaveBeenCalledWith({ scrollTop: 300 }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does nothing if there is no currently focused cell', () => { |
| 109 | + testCustomHook(() => |
| 110 | + useKeyboardFocusScrollWorkaround({ |
| 111 | + focusedCell: undefined, |
| 112 | + visibleRowCount: 0, |
| 113 | + gridRef, |
| 114 | + }) |
| 115 | + ); |
| 116 | + |
| 117 | + expect(gridRef.current.scrollToItem).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments