|
| 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 | + * Selection mode ↔ spec vocabulary parity + behavior (#2941). |
| 11 | + * |
| 12 | + * `SelectionConfigSchema.type` (`ui/view.zod.ts`) publishes three names: |
| 13 | + * `none | single | multiple`. The table used to treat `selectable` as a bare |
| 14 | + * truthy, so a view authored with `selection.type: 'single'` rendered the full |
| 15 | + * multi-select UX — per-row checkboxes that accumulate AND a select-all |
| 16 | + * header. The output looked right and wasn't: `single` was never |
| 17 | + * distinguished from `multiple`. |
| 18 | + * |
| 19 | + * Contract under test: |
| 20 | + * - parity: the table's declared mode set equals the spec enum, both ways; |
| 21 | + * - `single` offers no select-all and replaces the selection on each pick; |
| 22 | + * - `multiple` (and legacy `true`) keeps the accumulate + select-all UX. |
| 23 | + */ |
| 24 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 25 | +import { fireEvent } from '@testing-library/react'; |
| 26 | +import '@testing-library/jest-dom'; |
| 27 | +import { SelectionConfigSchema } from '@objectstack/spec/ui'; |
| 28 | +import { renderComponent } from './test-utils'; |
| 29 | +import { SUPPORTED_SELECTION_MODES } from '../renderers/complex/data-table'; |
| 30 | + |
| 31 | +beforeAll(async () => { |
| 32 | + await import('../renderers'); |
| 33 | +}, 30000); |
| 34 | + |
| 35 | +/** The spec's selection vocabulary, read through the `.default()` wrapper. */ |
| 36 | +function specSelectionModes(): string[] { |
| 37 | + const typeSchema = (SelectionConfigSchema as unknown as { shape?: Record<string, unknown> }) |
| 38 | + .shape?.type as { def?: { innerType?: { options?: readonly string[] } } } | undefined; |
| 39 | + const options = typeSchema?.def?.innerType?.options; |
| 40 | + return Array.isArray(options) ? [...options] : []; |
| 41 | +} |
| 42 | + |
| 43 | +const baseSchema = { |
| 44 | + type: 'data-table' as const, |
| 45 | + columns: [{ header: 'Name', accessorKey: 'name' }], |
| 46 | + data: [ |
| 47 | + { id: '1', name: 'Alice' }, |
| 48 | + { id: '2', name: 'Bob' }, |
| 49 | + { id: '3', name: 'Carol' }, |
| 50 | + ], |
| 51 | + pagination: false, |
| 52 | + searchable: false, |
| 53 | +}; |
| 54 | + |
| 55 | +describe('data-table selection mode covers the spec selection vocabulary', () => { |
| 56 | + const specNames = specSelectionModes(); |
| 57 | + |
| 58 | + it('reads a non-empty enum from the spec', () => { |
| 59 | + expect(specNames, 'could not read SelectionConfigSchema.shape.type options from the spec').not.toEqual([]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('implements every selection mode the spec accepts', () => { |
| 63 | + const unimplemented = specNames.filter((name) => !SUPPORTED_SELECTION_MODES.has(name)); |
| 64 | + expect( |
| 65 | + unimplemented, |
| 66 | + 'these pass schema validation but render an undistinguished selection UX — implement them in data-table', |
| 67 | + ).toEqual([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('does not accept selection modes the spec rejects', () => { |
| 71 | + const extra = [...SUPPORTED_SELECTION_MODES].filter((name) => !specNames.includes(name)); |
| 72 | + expect( |
| 73 | + extra, |
| 74 | + 'these are renderer-local dialect — promote them into @objectstack/spec instead', |
| 75 | + ).toEqual([]); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('data-table selection behavior per mode', () => { |
| 80 | + it("'single' renders no select-all header and replaces the selection on each pick", () => { |
| 81 | + const onSelectionChange = vi.fn(); |
| 82 | + const { getAllByRole } = renderComponent({ |
| 83 | + ...baseSchema, |
| 84 | + selectable: 'single', |
| 85 | + onSelectionChange, |
| 86 | + } as any); |
| 87 | + |
| 88 | + // 3 rows → exactly 3 checkboxes; a select-all header would make it 4. |
| 89 | + const checkboxes = getAllByRole('checkbox'); |
| 90 | + expect(checkboxes).toHaveLength(3); |
| 91 | + |
| 92 | + fireEvent.click(checkboxes[0]); |
| 93 | + expect(onSelectionChange).toHaveBeenLastCalledWith([expect.objectContaining({ name: 'Alice' })]); |
| 94 | + |
| 95 | + // Picking Bob must REPLACE Alice, never accumulate to two rows. |
| 96 | + fireEvent.click(checkboxes[1]); |
| 97 | + expect(onSelectionChange).toHaveBeenLastCalledWith([expect.objectContaining({ name: 'Bob' })]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("'single' allows deselecting the picked row", () => { |
| 101 | + const onSelectionChange = vi.fn(); |
| 102 | + const { getAllByRole } = renderComponent({ |
| 103 | + ...baseSchema, |
| 104 | + selectable: 'single', |
| 105 | + onSelectionChange, |
| 106 | + } as any); |
| 107 | + |
| 108 | + const checkboxes = getAllByRole('checkbox'); |
| 109 | + fireEvent.click(checkboxes[0]); |
| 110 | + fireEvent.click(checkboxes[0]); |
| 111 | + expect(onSelectionChange).toHaveBeenLastCalledWith([]); |
| 112 | + }); |
| 113 | + |
| 114 | + it("'multiple' keeps the select-all header and accumulates picks", () => { |
| 115 | + const onSelectionChange = vi.fn(); |
| 116 | + const { getAllByRole } = renderComponent({ |
| 117 | + ...baseSchema, |
| 118 | + selectable: 'multiple', |
| 119 | + onSelectionChange, |
| 120 | + } as any); |
| 121 | + |
| 122 | + // 3 rows + the select-all header. |
| 123 | + const checkboxes = getAllByRole('checkbox'); |
| 124 | + expect(checkboxes).toHaveLength(4); |
| 125 | + |
| 126 | + // First checkbox is the header select-all; rows follow. |
| 127 | + fireEvent.click(checkboxes[1]); |
| 128 | + fireEvent.click(checkboxes[2]); |
| 129 | + expect(onSelectionChange).toHaveBeenLastCalledWith([ |
| 130 | + expect.objectContaining({ name: 'Alice' }), |
| 131 | + expect.objectContaining({ name: 'Bob' }), |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it('legacy `selectable: true` still means multi-select', () => { |
| 136 | + const { getAllByRole } = renderComponent({ |
| 137 | + ...baseSchema, |
| 138 | + selectable: true, |
| 139 | + } as any); |
| 140 | + expect(getAllByRole('checkbox')).toHaveLength(4); |
| 141 | + }); |
| 142 | + |
| 143 | + it("'none' renders no selection column", () => { |
| 144 | + const { queryAllByRole } = renderComponent({ |
| 145 | + ...baseSchema, |
| 146 | + selectable: 'none', |
| 147 | + } as any); |
| 148 | + expect(queryAllByRole('checkbox')).toHaveLength(0); |
| 149 | + }); |
| 150 | +}); |
0 commit comments