2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-11-11 18:15:02 +08:00
|
|
|
import type { KeyWiseTransferItem } from '..';
|
2022-09-08 21:55:23 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2022-09-08 23:47:22 +08:00
|
|
|
import type { TransferListProps } from '../list';
|
2022-11-11 18:15:02 +08:00
|
|
|
import type TransferList from '../list';
|
2022-06-22 14:57:09 +08:00
|
|
|
import List from '../list';
|
2016-12-16 17:59:52 +08:00
|
|
|
|
2022-09-08 23:47:22 +08:00
|
|
|
const listCommonProps: TransferListProps<any> = {
|
2016-12-16 17:59:52 +08:00
|
|
|
prefixCls: 'ant-transfer-list',
|
2018-12-07 16:17:45 +08:00
|
|
|
dataSource: [
|
2022-09-08 23:47:22 +08:00
|
|
|
{ key: 'a', title: 'a' },
|
|
|
|
{ key: 'b', title: 'b' },
|
|
|
|
{ key: 'c', title: 'c', disabled: true },
|
2018-12-07 16:17:45 +08:00
|
|
|
],
|
2016-12-16 17:59:52 +08:00
|
|
|
checkedKeys: ['a'],
|
2017-03-17 15:23:25 +08:00
|
|
|
notFoundContent: 'Not Found',
|
2022-09-08 23:47:22 +08:00
|
|
|
} as TransferListProps<any>;
|
2016-12-16 17:59:52 +08:00
|
|
|
|
2022-10-22 15:00:26 +08:00
|
|
|
const listProps: TransferListProps<any> = {
|
|
|
|
...listCommonProps,
|
|
|
|
dataSource: undefined as unknown as any[],
|
|
|
|
};
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
describe('Transfer.List', () => {
|
2016-12-16 17:59:52 +08:00
|
|
|
it('should render correctly', () => {
|
2022-09-08 21:55:23 +08:00
|
|
|
const { container } = render(<List {...listCommonProps} />);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should check top Checkbox while all available items are checked', () => {
|
2022-09-08 21:55:23 +08:00
|
|
|
const { container } = render(<List {...listCommonProps} checkedKeys={['a', 'b']} />);
|
|
|
|
expect(
|
2022-09-08 23:47:22 +08:00
|
|
|
container.querySelector<HTMLInputElement>('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
?.checked,
|
2022-09-08 21:55:23 +08:00
|
|
|
).toBeTruthy();
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|
2019-03-04 11:19:16 +08:00
|
|
|
|
|
|
|
it('when component has been unmounted, componentWillUnmount should be called', () => {
|
2022-11-11 18:15:02 +08:00
|
|
|
const instance = React.createRef<TransferList<KeyWiseTransferItem>>();
|
2022-09-08 23:47:22 +08:00
|
|
|
const { unmount } = render(<List ref={instance} {...listCommonProps} />);
|
2022-11-11 18:15:02 +08:00
|
|
|
const willUnmount = jest.spyOn(instance.current!, 'componentWillUnmount');
|
2022-09-08 23:47:22 +08:00
|
|
|
unmount();
|
2019-03-04 11:19:16 +08:00
|
|
|
expect(willUnmount).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when value is not exists, handleFilter should return', () => {
|
|
|
|
const handleFilter = jest.fn();
|
2022-11-11 18:15:02 +08:00
|
|
|
const instance = React.createRef<TransferList<KeyWiseTransferItem>>();
|
2022-09-08 23:47:22 +08:00
|
|
|
render(<List ref={instance} {...listCommonProps} handleFilter={handleFilter} />);
|
2022-11-11 18:15:02 +08:00
|
|
|
expect(
|
|
|
|
instance.current?.handleFilter({
|
|
|
|
target: 'test',
|
|
|
|
} as unknown as React.ChangeEvent<HTMLInputElement>),
|
|
|
|
).toBe(undefined);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleFilter).toHaveBeenCalled();
|
2019-03-04 11:19:16 +08:00
|
|
|
});
|
2022-10-22 15:00:26 +08:00
|
|
|
it('should render correctly when dataSource is not exists', () => {
|
|
|
|
expect(() => {
|
|
|
|
render(<List {...listProps} />);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|