@spacesops/wdk-react-native-core 1.0.0-beta.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +551 -0
- package/dist/__mocks__/react.d.ts +40 -0
- package/dist/__mocks__/react.js +33 -0
- package/dist/__mocks__/react.js.map +1 -0
- package/dist/__mocks__/secureStorage.d.ts +26 -0
- package/dist/__mocks__/secureStorage.js +80 -0
- package/dist/__mocks__/secureStorage.js.map +1 -0
- package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
- package/dist/__tests__/hooks/useBalance.test.js +217 -0
- package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
- package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWallet.test.js +105 -0
- package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
- package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
- package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
- package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
- package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
- package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWorklet.test.js +168 -0
- package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
- package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
- package/dist/__tests__/services/accountService.test.d.ts +6 -0
- package/dist/__tests__/services/accountService.test.js +216 -0
- package/dist/__tests__/services/accountService.test.js.map +1 -0
- package/dist/__tests__/services/addressService.test.d.ts +6 -0
- package/dist/__tests__/services/addressService.test.js +246 -0
- package/dist/__tests__/services/addressService.test.js.map +1 -0
- package/dist/__tests__/services/balanceService.test.d.ts +6 -0
- package/dist/__tests__/services/balanceService.test.js +333 -0
- package/dist/__tests__/services/balanceService.test.js.map +1 -0
- package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSetupService.test.js +304 -0
- package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
- package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
- package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
- package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
- package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
- package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
- package/dist/__tests__/setup.d.ts +7 -0
- package/dist/__tests__/setup.js +49 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
- package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
- package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
- package/dist/__tests__/store/walletStore.test.d.ts +4 -0
- package/dist/__tests__/store/walletStore.test.js +86 -0
- package/dist/__tests__/store/walletStore.test.js.map +1 -0
- package/dist/__tests__/store/workletStore.test.d.ts +4 -0
- package/dist/__tests__/store/workletStore.test.js +255 -0
- package/dist/__tests__/store/workletStore.test.js.map +1 -0
- package/dist/__tests__/types/hrpc.test.d.ts +4 -0
- package/dist/__tests__/types/hrpc.test.js +99 -0
- package/dist/__tests__/types/hrpc.test.js.map +1 -0
- package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/balanceUtils.test.js +133 -0
- package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
- package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
- package/dist/__tests__/utils/errorHandling.test.js +119 -0
- package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
- package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/errorUtils.test.js +133 -0
- package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
- package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
- package/dist/__tests__/utils/initializationState.test.js +243 -0
- package/dist/__tests__/utils/initializationState.test.js.map +1 -0
- package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/jsonUtils.test.js +118 -0
- package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
- package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
- package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
- package/dist/__tests__/utils/raceConditions.test.js +239 -0
- package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
- package/dist/__tests__/utils/result.test.d.ts +4 -0
- package/dist/__tests__/utils/result.test.js +66 -0
- package/dist/__tests__/utils/result.test.js.map +1 -0
- package/dist/__tests__/utils/schemas.test.d.ts +4 -0
- package/dist/__tests__/utils/schemas.test.js +66 -0
- package/dist/__tests__/utils/schemas.test.js.map +1 -0
- package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
- package/dist/__tests__/utils/storeHelpers.test.js +227 -0
- package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
- package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
- package/dist/__tests__/utils/typeGuards.test.js +175 -0
- package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
- package/dist/__tests__/utils/validation.test.d.ts +4 -0
- package/dist/__tests__/utils/validation.test.js +126 -0
- package/dist/__tests__/utils/validation.test.js.map +1 -0
- package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/walletUtils.test.js +156 -0
- package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
- package/dist/hooks/useBalance.d.ts +170 -0
- package/dist/hooks/useBalance.js +442 -0
- package/dist/hooks/useBalance.js.map +1 -0
- package/dist/hooks/useWallet.d.ts +43 -0
- package/dist/hooks/useWallet.js +193 -0
- package/dist/hooks/useWallet.js.map +1 -0
- package/dist/hooks/useWalletManager.d.ts +100 -0
- package/dist/hooks/useWalletManager.js +526 -0
- package/dist/hooks/useWalletManager.js.map +1 -0
- package/dist/hooks/useWdkApp.d.ts +66 -0
- package/dist/hooks/useWdkApp.js +74 -0
- package/dist/hooks/useWdkApp.js.map +1 -0
- package/dist/hooks/useWorklet.d.ts +66 -0
- package/dist/hooks/useWorklet.js +42 -0
- package/dist/hooks/useWorklet.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/provider/WdkAppProvider.d.ts +112 -0
- package/dist/provider/WdkAppProvider.js +558 -0
- package/dist/provider/WdkAppProvider.js.map +1 -0
- package/dist/services/accountService.d.ts +52 -0
- package/dist/services/accountService.js +119 -0
- package/dist/services/accountService.js.map +1 -0
- package/dist/services/addressService.d.ts +22 -0
- package/dist/services/addressService.js +110 -0
- package/dist/services/addressService.js.map +1 -0
- package/dist/services/balanceService.d.ts +114 -0
- package/dist/services/balanceService.js +208 -0
- package/dist/services/balanceService.js.map +1 -0
- package/dist/services/walletSetupService.d.ts +217 -0
- package/dist/services/walletSetupService.js +535 -0
- package/dist/services/walletSetupService.js.map +1 -0
- package/dist/services/walletSwitchingService.d.ts +48 -0
- package/dist/services/walletSwitchingService.js +143 -0
- package/dist/services/walletSwitchingService.js.map +1 -0
- package/dist/services/workletLifecycleService.d.ts +117 -0
- package/dist/services/workletLifecycleService.js +433 -0
- package/dist/services/workletLifecycleService.js.map +1 -0
- package/dist/storage/mmkvStorage.d.ts +44 -0
- package/dist/storage/mmkvStorage.js +348 -0
- package/dist/storage/mmkvStorage.js.map +1 -0
- package/dist/store/walletStore.d.ts +130 -0
- package/dist/store/walletStore.js +204 -0
- package/dist/store/walletStore.js.map +1 -0
- package/dist/store/workletStore.d.ts +121 -0
- package/dist/store/workletStore.js +256 -0
- package/dist/store/workletStore.js.map +1 -0
- package/dist/types/hrpc.d.ts +60 -0
- package/dist/types/hrpc.js +26 -0
- package/dist/types/hrpc.js.map +1 -0
- package/dist/types.d.ts +168 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/balanceUtils.d.ts +30 -0
- package/dist/utils/balanceUtils.js +86 -0
- package/dist/utils/balanceUtils.js.map +1 -0
- package/dist/utils/constants.d.ts +66 -0
- package/dist/utils/constants.js +73 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/errorHandling.d.ts +47 -0
- package/dist/utils/errorHandling.js +54 -0
- package/dist/utils/errorHandling.js.map +1 -0
- package/dist/utils/errorUtils.d.ts +89 -0
- package/dist/utils/errorUtils.js +264 -0
- package/dist/utils/errorUtils.js.map +1 -0
- package/dist/utils/initializationState.d.ts +129 -0
- package/dist/utils/initializationState.js +220 -0
- package/dist/utils/initializationState.js.map +1 -0
- package/dist/utils/jsonUtils.d.ts +21 -0
- package/dist/utils/jsonUtils.js +67 -0
- package/dist/utils/jsonUtils.js.map +1 -0
- package/dist/utils/logger.d.ts +27 -0
- package/dist/utils/logger.js +50 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/mmkvKeyManager.d.ts +57 -0
- package/dist/utils/mmkvKeyManager.js +214 -0
- package/dist/utils/mmkvKeyManager.js.map +1 -0
- package/dist/utils/mnemonicUtils.d.ts +17 -0
- package/dist/utils/mnemonicUtils.js +28 -0
- package/dist/utils/mnemonicUtils.js.map +1 -0
- package/dist/utils/operationMutex.d.ts +69 -0
- package/dist/utils/operationMutex.js +150 -0
- package/dist/utils/operationMutex.js.map +1 -0
- package/dist/utils/result.d.ts +34 -0
- package/dist/utils/result.js +43 -0
- package/dist/utils/result.js.map +1 -0
- package/dist/utils/schemas.d.ts +365 -0
- package/dist/utils/schemas.js +152 -0
- package/dist/utils/schemas.js.map +1 -0
- package/dist/utils/storeHelpers.d.ts +112 -0
- package/dist/utils/storeHelpers.js +198 -0
- package/dist/utils/storeHelpers.js.map +1 -0
- package/dist/utils/typeGuards.d.ts +70 -0
- package/dist/utils/typeGuards.js +95 -0
- package/dist/utils/typeGuards.js.map +1 -0
- package/dist/utils/validation.d.ts +53 -0
- package/dist/utils/validation.js +152 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/walletStateHelpers.d.ts +62 -0
- package/dist/utils/walletStateHelpers.js +81 -0
- package/dist/utils/walletStateHelpers.js.map +1 -0
- package/dist/utils/walletUtils.d.ts +38 -0
- package/dist/utils/walletUtils.js +87 -0
- package/dist/utils/walletUtils.js.map +1 -0
- package/package.json +79 -0
- package/src/__mocks__/react.ts +34 -0
- package/src/__mocks__/secureStorage.ts +87 -0
- package/src/__tests__/hooks/useBalance.test.ts +286 -0
- package/src/__tests__/hooks/useWallet.test.ts +122 -0
- package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
- package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
- package/src/__tests__/hooks/useWorklet.test.ts +195 -0
- package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
- package/src/__tests__/services/accountService.test.ts +337 -0
- package/src/__tests__/services/addressService.test.ts +312 -0
- package/src/__tests__/services/balanceService.test.ts +411 -0
- package/src/__tests__/services/walletSetupService.test.ts +463 -0
- package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
- package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
- package/src/__tests__/setup.ts +54 -0
- package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
- package/src/__tests__/store/walletStore.test.ts +101 -0
- package/src/__tests__/store/workletStore.test.ts +329 -0
- package/src/__tests__/types/hrpc.test.ts +125 -0
- package/src/__tests__/utils/balanceUtils.test.ts +151 -0
- package/src/__tests__/utils/errorHandling.test.ts +147 -0
- package/src/__tests__/utils/errorUtils.test.ts +170 -0
- package/src/__tests__/utils/initializationState.test.ts +337 -0
- package/src/__tests__/utils/jsonUtils.test.ts +131 -0
- package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
- package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
- package/src/__tests__/utils/raceConditions.test.ts +292 -0
- package/src/__tests__/utils/result.test.ts +73 -0
- package/src/__tests__/utils/schemas.test.ts +82 -0
- package/src/__tests__/utils/storeHelpers.test.ts +323 -0
- package/src/__tests__/utils/typeGuards.test.ts +209 -0
- package/src/__tests__/utils/validation.test.ts +152 -0
- package/src/__tests__/utils/walletUtils.test.ts +195 -0
- package/src/hooks/useBalance.ts +597 -0
- package/src/hooks/useWallet.ts +288 -0
- package/src/hooks/useWalletManager.ts +654 -0
- package/src/hooks/useWdkApp.ts +78 -0
- package/src/hooks/useWorklet.ts +109 -0
- package/src/index.ts +112 -0
- package/src/provider/WdkAppProvider.tsx +764 -0
- package/src/services/accountService.ts +134 -0
- package/src/services/addressService.ts +125 -0
- package/src/services/balanceService.ts +291 -0
- package/src/services/walletSetupService.ts +695 -0
- package/src/services/walletSwitchingService.ts +157 -0
- package/src/services/workletLifecycleService.ts +561 -0
- package/src/storage/mmkvStorage.ts +417 -0
- package/src/store/walletStore.ts +301 -0
- package/src/store/workletStore.ts +340 -0
- package/src/types/hrpc.ts +78 -0
- package/src/types.ts +188 -0
- package/src/utils/balanceUtils.ts +96 -0
- package/src/utils/constants.ts +84 -0
- package/src/utils/errorHandling.md +163 -0
- package/src/utils/errorHandling.ts +61 -0
- package/src/utils/errorUtils.ts +300 -0
- package/src/utils/initializationState.ts +242 -0
- package/src/utils/jsonUtils.ts +75 -0
- package/src/utils/logger.ts +54 -0
- package/src/utils/mmkvKeyManager.ts +256 -0
- package/src/utils/mnemonicUtils.ts +35 -0
- package/src/utils/operationMutex.ts +181 -0
- package/src/utils/result.ts +57 -0
- package/src/utils/schemas.ts +190 -0
- package/src/utils/storeHelpers.ts +236 -0
- package/src/utils/typeGuards.ts +129 -0
- package/src/utils/validation.ts +174 -0
- package/src/utils/walletStateHelpers.ts +104 -0
- package/src/utils/walletUtils.ts +107 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock SecureStorage for testing
|
|
3
|
+
*
|
|
4
|
+
* Supports identifier parameter for multi-wallet testing
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Internal storage to simulate per-identifier wallet storage
|
|
8
|
+
const storage: Record<string, {
|
|
9
|
+
encryptionKey: string | null
|
|
10
|
+
encryptedSeed: string | null
|
|
11
|
+
encryptedEntropy: string | null
|
|
12
|
+
}> = {}
|
|
13
|
+
|
|
14
|
+
const getStorageKey = (identifier?: string): string => {
|
|
15
|
+
return identifier || 'default'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const mockSecureStorage = {
|
|
19
|
+
authenticate: jest.fn(() => Promise.resolve(true)),
|
|
20
|
+
hasWallet: jest.fn((identifier?: string) => {
|
|
21
|
+
const key = getStorageKey(identifier)
|
|
22
|
+
const wallet = storage[key]
|
|
23
|
+
return Promise.resolve(wallet !== undefined && wallet.encryptionKey !== null)
|
|
24
|
+
}),
|
|
25
|
+
setEncryptionKey: jest.fn((key: string, identifier?: string) => {
|
|
26
|
+
const storageKey = getStorageKey(identifier)
|
|
27
|
+
if (!storage[storageKey]) {
|
|
28
|
+
storage[storageKey] = { encryptionKey: null, encryptedSeed: null, encryptedEntropy: null }
|
|
29
|
+
}
|
|
30
|
+
storage[storageKey].encryptionKey = key
|
|
31
|
+
return Promise.resolve()
|
|
32
|
+
}),
|
|
33
|
+
getEncryptionKey: jest.fn((identifier?: string) => {
|
|
34
|
+
const storageKey = getStorageKey(identifier)
|
|
35
|
+
return Promise.resolve(storage[storageKey]?.encryptionKey || null)
|
|
36
|
+
}),
|
|
37
|
+
setEncryptedSeed: jest.fn((seed: string, identifier?: string) => {
|
|
38
|
+
const storageKey = getStorageKey(identifier)
|
|
39
|
+
if (!storage[storageKey]) {
|
|
40
|
+
storage[storageKey] = { encryptionKey: null, encryptedSeed: null, encryptedEntropy: null }
|
|
41
|
+
}
|
|
42
|
+
storage[storageKey].encryptedSeed = seed
|
|
43
|
+
return Promise.resolve()
|
|
44
|
+
}),
|
|
45
|
+
getEncryptedSeed: jest.fn((identifier?: string) => {
|
|
46
|
+
const storageKey = getStorageKey(identifier)
|
|
47
|
+
return Promise.resolve(storage[storageKey]?.encryptedSeed || null)
|
|
48
|
+
}),
|
|
49
|
+
setEncryptedEntropy: jest.fn((entropy: string, identifier?: string) => {
|
|
50
|
+
const storageKey = getStorageKey(identifier)
|
|
51
|
+
if (!storage[storageKey]) {
|
|
52
|
+
storage[storageKey] = { encryptionKey: null, encryptedSeed: null, encryptedEntropy: null }
|
|
53
|
+
}
|
|
54
|
+
storage[storageKey].encryptedEntropy = entropy
|
|
55
|
+
return Promise.resolve()
|
|
56
|
+
}),
|
|
57
|
+
getEncryptedEntropy: jest.fn((identifier?: string) => {
|
|
58
|
+
const storageKey = getStorageKey(identifier)
|
|
59
|
+
return Promise.resolve(storage[storageKey]?.encryptedEntropy || null)
|
|
60
|
+
}),
|
|
61
|
+
getAllEncrypted: jest.fn((identifier?: string) => {
|
|
62
|
+
const storageKey = getStorageKey(identifier)
|
|
63
|
+
const wallet = storage[storageKey]
|
|
64
|
+
return Promise.resolve({
|
|
65
|
+
encryptedSeed: wallet?.encryptedSeed || null,
|
|
66
|
+
encryptedEntropy: wallet?.encryptedEntropy || null,
|
|
67
|
+
encryptionKey: wallet?.encryptionKey || null,
|
|
68
|
+
})
|
|
69
|
+
}),
|
|
70
|
+
clearAll: jest.fn(() => {
|
|
71
|
+
Object.keys(storage).forEach(key => delete storage[key])
|
|
72
|
+
return Promise.resolve()
|
|
73
|
+
}),
|
|
74
|
+
isBiometricAvailable: jest.fn(() => Promise.resolve(true)),
|
|
75
|
+
deleteWallet: jest.fn((identifier?: string) => {
|
|
76
|
+
const storageKey = getStorageKey(identifier)
|
|
77
|
+
delete storage[storageKey]
|
|
78
|
+
return Promise.resolve()
|
|
79
|
+
}),
|
|
80
|
+
cleanup: jest.fn(),
|
|
81
|
+
// Helper method to clear storage between tests
|
|
82
|
+
_clearStorage: () => {
|
|
83
|
+
Object.keys(storage).forEach(key => delete storage[key])
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default mockSecureStorage
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useBalance hook
|
|
3
|
+
*
|
|
4
|
+
* Tests balance hook logic with TanStack Query integration
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { balanceQueryKeys } from '../../hooks/useBalance'
|
|
8
|
+
import { AccountService } from '../../services/accountService'
|
|
9
|
+
import { BalanceService } from '../../services/balanceService'
|
|
10
|
+
import { getWorkletStore } from '../../store/workletStore'
|
|
11
|
+
import { getWalletStore } from '../../store/walletStore'
|
|
12
|
+
import { convertBalanceToString } from '../../utils/balanceUtils'
|
|
13
|
+
import { NATIVE_TOKEN_KEY } from '../../utils/constants'
|
|
14
|
+
|
|
15
|
+
// Mock TanStack Query
|
|
16
|
+
jest.mock('@tanstack/react-query', () => ({
|
|
17
|
+
useQuery: jest.fn(),
|
|
18
|
+
useMutation: jest.fn(),
|
|
19
|
+
useQueryClient: jest.fn(),
|
|
20
|
+
}))
|
|
21
|
+
|
|
22
|
+
// Mock stores and services
|
|
23
|
+
jest.mock('../../store/workletStore', () => ({
|
|
24
|
+
getWorkletStore: jest.fn(),
|
|
25
|
+
}))
|
|
26
|
+
|
|
27
|
+
jest.mock('../../store/walletStore', () => ({
|
|
28
|
+
getWalletStore: jest.fn(),
|
|
29
|
+
}))
|
|
30
|
+
|
|
31
|
+
jest.mock('../../services/accountService', () => ({
|
|
32
|
+
AccountService: {
|
|
33
|
+
callAccountMethod: jest.fn(),
|
|
34
|
+
},
|
|
35
|
+
}))
|
|
36
|
+
|
|
37
|
+
jest.mock('../../services/balanceService', () => ({
|
|
38
|
+
BalanceService: {
|
|
39
|
+
updateBalance: jest.fn(),
|
|
40
|
+
updateLastBalanceUpdate: jest.fn(),
|
|
41
|
+
getBalance: jest.fn(),
|
|
42
|
+
},
|
|
43
|
+
}))
|
|
44
|
+
|
|
45
|
+
jest.mock('../../utils/balanceUtils', () => ({
|
|
46
|
+
convertBalanceToString: jest.fn((val) => String(val)),
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
jest.mock('../../utils/storeHelpers', () => ({
|
|
50
|
+
resolveWalletId: jest.fn((id) => id || 'default-wallet'),
|
|
51
|
+
}))
|
|
52
|
+
|
|
53
|
+
jest.mock('../../utils/validation', () => ({
|
|
54
|
+
validateWalletParams: jest.fn(),
|
|
55
|
+
}))
|
|
56
|
+
|
|
57
|
+
jest.mock('../../utils/logger', () => ({
|
|
58
|
+
log: jest.fn(),
|
|
59
|
+
logError: jest.fn(),
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
describe('useBalance', () => {
|
|
63
|
+
let mockWorkletStore: any
|
|
64
|
+
let mockWalletStore: any
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
jest.clearAllMocks()
|
|
68
|
+
|
|
69
|
+
mockWorkletStore = {
|
|
70
|
+
getState: jest.fn(() => ({
|
|
71
|
+
isInitialized: true,
|
|
72
|
+
})),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
mockWalletStore = {
|
|
76
|
+
getState: jest.fn(() => ({
|
|
77
|
+
activeWalletId: 'test-wallet-1',
|
|
78
|
+
})),
|
|
79
|
+
}
|
|
80
|
+
;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
|
|
81
|
+
;(getWalletStore as jest.Mock).mockReturnValue(mockWalletStore)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
describe('balanceQueryKeys', () => {
|
|
85
|
+
it('should create correct query keys', () => {
|
|
86
|
+
expect(balanceQueryKeys.all).toEqual(['balances'])
|
|
87
|
+
|
|
88
|
+
const walletKey = balanceQueryKeys.byWallet('wallet-1', 0)
|
|
89
|
+
expect(walletKey).toEqual(['balances', 'wallet', 'wallet-1', 0])
|
|
90
|
+
|
|
91
|
+
const networkKey = balanceQueryKeys.byNetwork('ethereum')
|
|
92
|
+
expect(networkKey).toEqual(['balances', 'network', 'ethereum'])
|
|
93
|
+
|
|
94
|
+
const walletNetworkKey = balanceQueryKeys.byWalletAndNetwork(
|
|
95
|
+
'wallet-1',
|
|
96
|
+
0,
|
|
97
|
+
'ethereum',
|
|
98
|
+
)
|
|
99
|
+
expect(walletNetworkKey).toEqual([
|
|
100
|
+
'balances',
|
|
101
|
+
'wallet',
|
|
102
|
+
'wallet-1',
|
|
103
|
+
0,
|
|
104
|
+
'network',
|
|
105
|
+
'ethereum',
|
|
106
|
+
])
|
|
107
|
+
|
|
108
|
+
const nativeTokenKey = balanceQueryKeys.byToken(
|
|
109
|
+
'wallet-1',
|
|
110
|
+
0,
|
|
111
|
+
'ethereum',
|
|
112
|
+
null,
|
|
113
|
+
)
|
|
114
|
+
expect(nativeTokenKey).toEqual([
|
|
115
|
+
'balances',
|
|
116
|
+
'wallet',
|
|
117
|
+
'wallet-1',
|
|
118
|
+
0,
|
|
119
|
+
'network',
|
|
120
|
+
'ethereum',
|
|
121
|
+
'token',
|
|
122
|
+
NATIVE_TOKEN_KEY,
|
|
123
|
+
])
|
|
124
|
+
|
|
125
|
+
const tokenKey = balanceQueryKeys.byToken(
|
|
126
|
+
'wallet-1',
|
|
127
|
+
0,
|
|
128
|
+
'ethereum',
|
|
129
|
+
'0x123',
|
|
130
|
+
)
|
|
131
|
+
expect(tokenKey).toEqual([
|
|
132
|
+
'balances',
|
|
133
|
+
'wallet',
|
|
134
|
+
'wallet-1',
|
|
135
|
+
0,
|
|
136
|
+
'network',
|
|
137
|
+
'ethereum',
|
|
138
|
+
'token',
|
|
139
|
+
'0x123',
|
|
140
|
+
])
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
describe('fetchBalance function (tested via hook)', () => {
|
|
145
|
+
it('should handle wallet not initialized', async () => {
|
|
146
|
+
mockWorkletStore.getState.mockReturnValue({ isInitialized: false })
|
|
147
|
+
|
|
148
|
+
// Import after mocks are set up
|
|
149
|
+
const { useBalance } = await import('../../hooks/useBalance')
|
|
150
|
+
const { useQuery } = await import('@tanstack/react-query')
|
|
151
|
+
|
|
152
|
+
const mockUseQuery = useQuery as jest.Mock
|
|
153
|
+
mockUseQuery.mockReturnValue({
|
|
154
|
+
data: {
|
|
155
|
+
success: false,
|
|
156
|
+
network: 'ethereum',
|
|
157
|
+
accountIndex: 0,
|
|
158
|
+
tokenAddress: null,
|
|
159
|
+
balance: null,
|
|
160
|
+
error: 'Wallet not initialized',
|
|
161
|
+
},
|
|
162
|
+
isLoading: false,
|
|
163
|
+
error: null,
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
// The hook would be called in a React component, but we can test the query function
|
|
167
|
+
expect(mockUseQuery).toBeDefined()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('should fetch native balance successfully', async () => {
|
|
171
|
+
const mockBalance = '1000000000000000000'
|
|
172
|
+
;(AccountService.callAccountMethod as jest.Mock).mockResolvedValue(
|
|
173
|
+
mockBalance,
|
|
174
|
+
)
|
|
175
|
+
;(convertBalanceToString as jest.Mock).mockReturnValue(mockBalance)
|
|
176
|
+
|
|
177
|
+
const { useQuery } = await import('@tanstack/react-query')
|
|
178
|
+
const mockUseQuery = useQuery as jest.Mock
|
|
179
|
+
|
|
180
|
+
// Simulate query function call
|
|
181
|
+
const queryFn = mockUseQuery.mock.calls[0]?.[0]?.queryFn
|
|
182
|
+
if (queryFn) {
|
|
183
|
+
const result = await queryFn()
|
|
184
|
+
expect(result.success).toBe(true)
|
|
185
|
+
expect(result.balance).toBe(mockBalance)
|
|
186
|
+
expect(AccountService.callAccountMethod).toHaveBeenCalled()
|
|
187
|
+
expect(BalanceService.updateBalance).toHaveBeenCalled()
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('should fetch token balance successfully', async () => {
|
|
192
|
+
const mockBalance = '2000000000000000000'
|
|
193
|
+
const tokenAddress = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
|
|
194
|
+
;(AccountService.callAccountMethod as jest.Mock).mockResolvedValue(
|
|
195
|
+
mockBalance,
|
|
196
|
+
)
|
|
197
|
+
;(convertBalanceToString as jest.Mock).mockReturnValue(mockBalance)
|
|
198
|
+
|
|
199
|
+
const { useQuery } = await import('@tanstack/react-query')
|
|
200
|
+
const mockUseQuery = useQuery as jest.Mock
|
|
201
|
+
|
|
202
|
+
// Simulate query function call
|
|
203
|
+
const queryFn = mockUseQuery.mock.calls[0]?.[0]?.queryFn
|
|
204
|
+
if (queryFn) {
|
|
205
|
+
const result = await queryFn()
|
|
206
|
+
expect(result.success).toBe(true)
|
|
207
|
+
expect(result.balance).toBe(mockBalance)
|
|
208
|
+
expect(AccountService.callAccountMethod).toHaveBeenCalledWith(
|
|
209
|
+
'ethereum',
|
|
210
|
+
0,
|
|
211
|
+
'getTokenBalance',
|
|
212
|
+
tokenAddress,
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('should handle fetch errors', async () => {
|
|
218
|
+
const error = new Error('Network error')
|
|
219
|
+
;(AccountService.callAccountMethod as jest.Mock).mockRejectedValue(error)
|
|
220
|
+
|
|
221
|
+
const { useQuery } = await import('@tanstack/react-query')
|
|
222
|
+
const mockUseQuery = useQuery as jest.Mock
|
|
223
|
+
|
|
224
|
+
// Simulate query function call
|
|
225
|
+
const queryFn = mockUseQuery.mock.calls[0]?.[0]?.queryFn
|
|
226
|
+
if (queryFn) {
|
|
227
|
+
const result = await queryFn()
|
|
228
|
+
expect(result.success).toBe(false)
|
|
229
|
+
expect(result.error).toBe('Network error')
|
|
230
|
+
expect(result.balance).toBeNull()
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
describe('useBalancesForWallet', () => {
|
|
236
|
+
it('should build query keys for all tokens', async () => {
|
|
237
|
+
const tokenConfigs = {
|
|
238
|
+
ethereum: {
|
|
239
|
+
native: { address: null, symbol: 'ETH', decimals: 18 },
|
|
240
|
+
tokens: [{ address: '0x123', symbol: 'USDC', decimals: 6 }],
|
|
241
|
+
},
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const { useQuery } = await import('@tanstack/react-query')
|
|
245
|
+
const mockUseQuery = useQuery as jest.Mock
|
|
246
|
+
mockUseQuery.mockReturnValue({
|
|
247
|
+
data: [],
|
|
248
|
+
isLoading: false,
|
|
249
|
+
error: null,
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
// Test that query keys are built correctly
|
|
253
|
+
// This would be tested in a React component, but we verify the structure
|
|
254
|
+
expect(mockUseQuery).toBeDefined()
|
|
255
|
+
})
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
describe('useRefreshBalance', () => {
|
|
259
|
+
it('should invalidate queries correctly', async () => {
|
|
260
|
+
// Since we can't actually call React hooks in Node environment,
|
|
261
|
+
// we verify that the hook exports exist and the query key functions work
|
|
262
|
+
const { useRefreshBalance, balanceQueryKeys } = await import(
|
|
263
|
+
'../../hooks/useBalance'
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
// Verify the hook is exported
|
|
267
|
+
expect(typeof useRefreshBalance).toBe('function')
|
|
268
|
+
|
|
269
|
+
// Verify query keys can be used for invalidation
|
|
270
|
+
const allKeys = balanceQueryKeys.all
|
|
271
|
+
const walletKeys = balanceQueryKeys.byWallet('wallet-1', 0)
|
|
272
|
+
const networkKeys = balanceQueryKeys.byNetwork('ethereum')
|
|
273
|
+
const tokenKeys = balanceQueryKeys.byToken(
|
|
274
|
+
'wallet-1',
|
|
275
|
+
0,
|
|
276
|
+
'ethereum',
|
|
277
|
+
null,
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
expect(allKeys).toEqual(['balances'])
|
|
281
|
+
expect(walletKeys).toEqual(['balances', 'wallet', 'wallet-1', 0])
|
|
282
|
+
expect(networkKeys).toEqual(['balances', 'network', 'ethereum'])
|
|
283
|
+
expect(tokenKeys).toContain('balances')
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
})
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useWallet hook
|
|
3
|
+
*
|
|
4
|
+
* Tests wallet hook logic without React rendering
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { getWorkletStore } from '../../store/workletStore'
|
|
8
|
+
import { getWalletStore } from '../../store/walletStore'
|
|
9
|
+
import { AddressService } from '../../services/addressService'
|
|
10
|
+
import { AccountService } from '../../services/accountService'
|
|
11
|
+
import { BalanceService } from '../../services/balanceService'
|
|
12
|
+
|
|
13
|
+
// Mock stores and services
|
|
14
|
+
jest.mock('../../store/workletStore', () => ({
|
|
15
|
+
getWorkletStore: jest.fn(),
|
|
16
|
+
}))
|
|
17
|
+
|
|
18
|
+
jest.mock('../../store/walletStore', () => ({
|
|
19
|
+
getWalletStore: jest.fn(),
|
|
20
|
+
}))
|
|
21
|
+
|
|
22
|
+
jest.mock('../../services/addressService', () => ({
|
|
23
|
+
AddressService: {
|
|
24
|
+
getAddress: jest.fn(),
|
|
25
|
+
},
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
jest.mock('../../services/accountService', () => ({
|
|
29
|
+
AccountService: {
|
|
30
|
+
callAccountMethod: jest.fn(),
|
|
31
|
+
},
|
|
32
|
+
}))
|
|
33
|
+
|
|
34
|
+
jest.mock('../../services/balanceService', () => ({
|
|
35
|
+
BalanceService: {
|
|
36
|
+
updateBalance: jest.fn(),
|
|
37
|
+
getBalance: jest.fn(),
|
|
38
|
+
getBalancesForWallet: jest.fn(),
|
|
39
|
+
setBalanceLoading: jest.fn(),
|
|
40
|
+
isBalanceLoading: jest.fn(),
|
|
41
|
+
updateLastBalanceUpdate: jest.fn(),
|
|
42
|
+
getLastBalanceUpdate: jest.fn(),
|
|
43
|
+
clearBalances: jest.fn(),
|
|
44
|
+
},
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
describe('useWallet', () => {
|
|
48
|
+
let mockWorkletStore: any
|
|
49
|
+
let mockWalletStore: any
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
jest.clearAllMocks()
|
|
53
|
+
|
|
54
|
+
mockWorkletStore = jest.fn((selector: any) => {
|
|
55
|
+
const state = {
|
|
56
|
+
isInitialized: true,
|
|
57
|
+
}
|
|
58
|
+
return selector ? selector(state) : state
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
mockWalletStore = jest.fn((selector: any) => {
|
|
62
|
+
const state = {
|
|
63
|
+
addresses: {
|
|
64
|
+
ethereum: {
|
|
65
|
+
0: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
walletLoading: {},
|
|
69
|
+
balances: {
|
|
70
|
+
ethereum: {
|
|
71
|
+
0: {
|
|
72
|
+
native: '1000000000000000000',
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
balanceLoading: {},
|
|
77
|
+
lastBalanceUpdate: {
|
|
78
|
+
ethereum: {
|
|
79
|
+
0: 1234567890,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
return selector ? selector(state) : state
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
|
|
87
|
+
;(getWalletStore as jest.Mock).mockReturnValue(mockWalletStore)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should call services correctly', () => {
|
|
91
|
+
// Test that the hook calls the correct services
|
|
92
|
+
// Since we can't easily test React hooks in Node, we verify the service calls
|
|
93
|
+
|
|
94
|
+
const mockAddress = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
|
|
95
|
+
;(AddressService.getAddress as jest.Mock).mockResolvedValue(mockAddress)
|
|
96
|
+
|
|
97
|
+
// Verify service methods are callable
|
|
98
|
+
expect(typeof AddressService.getAddress).toBe('function')
|
|
99
|
+
expect(typeof AccountService.callAccountMethod).toBe('function')
|
|
100
|
+
expect(typeof BalanceService.updateBalance).toBe('function')
|
|
101
|
+
expect(typeof BalanceService.getBalance).toBe('function')
|
|
102
|
+
expect(typeof BalanceService.getBalancesForWallet).toBe('function')
|
|
103
|
+
expect(typeof BalanceService.setBalanceLoading).toBe('function')
|
|
104
|
+
expect(typeof BalanceService.isBalanceLoading).toBe('function')
|
|
105
|
+
expect(typeof BalanceService.updateLastBalanceUpdate).toBe('function')
|
|
106
|
+
expect(typeof BalanceService.getLastBalanceUpdate).toBe('function')
|
|
107
|
+
expect(typeof BalanceService.clearBalances).toBe('function')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('should have correct store structure', () => {
|
|
111
|
+
// Verify stores return expected structure
|
|
112
|
+
expect(getWorkletStore).toBeDefined()
|
|
113
|
+
expect(getWalletStore).toBeDefined()
|
|
114
|
+
|
|
115
|
+
const workletStore = getWorkletStore()
|
|
116
|
+
const walletStore = getWalletStore()
|
|
117
|
+
|
|
118
|
+
expect(workletStore).toBeDefined()
|
|
119
|
+
expect(walletStore).toBeDefined()
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|