|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | + |
| 3 | +import { Breadcrumb, BreadcrumbItem } from "./Breadcrumb"; |
| 4 | + |
| 5 | +describe("Tests for Breadcrumb component", () => { |
| 6 | + test("Should render correctly with no items", () => { |
| 7 | + render( |
| 8 | + <Breadcrumb> |
| 9 | + <div>Dummy Child</div> |
| 10 | + </Breadcrumb> |
| 11 | + ); |
| 12 | + |
| 13 | + const breadcrumbNav = screen.getByRole("navigation"); |
| 14 | + expect(breadcrumbNav).toBeInTheDocument(); |
| 15 | + |
| 16 | + const separators = screen.queryAllByText("/"); |
| 17 | + expect(separators).toHaveLength(0); |
| 18 | + }); |
| 19 | + |
| 20 | + test("Should render correctly with custom list props", () => { |
| 21 | + render( |
| 22 | + <Breadcrumb> |
| 23 | + <BreadcrumbItem href="/" listProps={{ className: "custom-list" }}> |
| 24 | + Home |
| 25 | + </BreadcrumbItem> |
| 26 | + <BreadcrumbItem href="/about" listProps={{ className: "custom-list" }}> |
| 27 | + About |
| 28 | + </BreadcrumbItem> |
| 29 | + <BreadcrumbItem href="/contact" listProps={{ className: "custom-list" }}> |
| 30 | + Contact |
| 31 | + </BreadcrumbItem> |
| 32 | + </Breadcrumb> |
| 33 | + ); |
| 34 | + |
| 35 | + const customListItems = document.querySelectorAll(".custom-list"); |
| 36 | + expect(customListItems.length).toBe(3); |
| 37 | + }); |
| 38 | + |
| 39 | + test("Should generate correct hrefs and labels", () => { |
| 40 | + render( |
| 41 | + <Breadcrumb> |
| 42 | + <BreadcrumbItem href="/category">Category</BreadcrumbItem> |
| 43 | + <BreadcrumbItem href="/category/item">Item</BreadcrumbItem> |
| 44 | + </Breadcrumb> |
| 45 | + ); |
| 46 | + |
| 47 | + const categoryLink = screen.getByText("Category"); |
| 48 | + const itemLink = screen.getByText("Item"); |
| 49 | + |
| 50 | + expect(categoryLink.getAttribute("href")).toBe("/category"); |
| 51 | + expect(itemLink.getAttribute("href")).toBe("/category/item"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("Should /category be a anchor tag", async () => { |
| 55 | + render( |
| 56 | + <Breadcrumb> |
| 57 | + <BreadcrumbItem href="/category">Category</BreadcrumbItem> |
| 58 | + <BreadcrumbItem href="/category/item">Item</BreadcrumbItem> |
| 59 | + </Breadcrumb> |
| 60 | + ); |
| 61 | + |
| 62 | + const categoryLink = screen.getByText("Category"); |
| 63 | + const categoryAnchor = categoryLink.closest("a"); |
| 64 | + const categoryItem = categoryAnchor?.parentElement; |
| 65 | + |
| 66 | + expect(categoryAnchor).toBeInTheDocument(); |
| 67 | + expect(categoryItem?.tagName).toBe("LI"); |
| 68 | + expect(categoryAnchor?.getAttribute("href")).toBe("/category"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("Should not render separators when there is only one item", () => { |
| 72 | + render( |
| 73 | + <Breadcrumb> |
| 74 | + <BreadcrumbItem href="/">Home</BreadcrumbItem> |
| 75 | + </Breadcrumb> |
| 76 | + ); |
| 77 | + |
| 78 | + const separators = screen.queryAllByText("/"); |
| 79 | + expect(separators).toHaveLength(0); |
| 80 | + }); |
| 81 | + |
| 82 | + test("Should render breadcrumbs with correct order when rendered in reverse order", () => { |
| 83 | + render( |
| 84 | + <Breadcrumb> |
| 85 | + <BreadcrumbItem href="/contact">Contact</BreadcrumbItem> |
| 86 | + <BreadcrumbItem href="/about">About</BreadcrumbItem> |
| 87 | + <BreadcrumbItem href="/">Home</BreadcrumbItem> |
| 88 | + </Breadcrumb> |
| 89 | + ); |
| 90 | + |
| 91 | + const breadcrumbList = screen.getByRole("list"); |
| 92 | + const breadcrumbItems = screen.getAllByRole("listitem"); |
| 93 | + |
| 94 | + expect(breadcrumbItems).toHaveLength(3); |
| 95 | + expect(breadcrumbList).toContainElement(breadcrumbItems[2]); |
| 96 | + expect(breadcrumbList).toContainElement(breadcrumbItems[1]); |
| 97 | + expect(breadcrumbList).toContainElement(breadcrumbItems[0]); |
| 98 | + expect(breadcrumbItems[2]).toHaveTextContent("Home"); |
| 99 | + expect(breadcrumbItems[1]).toHaveTextContent("About"); |
| 100 | + expect(breadcrumbItems[0]).toHaveTextContent("Contact"); |
| 101 | + |
| 102 | + const separators = screen.getAllByText("/"); |
| 103 | + expect(separators).toHaveLength(2); |
| 104 | + }); |
| 105 | +}); |
0 commit comments