Skip to content

Commit

Permalink
test: add unit tests for BaseSelct
Browse files Browse the repository at this point in the history
  • Loading branch information
su-muzhi committed Nov 11, 2024
1 parent 3f8c624 commit 9a44b6e
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion tests/BaseSelect.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused import useState.
import BaseSelect from '../src/BaseSelect';

const OptionList = forwardRef<RefOptionListProps, OptionListProps>(() => (
Expand Down Expand Up @@ -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(
<BaseSelect
prefixCls="rc-select"
mode="multiple"
id="test"
displayValues={[]}
onDisplayValuesChange={() => {}}
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(
<BaseSelect
prefixCls="rc-select"
mode="multiple"
id="test"
displayValues={[]}
onDisplayValuesChange={() => {}}
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' });
});
});
});

0 comments on commit 9a44b6e

Please sign in to comment.