|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createThirdwebClient } from "../../../../client/client.js"; |
| 3 | +import { getClientFetch } from "../../../../utils/fetch.js"; |
| 4 | +import type { ClientScopedStorage } from "./client-scoped-storage.js"; |
| 5 | +import { |
| 6 | + getLinkedProfilesInternal, |
| 7 | + linkAccount, |
| 8 | + unlinkAccount, |
| 9 | +} from "./linkAccount.js"; |
| 10 | +import type { Profile } from "./types.js"; |
| 11 | + |
| 12 | +vi.mock("../../../../utils/fetch.js"); |
| 13 | + |
| 14 | +describe("Account linking functions", () => { |
| 15 | + const mockClient = createThirdwebClient({ clientId: "mock-client-id" }); |
| 16 | + const mockStorage = { |
| 17 | + getAuthCookie: vi.fn(), |
| 18 | + } as unknown as ClientScopedStorage; |
| 19 | + const mockFetch = vi.fn(); |
| 20 | + const mockLinkedAccounts = [ |
| 21 | + { type: "email", details: { email: "[email protected]" } }, |
| 22 | + { type: "phone", details: { phone: "1234567890" } }, |
| 23 | + { type: "wallet", details: { address: "0x123456789" } }, |
| 24 | + ] satisfies Profile[]; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + vi.mocked(getClientFetch).mockReturnValue(mockFetch); |
| 29 | + vi.mocked(mockStorage.getAuthCookie).mockResolvedValue("mock-token"); |
| 30 | + mockFetch.mockResolvedValue({ |
| 31 | + ok: true, |
| 32 | + json: () => Promise.resolve({ linkedAccounts: mockLinkedAccounts }), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("linkAccount", () => { |
| 37 | + it("should successfully link an account", async () => { |
| 38 | + const result = await linkAccount({ |
| 39 | + client: mockClient, |
| 40 | + tokenToLink: "token-to-link", |
| 41 | + storage: mockStorage, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(mockFetch).toHaveBeenCalledWith( |
| 45 | + "https://embedded-wallet.thirdweb.com/api/2024-05-05/account/connect", |
| 46 | + { |
| 47 | + method: "POST", |
| 48 | + headers: { |
| 49 | + Authorization: "Bearer iaw-auth-token:mock-token", |
| 50 | + "Content-Type": "application/json", |
| 51 | + }, |
| 52 | + body: JSON.stringify({ |
| 53 | + accountAuthTokenToConnect: "token-to-link", |
| 54 | + }), |
| 55 | + }, |
| 56 | + ); |
| 57 | + expect(result).toEqual(mockLinkedAccounts); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should throw error when no user is logged in", async () => { |
| 61 | + vi.mocked(mockStorage.getAuthCookie).mockResolvedValue(null); |
| 62 | + |
| 63 | + await expect( |
| 64 | + linkAccount({ |
| 65 | + client: mockClient, |
| 66 | + tokenToLink: "token-to-link", |
| 67 | + storage: mockStorage, |
| 68 | + }), |
| 69 | + ).rejects.toThrow("Failed to link account, no user logged in"); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("unlinkAccount", () => { |
| 74 | + const profileToUnlink = { |
| 75 | + type: "email", |
| 76 | + details: { email: "[email protected]" }, |
| 77 | + } satisfies Profile; |
| 78 | + it("should successfully unlink an account", async () => { |
| 79 | + const result = await unlinkAccount({ |
| 80 | + client: mockClient, |
| 81 | + profileToUnlink, |
| 82 | + storage: mockStorage, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(mockFetch).toHaveBeenCalledWith( |
| 86 | + "https://embedded-wallet.thirdweb.com/api/2024-05-05/account/disconnect", |
| 87 | + { |
| 88 | + method: "POST", |
| 89 | + headers: { |
| 90 | + Authorization: "Bearer iaw-auth-token:mock-token", |
| 91 | + "Content-Type": "application/json", |
| 92 | + }, |
| 93 | + body: JSON.stringify(profileToUnlink), |
| 94 | + }, |
| 95 | + ); |
| 96 | + expect(result).toEqual(mockLinkedAccounts); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should throw error when no user is logged in", async () => { |
| 100 | + vi.mocked(mockStorage.getAuthCookie).mockResolvedValue(null); |
| 101 | + |
| 102 | + await expect( |
| 103 | + unlinkAccount({ |
| 104 | + client: mockClient, |
| 105 | + profileToUnlink, |
| 106 | + storage: mockStorage, |
| 107 | + }), |
| 108 | + ).rejects.toThrow("Failed to unlink account, no user logged in"); |
| 109 | + }); |
| 110 | + it("should handle API errors", async () => { |
| 111 | + mockFetch.mockResolvedValue({ |
| 112 | + ok: false, |
| 113 | + json: () => Promise.resolve({ message: "API Error" }), |
| 114 | + }); |
| 115 | + |
| 116 | + await expect( |
| 117 | + unlinkAccount({ |
| 118 | + client: mockClient, |
| 119 | + profileToUnlink, |
| 120 | + storage: mockStorage, |
| 121 | + }), |
| 122 | + ).rejects.toThrow("API Error"); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe("getLinkedProfilesInternal", () => { |
| 127 | + it("should successfully get linked profiles", async () => { |
| 128 | + const result = await getLinkedProfilesInternal({ |
| 129 | + client: mockClient, |
| 130 | + storage: mockStorage, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(mockFetch).toHaveBeenCalledWith( |
| 134 | + "https://embedded-wallet.thirdweb.com/api/2024-05-05/accounts", |
| 135 | + { |
| 136 | + method: "GET", |
| 137 | + headers: { |
| 138 | + Authorization: "Bearer iaw-auth-token:mock-token", |
| 139 | + "Content-Type": "application/json", |
| 140 | + }, |
| 141 | + }, |
| 142 | + ); |
| 143 | + expect(result).toEqual(mockLinkedAccounts); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should handle API errors", async () => { |
| 147 | + mockFetch.mockResolvedValue({ |
| 148 | + ok: false, |
| 149 | + json: () => Promise.resolve({ message: "API Error" }), |
| 150 | + }); |
| 151 | + |
| 152 | + await expect( |
| 153 | + getLinkedProfilesInternal({ |
| 154 | + client: mockClient, |
| 155 | + storage: mockStorage, |
| 156 | + }), |
| 157 | + ).rejects.toThrow("API Error"); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments