2022-04-18 21:02:11 +08:00
|
|
|
import { render as testLibRender } from '@testing-library/react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2018-10-12 23:47:42 +08:00
|
|
|
import Transfer from '../index';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Search from '../search';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
describe('Transfer.Search', () => {
|
2018-10-12 23:47:42 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
2020-08-22 17:54:57 +08:00
|
|
|
const dataSource = [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
description: 'a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
description: 'b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'c',
|
|
|
|
title: 'c',
|
|
|
|
description: 'c',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-10-12 23:47:42 +08:00
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('should show cross icon when input value exists', () => {
|
2022-09-08 21:55:23 +08:00
|
|
|
const { container, rerender } = render(<Search value="" />);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
rerender(<Search value="a" />);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2018-10-12 23:47:42 +08:00
|
|
|
|
|
|
|
it('onSearch', () => {
|
2019-09-26 11:37:06 +08:00
|
|
|
jest.useFakeTimers();
|
2018-10-12 23:47:42 +08:00
|
|
|
|
|
|
|
const onSearch = jest.fn();
|
2022-09-08 21:55:23 +08:00
|
|
|
const { container } = render(
|
2018-10-12 23:47:42 +08:00
|
|
|
<Transfer
|
|
|
|
dataSource={dataSource}
|
|
|
|
selectedKeys={[]}
|
|
|
|
targetKeys={[]}
|
|
|
|
render={item => item.title}
|
|
|
|
onSearch={onSearch}
|
|
|
|
showSearch
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-10-12 23:47:42 +08:00
|
|
|
);
|
2022-09-08 21:55:23 +08:00
|
|
|
fireEvent.change(container.querySelectorAll('.ant-input').item(0), { target: { value: 'a' } });
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith('left', 'a');
|
2018-10-12 23:47:42 +08:00
|
|
|
onSearch.mockReset();
|
2022-09-08 21:55:23 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-input-clear-icon').item(0));
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith('left', '');
|
2019-09-26 11:37:06 +08:00
|
|
|
jest.useRealTimers();
|
2018-10-12 23:47:42 +08:00
|
|
|
});
|
|
|
|
|
2019-07-09 11:46:21 +08:00
|
|
|
it('legacy props#onSearchChange doesnot work anymore', () => {
|
2018-10-12 23:47:42 +08:00
|
|
|
const onSearchChange = jest.fn();
|
2022-09-08 23:47:22 +08:00
|
|
|
const props = { onSearchChange };
|
|
|
|
const { container } = render(<Transfer render={item => item.title!} {...props} showSearch />);
|
|
|
|
fireEvent.change(container.querySelector('.ant-input')!, { target: { value: 'a' } });
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2019-07-09 11:46:21 +08:00
|
|
|
expect(onSearchChange).not.toHaveBeenCalled();
|
2018-10-12 23:47:42 +08:00
|
|
|
});
|
2020-08-22 17:54:57 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/26208
|
|
|
|
it('typing space should trigger filterOption', () => {
|
|
|
|
const filterOption = jest.fn();
|
2022-04-18 21:02:11 +08:00
|
|
|
|
|
|
|
// We use origin testing lib here since StrictMode will render multiple times
|
|
|
|
const { container } = testLibRender(
|
2020-08-22 17:54:57 +08:00
|
|
|
<Transfer filterOption={filterOption} dataSource={dataSource} showSearch />,
|
|
|
|
);
|
2022-04-18 21:02:11 +08:00
|
|
|
|
2022-09-08 23:47:22 +08:00
|
|
|
fireEvent.change(container.querySelector('.ant-input')!, { target: { value: ' ' } });
|
2022-04-18 21:02:11 +08:00
|
|
|
|
2020-08-22 17:54:57 +08:00
|
|
|
expect(filterOption).toHaveBeenCalledTimes(dataSource.length);
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|