test(tree-view): coverage for expandAll and expandNodes (#2063)

This commit is contained in:
Eric Liu 2024-12-08 11:09:23 -08:00 committed by GitHub
commit f1a27ec855
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 97 additions and 3 deletions

View file

@ -17,6 +17,16 @@ describe("TreeView", () => {
});
};
const noExpandedItems = () => {
expect(screen.queryAllByRole("treeitem", { expanded: true })).toHaveLength(
0,
);
};
const getAllExpandedItems = () => {
return screen.getAllByRole("treeitem", { expanded: true });
};
it("can select a node", async () => {
const consoleLog = vi.spyOn(console, "log");
@ -37,4 +47,30 @@ describe("TreeView", () => {
text: "AI / Machine learning",
});
});
it("can expand all nodes", async () => {
render(TreeView);
noExpandedItems();
const expandAllButton = screen.getByText("Expand all");
await user.click(expandAllButton);
expect(getAllExpandedItems()).toHaveLength(5);
});
it("can expand some nodes", async () => {
render(TreeView);
noExpandedItems();
const expandSomeNodesButton = screen.getByText("Expand some nodes");
await user.click(expandSomeNodesButton);
expect(getAllExpandedItems()).toHaveLength(2);
expect(
screen.getByText("IBM Analytics Engine").parentNode?.parentNode,
).toHaveAttribute("aria-expanded", "true");
});
});