|
| 1 | +import { fireEvent, render } from '@testing-library/react'; |
| 2 | +import type { TextareaHTMLAttributes } from 'react'; |
| 3 | +import Mentions from '../src'; |
| 4 | + |
| 5 | +describe('should support allowClear', () => { |
| 6 | + it('should change type when click', () => { |
| 7 | + const { container } = render(<Mentions allowClear />); |
| 8 | + fireEvent.change(container.querySelector('textarea')!, { |
| 9 | + target: { value: '111' }, |
| 10 | + }); |
| 11 | + expect(container.querySelector('textarea')?.value).toEqual('111'); |
| 12 | + expect( |
| 13 | + container.querySelector('.rc-mentions-clear-icon-hidden'), |
| 14 | + ).toBeFalsy(); |
| 15 | + fireEvent.click(container.querySelector('.rc-mentions-clear-icon')!); |
| 16 | + expect( |
| 17 | + container.querySelector('.rc-mentions-clear-icon-hidden'), |
| 18 | + ).toBeTruthy(); |
| 19 | + expect(container.querySelector('textarea')?.value).toEqual(''); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should not show icon if value is undefined, null or empty string', () => { |
| 23 | + const wrappers = [null, undefined, ''].map(val => |
| 24 | + render( |
| 25 | + <Mentions |
| 26 | + allowClear |
| 27 | + value={val as TextareaHTMLAttributes<HTMLTextAreaElement>['value']} |
| 28 | + />, |
| 29 | + ), |
| 30 | + ); |
| 31 | + wrappers.forEach(({ asFragment, container }) => { |
| 32 | + expect(container.querySelector('textarea')?.value).toEqual(''); |
| 33 | + expect( |
| 34 | + container.querySelector('.rc-mentions-clear-icon-hidden'), |
| 35 | + ).toBeTruthy(); |
| 36 | + expect(asFragment().firstChild).toMatchSnapshot(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not show icon if defaultValue is undefined, null or empty string', () => { |
| 41 | + const wrappers = [null, undefined, ''].map(val => |
| 42 | + render( |
| 43 | + <Mentions |
| 44 | + allowClear |
| 45 | + defaultValue={ |
| 46 | + val as TextareaHTMLAttributes<HTMLTextAreaElement>['value'] |
| 47 | + } |
| 48 | + />, |
| 49 | + ), |
| 50 | + ); |
| 51 | + wrappers.forEach(({ asFragment, container }) => { |
| 52 | + expect(container.querySelector('textarea')?.value).toEqual(''); |
| 53 | + expect( |
| 54 | + container.querySelector('.rc-mentions-clear-icon-hidden'), |
| 55 | + ).toBeTruthy(); |
| 56 | + expect(asFragment().firstChild).toMatchSnapshot(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should trigger event correctly', () => { |
| 61 | + const onChange = jest.fn(); |
| 62 | + const { container } = render( |
| 63 | + <Mentions allowClear defaultValue="111" onChange={onChange} />, |
| 64 | + ); |
| 65 | + fireEvent.click(container.querySelector('.rc-mentions-clear-icon')!); |
| 66 | + expect(onChange).toHaveBeenCalledWith(''); |
| 67 | + expect(container.querySelector('textarea')?.value).toBe(''); |
| 68 | + }); |
| 69 | +}); |
0 commit comments