|
| 1 | +import { screen } from "@testing-library/react" |
| 2 | +import React from "react" |
| 3 | +import { render } from "../../test_helpers" |
| 4 | +import { MockUser } from "../../test_helpers/entities" |
| 5 | +import { UserDropdown, UserDropdownProps } from "./UserDropdown" |
| 6 | + |
| 7 | +const renderAndClick = async (props: Partial<UserDropdownProps> = {}) => { |
| 8 | + render(<UserDropdown user={props.user ?? MockUser} onSignOut={props.onSignOut ?? jest.fn()} />) |
| 9 | + const trigger = await screen.findByTestId("user-dropdown-trigger") |
| 10 | + trigger.click() |
| 11 | +} |
| 12 | + |
| 13 | +describe("UserDropdown", () => { |
| 14 | + describe("when the trigger is clicked", () => { |
| 15 | + it("opens the menu", async () => { |
| 16 | + await renderAndClick() |
| 17 | + expect(screen.getByText(/account/i)).toBeDefined() |
| 18 | + expect(screen.getByText(/documentation/i)).toBeDefined() |
| 19 | + expect(screen.getByText(/sign out/i)).toBeDefined() |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + describe("when the menu is open", () => { |
| 24 | + describe("and sign out is clicked", () => { |
| 25 | + it("calls the onSignOut function", async () => { |
| 26 | + const onSignOut = jest.fn() |
| 27 | + await renderAndClick({ onSignOut }) |
| 28 | + screen.getByText(/sign out/i).click() |
| 29 | + expect(onSignOut).toBeCalledTimes(1) |
| 30 | + }) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it("has the correct link for the documentation item", async () => { |
| 35 | + await renderAndClick() |
| 36 | + |
| 37 | + const link = screen.getByText(/documentation/i).closest("a") |
| 38 | + if (!link) { |
| 39 | + throw new Error("Anchor tag not found for the documentation menu item") |
| 40 | + } |
| 41 | + |
| 42 | + expect(link.getAttribute("href")).toBe("https://coder.com/docs") |
| 43 | + }) |
| 44 | + |
| 45 | + it("has the correct link for the account item", async () => { |
| 46 | + await renderAndClick() |
| 47 | + |
| 48 | + const link = screen.getByText(/account/i).closest("a") |
| 49 | + if (!link) { |
| 50 | + throw new Error("Anchor tag not found for the account menu item") |
| 51 | + } |
| 52 | + |
| 53 | + expect(link.getAttribute("href")).toBe("/preferences") |
| 54 | + }) |
| 55 | +}) |
0 commit comments