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

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-06-22 14:57:09 +08:00
import React from 'react';
2022-11-11 18:15:02 +08:00
import type { KeyWiseTransferItem } from '..';
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';
2022-09-08 23:47:22 +08:00
const listCommonProps: TransferListProps<any> = {
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
],
checkedKeys: ['a'],
notFoundContent: 'Not Found',
2022-09-08 23:47:22 +08:00
} as TransferListProps<any>;
const listProps: TransferListProps<any> = {
...listCommonProps,
dataSource: undefined as unknown as any[],
};
describe('Transfer.List', () => {
it('should render correctly', () => {
const { container } = render(<List {...listCommonProps} />);
expect(container.firstChild).toMatchSnapshot();
});
it('should check top Checkbox while all available items are checked', () => {
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,
).toBeTruthy();
});
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);
expect(handleFilter).toHaveBeenCalled();
2019-03-04 11:19:16 +08:00
});
it('should render correctly when dataSource is not exists', () => {
expect(() => {
render(<List {...listProps} />);
}).not.toThrow();
});
});