|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file experimental.test.ts |
| 5 | + * @input The `@astryxdesign/core/List/experimental` entry, the package root, |
| 6 | + * the ordinary List subpath, the hooks and utils subpaths, and |
| 7 | + * package.json's exports map |
| 8 | + * @output Proof that the experimental List reorder surface is exported where |
| 9 | + * spec:AST-017 FR21 puts it and nowhere else, and that it reorders a list |
| 10 | + * whose rows are not ListItems |
| 11 | + * @position Testing for /packages/core/src/List/experimental.ts |
| 12 | + */ |
| 13 | + |
| 14 | +import {useId, useRef, useState, type ReactNode} from 'react'; |
| 15 | +import {describe, expect, it, vi} from 'vitest'; |
| 16 | +import {render, screen} from '@testing-library/react'; |
| 17 | +import userEvent from '@testing-library/user-event'; |
| 18 | +import pkg from '../../package.json'; |
| 19 | +import * as hooks from '../hooks'; |
| 20 | +import * as root from '../index'; |
| 21 | +import * as utils from '../utils'; |
| 22 | +import * as list from './index'; |
| 23 | +import * as reorder from './experimental'; |
| 24 | + |
| 25 | +const NAMES = [ |
| 26 | + 'useListReorder', |
| 27 | + 'ListReorderContext', |
| 28 | + 'ListReorderHandle', |
| 29 | + 'LIST_REORDER_FIXED_ROW_ATTRIBUTE', |
| 30 | +] as const; |
| 31 | + |
| 32 | +describe('@astryxdesign/core/List/experimental', () => { |
| 33 | + it('exports the reorder surface', () => { |
| 34 | + for (const name of NAMES) { |
| 35 | + expect(reorder[name], name).toBeDefined(); |
| 36 | + } |
| 37 | + expect(reorder.LIST_REORDER_FIXED_ROW_ATTRIBUTE).toBe( |
| 38 | + 'data-astryx-reorder-fixed', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('is a subpath of its own, never re-exported from the root or an ordinary subpath', () => { |
| 43 | + expect(pkg.exports['./List/experimental']).toMatchObject({ |
| 44 | + source: './src/List/experimental.ts', |
| 45 | + }); |
| 46 | + for (const name of NAMES) { |
| 47 | + expect(name in root, `root re-exports ${name}`).toBe(false); |
| 48 | + expect(name in list, `List re-exports ${name}`).toBe(false); |
| 49 | + expect(name in hooks, `hooks re-exports ${name}`).toBe(false); |
| 50 | + expect(name in utils, `utils re-exports ${name}`).toBe(false); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('reorders a list whose rows hold their own content', async () => { |
| 55 | + const onChange = vi.fn(); |
| 56 | + function Records(): ReactNode { |
| 57 | + const listRef = useRef<HTMLOListElement>(null); |
| 58 | + const hintId = useId(); |
| 59 | + const [items, setItems] = useState(['Alpha', 'Beta', 'Gamma']); |
| 60 | + const session = reorder.useListReorder( |
| 61 | + listRef, |
| 62 | + (fromIndex, toIndex) => { |
| 63 | + const next = [...items]; |
| 64 | + const [moved] = next.splice(fromIndex, 1); |
| 65 | + next.splice(toIndex, 0, moved); |
| 66 | + onChange(next); |
| 67 | + setItems(next); |
| 68 | + }, |
| 69 | + hintId, |
| 70 | + ); |
| 71 | + return ( |
| 72 | + <reorder.ListReorderContext value={session}> |
| 73 | + <ol ref={listRef}> |
| 74 | + {items.map(item => ( |
| 75 | + <li |
| 76 | + key={item} |
| 77 | + {...(item === 'Gamma' |
| 78 | + ? {[reorder.LIST_REORDER_FIXED_ROW_ATTRIBUTE]: ''} |
| 79 | + : null)}> |
| 80 | + <input aria-label={`${item} name`} defaultValue={item} /> |
| 81 | + {item === 'Gamma' ? null : ( |
| 82 | + <reorder.ListReorderHandle |
| 83 | + label={item} |
| 84 | + aria-label={item === 'Beta' ? 'Move record 2' : undefined} |
| 85 | + /> |
| 86 | + )} |
| 87 | + </li> |
| 88 | + ))} |
| 89 | + </ol> |
| 90 | + <span id={hintId} hidden> |
| 91 | + Reorder instructions |
| 92 | + </span> |
| 93 | + </reorder.ListReorderContext> |
| 94 | + ); |
| 95 | + } |
| 96 | + const user = userEvent.setup(); |
| 97 | + render(<Records />); |
| 98 | + const handle = screen.getByRole('button', {name: 'Reorder Alpha'}); |
| 99 | + expect(handle).toHaveAccessibleDescription('Reorder instructions'); |
| 100 | + expect(screen.queryByRole('button', {name: 'Reorder Gamma'})).toBeNull(); |
| 101 | + // A whole name replaces "Reorder {label}". |
| 102 | + expect(screen.getByRole('button', {name: 'Move record 2'})).toBeVisible(); |
| 103 | + handle.focus(); |
| 104 | + await user.keyboard('{ArrowDown}'); |
| 105 | + expect(onChange).toHaveBeenLastCalledWith(['Beta', 'Alpha', 'Gamma']); |
| 106 | + // The fixed row bounds the others: Alpha cannot pass Gamma. |
| 107 | + await user.keyboard('{ArrowDown}'); |
| 108 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 109 | + expect(screen.getByRole('button', {name: 'Reorder Alpha'})).toHaveFocus(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('moves rows of any tag, with the handle deep inside each', async () => { |
| 113 | + const onReorder = vi.fn(); |
| 114 | + function Cards(): ReactNode { |
| 115 | + const listRef = useRef<HTMLDivElement>(null); |
| 116 | + const hintId = useId(); |
| 117 | + const session = reorder.useListReorder(listRef, onReorder, hintId); |
| 118 | + return ( |
| 119 | + <reorder.ListReorderContext value={session}> |
| 120 | + <div ref={listRef} role="list"> |
| 121 | + {['Alpha', 'Beta'].map(item => ( |
| 122 | + <div key={item} role="listitem"> |
| 123 | + <span> |
| 124 | + {item} |
| 125 | + <reorder.ListReorderHandle label={item} /> |
| 126 | + </span> |
| 127 | + </div> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + <span id={hintId} hidden> |
| 131 | + Reorder instructions |
| 132 | + </span> |
| 133 | + </reorder.ListReorderContext> |
| 134 | + ); |
| 135 | + } |
| 136 | + const user = userEvent.setup(); |
| 137 | + render(<Cards />); |
| 138 | + screen.getByRole('button', {name: 'Reorder Alpha'}).focus(); |
| 139 | + await user.keyboard('{ArrowDown}'); |
| 140 | + expect(onReorder).toHaveBeenCalledWith(0, 1); |
| 141 | + }); |
| 142 | +}); |
0 commit comments