|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { Geometry, Feature } from 'geojson'; |
| 4 | +import SearchField from './SearchField'; |
| 5 | + |
| 6 | +const features: Feature<Geometry, Record<string, any>>[] = [ |
| 7 | + { |
| 8 | + type: 'Feature', |
| 9 | + geometry: { type: 'Point', coordinates: [1, 2] }, |
| 10 | + properties: { name: 'A' } |
| 11 | + }, |
| 12 | + { |
| 13 | + type: 'Feature', |
| 14 | + geometry: { type: 'Point', coordinates: [3, 4] }, |
| 15 | + properties: { name: 'B' } |
| 16 | + } |
| 17 | +]; |
| 18 | + |
| 19 | +const mockSearchFunction = jest.fn(async (term: string) => { |
| 20 | + if (!term) return { type: "FeatureCollection" as const, features: [] }; |
| 21 | + return { |
| 22 | + type: "FeatureCollection" as const, |
| 23 | + features: features.filter(f => f.properties?.name?.toLowerCase().includes(term.toLowerCase())) |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +describe('<SearchField />', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('renders input and allows typing', () => { |
| 33 | + render(<SearchField searchFunction={mockSearchFunction} />); |
| 34 | + const input = screen.getByRole('combobox'); |
| 35 | + expect(input).toBeInTheDocument(); |
| 36 | + fireEvent.change(input, { target: { value: 'MJMJ' } }); |
| 37 | + expect(input).toHaveValue('MJMJ'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('disables autocomplete popup if autoCompleteDisabled is true', async () => { |
| 41 | + render(<SearchField searchFunction={mockSearchFunction} autoCompleteDisabled={true} />); |
| 42 | + const input = screen.getByRole('combobox'); |
| 43 | + fireEvent.change(input, { target: { value: 'A' } }); |
| 44 | + // Wait a bit to ensure popup would have rendered if enabled |
| 45 | + await new Promise(res => setTimeout(res, 300)); |
| 46 | + expect(screen.queryByText('A')).not.toBeInTheDocument(); |
| 47 | + }); |
| 48 | +}); |
0 commit comments