|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { act, renderHook } from "@testing-library/react"; |
| 3 | +import type React from "react"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 6 | +import { useConnectedWallets } from "../../../../react/core/hooks/wallets/useConnectedWallets.js"; |
| 7 | +import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js"; |
| 8 | +import { unlinkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js"; |
| 9 | +import type { Wallet } from "../../../../wallets/interfaces/wallet.js"; |
| 10 | +import { useUnlinkProfile } from "./useUnlinkProfile.js"; |
| 11 | + |
| 12 | +vi.mock("../../../../wallets/in-app/web/lib/auth/index.js"); |
| 13 | +vi.mock("../../../core/hooks/wallets/useConnectedWallets.js"); |
| 14 | + |
| 15 | +describe("useUnlinkProfile", () => { |
| 16 | + const queryClient = new QueryClient(); |
| 17 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 18 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 19 | + ); |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + vi.spyOn(queryClient, "invalidateQueries"); |
| 24 | + }); |
| 25 | + |
| 26 | + const mockProfile = {} as unknown as Profile; |
| 27 | + it("should call unlinkProfile with correct parameters", async () => { |
| 28 | + vi.mocked(useConnectedWallets).mockReturnValue([]); |
| 29 | + |
| 30 | + const { result } = renderHook(() => useUnlinkProfile(), { |
| 31 | + wrapper, |
| 32 | + }); |
| 33 | + const mutationFn = result.current.mutateAsync; |
| 34 | + |
| 35 | + await act(async () => { |
| 36 | + await mutationFn({ client: TEST_CLIENT, profileToUnlink: mockProfile }); |
| 37 | + }); |
| 38 | + |
| 39 | + expect(unlinkProfile).toHaveBeenCalledWith({ |
| 40 | + client: TEST_CLIENT, |
| 41 | + ecosystem: undefined, |
| 42 | + profileToUnlink: mockProfile, |
| 43 | + }); |
| 44 | + expect(queryClient.invalidateQueries).toHaveBeenCalledWith({ |
| 45 | + queryKey: ["profiles"], |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should include ecosystem if ecosystem wallet is found", async () => { |
| 50 | + const mockWallet = { |
| 51 | + id: "ecosystem.wallet-id", |
| 52 | + getConfig: () => ({ partnerId: "partner-id" }), |
| 53 | + } as unknown as Wallet; |
| 54 | + vi.mocked(useConnectedWallets).mockReturnValue([mockWallet]); |
| 55 | + |
| 56 | + const { result } = renderHook(() => useUnlinkProfile(), { |
| 57 | + wrapper, |
| 58 | + }); |
| 59 | + const mutationFn = result.current.mutateAsync; |
| 60 | + |
| 61 | + await act(async () => { |
| 62 | + await mutationFn({ client: TEST_CLIENT, profileToUnlink: mockProfile }); |
| 63 | + }); |
| 64 | + |
| 65 | + expect(unlinkProfile).toHaveBeenCalledWith({ |
| 66 | + client: TEST_CLIENT, |
| 67 | + ecosystem: { |
| 68 | + id: mockWallet.id, |
| 69 | + partnerId: (mockWallet as Wallet<`ecosystem.${string}`>).getConfig() |
| 70 | + ?.partnerId, |
| 71 | + }, |
| 72 | + profileToUnlink: mockProfile, |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments