Skip to content

Commit

Permalink
fix: Transfer filterOption not trigger by spaces (ant-design#26335)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Aug 22, 2020
1 parent 06b7a7a commit 152cf6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
62 changes: 32 additions & 30 deletions components/transfer/__tests__/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import Transfer from '../index';
describe('Transfer.Search', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const dataSource = [
{
key: 'a',
title: 'a',
description: 'a',
},
{
key: 'b',
title: 'b',
description: 'b',
},
{
key: 'c',
title: 'c',
description: 'c',
},
];

afterEach(() => {
errorSpy.mockReset();
});
Expand All @@ -16,33 +34,13 @@ describe('Transfer.Search', () => {

it('should show cross icon when input value exists', () => {
const wrapper = mount(<Search value="" />);

expect(wrapper).toMatchSnapshot();

wrapper.setProps({ value: 'a' });

expect(wrapper).toMatchSnapshot();
});

it('onSearch', () => {
jest.useFakeTimers();
const dataSource = [
{
key: 'a',
title: 'a',
description: 'a',
},
{
key: 'b',
title: 'b',
description: 'b',
},
{
key: 'c',
title: 'c',
description: 'c',
},
];

const onSearch = jest.fn();
const wrapper = mount(
Expand All @@ -55,36 +53,40 @@ describe('Transfer.Search', () => {
showSearch
/>,
);

wrapper
.find('.ant-input')
.at(0)
.simulate('change', { target: { value: 'a' } });
expect(onSearch).toHaveBeenCalledWith('left', 'a');

onSearch.mockReset();

wrapper
.find('.ant-transfer-list-search-action')
.at(0)
.simulate('click');
wrapper.find('.ant-transfer-list-search-action').at(0).simulate('click');
expect(onSearch).toHaveBeenCalledWith('left', '');
jest.useRealTimers();
});

it('legacy props#onSearchChange doesnot work anymore', () => {
const onSearchChange = jest.fn();

const wrapper = mount(
<Transfer render={item => item.title} onSearchChange={onSearchChange} showSearch />,
);

wrapper
.find('.ant-input')
.at(0)
.simulate('change', { target: { value: 'a' } });

expect(errorSpy.mock.calls.length).toBe(0);
expect(onSearchChange).not.toHaveBeenCalled();
});

// https://github.com/ant-design/ant-design/issues/26208
it('typing space should trigger filterOption', () => {
const filterOption = jest.fn();
const wrapper = mount(
<Transfer filterOption={filterOption} dataSource={dataSource} showSearch />,
);
wrapper
.find('.ant-input')
.at(0)
.simulate('change', { target: { value: ' ' } });
expect(filterOption).toHaveBeenCalledTimes(dataSource.length);
});
});
2 changes: 1 addition & 1 deletion components/transfer/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class TransferList extends React.PureComponent<
const { renderedText } = renderedItem;

// Filter skip
if (filterValue && filterValue.trim() && !this.matchFilter(renderedText, item)) {
if (filterValue && !this.matchFilter(renderedText, item)) {
return null;
}

Expand Down

0 comments on commit 152cf6f

Please sign in to comment.