|
1 | 1 | import * as React from 'react' |
2 | | -import { fireEvent, render } from '@testing-library/react' |
| 2 | +import { render, fireEvent, waitFor } from '@testing-library/react' |
3 | 3 | import '@testing-library/jest-dom' |
| 4 | +import { useState } from 'react' |
4 | 5 | import SearchBar from '@/packages/searchbar' |
5 | 6 |
|
6 | | -test('basic usage', () => { |
7 | | - const { container } = render(<SearchBar placeholder="请输入文字" />) |
8 | | - expect(container.querySelector('.nut-searchbar-input')).toHaveAttribute( |
9 | | - 'placeholder', |
10 | | - '请输入文字' |
| 7 | +test('should render with placeholder', () => { |
| 8 | + const { getByPlaceholderText } = render( |
| 9 | + <SearchBar placeholder="请输入文字" /> |
11 | 10 | ) |
| 11 | + expect(getByPlaceholderText('请输入文字')).toBeInTheDocument() |
12 | 12 | }) |
13 | 13 |
|
14 | 14 | test('should limit maxlength of input value when using maxlength prop', () => { |
15 | 15 | const { container } = render(<SearchBar shape="round" maxLength={5} />) |
16 | | - expect(container.querySelector('.nut-searchbar-input')).toHaveAttribute( |
17 | | - 'maxlength', |
18 | | - '5' |
19 | | - ) |
20 | | - expect(container.querySelector('.nut-searchbar-content')).toHaveClass( |
21 | | - 'nut-searchbar-round' |
22 | | - ) |
| 16 | + const input = container.querySelector('.nut-searchbar-input') |
| 17 | + expect(input).toHaveAttribute('maxlength', '5') |
| 18 | + expect(input?.parentNode).toHaveClass('nut-searchbar-round') |
23 | 19 | }) |
24 | 20 |
|
25 | | -test('Search box text settings', () => { |
| 21 | +test('should display left and right text', () => { |
26 | 22 | const { container } = render(<SearchBar left="文本" right="确定" />) |
27 | 23 | expect(container.querySelector('.nut-searchbar-left')?.innerHTML).toBe('文本') |
28 | 24 | expect(container.querySelector('.nut-searchbar-right')?.innerHTML).toBe( |
29 | 25 | '确定' |
30 | 26 | ) |
31 | 27 | }) |
32 | 28 |
|
33 | | -test('Search clear & change', () => { |
34 | | - const change = vi.fn() |
35 | | - const { container } = render( |
36 | | - <SearchBar value="123" onChange={change} maxLength={10} /> |
37 | | - ) |
38 | | - const input = container.querySelector('.nut-searchbar-input') |
39 | | - expect(input?.getAttribute('value')).toBe('123') |
40 | | - const clear = container.querySelector('.nut-searchbar-clear') |
41 | | - fireEvent.click(clear as Element) |
42 | | - expect(change).toBeCalledWith('') |
43 | | - expect(input?.getAttribute('value')).toBe('') |
| 29 | +test('should render with tags', () => { |
| 30 | + const { container } = render(<SearchBar tag value="add,add3" />) |
| 31 | + const dvalues = container.querySelectorAll('.nut-searchbar-value') |
| 32 | + expect(dvalues.length).toBe(2) |
| 33 | +}) |
| 34 | + |
| 35 | +test('should render right-in element', () => { |
| 36 | + const { container, rerender } = render(<SearchBar rightIn="搜索" />) |
| 37 | + const rightin = container.querySelectorAll('.nut-searchbar-rightin') |
| 38 | + expect(rightin.length).toBe(1) |
| 39 | + rerender(<SearchBar rightIn={<div className="test">搜索</div>} />) |
| 40 | + const test = container.querySelectorAll('.test') |
| 41 | + expect(test.length).toBe(1) |
| 42 | +}) |
| 43 | + |
| 44 | +test('should handle all events correctly', async () => { |
| 45 | + const handleChange = vi.fn() |
| 46 | + const handleFocus = vi.fn() |
| 47 | + const handleBlur = vi.fn() |
| 48 | + const handleClick = vi.fn() |
| 49 | + const handleClear = vi.fn() |
| 50 | + const Demo = () => { |
| 51 | + const [value, setValue] = useState('奶茶') |
| 52 | + const onChange = (newValue: string) => { |
| 53 | + setValue(newValue) // 更新状态 |
| 54 | + handleChange(newValue) // 调用传入的 onChange 处理函数 |
| 55 | + } |
| 56 | + return ( |
| 57 | + <SearchBar |
| 58 | + value={value} |
| 59 | + autoFocus |
| 60 | + onChange={onChange} |
| 61 | + onFocus={handleFocus} |
| 62 | + onBlur={handleBlur} |
| 63 | + onInputClick={handleClick} |
| 64 | + onClear={handleClear} |
| 65 | + /> |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + const { container } = render(<Demo />) |
| 70 | + const inputEl = container.querySelector('.nut-searchbar-input') as Element |
| 71 | + expect(inputEl).toHaveValue('奶茶') |
| 72 | + fireEvent.click(inputEl) |
| 73 | + expect(handleClick).toHaveBeenCalledTimes(1) |
| 74 | + fireEvent.change(inputEl, { target: { value: '冰激凌' } }) |
| 75 | + |
| 76 | + await waitFor(() => { |
| 77 | + expect(handleFocus).toHaveBeenCalledTimes(1) |
| 78 | + expect(handleChange).toHaveBeenCalledTimes(1) |
| 79 | + expect(inputEl).toHaveValue('冰激凌') |
| 80 | + fireEvent.blur(inputEl) |
| 81 | + expect(handleBlur).toHaveBeenCalled() |
| 82 | + }) |
| 83 | + |
| 84 | + const clear = container.querySelector('.nut-searchbar-clear') as Element |
| 85 | + fireEvent.click(clear) |
| 86 | + await waitFor(() => { |
| 87 | + expect(inputEl).toHaveValue('') |
| 88 | + }) |
44 | 89 | }) |
0 commit comments