-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.test.tsx
More file actions
56 lines (45 loc) · 1.76 KB
/
Header.test.tsx
File metadata and controls
56 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import Header from "./Header";
describe("Header component", () => {
const mockLogout = vi.fn();
const defaultProps = {
user: { name: "Karuna" },
onLogout: mockLogout,
};
it("renders the navbar container", () => {
render(<Header {...defaultProps} />);
expect(screen.getByRole("banner")).toBeInTheDocument();
});
it("renders the app title", () => {
render(<Header {...defaultProps} />);
expect(screen.getByText("daisyUI")).toBeInTheDocument();
});
it("renders the hamburger menu link", () => {
render(<Header {...defaultProps} />);
const menuLink = screen.getAllByRole("link")[0];
expect(menuLink).toHaveAttribute("href", "/");
});
it("renders profile button", () => {
render(<Header {...defaultProps} />);
expect(screen.getByRole("button")).toBeInTheDocument();
});
it("shows the first letter of the user's name in profile image", () => {
render(<Header {...defaultProps} />);
const profileImg = screen.getByAltText("Karuna profile");
expect(profileImg).toHaveAttribute(
"src",
expect.stringContaining("K")
);
});
it("uses fallback text when user is undefined", () => {
render(<Header onLogout={mockLogout} />);
expect(screen.getByAltText("User profile")).toBeInTheDocument();
});
it("calls onLogout when profile button is clicked", () => {
render(<Header {...defaultProps} />);
const profileButton = screen.getByRole("button");
fireEvent.click(profileButton);
expect(mockLogout).toHaveBeenCalledOnce();
});
});