| 1 |
import React from "react"; |
| 2 |
import { cleanup, render, screen } from "@testing-library/react"; |
| 3 |
import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 |
// Component |
| 5 |
import BreadCrumb, { BreadCrumbItem } from "./BreadCrumb"; |
| 6 |
|
| 7 |
describe("BreadCrumb Component", () => { |
| 8 |
afterEach(() => { |
| 9 |
vi.clearAllMocks(); |
| 10 |
cleanup(); |
| 11 |
}); |
| 12 |
|
| 13 |
it("renders the BreadCrumb component with items", async () => { |
| 14 |
const breadCrumbItems: BreadCrumbItem[] = [ |
| 15 |
{ |
| 16 |
name: "Active users", |
| 17 |
url: "/active-users", |
| 18 |
}, |
| 19 |
{ |
| 20 |
name: "user1", |
| 21 |
url: "/active-users/user1", |
| 22 |
isActive: true, |
| 23 |
}, |
| 24 |
]; |
| 25 |
|
| 26 |
render(<BreadCrumb breadcrumbItems={breadCrumbItems} />); |
| 27 |
expect(screen.getByText("Active users")).toBeInTheDocument(); |
| 28 |
expect(screen.getByText("user1")).toBeInTheDocument(); |
| 29 |
}); |
| 30 |
|
| 31 |
it("renders the BreadCrumb component with preText", async () => { |
| 32 |
const breadCrumbItems: BreadCrumbItem[] = [ |
| 33 |
{ |
| 34 |
name: "Active users", |
| 35 |
url: "/active-users", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "user1", |
| 39 |
url: "/active-users/user1", |
| 40 |
isActive: true, |
| 41 |
}, |
| 42 |
]; |
| 43 |
|
| 44 |
render(<BreadCrumb breadcrumbItems={breadCrumbItems} preText="User:" />); |
| 45 |
expect(screen.getByText("Active users")).toBeInTheDocument(); |
| 46 |
expect(screen.getByText("User: user1")).toBeInTheDocument(); |
| 47 |
}); |
| 48 |
|
| 49 |
it("has a valid link", () => { |
| 50 |
const breadCrumbItems: BreadCrumbItem[] = [ |
| 51 |
{ |
| 52 |
name: "Active users", |
| 53 |
url: "/active-users", |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "user1", |
| 57 |
url: "/active-users/user1", |
| 58 |
isActive: true, |
| 59 |
}, |
| 60 |
]; |
| 61 |
|
| 62 |
render(<BreadCrumb breadcrumbItems={breadCrumbItems} />); |
| 63 |
|
| 64 |
/** |
| 65 |
* Vitest is simulating a browser, but it has some limitations. |
| 66 |
* One of these limitations is the fact that you can't change |
| 67 |
* the location while executing a test. So in this case, the |
| 68 |
* test will check if the link has the correct href attribute. |
| 69 |
*/ |
| 70 |
const link = screen.getByText("Active users"); |
| 71 |
expect(link).toHaveAttribute("href", "/active-users"); |
| 72 |
}); |
| 73 |
}); |