mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
feat: add toHierarchy
utility for TreeView
, RecursiveList
(#2072)
Co-authored-by: Bram <bramhavers@gmail.com>
This commit is contained in:
parent
f1a27ec855
commit
48afd18e5e
19 changed files with 413 additions and 23 deletions
24
tests/RecursiveList/RecursiveList.hierarchy.test.svelte
Normal file
24
tests/RecursiveList/RecursiveList.hierarchy.test.svelte
Normal file
|
@ -0,0 +1,24 @@
|
|||
<script lang="ts">
|
||||
import { RecursiveList } from "carbon-components-svelte";
|
||||
import toHierarchy from "../../src/utils/toHierarchy";
|
||||
|
||||
let nodes = toHierarchy(
|
||||
[
|
||||
{ id: 1, text: "Item 1" },
|
||||
{ id: 2, text: "Item 1a", pid: 1 },
|
||||
{ id: 3, html: "<h5>HTML content</h5>", pid: 2 },
|
||||
{ id: 4, text: "Item 2" },
|
||||
{ id: 5, href: "https://svelte.dev/", pid: 4 },
|
||||
{
|
||||
id: 6,
|
||||
href: "https://svelte.dev/",
|
||||
text: "Link with custom text",
|
||||
pid: 4,
|
||||
},
|
||||
{ id: 7, text: "Item 3" },
|
||||
],
|
||||
(node) => node.pid,
|
||||
);
|
||||
</script>
|
||||
|
||||
<RecursiveList type="ordered" {nodes} />
|
28
tests/RecursiveList/RecursiveList.test.svelte
Normal file
28
tests/RecursiveList/RecursiveList.test.svelte
Normal file
|
@ -0,0 +1,28 @@
|
|||
<script lang="ts">
|
||||
import { RecursiveList } from "carbon-components-svelte";
|
||||
|
||||
const nodes = [
|
||||
{
|
||||
text: "Item 1",
|
||||
nodes: [
|
||||
{
|
||||
text: "Item 1a",
|
||||
nodes: [{ html: "<h5>HTML content</h5>" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text: "Item 2",
|
||||
nodes: [
|
||||
{ href: "https://svelte.dev/" },
|
||||
{
|
||||
href: "https://svelte.dev/",
|
||||
text: "Link with custom text",
|
||||
},
|
||||
],
|
||||
},
|
||||
{ text: "Item 3" },
|
||||
];
|
||||
</script>
|
||||
|
||||
<RecursiveList type="ordered" {nodes} />
|
47
tests/RecursiveList/RecursiveList.test.ts
Normal file
47
tests/RecursiveList/RecursiveList.test.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { render, screen } from "@testing-library/svelte";
|
||||
import RecursiveListHierarchyTest from "./RecursiveList.hierarchy.test.svelte";
|
||||
import RecursiveListTest from "./RecursiveList.test.svelte";
|
||||
|
||||
const testCases = [
|
||||
{ name: "RecursiveList", component: RecursiveListTest },
|
||||
{ name: "RecursiveList hierarchy", component: RecursiveListHierarchyTest },
|
||||
];
|
||||
|
||||
describe.each(testCases)("$name", ({ component }) => {
|
||||
it("renders all top-level items", () => {
|
||||
render(component);
|
||||
|
||||
expect(screen.getByText("Item 1")).toBeInTheDocument();
|
||||
expect(screen.getByText("Item 2")).toBeInTheDocument();
|
||||
expect(screen.getByText("Item 3")).toBeInTheDocument();
|
||||
|
||||
expect(screen.getAllByRole("list")).toHaveLength(4);
|
||||
|
||||
// Nested items
|
||||
expect(screen.getByText("Item 1a")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders HTML content", () => {
|
||||
render(component);
|
||||
|
||||
const htmlContent = screen.getByText("HTML content");
|
||||
expect(htmlContent.tagName).toBe("H5");
|
||||
});
|
||||
|
||||
it("renders links correctly", () => {
|
||||
render(component);
|
||||
|
||||
const links = screen.getAllByRole("link");
|
||||
expect(links).toHaveLength(2);
|
||||
|
||||
// Link with custom text
|
||||
const customLink = screen.getByText("Link with custom text");
|
||||
expect(customLink).toHaveAttribute("href", "https://svelte.dev/");
|
||||
|
||||
// Plain link
|
||||
const plainLink = links.find(
|
||||
(link) => link.textContent === "https://svelte.dev/",
|
||||
);
|
||||
expect(plainLink).toHaveAttribute("href", "https://svelte.dev/");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue