|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#28923 — the `data-table` row ⋯ trigger is visible at rest. |
| 11 | + * |
| 12 | + * The table draws an `Actions` header whenever rows carry actions, so a |
| 13 | + * trigger hidden until the pointer reaches a row left a headed column of cells |
| 14 | + * that looked empty, read as "no actions available". As the list grid's |
| 15 | + * `RowActionMenu` does since objectstack#28871, the trigger is now drawn on |
| 16 | + * every row at rest in the muted tone, on every pointer type alike, and rises |
| 17 | + * to full strength on row hover, focus-within, a selected row, its own focus, |
| 18 | + * an open menu and a coarse pointer (which cannot hover). |
| 19 | + * |
| 20 | + * jsdom applies no stylesheet, so "visible" is read off the classes that decide |
| 21 | + * it: no `opacity-0` / `invisible` / `hidden` at rest, the muted tone as the |
| 22 | + * resting colour, and the variants that lift it. |
| 23 | + */ |
| 24 | +import { describe, it, expect, vi } from 'vitest'; |
| 25 | +import { render, screen } from '@testing-library/react'; |
| 26 | +import userEvent from '@testing-library/user-event'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +import React from 'react'; |
| 29 | +import { ComponentRegistry } from '@object-ui/core'; |
| 30 | +import { PredicateScopeProvider } from '@object-ui/react'; |
| 31 | +import '../data-table'; |
| 32 | + |
| 33 | +const ROWS = [ |
| 34 | + { id: 'a', name: 'Ada' }, |
| 35 | + { id: 'b', name: 'Grace' }, |
| 36 | +]; |
| 37 | + |
| 38 | +function renderTable() { |
| 39 | + const DataTable = ComponentRegistry.get('data-table') as any; |
| 40 | + if (!DataTable) throw new Error('data-table not registered'); |
| 41 | + return render( |
| 42 | + <PredicateScopeProvider scope={{}}> |
| 43 | + <DataTable |
| 44 | + schema={{ |
| 45 | + type: 'data-table', |
| 46 | + data: ROWS, |
| 47 | + pagination: false, |
| 48 | + searchable: false, |
| 49 | + columns: [{ header: 'Name', accessorKey: 'name' }], |
| 50 | + rowActions: true, |
| 51 | + onRowEdit: vi.fn(), |
| 52 | + }} |
| 53 | + /> |
| 54 | + </PredicateScopeProvider>, |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +const classesOf = (el: HTMLElement) => el.className.split(/\s+/).filter(Boolean); |
| 59 | + |
| 60 | +describe('data-table row-actions trigger at rest (objectstack#28923)', () => { |
| 61 | + it('is drawn at rest on every row: nothing hides it before the pointer reaches the row', () => { |
| 62 | + renderTable(); |
| 63 | + const triggers = screen.getAllByTestId('data-table-row-actions'); |
| 64 | + expect(triggers).toHaveLength(ROWS.length); |
| 65 | + for (const trigger of triggers) { |
| 66 | + for (const hiding of ['opacity-0', 'invisible', 'hidden', 'sr-only']) { |
| 67 | + expect(trigger).not.toHaveClass(hiding); |
| 68 | + } |
| 69 | + // No zero opacity under any variant either: a hover reveal written as |
| 70 | + // `opacity-0 hover:opacity-100` is still empty at rest. |
| 71 | + expect(classesOf(trigger).filter((c) => /(^|:)opacity-0$/.test(c))).toEqual([]); |
| 72 | + expect(trigger).toHaveAccessibleName('Row actions'); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('rests in the muted tone and lifts to full strength on hover, focus, selection and an open menu', () => { |
| 77 | + renderTable(); |
| 78 | + for (const trigger of screen.getAllByTestId('data-table-row-actions')) { |
| 79 | + expect(trigger).toHaveClass( |
| 80 | + 'text-muted-foreground', |
| 81 | + 'group-hover/row:text-foreground', |
| 82 | + 'group-focus-within/row:text-foreground', |
| 83 | + 'group-data-[state=selected]/row:text-foreground', |
| 84 | + 'data-[state=open]:text-foreground', |
| 85 | + 'focus-visible:text-foreground', |
| 86 | + ); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it('is drawn at rest on every pointer type alike; a coarse pointer, which cannot hover, gets full strength', () => { |
| 91 | + renderTable(); |
| 92 | + for (const trigger of screen.getAllByTestId('data-table-row-actions')) { |
| 93 | + // The coarse pointer only strengthens the tone; it is never the thing |
| 94 | + // that makes the trigger appear. |
| 95 | + expect(classesOf(trigger).filter((c) => c.startsWith('pointer-coarse:'))).toEqual([ |
| 96 | + 'pointer-coarse:text-foreground', |
| 97 | + ]); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it('control: the open menu marks the trigger open, the state the full-strength variant keys on', async () => { |
| 102 | + renderTable(); |
| 103 | + const [trigger] = screen.getAllByTestId('data-table-row-actions'); |
| 104 | + expect(trigger).toHaveAttribute('data-state', 'closed'); |
| 105 | + await userEvent.click(trigger); |
| 106 | + expect(trigger).toHaveAttribute('data-state', 'open'); |
| 107 | + expect(await screen.findByRole('menuitem', { name: /edit/i })).toBeInTheDocument(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments