|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { useClerk } from "@clerk/clerk-react"; |
| 4 | +import { AccountSecuritySection } from "./account-security-section"; |
| 5 | + |
| 6 | +// Mock Clerk |
| 7 | +const mockBuildUserProfileUrl = vi.fn(); |
| 8 | +vi.mock("@clerk/clerk-react", () => ({ |
| 9 | + useClerk: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock UI components |
| 13 | +vi.mock("@/components/ui/button", () => ({ |
| 14 | + Button: ({ children, asChild, ...props }: any) => { |
| 15 | + if (asChild) { |
| 16 | + return ( |
| 17 | + <div data-testid="button-wrapper" {...props}> |
| 18 | + {children} |
| 19 | + </div> |
| 20 | + ); |
| 21 | + } |
| 22 | + return <button {...props}>{children}</button>; |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("@/components/ui/card", () => ({ |
| 27 | + CardHeader: ({ children }: any) => <div data-testid="card-header">{children}</div>, |
| 28 | + CardTitle: ({ children }: any) => <h2 data-testid="card-title">{children}</h2>, |
| 29 | + CardContent: ({ children, className }: any) => ( |
| 30 | + <div data-testid="card-content" className={className}> |
| 31 | + {children} |
| 32 | + </div> |
| 33 | + ), |
| 34 | +})); |
| 35 | + |
| 36 | +// Mock lucide-react icon |
| 37 | +vi.mock("lucide-react", () => ({ |
| 38 | + ExternalLink: ({ className }: any) => ( |
| 39 | + <span data-testid="external-link-icon" className={className}> |
| 40 | + 🔗 |
| 41 | + </span> |
| 42 | + ), |
| 43 | +})); |
| 44 | + |
| 45 | +describe("AccountSecuritySection", () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks(); |
| 48 | + vi.mocked(useClerk).mockReturnValue({ |
| 49 | + buildUserProfileUrl: mockBuildUserProfileUrl, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should render the section with title and description", () => { |
| 54 | + mockBuildUserProfileUrl.mockReturnValue("https://test.clerk.accounts.dev/user"); |
| 55 | + |
| 56 | + render(<AccountSecuritySection />); |
| 57 | + |
| 58 | + expect(screen.getByTestId("card-title")).toHaveTextContent("Account Security"); |
| 59 | + expect(screen.getByText(/TrendWeight uses Clerk to handle authentication/)).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should render both account links with Clerk URLs when available", () => { |
| 63 | + const clerkProfileUrl = "https://test.clerk.accounts.dev/user"; |
| 64 | + mockBuildUserProfileUrl.mockReturnValue(clerkProfileUrl); |
| 65 | + |
| 66 | + render(<AccountSecuritySection />); |
| 67 | + |
| 68 | + // Check Account Profile link |
| 69 | + const profileLink = screen.getByRole("link", { name: /Open Account Profile/ }); |
| 70 | + expect(profileLink).toHaveAttribute("href", clerkProfileUrl); |
| 71 | + expect(profileLink).toHaveAttribute("target", "_blank"); |
| 72 | + expect(profileLink).toHaveAttribute("rel", "noopener noreferrer"); |
| 73 | + |
| 74 | + // Check Security Settings link |
| 75 | + const securityLink = screen.getByRole("link", { name: /Open Security Settings/ }); |
| 76 | + expect(securityLink).toHaveAttribute("href", "https://test.clerk.accounts.dev/user/security"); |
| 77 | + expect(securityLink).toHaveAttribute("target", "_blank"); |
| 78 | + expect(securityLink).toHaveAttribute("rel", "noopener noreferrer"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should use fallback URLs when Clerk buildUserProfileUrl is not available", () => { |
| 82 | + // Mock useClerk to return an object without buildUserProfileUrl |
| 83 | + vi.mocked(useClerk).mockReturnValue({}); |
| 84 | + |
| 85 | + render(<AccountSecuritySection />); |
| 86 | + |
| 87 | + // Check Account Profile link uses fallback |
| 88 | + const profileLink = screen.getByRole("link", { name: /Open Account Profile/ }); |
| 89 | + expect(profileLink).toHaveAttribute("href", "https://accounts.trendweight.com/user"); |
| 90 | + |
| 91 | + // Check Security Settings link uses fallback |
| 92 | + const securityLink = screen.getByRole("link", { name: /Open Security Settings/ }); |
| 93 | + expect(securityLink).toHaveAttribute("href", "https://accounts.trendweight.com/user/security"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should use fallback URLs when Clerk is not available", () => { |
| 97 | + // Mock useClerk to return null |
| 98 | + vi.mocked(useClerk).mockReturnValue(null); |
| 99 | + |
| 100 | + render(<AccountSecuritySection />); |
| 101 | + |
| 102 | + // Check Account Profile link uses fallback |
| 103 | + const profileLink = screen.getByRole("link", { name: /Open Account Profile/ }); |
| 104 | + expect(profileLink).toHaveAttribute("href", "https://accounts.trendweight.com/user"); |
| 105 | + |
| 106 | + // Check Security Settings link uses fallback |
| 107 | + const securityLink = screen.getByRole("link", { name: /Open Security Settings/ }); |
| 108 | + expect(securityLink).toHaveAttribute("href", "https://accounts.trendweight.com/user/security"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should render external link icons", () => { |
| 112 | + mockBuildUserProfileUrl.mockReturnValue("https://test.clerk.accounts.dev/user"); |
| 113 | + |
| 114 | + render(<AccountSecuritySection />); |
| 115 | + |
| 116 | + const icons = screen.getAllByTestId("external-link-icon"); |
| 117 | + expect(icons).toHaveLength(2); |
| 118 | + |
| 119 | + // Check that icons have the correct styling |
| 120 | + icons.forEach((icon) => { |
| 121 | + expect(icon).toHaveClass("ml-1", "h-3", "w-3"); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should render descriptive text for both sections", () => { |
| 126 | + mockBuildUserProfileUrl.mockReturnValue("https://test.clerk.accounts.dev/user"); |
| 127 | + |
| 128 | + render(<AccountSecuritySection />); |
| 129 | + |
| 130 | + // Check Account Profile description |
| 131 | + expect(screen.getByText(/You can use the profile page to update your email address/)).toBeInTheDocument(); |
| 132 | + |
| 133 | + // Check Security Settings description |
| 134 | + expect(screen.getByText(/On the security page you can add passkeys/)).toBeInTheDocument(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should call Clerk buildUserProfileUrl method when available", () => { |
| 138 | + mockBuildUserProfileUrl.mockReturnValue("https://test.clerk.accounts.dev/user"); |
| 139 | + |
| 140 | + render(<AccountSecuritySection />); |
| 141 | + |
| 142 | + // The component calls buildUserProfileUrl during render to get the profile URL |
| 143 | + expect(mockBuildUserProfileUrl).toHaveBeenCalledTimes(1); |
| 144 | + }); |
| 145 | +}); |
0 commit comments