import React from 'react'; import { render, screen } from '../../../tests/utils'; import Tree from '../index'; import type { AntTreeNodeProps } from '../Tree'; const { TreeNode } = Tree; describe('Tree', () => { it('icon and switcherIcon of Tree with showLine should render correctly', () => { const { asFragment } = render( , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('switcherIcon in Tree should not render at leaf nodes', () => { const { container } = render( } defaultExpandAll> , ); expect(container.querySelectorAll('.switcherIcon').length).toBe(1); }); it('leaf nodes should render custom icons when provided', () => { const { container } = render( }} defaultExpandAll> , ); expect(container.querySelectorAll('.customLeafIcon').length).toBe(2); }); it('leaf nodes should render custom icons when provided as render function', () => { const { container } = render( }} defaultExpandAll> , ); expect(container.querySelectorAll('.customLeafIcon').length).toBe(2); }); it('leaf nodes should render custom icons when provided as string', async () => { render( , ); const customIcons = await screen.findAllByText('customLeafIcon'); expect(customIcons).toHaveLength(2); }); it('switcherIcon in Tree could be string', () => { const { asFragment } = render( , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('switcherIcon should be loading icon when loadData', () => { const onLoadData = () => new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); }); const { asFragment } = render( , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('support switcherLoadingIcon prop when loadData', () => { const onLoadData = () => new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); }); const { asFragment } = render( loading...} > , ); expect(asFragment().firstChild).toMatchSnapshot(); }); it('switcherIcon in Tree could be render prop function', () => { const { container } = render( expanded ? : } > , ); expect(container.querySelectorAll('.open').length).toBe(1); }); // https://github.com/ant-design/ant-design/issues/23261 it('showLine is object type should render correctly', () => { const { asFragment } = render( , ); expect(asFragment().firstChild).toMatchSnapshot(); }); describe('draggable', () => { const dragTreeData = [ { title: 'bamboo', key: 'bamboo', }, ]; it('hide icon', () => { const { container } = render(); expect(container.querySelector('.anticon-holder')).toBeFalsy(); }); it('customize icon', () => { const { container } = render( }} />, ); expect(container.querySelector('.little')).toBeTruthy(); }); it('nodeDraggable', () => { const nodeDraggable = jest.fn(() => false); render(); expect(nodeDraggable).toHaveBeenCalledWith(dragTreeData[0]); }); it('nodeDraggable func', () => { const nodeDraggable = jest.fn(() => false); render(); expect(nodeDraggable).toHaveBeenCalledWith(dragTreeData[0]); }); }); describe('hidden switcherIcon', () => { it('use `switcherIcon={() => null}`', () => { const { container } = render( null}> , ); container.querySelectorAll('.ant-tree-switcher').forEach((el) => { expect(el.children.length).toBe(0); }); }); it('use `switcherIcon={null}`', () => { const { container } = render( , ); container.querySelectorAll('.ant-tree-switcher').forEach((el) => { expect(el.children.length).toBe(0); }); }); }); });