From 9a44b6ef4c9092fbb409d95896575f5940789609 Mon Sep 17 00:00:00 2001 From: suzhi Date: Mon, 11 Nov 2024 13:57:46 +0800 Subject: [PATCH] test: add unit tests for BaseSelct --- tests/BaseSelect.test.tsx | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/BaseSelect.test.tsx b/tests/BaseSelect.test.tsx index 6a1fb514..ce5c113b 100644 --- a/tests/BaseSelect.test.tsx +++ b/tests/BaseSelect.test.tsx @@ -1,6 +1,6 @@ import type { OptionListProps, RefOptionListProps } from '@/OptionList'; import { fireEvent, render } from '@testing-library/react'; -import { forwardRef, act } from 'react'; +import { forwardRef, act, useState } from 'react'; import BaseSelect from '../src/BaseSelect'; const OptionList = forwardRef(() => ( @@ -123,4 +123,54 @@ describe('BaseSelect', () => { expect(container.querySelector('.rc-select-dropdown-placement-fallback')).toBeTruthy(); }); + + describe("Testing BaseSelect component's onContainerBlur params", () => { + it('BaseSelect with showSearch, onContainerBlur params is effect', () => { + const onSearch = jest.fn(); + const { container } = render( + {}} + searchValue="1" + showSearch + open + onSearch={onSearch} + OptionList={OptionList} + emptyOptions + />, + ); + expect(container.querySelector('div.rc-select')).toBeTruthy(); + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); + fireEvent.blur(container.querySelector('div.rc-select')); + expect(onSearch).toHaveBeenCalledWith('', { source: 'effect' }); + }); + + it('BaseSelect without showSearch, onContainerBlur params is blur', () => { + const onSearch = jest.fn(); + const { container } = render( + {}} + searchValue="1" + showSearch={false} + open + onSearch={onSearch} + OptionList={OptionList} + emptyOptions + />, + ); + expect(container.querySelector('div.rc-select')).toBeTruthy(); + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); + fireEvent.blur(container.querySelector('div.rc-select')); + expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' }); + }); + }); });