|
| 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#27807 — with some rows selected, the header's select-all is |
| 11 | + * announced as mixed AND drawn as a dash, never as the check mark that means |
| 12 | + * "all selected". Controls: all selected draws the check; none draws nothing. |
| 13 | + */ |
| 14 | +import { describe, it, expect } from 'vitest'; |
| 15 | +import { render, fireEvent } from '@testing-library/react'; |
| 16 | +import '@testing-library/jest-dom'; |
| 17 | +import React from 'react'; |
| 18 | +import { ComponentRegistry } from '@object-ui/core'; |
| 19 | +import '../data-table'; |
| 20 | + |
| 21 | +function renderTable() { |
| 22 | + const DataTable = ComponentRegistry.get('data-table') as any; |
| 23 | + return render( |
| 24 | + <DataTable |
| 25 | + schema={{ |
| 26 | + data: [ |
| 27 | + { id: 'a', name: 'Ada' }, |
| 28 | + { id: 'b', name: 'Grace' }, |
| 29 | + { id: 'c', name: 'Linus' }, |
| 30 | + ], |
| 31 | + pagination: false, |
| 32 | + searchable: false, |
| 33 | + selectable: true, |
| 34 | + columns: [{ header: 'Name', accessorKey: 'name' }], |
| 35 | + }} |
| 36 | + />, |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function selectAllOf(container: HTMLElement) { |
| 41 | + return container.querySelector('thead [data-slot="select-all"] [role="checkbox"]') as HTMLElement; |
| 42 | +} |
| 43 | + |
| 44 | +function rowCheckboxes(container: HTMLElement) { |
| 45 | + return Array.from(container.querySelectorAll('tbody [data-slot="row-select"] [role="checkbox"]')) as HTMLElement[]; |
| 46 | +} |
| 47 | + |
| 48 | +describe('data-table select-all mixed state (objectstack#27807)', () => { |
| 49 | + it('a partial selection draws the dash, not the check, and stays aria-checked="mixed"', () => { |
| 50 | + const { container } = renderTable(); |
| 51 | + const rows = rowCheckboxes(container); |
| 52 | + expect(rows).toHaveLength(3); |
| 53 | + fireEvent.click(rows[0]); |
| 54 | + const header = selectAllOf(container); |
| 55 | + expect(header).toHaveAttribute('aria-checked', 'mixed'); |
| 56 | + expect(header.querySelector('[data-glyph="dash"]')).not.toBeNull(); |
| 57 | + expect(header.querySelector('[data-glyph="check"]')).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('control: all rows selected draws the check', () => { |
| 61 | + const { container } = renderTable(); |
| 62 | + for (const row of rowCheckboxes(container)) fireEvent.click(row); |
| 63 | + const header = selectAllOf(container); |
| 64 | + expect(header).toHaveAttribute('aria-checked', 'true'); |
| 65 | + expect(header.querySelector('[data-glyph="check"]')).not.toBeNull(); |
| 66 | + expect(header.querySelector('[data-glyph="dash"]')).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('control: no rows selected draws nothing', () => { |
| 70 | + const { container } = renderTable(); |
| 71 | + const header = selectAllOf(container); |
| 72 | + expect(header).toHaveAttribute('aria-checked', 'false'); |
| 73 | + expect(header.querySelector('svg')).toBeNull(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments