|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react' |
| 2 | + |
| 3 | +import type { ComponentCredentialSchema } from '@/core/types' |
| 4 | + |
| 5 | +import { CredentialTypeSelector } from './CredentialTypeSelector' |
| 6 | + |
| 7 | +// ─── Mocks ──────────────────────────────────────────────────────────────────── |
| 8 | + |
| 9 | +jest.mock('@tabler/icons-react', () => ({ |
| 10 | + IconKey: () => <span data-testid='icon-key-fallback' />, |
| 11 | + IconSearch: () => <span data-testid='icon-search' />, |
| 12 | + IconX: (props: { onClick?: () => void }) => <button data-testid='icon-x' onClick={props.onClick} /> |
| 13 | +})) |
| 14 | + |
| 15 | +// ─── Fixtures ───────────────────────────────────────────────────────────────── |
| 16 | + |
| 17 | +const schemas: ComponentCredentialSchema[] = [ |
| 18 | + { label: 'HTTP Basic Auth', name: 'httpBasicAuth', inputs: [] }, |
| 19 | + { label: 'HTTP Bearer Token', name: 'httpBearerToken', inputs: [] }, |
| 20 | + { label: 'HTTP Api Key', name: 'httpApiKey', inputs: [] } |
| 21 | +] |
| 22 | + |
| 23 | +const apiBaseUrl = 'http://localhost:3000' |
| 24 | + |
| 25 | +// ─── Tests ──────────────────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +describe('CredentialTypeSelector', () => { |
| 28 | + it('renders search input with placeholder', () => { |
| 29 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 30 | + |
| 31 | + expect(screen.getByPlaceholderText('Search credential')).toBeInTheDocument() |
| 32 | + }) |
| 33 | + |
| 34 | + it('renders all credential cards with labels and icons', () => { |
| 35 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 36 | + |
| 37 | + expect(screen.getByText('HTTP Basic Auth')).toBeInTheDocument() |
| 38 | + expect(screen.getByText('HTTP Bearer Token')).toBeInTheDocument() |
| 39 | + expect(screen.getByText('HTTP Api Key')).toBeInTheDocument() |
| 40 | + |
| 41 | + const images = screen.getAllByRole('img') |
| 42 | + expect(images).toHaveLength(3) |
| 43 | + expect(images[0]).toHaveAttribute('src', 'http://localhost:3000/api/v1/components-credentials-icon/httpBasicAuth') |
| 44 | + expect(images[0]).toHaveAttribute('alt', 'httpBasicAuth') |
| 45 | + }) |
| 46 | + |
| 47 | + it('calls onSelect with the clicked schema', () => { |
| 48 | + const onSelect = jest.fn() |
| 49 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={onSelect} />) |
| 50 | + |
| 51 | + fireEvent.click(screen.getByText('HTTP Bearer Token')) |
| 52 | + |
| 53 | + expect(onSelect).toHaveBeenCalledTimes(1) |
| 54 | + expect(onSelect).toHaveBeenCalledWith(schemas[1]) |
| 55 | + }) |
| 56 | + |
| 57 | + it('filters schemas by search input', () => { |
| 58 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 59 | + |
| 60 | + const searchInput = screen.getByPlaceholderText('Search credential') |
| 61 | + fireEvent.change(searchInput, { target: { value: 'bearer' } }) |
| 62 | + |
| 63 | + expect(screen.getByText('HTTP Bearer Token')).toBeInTheDocument() |
| 64 | + expect(screen.queryByText('HTTP Basic Auth')).not.toBeInTheDocument() |
| 65 | + expect(screen.queryByText('HTTP Api Key')).not.toBeInTheDocument() |
| 66 | + }) |
| 67 | + |
| 68 | + it('search is case-insensitive', () => { |
| 69 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 70 | + |
| 71 | + fireEvent.change(screen.getByPlaceholderText('Search credential'), { target: { value: 'API' } }) |
| 72 | + |
| 73 | + expect(screen.getByText('HTTP Api Key')).toBeInTheDocument() |
| 74 | + expect(screen.queryByText('HTTP Basic Auth')).not.toBeInTheDocument() |
| 75 | + }) |
| 76 | + |
| 77 | + it('clears search when clear button is clicked', () => { |
| 78 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 79 | + |
| 80 | + const searchInput = screen.getByPlaceholderText('Search credential') |
| 81 | + fireEvent.change(searchInput, { target: { value: 'bearer' } }) |
| 82 | + |
| 83 | + expect(screen.queryByText('HTTP Basic Auth')).not.toBeInTheDocument() |
| 84 | + |
| 85 | + fireEvent.click(screen.getByTestId('icon-x')) |
| 86 | + |
| 87 | + expect(screen.getByText('HTTP Basic Auth')).toBeInTheDocument() |
| 88 | + expect(screen.getByText('HTTP Bearer Token')).toBeInTheDocument() |
| 89 | + expect(screen.getByText('HTTP Api Key')).toBeInTheDocument() |
| 90 | + }) |
| 91 | + |
| 92 | + it('shows no cards when search matches nothing', () => { |
| 93 | + render(<CredentialTypeSelector schemas={schemas} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 94 | + |
| 95 | + fireEvent.change(screen.getByPlaceholderText('Search credential'), { target: { value: 'nonexistent' } }) |
| 96 | + |
| 97 | + expect(screen.queryByText('HTTP Basic Auth')).not.toBeInTheDocument() |
| 98 | + expect(screen.queryByText('HTTP Bearer Token')).not.toBeInTheDocument() |
| 99 | + expect(screen.queryByText('HTTP Api Key')).not.toBeInTheDocument() |
| 100 | + expect(screen.queryAllByRole('img')).toHaveLength(0) |
| 101 | + }) |
| 102 | + |
| 103 | + it('renders empty list when schemas is empty', () => { |
| 104 | + render(<CredentialTypeSelector schemas={[]} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 105 | + |
| 106 | + expect(screen.getByPlaceholderText('Search credential')).toBeInTheDocument() |
| 107 | + expect(screen.queryAllByRole('img')).toHaveLength(0) |
| 108 | + }) |
| 109 | + |
| 110 | + it('shows fallback key icon when credential icon fails to load', () => { |
| 111 | + render(<CredentialTypeSelector schemas={[schemas[0]]} apiBaseUrl={apiBaseUrl} onSelect={jest.fn()} />) |
| 112 | + |
| 113 | + const img = screen.getByAltText('httpBasicAuth') |
| 114 | + fireEvent.error(img) |
| 115 | + |
| 116 | + expect(screen.queryByAltText('httpBasicAuth')).not.toBeInTheDocument() |
| 117 | + expect(screen.getByTestId('icon-key-fallback')).toBeInTheDocument() |
| 118 | + }) |
| 119 | +}) |
0 commit comments