|
| 1 | +import { ConcordiumAccount } from "@ledgerhq/coin-concordium/types"; |
| 2 | +import { createEmptyHistoryCache } from "@ledgerhq/coin-framework/account/balanceHistoryCache"; |
| 3 | +import { |
| 4 | + getDerivationModesForCurrency, |
| 5 | + getDerivationScheme, |
| 6 | + runDerivationScheme, |
| 7 | +} from "@ledgerhq/coin-framework/derivation"; |
| 8 | +import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets"; |
| 9 | +import { Account } from "@ledgerhq/types-live"; |
| 10 | +import BigNumber from "bignumber.js"; |
| 11 | +import { renderHook } from "tests/testSetup"; |
| 12 | +import { useConcordiumCreatableAccounts } from "./useConcordiumCreatableAccounts"; |
| 13 | + |
| 14 | +const createMockAccount = ( |
| 15 | + id: string, |
| 16 | + currencyId: string, |
| 17 | + used: boolean = false, |
| 18 | + isOnboarded: boolean = false, |
| 19 | +): Account => { |
| 20 | + const currency = getCryptoCurrencyById(currencyId); |
| 21 | + const derivationMode = getDerivationModesForCurrency(currency)[0]; |
| 22 | + const scheme = getDerivationScheme({ derivationMode, currency }); |
| 23 | + const baseAccount: Account = { |
| 24 | + id, |
| 25 | + type: "Account", |
| 26 | + used, |
| 27 | + currency, |
| 28 | + derivationMode, |
| 29 | + index: 0, |
| 30 | + freshAddress: "test_address", |
| 31 | + freshAddressPath: runDerivationScheme(scheme, currency, { |
| 32 | + account: 0, |
| 33 | + node: 0, |
| 34 | + address: 0, |
| 35 | + }), |
| 36 | + creationDate: new Date(), |
| 37 | + lastSyncDate: new Date(), |
| 38 | + balance: new BigNumber(0), |
| 39 | + spendableBalance: new BigNumber(0), |
| 40 | + seedIdentifier: "test_seed", |
| 41 | + blockHeight: 0, |
| 42 | + operationsCount: 0, |
| 43 | + operations: [], |
| 44 | + pendingOperations: [], |
| 45 | + balanceHistoryCache: createEmptyHistoryCache(), |
| 46 | + swapHistory: [], |
| 47 | + subAccounts: [], |
| 48 | + }; |
| 49 | + |
| 50 | + if (currency.family === "concordium") { |
| 51 | + return { |
| 52 | + ...baseAccount, |
| 53 | + concordiumResources: { |
| 54 | + isOnboarded, |
| 55 | + credId: "test_cred_id", |
| 56 | + publicKey: "test_public_key", |
| 57 | + identityIndex: 0, |
| 58 | + credNumber: 0, |
| 59 | + ipIdentity: 0, |
| 60 | + }, |
| 61 | + } as ConcordiumAccount; |
| 62 | + } |
| 63 | + |
| 64 | + return baseAccount; |
| 65 | +}; |
| 66 | + |
| 67 | +describe("useConcordiumCreatableAccounts", () => { |
| 68 | + it("should return false when no Concordium accounts are present", () => { |
| 69 | + const scannedAccounts: Account[] = [createMockAccount("1", "bitcoin", false)]; |
| 70 | + const selectedIds = ["1"]; |
| 71 | + |
| 72 | + const { result } = renderHook(() => |
| 73 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 74 | + ); |
| 75 | + |
| 76 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(false); |
| 77 | + expect(result.current.selectedConcordiumAccounts).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should return false when Concordium accounts are onboarded", () => { |
| 81 | + const scannedAccounts: Account[] = [createMockAccount("1", "concordium", false, true)]; |
| 82 | + const selectedIds = ["1"]; |
| 83 | + |
| 84 | + const { result } = renderHook(() => |
| 85 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(false); |
| 89 | + expect(result.current.selectedConcordiumAccounts).toHaveLength(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should return false when Concordium accounts are not selected", () => { |
| 93 | + const scannedAccounts: Account[] = [createMockAccount("1", "concordium", false)]; |
| 94 | + const selectedIds: string[] = []; |
| 95 | + |
| 96 | + const { result } = renderHook(() => |
| 97 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 98 | + ); |
| 99 | + |
| 100 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(false); |
| 101 | + expect(result.current.selectedConcordiumAccounts).toEqual([]); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should return true when Concordium creatable accounts are selected", () => { |
| 105 | + const scannedAccounts: Account[] = [createMockAccount("1", "concordium", false, false)]; |
| 106 | + const selectedIds = ["1"]; |
| 107 | + |
| 108 | + const { result } = renderHook(() => |
| 109 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 110 | + ); |
| 111 | + |
| 112 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(true); |
| 113 | + expect(result.current.selectedConcordiumAccounts).toHaveLength(1); |
| 114 | + expect(result.current.selectedConcordiumAccounts.map(a => a.id)).toEqual(["1"]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should handle empty scanned accounts array", () => { |
| 118 | + const scannedAccounts: Account[] = []; |
| 119 | + const selectedIds: string[] = []; |
| 120 | + |
| 121 | + const { result } = renderHook(() => |
| 122 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(false); |
| 126 | + expect(result.current.selectedConcordiumAccounts).toEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should update results when props change", () => { |
| 130 | + const initialScannedAccounts: Account[] = [createMockAccount("1", "concordium", false, false)]; |
| 131 | + const initialSelectedIds = ["1"]; |
| 132 | + |
| 133 | + const { result, rerender } = renderHook( |
| 134 | + ({ scannedAccounts, selectedIds }) => |
| 135 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 136 | + { |
| 137 | + initialProps: { |
| 138 | + scannedAccounts: initialScannedAccounts, |
| 139 | + selectedIds: initialSelectedIds, |
| 140 | + }, |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(true); |
| 145 | + |
| 146 | + // Update to deselect the account |
| 147 | + rerender({ |
| 148 | + scannedAccounts: initialScannedAccounts, |
| 149 | + selectedIds: [], |
| 150 | + }); |
| 151 | + |
| 152 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(false); |
| 153 | + expect(result.current.selectedConcordiumAccounts).toEqual([]); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should include both onboarded and creatable accounts in selectedConcordiumAccounts", () => { |
| 157 | + const scannedAccounts: Account[] = [ |
| 158 | + createMockAccount("1", "concordium", false, true), // onboarded |
| 159 | + createMockAccount("2", "concordium", false, false), // creatable |
| 160 | + ]; |
| 161 | + const selectedIds = ["1", "2"]; |
| 162 | + |
| 163 | + const { result } = renderHook(() => |
| 164 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 165 | + ); |
| 166 | + |
| 167 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(true); |
| 168 | + expect(result.current.selectedConcordiumAccounts).toHaveLength(2); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should filter out non-Concordium accounts from selectedConcordiumAccounts", () => { |
| 172 | + const scannedAccounts: Account[] = [ |
| 173 | + createMockAccount("1", "concordium", false, false), |
| 174 | + createMockAccount("2", "bitcoin", false), |
| 175 | + ]; |
| 176 | + const selectedIds = ["1", "2"]; |
| 177 | + |
| 178 | + const { result } = renderHook(() => |
| 179 | + useConcordiumCreatableAccounts({ scannedAccounts, selectedIds }), |
| 180 | + ); |
| 181 | + |
| 182 | + expect(result.current.hasConcordiumCreatableAccounts).toBe(true); |
| 183 | + expect(result.current.selectedConcordiumAccounts).toHaveLength(1); |
| 184 | + expect(result.current.selectedConcordiumAccounts[0].id).toBe("1"); |
| 185 | + }); |
| 186 | +}); |
0 commit comments