Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage for TreeView component #18072

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 125 additions & 2 deletions packages/react/src/components/TreeView/TreeView-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import { fireEvent, render, screen, within } from '@testing-library/react';

import React from 'react';
import TreeView from './TreeView';
import TreeNode from './TreeNode';
import TreeView from './TreeView';
import userEvent from '@testing-library/user-event';
import { fireEvent, render, screen, within } from '@testing-library/react';

const prefix = 'cds';

Expand Down Expand Up @@ -296,4 +297,126 @@ describe('TreeView', () => {
expect(parentNode).toHaveFocus();
});
});
it('should respect multiselect prop (deselecting nodes)', async () => {
const user = userEvent.setup();
render(
<TreeView multiselect label="Tree">
<TreeNode data-testid="Node 1" label="Node 1" />
<TreeNode data-testid="Node 2" label="Node 2" />
</TreeView>
);
const lists = screen.getAllByRole('treeitem');
await user.keyboard('[ControlLeft>]');
await user.click(lists[0]);
await user.click(lists[1]);
expect(lists[0]).toHaveAttribute('aria-selected', 'true');
expect(lists[1]).toHaveAttribute('aria-selected', 'true');
await user.keyboard('[ControlLeft>]');
await user.click(lists[0]);
expect(lists[0]).toHaveAttribute('aria-selected', 'false');
expect(lists[1]).toHaveAttribute('aria-selected', 'true');
});
it('should render tree with custom icons', () => {
const CustomIcon = () => <svg data-testid="test-icon" />;
render(
<TreeView label="Tree View">
<TreeNode renderIcon={CustomIcon} data-testid="Node 1" label="Node 1" />
</TreeView>
);
expect(screen.getByTestId('test-icon')).toBeInTheDocument();
});
it('should focus on the first child node when right arrow is pressed on an expanded parent node', async () => {
const user = userEvent.setup();
render(
<TreeView label="Tree View">
<TreeNode
data-testid="parent-node"
label="Parent Node"
isExpanded={true}>
<TreeNode data-testid="child-node-1" label="Child Node 1" />
<TreeNode data-testid="child-node-2" label="Child Node 2" />
</TreeNode>
</TreeView>
);
const parentNode = screen.getByTestId('parent-node');
const childNode1 = screen.getByTestId('child-node-1');
parentNode.focus();
expect(parentNode).toHaveFocus();
await user.keyboard('[ArrowRight]');
expect(childNode1).toHaveFocus();
});
it('should expand a collapsed parent node when right arrow is pressed', async () => {
const user = userEvent.setup();
render(
<TreeView label="Tree View">
<TreeNode
data-testid="parent-node"
label="Parent Node"
isExpanded={false}>
<TreeNode data-testid="child-node" label="Child Node" />
</TreeNode>
</TreeView>
);
const parentNode = screen.getByTestId('parent-node');
expect(parentNode).not.toHaveAttribute('aria-expanded', 'true');
parentNode.focus();
expect(parentNode).toHaveFocus();
await user.keyboard('[ArrowRight]');
expect(parentNode).toHaveAttribute('aria-expanded', 'true');
const childNode = screen.getByTestId('child-node');
expect(childNode).toBeInTheDocument();
});
it('should navigate between nodes using ArrowUp and ArrowDown', async () => {
const user = userEvent.setup();
render(
<TreeView label="Tree View">
<TreeNode data-testid="Node 1" label="Node 1" />
<TreeNode data-testid="Node 2" label="Node 2" />
</TreeView>
);
const node1 = screen.getByTestId('Node 1');
const node2 = screen.getByTestId('Node 2');
node1.focus();
expect(node1).toHaveFocus();
await user.keyboard('[ArrowDown]');
expect(node2).toHaveFocus();
await user.keyboard('[ArrowUp]');
expect(node1).toHaveFocus();
});

it('should not render label when hideLabel is true', () => {
render(
<TreeView label="Tree View" hideLabel>
<TreeNode id="Node 1" label="Node 1" />
</TreeView>
);
expect(screen.queryByText('Tree View')).not.toBeInTheDocument();
});

it('should render custom icons in TreeNode', () => {
const CustomIcon = () => <svg data-testid="custom-icon" />;

render(
<TreeView label="Tree View">
<TreeNode id="Node 1" label="Node 1" renderIcon={CustomIcon} />
<TreeNode id="Node 2" label="Node 2" />
</TreeView>
);

const node1Icon = screen.getByTestId('custom-icon');
const node2 = screen.getByText('Node 2');

expect(node1Icon).toBeInTheDocument();
expect(node2.querySelector('svg')).toBeNull();
});
it('should render the label correctly', () => {
render(
<TreeView label="My Tree View">
<TreeNode id="Node 1" label="Node 1" />
<TreeNode id="Node 2" label="Node 2" />
</TreeView>
);

expect(screen.getByLabelText('My Tree View')).toBeInTheDocument();
});
});
Loading