|
1 | | -import React from 'react'; |
2 | | -import { |
3 | | - render, screen, fireEvent, |
4 | | -} from '@testing-library/react'; |
| 1 | +import { useState } from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
5 | 4 | import SearchFilter from './SearchFilter'; |
6 | 5 |
|
7 | 6 | describe('SearchFilter', () => { |
8 | | - const mockSetFilter = jest.fn(); |
9 | | - |
10 | | - const defaultProps = { |
11 | | - filterValue: '', |
12 | | - setFilter: mockSetFilter, |
13 | | - placeholder: 'Search placeholder', |
14 | | - }; |
15 | | - |
16 | 7 | beforeEach(() => { |
17 | 8 | jest.clearAllMocks(); |
18 | 9 | }); |
19 | 10 |
|
| 11 | + const SearchFilterWrapper = ({ |
| 12 | + initFilterValue = '', customPlaceholder = 'Search placeholder', |
| 13 | + }:{ initFilterValue?: string; customPlaceholder?:string }) => { |
| 14 | + const [filter, setFilter] = useState(initFilterValue); |
| 15 | + return ( |
| 16 | + <SearchFilter |
| 17 | + filterValue={filter} |
| 18 | + setFilter={setFilter} |
| 19 | + placeholder={customPlaceholder} |
| 20 | + /> |
| 21 | + ); |
| 22 | + }; |
| 23 | + |
20 | 24 | it('should render search input with correct placeholder', () => { |
21 | | - render(<SearchFilter {...defaultProps} />); |
| 25 | + render(<SearchFilterWrapper />); |
22 | 26 |
|
23 | 27 | const input = screen.getByPlaceholderText('Search placeholder'); |
24 | 28 | expect(input).toBeInTheDocument(); |
25 | 29 | expect(input).toHaveAttribute('type', 'text'); |
26 | 30 | }); |
27 | 31 |
|
28 | 32 | it('should display empty value when filterValue is undefined', () => { |
29 | | - const propsWithUndefined = { ...defaultProps, filterValue: undefined as any }; |
30 | | - render(<SearchFilter {...propsWithUndefined} />); |
| 33 | + render(<SearchFilterWrapper initFilterValue={undefined} />); |
31 | 34 |
|
32 | 35 | const input = screen.getByPlaceholderText('Search placeholder'); |
33 | 36 | expect(input).toHaveValue(''); |
34 | 37 | }); |
35 | 38 |
|
36 | 39 | it('should display filterValue if provided', () => { |
37 | | - const propsWithString = { ...defaultProps, filterValue: 'test search' }; |
38 | | - render(<SearchFilter {...propsWithString} />); |
| 40 | + render(<SearchFilterWrapper initFilterValue="test search" />); |
39 | 41 |
|
40 | 42 | const input = screen.getByPlaceholderText('Search placeholder'); |
41 | 43 | expect(input).toHaveValue('test search'); |
42 | 44 | }); |
43 | 45 |
|
44 | | - it('should call setFilter with input value when typing', () => { |
45 | | - render(<SearchFilter {...defaultProps} />); |
| 46 | + it('should call setFilter with input value when typing', async () => { |
| 47 | + const user = userEvent.setup(); |
| 48 | + render(<SearchFilterWrapper />); |
46 | 49 |
|
47 | 50 | const input = screen.getByPlaceholderText('Search placeholder'); |
48 | | - fireEvent.change(input, { target: { value: 'new search term' } }); |
| 51 | + await user.click(input); |
| 52 | + await user.type(input, 'new search term'); |
49 | 53 |
|
50 | | - expect(mockSetFilter).toHaveBeenCalledWith('new search term'); |
| 54 | + expect(input).toHaveValue('new search term'); |
51 | 55 | }); |
52 | 56 |
|
53 | | - it('should call setFilter with undefined when input is cleared', () => { |
54 | | - const propsWithValue = { ...defaultProps, filterValue: 'existing value' as any }; |
55 | | - render(<SearchFilter {...propsWithValue} />); |
| 57 | + it('should clear the input correctly', async () => { |
| 58 | + const user = userEvent.setup(); |
| 59 | + render(<SearchFilterWrapper initFilterValue="existing value" />); |
56 | 60 |
|
57 | 61 | const input = screen.getByPlaceholderText('Search placeholder'); |
58 | | - fireEvent.change(input, { target: { value: '' } }); |
59 | | - |
60 | | - expect(mockSetFilter).toHaveBeenCalledWith(undefined); |
| 62 | + await user.click(input); |
| 63 | + await user.clear(input); |
| 64 | + expect(input).toHaveValue(''); |
61 | 65 | }); |
62 | 66 |
|
63 | | - it('should handle multiple character input correctly', () => { |
64 | | - render(<SearchFilter {...defaultProps} />); |
| 67 | + it('should handle multiple character input correctly', async () => { |
| 68 | + const user = userEvent.setup(); |
| 69 | + |
| 70 | + render(<SearchFilterWrapper />); |
65 | 71 |
|
66 | 72 | const input = screen.getByPlaceholderText('Search placeholder'); |
| 73 | + await user.click(input); |
67 | 74 |
|
68 | 75 | // Type multiple characters |
69 | | - fireEvent.change(input, { target: { value: 'a' } }); |
70 | | - expect(mockSetFilter).toHaveBeenCalledWith('a'); |
| 76 | + await user.type(input, 'a'); |
| 77 | + expect(input).toHaveValue('a'); |
71 | 78 |
|
72 | | - fireEvent.change(input, { target: { value: 'ab' } }); |
73 | | - expect(mockSetFilter).toHaveBeenCalledWith('ab'); |
| 79 | + await user.type(input, 'b'); |
| 80 | + expect(input).toHaveValue('ab'); |
74 | 81 |
|
75 | | - fireEvent.change(input, { target: { value: 'abc' } }); |
76 | | - expect(mockSetFilter).toHaveBeenCalledWith('abc'); |
| 82 | + await user.type(input, 'c'); |
| 83 | + expect(input).toHaveValue('abc'); |
77 | 84 | }); |
78 | 85 |
|
79 | 86 | it('should handle different placeholder text', () => { |
80 | 87 | const customPlaceholder = 'Enter search term here...'; |
81 | | - render(<SearchFilter {...defaultProps} placeholder={customPlaceholder} />); |
| 88 | + render(<SearchFilterWrapper customPlaceholder={customPlaceholder} />); |
82 | 89 |
|
83 | 90 | const input = screen.getByPlaceholderText(customPlaceholder); |
84 | 91 | expect(input).toBeInTheDocument(); |
|
0 commit comments