|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | import React from 'react'; |
| 4 | +import { AutoScrollOptions, TraversalOrder } from '@dnd-kit/core'; |
4 | 5 | import { act, fireEvent, render } from '@testing-library/react'; |
5 | 6 |
|
6 | 7 | import TestI18nProvider from '../../../lib/components/i18n/testing'; |
7 | 8 | import List, { ListProps } from '../../../lib/components/list'; |
8 | 9 | import createWrapper from '../../../lib/components/test-utils/dom'; |
9 | 10 | import InternalDragHandleWrapper from '../../../lib/components/test-utils/dom/internal/drag-handle'; |
10 | 11 |
|
| 12 | +const mockDndContextAutoScroll: { current: AutoScrollOptions | undefined } = { current: undefined }; |
| 13 | + |
| 14 | +jest.mock('@dnd-kit/core', () => { |
| 15 | + const actual = jest.requireActual('@dnd-kit/core'); |
| 16 | + const { createElement } = jest.requireActual('react'); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + DndContext: (props: { autoScroll?: AutoScrollOptions }) => { |
| 20 | + mockDndContextAutoScroll.current = props.autoScroll; |
| 21 | + return createElement(actual.DndContext, props); |
| 22 | + }, |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
11 | 26 | interface Item { |
12 | 27 | id: string; |
13 | 28 | content: string; |
@@ -246,4 +261,87 @@ describe('List - Sortable', () => { |
246 | 261 | dragHandle.click({ button: 1 }); |
247 | 262 | expect(dragWrapper.findVisibleDirectionButtonBlockEnd()).toBeFalsy(); |
248 | 263 | }); |
| 264 | + |
| 265 | + describe('auto-scroll', () => { |
| 266 | + const interact = (action: () => void) => |
| 267 | + act(async () => { |
| 268 | + action(); |
| 269 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 270 | + }); |
| 271 | + const canAutoScroll = (element: Element) => mockDndContextAutoScroll.current!.canScroll!(element); |
| 272 | + |
| 273 | + function renderInScrollContainers({ innerScrolls }: { innerScrolls: boolean }) { |
| 274 | + const { container } = render( |
| 275 | + <div data-testid="outer" style={{ overflowY: 'auto' }}> |
| 276 | + <div data-testid="inner" style={{ overflowY: 'auto' }}> |
| 277 | + <ControlledList |
| 278 | + items={defaultItems} |
| 279 | + renderItem={item => ({ id: item.id, content: item.content })} |
| 280 | + sortable={true} |
| 281 | + /> |
| 282 | + </div> |
| 283 | + </div> |
| 284 | + ); |
| 285 | + const outer = container.querySelector<HTMLElement>('[data-testid="outer"]')!; |
| 286 | + const inner = container.querySelector<HTMLElement>('[data-testid="inner"]')!; |
| 287 | + // jsdom has no layout, so make the containers overflow explicitly. |
| 288 | + const overflowing = innerScrolls ? [outer, inner] : []; |
| 289 | + for (const element of overflowing) { |
| 290 | + Object.defineProperty(element, 'scrollHeight', { configurable: true, value: 1000 }); |
| 291 | + Object.defineProperty(element, 'clientHeight', { configurable: true, value: 100 }); |
| 292 | + } |
| 293 | + const list = createWrapper(container).findList()!; |
| 294 | + const item = list.findItemById('item1')!.getElement(); |
| 295 | + const handle = list.findItemById('item1')!.findDragHandle()!.getElement(); |
| 296 | + return { outer, inner, item, handle }; |
| 297 | + } |
| 298 | + |
| 299 | + test('checks the innermost scroll container first', () => { |
| 300 | + renderInScrollContainers({ innerScrolls: true }); |
| 301 | + expect(mockDndContextAutoScroll.current!.order).toBe(TraversalOrder.ReversedTreeOrder); |
| 302 | + }); |
| 303 | + |
| 304 | + test('only auto-scrolls the nearest scroll container and its descendants while dragging', async () => { |
| 305 | + const { outer, inner, item, handle } = renderInScrollContainers({ innerScrolls: true }); |
| 306 | + |
| 307 | + await interact(() => pressKey(handle, 'Space')); |
| 308 | + |
| 309 | + expect(canAutoScroll(inner)).toBe(true); |
| 310 | + expect(canAutoScroll(item)).toBe(true); |
| 311 | + expect(canAutoScroll(outer)).toBe(false); |
| 312 | + expect(canAutoScroll(document.documentElement)).toBe(false); |
| 313 | + |
| 314 | + await interact(() => pressKey(handle, 'Space')); |
| 315 | + |
| 316 | + expect(canAutoScroll(outer)).toBe(true); |
| 317 | + expect(canAutoScroll(document.documentElement)).toBe(true); |
| 318 | + }); |
| 319 | + |
| 320 | + test('keeps every container eligible for auto-scroll when the list has no scrollable ancestor', async () => { |
| 321 | + const { outer, inner, handle } = renderInScrollContainers({ innerScrolls: false }); |
| 322 | + |
| 323 | + await interact(() => pressKey(handle, 'Space')); |
| 324 | + |
| 325 | + expect(canAutoScroll(inner)).toBe(true); |
| 326 | + expect(canAutoScroll(outer)).toBe(true); |
| 327 | + expect(canAutoScroll(document.documentElement)).toBe(true); |
| 328 | + |
| 329 | + await interact(() => pressKey(handle, 'Escape')); |
| 330 | + }); |
| 331 | + |
| 332 | + test('limits auto-scroll when the drag starts on the drag handle icon and stops limiting it on cancel', async () => { |
| 333 | + const { outer, inner, handle } = renderInScrollContainers({ innerScrolls: true }); |
| 334 | + const icon = handle.querySelector('svg')!; |
| 335 | + expect(icon).not.toBeNull(); |
| 336 | + |
| 337 | + await interact(() => fireEvent.click(icon)); |
| 338 | + |
| 339 | + expect(canAutoScroll(inner)).toBe(true); |
| 340 | + expect(canAutoScroll(outer)).toBe(false); |
| 341 | + |
| 342 | + await interact(() => pressKey(handle, 'Escape')); |
| 343 | + |
| 344 | + expect(canAutoScroll(outer)).toBe(true); |
| 345 | + }); |
| 346 | + }); |
249 | 347 | }); |
0 commit comments