|
1 | 1 | import { render, screen } from "@testing-library/react"; |
2 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
3 | 3 | import { UserMenu } from "@/components/user-menu"; |
| 4 | +import { signOut } from "@/lib/auth/auth-client"; |
4 | 5 |
|
5 | 6 | describe("UserMenu", () => { |
6 | 7 | it.each([ |
@@ -36,4 +37,26 @@ describe("UserMenu", () => { |
36 | 37 | const signOutItem = screen.getByText("Sign out"); |
37 | 38 | expect(signOutItem).toBeTruthy(); |
38 | 39 | }); |
| 40 | + |
| 41 | + it("calls signOut when sign out menu item is clicked", async () => { |
| 42 | + const userEvent = (await import("@testing-library/user-event")).default; |
| 43 | + render(<UserMenu userName="Jane Smith" />); |
| 44 | + const user = userEvent.setup(); |
| 45 | + |
| 46 | + // Open dropdown - use getAllByRole and find the one in this render |
| 47 | + const buttons = screen.getAllByRole("button"); |
| 48 | + const trigger = buttons.find((btn) => |
| 49 | + btn.textContent?.includes("Jane Smith"), |
| 50 | + ); |
| 51 | + expect(trigger).toBeTruthy(); |
| 52 | + if (!trigger) return; |
| 53 | + await user.click(trigger); |
| 54 | + |
| 55 | + // Find and click the sign out menu item (it's a div with role="menuitem") |
| 56 | + const signOutItem = screen.getByRole("menuitem", { name: /sign out/i }); |
| 57 | + await user.click(signOutItem); |
| 58 | + |
| 59 | + // Verify signOut was called |
| 60 | + expect(signOut).toHaveBeenCalledOnce(); |
| 61 | + }); |
39 | 62 | }); |
0 commit comments