account-settings-context-menu.test.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { render, screen } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { afterEach, describe, expect, it, test, vi } from "vitest";
  4. import { AccountSettingsContextMenu } from "#/components/context-menu/account-settings-context-menu";
  5. describe("AccountSettingsContextMenu", () => {
  6. const user = userEvent.setup();
  7. const onClickAccountSettingsMock = vi.fn();
  8. const onLogoutMock = vi.fn();
  9. const onCloseMock = vi.fn();
  10. afterEach(() => {
  11. onClickAccountSettingsMock.mockClear();
  12. onLogoutMock.mockClear();
  13. onCloseMock.mockClear();
  14. });
  15. it("should always render the right options", () => {
  16. render(
  17. <AccountSettingsContextMenu
  18. onClickAccountSettings={onClickAccountSettingsMock}
  19. onLogout={onLogoutMock}
  20. onClose={onCloseMock}
  21. isLoggedIn
  22. />,
  23. );
  24. expect(
  25. screen.getByTestId("account-settings-context-menu"),
  26. ).toBeInTheDocument();
  27. expect(screen.getByText("Account Settings")).toBeInTheDocument();
  28. expect(screen.getByText("Logout")).toBeInTheDocument();
  29. });
  30. it("should call onClickAccountSettings when the account settings option is clicked", async () => {
  31. render(
  32. <AccountSettingsContextMenu
  33. onClickAccountSettings={onClickAccountSettingsMock}
  34. onLogout={onLogoutMock}
  35. onClose={onCloseMock}
  36. isLoggedIn
  37. />,
  38. );
  39. const accountSettingsOption = screen.getByText("Account Settings");
  40. await user.click(accountSettingsOption);
  41. expect(onClickAccountSettingsMock).toHaveBeenCalledOnce();
  42. });
  43. it("should call onLogout when the logout option is clicked", async () => {
  44. render(
  45. <AccountSettingsContextMenu
  46. onClickAccountSettings={onClickAccountSettingsMock}
  47. onLogout={onLogoutMock}
  48. onClose={onCloseMock}
  49. isLoggedIn
  50. />,
  51. );
  52. const logoutOption = screen.getByText("Logout");
  53. await user.click(logoutOption);
  54. expect(onLogoutMock).toHaveBeenCalledOnce();
  55. });
  56. test("onLogout should be disabled if the user is not logged in", async () => {
  57. render(
  58. <AccountSettingsContextMenu
  59. onClickAccountSettings={onClickAccountSettingsMock}
  60. onLogout={onLogoutMock}
  61. onClose={onCloseMock}
  62. isLoggedIn={false}
  63. />,
  64. );
  65. const logoutOption = screen.getByText("Logout");
  66. await user.click(logoutOption);
  67. expect(onLogoutMock).not.toHaveBeenCalled();
  68. });
  69. it("should call onClose when clicking outside of the element", async () => {
  70. render(
  71. <AccountSettingsContextMenu
  72. onClickAccountSettings={onClickAccountSettingsMock}
  73. onLogout={onLogoutMock}
  74. onClose={onCloseMock}
  75. isLoggedIn
  76. />,
  77. );
  78. const accountSettingsButton = screen.getByText("Account Settings");
  79. await user.click(accountSettingsButton);
  80. await user.click(document.body);
  81. expect(onCloseMock).toHaveBeenCalledOnce();
  82. });
  83. });