|
| 1 | +import { renderHook } from '@testing-library/react-hooks'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { InteractionTagContextProvider } from '../../contexts/interactionTagContext'; |
| 5 | +import { |
| 6 | + useInteractionTagSecondary_unstable, |
| 7 | + useInteractionTagSecondaryBase_unstable, |
| 8 | +} from './useInteractionTagSecondary'; |
| 9 | + |
| 10 | +const baseContext = { |
| 11 | + appearance: 'filled' as const, |
| 12 | + disabled: false, |
| 13 | + handleTagDismiss: () => ({}), |
| 14 | + interactionTagPrimaryId: 'fui-InteractionTagPrimary-_test_', |
| 15 | + selected: false, |
| 16 | + selectedValues: [], |
| 17 | + shape: 'rounded' as const, |
| 18 | + size: 'medium' as const, |
| 19 | + value: 'test', |
| 20 | +}; |
| 21 | + |
| 22 | +const wrap = ( |
| 23 | + overrides: Partial<Parameters<typeof InteractionTagContextProvider>[0]['value']> = {}, |
| 24 | +): React.FC<{ children?: React.ReactNode }> => { |
| 25 | + const Wrapper: React.FC<{ children?: React.ReactNode }> = ({ children }) => ( |
| 26 | + <InteractionTagContextProvider value={{ ...baseContext, ...overrides }}>{children}</InteractionTagContextProvider> |
| 27 | + ); |
| 28 | + return Wrapper; |
| 29 | +}; |
| 30 | + |
| 31 | +describe('useInteractionTagSecondary_unstable', () => { |
| 32 | + it('should inject DismissRegular as default root children', () => { |
| 33 | + const ref = React.createRef<HTMLButtonElement>(); |
| 34 | + const { result } = renderHook(() => useInteractionTagSecondary_unstable({}, ref), { wrapper: wrap() }); |
| 35 | + expect(result.current.root.children).toBeDefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should preserve user-provided children instead of the default DismissRegular', () => { |
| 39 | + const ref = React.createRef<HTMLButtonElement>(); |
| 40 | + const { result } = renderHook(() => useInteractionTagSecondary_unstable({ children: 'X' }, ref), { |
| 41 | + wrapper: wrap(), |
| 42 | + }); |
| 43 | + expect(result.current.root.children).toBe('X'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should inherit appearance/shape/size from context', () => { |
| 47 | + const ref = React.createRef<HTMLButtonElement>(); |
| 48 | + const { result } = renderHook(() => useInteractionTagSecondary_unstable({}, ref), { |
| 49 | + wrapper: wrap({ appearance: 'outline', shape: 'circular', size: 'small' }), |
| 50 | + }); |
| 51 | + expect(result.current.appearance).toBe('outline'); |
| 52 | + expect(result.current.shape).toBe('circular'); |
| 53 | + expect(result.current.size).toBe('small'); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('useInteractionTagSecondaryBase_unstable', () => { |
| 58 | + it('should render root with type="button"', () => { |
| 59 | + const ref = React.createRef<HTMLButtonElement>(); |
| 60 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() }); |
| 61 | + expect(result.current.root.type).toBe('button'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should NOT inject DismissRegular children by default (icon injection lives in the styled hook)', () => { |
| 65 | + const ref = React.createRef<HTMLButtonElement>(); |
| 66 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() }); |
| 67 | + expect(result.current.root.children).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should attach onClick and onKeyDown handlers', () => { |
| 71 | + const ref = React.createRef<HTMLButtonElement>(); |
| 72 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() }); |
| 73 | + expect(result.current.root.onClick).toBeDefined(); |
| 74 | + expect(result.current.root.onKeyDown).toBeDefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should build aria-labelledby from interactionTagPrimaryId and own id', () => { |
| 78 | + const ref = React.createRef<HTMLButtonElement>(); |
| 79 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() }); |
| 80 | + expect(result.current.root['aria-labelledby']).toEqual( |
| 81 | + expect.stringMatching(/^fui-InteractionTagPrimary-_test_ fui-InteractionTagSecondary-/), |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should NOT expose design-only fields (appearance/shape/size)', () => { |
| 86 | + const ref = React.createRef<HTMLButtonElement>(); |
| 87 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() }); |
| 88 | + expect((result.current as unknown as { appearance?: unknown }).appearance).toBeUndefined(); |
| 89 | + expect((result.current as unknown as { shape?: unknown }).shape).toBeUndefined(); |
| 90 | + expect((result.current as unknown as { size?: unknown }).size).toBeUndefined(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should call handleTagDismiss on Delete/Backspace keyDown via context', () => { |
| 94 | + const handleTagDismiss = jest.fn(); |
| 95 | + const ref = React.createRef<HTMLButtonElement>(); |
| 96 | + const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { |
| 97 | + wrapper: wrap({ handleTagDismiss, value: 'val' }), |
| 98 | + }); |
| 99 | + |
| 100 | + const event = { key: 'Delete', defaultPrevented: false } as unknown as React.KeyboardEvent<HTMLButtonElement>; |
| 101 | + result.current.root.onKeyDown?.(event); |
| 102 | + |
| 103 | + expect(handleTagDismiss).toHaveBeenCalledWith(event, { value: 'val' }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments