|
| 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 | + * objectui#7687 — `options[].disabled` on a `combobox` node must be READ. |
| 11 | + * |
| 12 | + * The member was declared (`@object-ui/types` `ComboboxOption.disabled`), |
| 13 | + * validated (`ComboboxOptionSchema` in the zod mirror, pinned as `boolean` on |
| 14 | + * both faces by `disabled-twin-symmetry-7087.test.ts`) and never read: the |
| 15 | + * component mapped each option to a `CommandItem` with `key` / `value` / |
| 16 | + * `onSelect` only, so an option authored `disabled: true` passed validation, |
| 17 | + * type-checked against the published `ComboboxSchema`, and rendered as an |
| 18 | + * ordinary selectable option — a declared key with no read site behind it. |
| 19 | + * |
| 20 | + * ⛔ An attribute-only pin is not enough here: it passes on "styled disabled |
| 21 | + * but still clickable", which is the exact defect this card is about. So the |
| 22 | + * behaviour is pinned too — a disabled option must not fire `onValueChange`. |
| 23 | + * The third test is the CONTROL that keeps that negative from being vacuous: |
| 24 | + * without it, a popover that never opened would satisfy "not called". |
| 25 | + */ |
| 26 | +import { describe, it, expect, vi } from 'vitest'; |
| 27 | +import React from 'react'; |
| 28 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 29 | +import '@testing-library/jest-dom'; |
| 30 | +import { Combobox } from '../custom/combobox'; |
| 31 | + |
| 32 | +const OPTIONS = [ |
| 33 | + { value: 'alpha', label: 'Alpha' }, |
| 34 | + { value: 'beta', label: 'Beta', disabled: true }, |
| 35 | + { value: 'gamma', label: 'Gamma', disabled: false }, |
| 36 | +]; |
| 37 | + |
| 38 | +function openDropdown() { |
| 39 | + fireEvent.click(screen.getByRole('combobox')); |
| 40 | +} |
| 41 | + |
| 42 | +describe('Combobox honours options[].disabled (objectui#7687)', () => { |
| 43 | + it('marks an option authored `disabled: true` as disabled in the DOM', () => { |
| 44 | + render(<Combobox options={OPTIONS} value="" onValueChange={vi.fn()} />); |
| 45 | + openDropdown(); |
| 46 | + |
| 47 | + const beta = screen.getByRole('option', { name: /Beta/ }); |
| 48 | + // `data-disabled` is what the CommandItem wrapper's className already |
| 49 | + // styles (`data-[disabled=true]:opacity-50 …:pointer-events-none`); |
| 50 | + // `aria-disabled` is what assistive tech and cmdk's own valid-item |
| 51 | + // selector read. |
| 52 | + expect(beta).toHaveAttribute('data-disabled', 'true'); |
| 53 | + expect(beta).toHaveAttribute('aria-disabled', 'true'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('refuses to select a disabled option — no onValueChange', () => { |
| 57 | + const onValueChange = vi.fn(); |
| 58 | + render(<Combobox options={OPTIONS} value="" onValueChange={onValueChange} />); |
| 59 | + openDropdown(); |
| 60 | + |
| 61 | + fireEvent.click(screen.getByRole('option', { name: /Beta/ })); |
| 62 | + expect(onValueChange).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('refuses the keyboard path too — Enter on a search narrowed to the disabled option selects nothing', () => { |
| 66 | + const onValueChange = vi.fn(); |
| 67 | + render(<Combobox options={OPTIONS} value="" onValueChange={onValueChange} />); |
| 68 | + openDropdown(); |
| 69 | + |
| 70 | + // A separate code path from the click above: cmdk keeps disabled items out |
| 71 | + // of the valid-item selector its arrow keys and its auto-select walk, and |
| 72 | + // never registers the `cmdk-item-select` listener Enter dispatches. With |
| 73 | + // the list narrowed to `Beta` alone there is nothing left to select. |
| 74 | + const search = screen.getByPlaceholderText('Search...'); |
| 75 | + fireEvent.change(search, { target: { value: 'bet' } }); |
| 76 | + expect(screen.getByRole('option', { name: /Beta/ })).toBeInTheDocument(); |
| 77 | + expect(screen.queryByRole('option', { name: /Alpha/ })).toBeNull(); |
| 78 | + |
| 79 | + fireEvent.keyDown(search, { key: 'Enter' }); |
| 80 | + expect(onValueChange).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('CONTROL — options without `disabled: true` stay selectable', () => { |
| 84 | + const onValueChange = vi.fn(); |
| 85 | + render(<Combobox options={OPTIONS} value="" onValueChange={onValueChange} />); |
| 86 | + openDropdown(); |
| 87 | + |
| 88 | + // Omitted (`alpha`) and explicitly `false` (`gamma`) are both enabled. |
| 89 | + const alpha = screen.getByRole('option', { name: /Alpha/ }); |
| 90 | + const gamma = screen.getByRole('option', { name: /Gamma/ }); |
| 91 | + expect(alpha).toHaveAttribute('data-disabled', 'false'); |
| 92 | + expect(gamma).toHaveAttribute('data-disabled', 'false'); |
| 93 | + |
| 94 | + fireEvent.click(alpha); |
| 95 | + expect(onValueChange).toHaveBeenCalledWith('alpha'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('CONTROL — the keyboard path still selects an enabled option', () => { |
| 99 | + const onValueChange = vi.fn(); |
| 100 | + render(<Combobox options={OPTIONS} value="" onValueChange={onValueChange} />); |
| 101 | + openDropdown(); |
| 102 | + |
| 103 | + const search = screen.getByPlaceholderText('Search...'); |
| 104 | + fireEvent.change(search, { target: { value: 'gam' } }); |
| 105 | + fireEvent.keyDown(search, { key: 'Enter' }); |
| 106 | + expect(onValueChange).toHaveBeenCalledWith('gamma'); |
| 107 | + }); |
| 108 | +}); |
0 commit comments