ant-design/components/transfer/__tests__/dropdown.test.tsx

144 lines
3.8 KiB
TypeScript
Raw Normal View History

/* eslint no-use-before-define: "off" */
import React from 'react';
import { act } from 'react-dom/test-utils';
import Transfer from '..';
2022-06-22 14:57:09 +08:00
import { fireEvent, render } from '../../../tests/utils';
const listProps = {
dataSource: [
{
key: 'a',
title: 'a',
disabled: true,
},
{
key: 'b',
title: 'b',
},
{
key: 'c',
title: 'c',
},
{
key: 'd',
title: 'd',
},
{
key: 'e',
title: 'e',
},
],
selectedKeys: ['b'],
targetKeys: [],
pagination: { pageSize: 4 },
};
describe('Transfer.Dropdown', () => {
function clickItem(container: HTMLElement, index: number) {
const items = Array.from(
container
// Menu
.querySelector('.ant-dropdown-menu')!
// Items
.querySelectorAll('li.ant-dropdown-menu-item'),
);
fireEvent.click(items[index]);
}
it('select all', () => {
jest.useFakeTimers();
const onSelectChange = jest.fn();
const { container } = render(<Transfer {...listProps} onSelectChange={onSelectChange} />);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => {
jest.runAllTimers();
});
clickItem(container, 0);
expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd', 'e'], []);
jest.useRealTimers();
});
it('select current page', () => {
jest.useFakeTimers();
const onSelectChange = jest.fn();
const { container } = render(<Transfer {...listProps} onSelectChange={onSelectChange} />);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
act(() => {
jest.runAllTimers();
});
clickItem(container, 1);
expect(onSelectChange).toHaveBeenCalledWith(['b', 'c', 'd'], []);
jest.useRealTimers();
});
it('should hide checkbox and dropdown icon when showSelectAll={false}', () => {
const { container } = render(<Transfer {...listProps} showSelectAll={false} />);
expect(container.querySelector('.ant-transfer-list-header-dropdown')).toBeFalsy();
expect(
container.querySelector('.ant-transfer-list-header .ant-transfer-list-checkbox'),
).toBeFalsy();
});
2020-05-23 16:47:49 +08:00
describe('select invert', () => {
[
{ name: 'with pagination', props: listProps, index: 2, keys: ['c', 'd'] },
{
name: 'without pagination',
props: { ...listProps, pagination: null as any },
2020-05-23 16:47:49 +08:00
index: 1,
keys: ['c', 'd', 'e'],
},
].forEach(({ name, props, index, keys }) => {
it(name, () => {
jest.useFakeTimers();
2020-05-23 16:47:49 +08:00
const onSelectChange = jest.fn();
const { container } = render(<Transfer {...props} onSelectChange={onSelectChange} />);
fireEvent.mouseEnter(container.querySelector('.ant-transfer-list-header-dropdown')!);
2020-05-23 16:47:49 +08:00
act(() => {
jest.runAllTimers();
2020-05-23 16:47:49 +08:00
});
clickItem(container, index);
2020-05-23 16:47:49 +08:00
expect(onSelectChange).toHaveBeenCalledWith(keys, []);
jest.useRealTimers();
2020-05-23 16:47:49 +08:00
});
});
});
2020-05-23 16:47:49 +08:00
describe('oneWay to remove', () => {
[
{ name: 'with pagination', props: listProps },
{ name: 'without pagination', props: { ...listProps, pagination: null as any } },
2020-05-23 16:47:49 +08:00
].forEach(({ name, props }) => {
it(name, () => {
jest.useFakeTimers();
2020-05-23 16:47:49 +08:00
const onChange = jest.fn();
const { container } = render(
2020-05-23 16:47:49 +08:00
<Transfer {...props} targetKeys={['b', 'c']} oneWay onChange={onChange} />,
);
// Right dropdown
fireEvent.mouseEnter(container.querySelectorAll('.ant-transfer-list-header-dropdown')[1]!);
2020-05-23 16:47:49 +08:00
act(() => {
jest.runAllTimers();
2020-05-23 16:47:49 +08:00
});
clickItem(container, 0);
2020-05-23 16:47:49 +08:00
expect(onChange).toHaveBeenCalledWith([], 'left', ['b', 'c']);
jest.useRealTimers();
2020-05-23 16:47:49 +08:00
});
});
});
});