|
| 1 | +import * as React from 'react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { render, fireEvent } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | + |
| 6 | +import type { CardBaseProps, CardState } from './Card.types'; |
| 7 | +import { useCardBase_unstable } from './useCard'; |
| 8 | +import { renderCard_unstable } from './renderCard'; |
| 9 | +import { useCardContextValue } from './useCardContextValue'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Minimal styled wrapper that composes `useCardBase_unstable` exactly as a real |
| 13 | + * styled hook would: call the base hook, add design defaults, then render. |
| 14 | + */ |
| 15 | +const TestCard = React.forwardRef<HTMLDivElement, CardBaseProps>((props, ref) => { |
| 16 | + const baseState = useCardBase_unstable(props, ref); |
| 17 | + |
| 18 | + // Augment with design defaults to satisfy CardState for the renderer |
| 19 | + const state = Object.assign(baseState, { |
| 20 | + appearance: 'filled', |
| 21 | + orientation: 'vertical', |
| 22 | + size: 'medium', |
| 23 | + }) as CardState; |
| 24 | + |
| 25 | + const cardContextValue = useCardContextValue(state); |
| 26 | + return renderCard_unstable(state, cardContextValue); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('useCardBase_unstable', () => { |
| 30 | + // ── Slot structure ──────────────────────────────────────────────────── |
| 31 | + |
| 32 | + it('renders a div with role="group"', () => { |
| 33 | + const { getByRole } = render(<TestCard>Content</TestCard>); |
| 34 | + expect(getByRole('group')).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not render checkbox or floatingAction by default', () => { |
| 38 | + const { container } = render(<TestCard>Content</TestCard>); |
| 39 | + expect(container.querySelector('input[type="checkbox"]')).toBeNull(); |
| 40 | + expect(container.querySelector('.fui-Card__floatingAction')).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + // ── disabled ────────────────────────────────────────────────────────── |
| 44 | + |
| 45 | + it('sets aria-disabled on the root when disabled', () => { |
| 46 | + const { getByRole } = render(<TestCard disabled>Content</TestCard>); |
| 47 | + expect(getByRole('group')).toHaveAttribute('aria-disabled', 'true'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not fire onClick when disabled', () => { |
| 51 | + const onClick = jest.fn(); |
| 52 | + const { getByRole } = render( |
| 53 | + <TestCard disabled onClick={onClick}> |
| 54 | + Content |
| 55 | + </TestCard>, |
| 56 | + ); |
| 57 | + fireEvent.click(getByRole('group')); |
| 58 | + expect(onClick).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not add tabindex when disabled', () => { |
| 62 | + const onClick = jest.fn(); |
| 63 | + const { getByRole } = render( |
| 64 | + <TestCard disabled onClick={onClick}> |
| 65 | + <button>inner</button> |
| 66 | + </TestCard>, |
| 67 | + ); |
| 68 | + expect(getByRole('group').getAttribute('tabindex')).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + // ── focusMode / interactive ─────────────────────────────────────────── |
| 72 | + |
| 73 | + it('does not add tabindex for non-interactive card (no event handlers)', () => { |
| 74 | + const { getByTestId } = render( |
| 75 | + <TestCard data-testid="card"> |
| 76 | + <button>inner</button> |
| 77 | + </TestCard>, |
| 78 | + ); |
| 79 | + expect(getByTestId('card').getAttribute('tabindex')).toBeNull(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('adds tabindex=0 for interactive card (onClick present)', () => { |
| 83 | + const { getByTestId } = render( |
| 84 | + <TestCard data-testid="card" onClick={jest.fn()}> |
| 85 | + <button>inner</button> |
| 86 | + </TestCard>, |
| 87 | + ); |
| 88 | + userEvent.tab(); |
| 89 | + expect(getByTestId('card').getAttribute('tabindex')).toEqual('0'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('overrides default focusMode when explicitly set to "off"', () => { |
| 93 | + const { getByTestId } = render( |
| 94 | + <TestCard data-testid="card" onClick={jest.fn()} focusMode="off"> |
| 95 | + <button>inner</button> |
| 96 | + </TestCard>, |
| 97 | + ); |
| 98 | + expect(getByTestId('card').getAttribute('tabindex')).toBeNull(); |
| 99 | + }); |
| 100 | + |
| 101 | + // ── selectable ──────────────────────────────────────────────────────── |
| 102 | + |
| 103 | + it('renders a checkbox when onSelectionChange is provided', () => { |
| 104 | + const { getByRole } = render( |
| 105 | + <TestCard onSelectionChange={jest.fn()}>Content</TestCard>, |
| 106 | + ); |
| 107 | + expect(getByRole('checkbox')).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('toggles selection on click', () => { |
| 111 | + const onSelectionChange = jest.fn(); |
| 112 | + const { getByRole } = render( |
| 113 | + <TestCard defaultSelected={false} onSelectionChange={onSelectionChange}> |
| 114 | + Content |
| 115 | + </TestCard>, |
| 116 | + ); |
| 117 | + fireEvent.click(getByRole('group')); |
| 118 | + expect(onSelectionChange).toHaveBeenCalledWith(expect.anything(), { selected: true }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('does not toggle selection when disabled', () => { |
| 122 | + const onSelectionChange = jest.fn(); |
| 123 | + const { getByRole } = render( |
| 124 | + <TestCard disabled defaultSelected={false} onSelectionChange={onSelectionChange}> |
| 125 | + Content |
| 126 | + </TestCard>, |
| 127 | + ); |
| 128 | + fireEvent.click(getByRole('group')); |
| 129 | + expect(onSelectionChange).not.toHaveBeenCalled(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('disables checkbox when card is disabled', () => { |
| 133 | + const { getByRole } = render( |
| 134 | + <TestCard disabled selected={false} onSelectionChange={jest.fn()}> |
| 135 | + Content |
| 136 | + </TestCard>, |
| 137 | + ); |
| 138 | + expect(getByRole('checkbox')).toHaveAttribute('disabled'); |
| 139 | + }); |
| 140 | + |
| 141 | + // ── refs ────────────────────────────────────────────────────────────── |
| 142 | + |
| 143 | + it('forwards ref to the root element', () => { |
| 144 | + const ref = React.createRef<HTMLDivElement>(); |
| 145 | + render(<TestCard ref={ref}>Content</TestCard>); |
| 146 | + expect(ref.current).toBeInstanceOf(HTMLDivElement); |
| 147 | + }); |
| 148 | +}); |
0 commit comments