|
| 1 | +import { beforeEach } from "node:test"; |
| 2 | +import { |
| 3 | + QueryClient, |
| 4 | + QueryClientProvider, |
| 5 | + type UseQueryResult, |
| 6 | +} from "@tanstack/react-query"; |
| 7 | +import { describe, expect, it, vi } from "vitest"; |
| 8 | +import { render, screen } from "~test/react-render.js"; |
| 9 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 10 | +import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; |
| 11 | +import { useSocialProfiles } from "../../../../../react/core/social/useSocialProfiles.js"; |
| 12 | +import type { SocialProfile } from "../../../../../social/types.js"; |
| 13 | +import { shortenAddress } from "../../../../../utils/address.js"; |
| 14 | +import type { Profile } from "../../../../../wallets/in-app/core/authentication/types.js"; |
| 15 | +import { useProfiles } from "../../../hooks/wallets/useProfiles.js"; |
| 16 | +import type { ConnectLocale } from "../locale/types.js"; |
| 17 | +import { LinkedProfilesScreen } from "./LinkedProfilesScreen.js"; |
| 18 | + |
| 19 | +vi.mock("../../../hooks/wallets/useProfiles"); |
| 20 | +vi.mock("../../../../../react/core/social/useSocialProfiles"); |
| 21 | + |
| 22 | +describe("LinkedProfile component", () => { |
| 23 | + const locale = { |
| 24 | + manageWallet: { |
| 25 | + linkedProfiles: "Linked Profiles", |
| 26 | + linkProfile: "Link Profile", |
| 27 | + }, |
| 28 | + } as ConnectLocale; |
| 29 | + const queryClient = new QueryClient(); |
| 30 | + beforeEach(() => { |
| 31 | + vi.resetAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should render email profile correctly", () => { |
| 35 | + vi.mocked(useProfiles).mockReturnValue({ |
| 36 | + data: [{ type: "email", details: { email: "[email protected]" } }], |
| 37 | + isLoading: false, |
| 38 | + } as UseQueryResult<Profile[]>); |
| 39 | + vi.mocked(useSocialProfiles).mockReturnValue({ |
| 40 | + data: [], |
| 41 | + isLoading: false, |
| 42 | + } as unknown as UseQueryResult<SocialProfile[]>); |
| 43 | + |
| 44 | + render( |
| 45 | + <QueryClientProvider client={queryClient}> |
| 46 | + <LinkedProfilesScreen |
| 47 | + onBack={() => {}} |
| 48 | + setScreen={() => {}} |
| 49 | + locale={locale} |
| 50 | + client={TEST_CLIENT} |
| 51 | + /> |
| 52 | + </QueryClientProvider>, |
| 53 | + ); |
| 54 | + |
| 55 | + expect(screen.getByText("[email protected]")).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should render wallet address profile correctly", () => { |
| 59 | + vi.mocked(useProfiles).mockReturnValue({ |
| 60 | + data: [{ type: "wallet", details: { address: TEST_ACCOUNT_A.address } }], |
| 61 | + isLoading: false, |
| 62 | + } as UseQueryResult<Profile[]>); |
| 63 | + vi.mocked(useSocialProfiles).mockReturnValue({ |
| 64 | + data: [], |
| 65 | + isLoading: false, |
| 66 | + } as unknown as UseQueryResult<SocialProfile[]>); |
| 67 | + |
| 68 | + render( |
| 69 | + <QueryClientProvider client={queryClient}> |
| 70 | + <LinkedProfilesScreen |
| 71 | + onBack={() => {}} |
| 72 | + setScreen={() => {}} |
| 73 | + locale={locale} |
| 74 | + client={TEST_CLIENT} |
| 75 | + /> |
| 76 | + </QueryClientProvider>, |
| 77 | + ); |
| 78 | + |
| 79 | + expect( |
| 80 | + screen.getByText(shortenAddress(TEST_ACCOUNT_A.address, 6)), |
| 81 | + ).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should render unlink button when enableUnlinking is true", () => { |
| 85 | + vi.mocked(useProfiles).mockReturnValue({ |
| 86 | + data: [ |
| 87 | + { type: "email", details: { email: "[email protected]" } }, |
| 88 | + { type: "google", details: { email: "[email protected]" } }, |
| 89 | + ], |
| 90 | + isLoading: false, |
| 91 | + } as UseQueryResult<Profile[]>); |
| 92 | + vi.mocked(useSocialProfiles).mockReturnValue({ |
| 93 | + data: [], |
| 94 | + isLoading: false, |
| 95 | + } as unknown as UseQueryResult<SocialProfile[]>); |
| 96 | + |
| 97 | + render( |
| 98 | + <QueryClientProvider client={queryClient}> |
| 99 | + <LinkedProfilesScreen |
| 100 | + onBack={() => {}} |
| 101 | + setScreen={() => {}} |
| 102 | + locale={locale} |
| 103 | + client={TEST_CLIENT} |
| 104 | + /> |
| 105 | + </QueryClientProvider>, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(screen.getAllByRole("button", { name: "Unlink" })).toHaveLength(2); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should not render unlink button when enableUnlinking is false", () => { |
| 112 | + vi.mocked(useProfiles).mockReturnValue({ |
| 113 | + data: [{ type: "email", details: { email: "[email protected]" } }], |
| 114 | + isLoading: false, |
| 115 | + } as UseQueryResult<Profile[]>); |
| 116 | + vi.mocked(useSocialProfiles).mockReturnValue({ |
| 117 | + data: [], |
| 118 | + isLoading: false, |
| 119 | + } as unknown as UseQueryResult<SocialProfile[]>); |
| 120 | + |
| 121 | + render( |
| 122 | + <QueryClientProvider client={queryClient}> |
| 123 | + <LinkedProfilesScreen |
| 124 | + onBack={() => {}} |
| 125 | + setScreen={() => {}} |
| 126 | + locale={locale} |
| 127 | + client={TEST_CLIENT} |
| 128 | + /> |
| 129 | + </QueryClientProvider>, |
| 130 | + ); |
| 131 | + |
| 132 | + expect( |
| 133 | + screen.queryByRole("button", { name: "Unlink" }), |
| 134 | + ).not.toBeInTheDocument(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments