Skip to content

Commit 99b370f

Browse files
committed
Add unit tests for focus keyboard scroll workaround
1 parent 961677c commit 99b370f

1 file changed

Lines changed: 78 additions & 1 deletion

File tree

src/components/datagrid/utils/focus.test.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
* Side Public License, v 1.
77
*/
88

9-
import { getParentCellContent } from './focus';
9+
import { testCustomHook } from '../../../test';
10+
11+
import {
12+
getParentCellContent,
13+
useKeyboardFocusScrollWorkaround,
14+
} from './focus';
1015

1116
describe('getParentCellContent', () => {
1217
const doc = document.createDocumentFragment();
@@ -40,3 +45,75 @@ describe('getParentCellContent', () => {
4045
expect(getParentCellContent(body)).toBeNull();
4146
});
4247
});
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

Comments
 (0)