@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.
Files changed (275) hide show
  1. package/README.md +551 -0
  2. package/dist/__mocks__/react.d.ts +40 -0
  3. package/dist/__mocks__/react.js +33 -0
  4. package/dist/__mocks__/react.js.map +1 -0
  5. package/dist/__mocks__/secureStorage.d.ts +26 -0
  6. package/dist/__mocks__/secureStorage.js +80 -0
  7. package/dist/__mocks__/secureStorage.js.map +1 -0
  8. package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
  9. package/dist/__tests__/hooks/useBalance.test.js +217 -0
  10. package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
  11. package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
  12. package/dist/__tests__/hooks/useWallet.test.js +105 -0
  13. package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
  14. package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
  15. package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
  16. package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
  17. package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
  18. package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
  19. package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
  20. package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
  21. package/dist/__tests__/hooks/useWorklet.test.js +168 -0
  22. package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
  23. package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
  24. package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
  25. package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
  26. package/dist/__tests__/services/accountService.test.d.ts +6 -0
  27. package/dist/__tests__/services/accountService.test.js +216 -0
  28. package/dist/__tests__/services/accountService.test.js.map +1 -0
  29. package/dist/__tests__/services/addressService.test.d.ts +6 -0
  30. package/dist/__tests__/services/addressService.test.js +246 -0
  31. package/dist/__tests__/services/addressService.test.js.map +1 -0
  32. package/dist/__tests__/services/balanceService.test.d.ts +6 -0
  33. package/dist/__tests__/services/balanceService.test.js +333 -0
  34. package/dist/__tests__/services/balanceService.test.js.map +1 -0
  35. package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
  36. package/dist/__tests__/services/walletSetupService.test.js +304 -0
  37. package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
  38. package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
  39. package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
  40. package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
  41. package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
  42. package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
  43. package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
  44. package/dist/__tests__/setup.d.ts +7 -0
  45. package/dist/__tests__/setup.js +49 -0
  46. package/dist/__tests__/setup.js.map +1 -0
  47. package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
  48. package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
  49. package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
  50. package/dist/__tests__/store/walletStore.test.d.ts +4 -0
  51. package/dist/__tests__/store/walletStore.test.js +86 -0
  52. package/dist/__tests__/store/walletStore.test.js.map +1 -0
  53. package/dist/__tests__/store/workletStore.test.d.ts +4 -0
  54. package/dist/__tests__/store/workletStore.test.js +255 -0
  55. package/dist/__tests__/store/workletStore.test.js.map +1 -0
  56. package/dist/__tests__/types/hrpc.test.d.ts +4 -0
  57. package/dist/__tests__/types/hrpc.test.js +99 -0
  58. package/dist/__tests__/types/hrpc.test.js.map +1 -0
  59. package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
  60. package/dist/__tests__/utils/balanceUtils.test.js +133 -0
  61. package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
  62. package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
  63. package/dist/__tests__/utils/errorHandling.test.js +119 -0
  64. package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
  65. package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
  66. package/dist/__tests__/utils/errorUtils.test.js +133 -0
  67. package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
  68. package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
  69. package/dist/__tests__/utils/initializationState.test.js +243 -0
  70. package/dist/__tests__/utils/initializationState.test.js.map +1 -0
  71. package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
  72. package/dist/__tests__/utils/jsonUtils.test.js +118 -0
  73. package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
  74. package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
  75. package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
  76. package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
  77. package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
  78. package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
  79. package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
  80. package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
  81. package/dist/__tests__/utils/raceConditions.test.js +239 -0
  82. package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
  83. package/dist/__tests__/utils/result.test.d.ts +4 -0
  84. package/dist/__tests__/utils/result.test.js +66 -0
  85. package/dist/__tests__/utils/result.test.js.map +1 -0
  86. package/dist/__tests__/utils/schemas.test.d.ts +4 -0
  87. package/dist/__tests__/utils/schemas.test.js +66 -0
  88. package/dist/__tests__/utils/schemas.test.js.map +1 -0
  89. package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
  90. package/dist/__tests__/utils/storeHelpers.test.js +227 -0
  91. package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
  92. package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
  93. package/dist/__tests__/utils/typeGuards.test.js +175 -0
  94. package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
  95. package/dist/__tests__/utils/validation.test.d.ts +4 -0
  96. package/dist/__tests__/utils/validation.test.js +126 -0
  97. package/dist/__tests__/utils/validation.test.js.map +1 -0
  98. package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
  99. package/dist/__tests__/utils/walletUtils.test.js +156 -0
  100. package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
  101. package/dist/hooks/useBalance.d.ts +170 -0
  102. package/dist/hooks/useBalance.js +442 -0
  103. package/dist/hooks/useBalance.js.map +1 -0
  104. package/dist/hooks/useWallet.d.ts +43 -0
  105. package/dist/hooks/useWallet.js +193 -0
  106. package/dist/hooks/useWallet.js.map +1 -0
  107. package/dist/hooks/useWalletManager.d.ts +100 -0
  108. package/dist/hooks/useWalletManager.js +526 -0
  109. package/dist/hooks/useWalletManager.js.map +1 -0
  110. package/dist/hooks/useWdkApp.d.ts +66 -0
  111. package/dist/hooks/useWdkApp.js +74 -0
  112. package/dist/hooks/useWdkApp.js.map +1 -0
  113. package/dist/hooks/useWorklet.d.ts +66 -0
  114. package/dist/hooks/useWorklet.js +42 -0
  115. package/dist/hooks/useWorklet.js.map +1 -0
  116. package/dist/index.d.ts +33 -0
  117. package/dist/index.js +36 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/provider/WdkAppProvider.d.ts +112 -0
  120. package/dist/provider/WdkAppProvider.js +558 -0
  121. package/dist/provider/WdkAppProvider.js.map +1 -0
  122. package/dist/services/accountService.d.ts +52 -0
  123. package/dist/services/accountService.js +119 -0
  124. package/dist/services/accountService.js.map +1 -0
  125. package/dist/services/addressService.d.ts +22 -0
  126. package/dist/services/addressService.js +110 -0
  127. package/dist/services/addressService.js.map +1 -0
  128. package/dist/services/balanceService.d.ts +114 -0
  129. package/dist/services/balanceService.js +208 -0
  130. package/dist/services/balanceService.js.map +1 -0
  131. package/dist/services/walletSetupService.d.ts +217 -0
  132. package/dist/services/walletSetupService.js +535 -0
  133. package/dist/services/walletSetupService.js.map +1 -0
  134. package/dist/services/walletSwitchingService.d.ts +48 -0
  135. package/dist/services/walletSwitchingService.js +143 -0
  136. package/dist/services/walletSwitchingService.js.map +1 -0
  137. package/dist/services/workletLifecycleService.d.ts +117 -0
  138. package/dist/services/workletLifecycleService.js +433 -0
  139. package/dist/services/workletLifecycleService.js.map +1 -0
  140. package/dist/storage/mmkvStorage.d.ts +44 -0
  141. package/dist/storage/mmkvStorage.js +348 -0
  142. package/dist/storage/mmkvStorage.js.map +1 -0
  143. package/dist/store/walletStore.d.ts +130 -0
  144. package/dist/store/walletStore.js +204 -0
  145. package/dist/store/walletStore.js.map +1 -0
  146. package/dist/store/workletStore.d.ts +121 -0
  147. package/dist/store/workletStore.js +256 -0
  148. package/dist/store/workletStore.js.map +1 -0
  149. package/dist/types/hrpc.d.ts +60 -0
  150. package/dist/types/hrpc.js +26 -0
  151. package/dist/types/hrpc.js.map +1 -0
  152. package/dist/types.d.ts +168 -0
  153. package/dist/types.js +7 -0
  154. package/dist/types.js.map +1 -0
  155. package/dist/utils/balanceUtils.d.ts +30 -0
  156. package/dist/utils/balanceUtils.js +86 -0
  157. package/dist/utils/balanceUtils.js.map +1 -0
  158. package/dist/utils/constants.d.ts +66 -0
  159. package/dist/utils/constants.js +73 -0
  160. package/dist/utils/constants.js.map +1 -0
  161. package/dist/utils/errorHandling.d.ts +47 -0
  162. package/dist/utils/errorHandling.js +54 -0
  163. package/dist/utils/errorHandling.js.map +1 -0
  164. package/dist/utils/errorUtils.d.ts +89 -0
  165. package/dist/utils/errorUtils.js +264 -0
  166. package/dist/utils/errorUtils.js.map +1 -0
  167. package/dist/utils/initializationState.d.ts +129 -0
  168. package/dist/utils/initializationState.js +220 -0
  169. package/dist/utils/initializationState.js.map +1 -0
  170. package/dist/utils/jsonUtils.d.ts +21 -0
  171. package/dist/utils/jsonUtils.js +67 -0
  172. package/dist/utils/jsonUtils.js.map +1 -0
  173. package/dist/utils/logger.d.ts +27 -0
  174. package/dist/utils/logger.js +50 -0
  175. package/dist/utils/logger.js.map +1 -0
  176. package/dist/utils/mmkvKeyManager.d.ts +57 -0
  177. package/dist/utils/mmkvKeyManager.js +214 -0
  178. package/dist/utils/mmkvKeyManager.js.map +1 -0
  179. package/dist/utils/mnemonicUtils.d.ts +17 -0
  180. package/dist/utils/mnemonicUtils.js +28 -0
  181. package/dist/utils/mnemonicUtils.js.map +1 -0
  182. package/dist/utils/operationMutex.d.ts +69 -0
  183. package/dist/utils/operationMutex.js +150 -0
  184. package/dist/utils/operationMutex.js.map +1 -0
  185. package/dist/utils/result.d.ts +34 -0
  186. package/dist/utils/result.js +43 -0
  187. package/dist/utils/result.js.map +1 -0
  188. package/dist/utils/schemas.d.ts +365 -0
  189. package/dist/utils/schemas.js +152 -0
  190. package/dist/utils/schemas.js.map +1 -0
  191. package/dist/utils/storeHelpers.d.ts +112 -0
  192. package/dist/utils/storeHelpers.js +198 -0
  193. package/dist/utils/storeHelpers.js.map +1 -0
  194. package/dist/utils/typeGuards.d.ts +70 -0
  195. package/dist/utils/typeGuards.js +95 -0
  196. package/dist/utils/typeGuards.js.map +1 -0
  197. package/dist/utils/validation.d.ts +53 -0
  198. package/dist/utils/validation.js +152 -0
  199. package/dist/utils/validation.js.map +1 -0
  200. package/dist/utils/walletStateHelpers.d.ts +62 -0
  201. package/dist/utils/walletStateHelpers.js +81 -0
  202. package/dist/utils/walletStateHelpers.js.map +1 -0
  203. package/dist/utils/walletUtils.d.ts +38 -0
  204. package/dist/utils/walletUtils.js +87 -0
  205. package/dist/utils/walletUtils.js.map +1 -0
  206. package/package.json +79 -0
  207. package/src/__mocks__/react.ts +34 -0
  208. package/src/__mocks__/secureStorage.ts +87 -0
  209. package/src/__tests__/hooks/useBalance.test.ts +286 -0
  210. package/src/__tests__/hooks/useWallet.test.ts +122 -0
  211. package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
  212. package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
  213. package/src/__tests__/hooks/useWorklet.test.ts +195 -0
  214. package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
  215. package/src/__tests__/services/accountService.test.ts +337 -0
  216. package/src/__tests__/services/addressService.test.ts +312 -0
  217. package/src/__tests__/services/balanceService.test.ts +411 -0
  218. package/src/__tests__/services/walletSetupService.test.ts +463 -0
  219. package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
  220. package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
  221. package/src/__tests__/setup.ts +54 -0
  222. package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
  223. package/src/__tests__/store/walletStore.test.ts +101 -0
  224. package/src/__tests__/store/workletStore.test.ts +329 -0
  225. package/src/__tests__/types/hrpc.test.ts +125 -0
  226. package/src/__tests__/utils/balanceUtils.test.ts +151 -0
  227. package/src/__tests__/utils/errorHandling.test.ts +147 -0
  228. package/src/__tests__/utils/errorUtils.test.ts +170 -0
  229. package/src/__tests__/utils/initializationState.test.ts +337 -0
  230. package/src/__tests__/utils/jsonUtils.test.ts +131 -0
  231. package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
  232. package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
  233. package/src/__tests__/utils/raceConditions.test.ts +292 -0
  234. package/src/__tests__/utils/result.test.ts +73 -0
  235. package/src/__tests__/utils/schemas.test.ts +82 -0
  236. package/src/__tests__/utils/storeHelpers.test.ts +323 -0
  237. package/src/__tests__/utils/typeGuards.test.ts +209 -0
  238. package/src/__tests__/utils/validation.test.ts +152 -0
  239. package/src/__tests__/utils/walletUtils.test.ts +195 -0
  240. package/src/hooks/useBalance.ts +597 -0
  241. package/src/hooks/useWallet.ts +288 -0
  242. package/src/hooks/useWalletManager.ts +654 -0
  243. package/src/hooks/useWdkApp.ts +78 -0
  244. package/src/hooks/useWorklet.ts +109 -0
  245. package/src/index.ts +112 -0
  246. package/src/provider/WdkAppProvider.tsx +764 -0
  247. package/src/services/accountService.ts +134 -0
  248. package/src/services/addressService.ts +125 -0
  249. package/src/services/balanceService.ts +291 -0
  250. package/src/services/walletSetupService.ts +695 -0
  251. package/src/services/walletSwitchingService.ts +157 -0
  252. package/src/services/workletLifecycleService.ts +561 -0
  253. package/src/storage/mmkvStorage.ts +417 -0
  254. package/src/store/walletStore.ts +301 -0
  255. package/src/store/workletStore.ts +340 -0
  256. package/src/types/hrpc.ts +78 -0
  257. package/src/types.ts +188 -0
  258. package/src/utils/balanceUtils.ts +96 -0
  259. package/src/utils/constants.ts +84 -0
  260. package/src/utils/errorHandling.md +163 -0
  261. package/src/utils/errorHandling.ts +61 -0
  262. package/src/utils/errorUtils.ts +300 -0
  263. package/src/utils/initializationState.ts +242 -0
  264. package/src/utils/jsonUtils.ts +75 -0
  265. package/src/utils/logger.ts +54 -0
  266. package/src/utils/mmkvKeyManager.ts +256 -0
  267. package/src/utils/mnemonicUtils.ts +35 -0
  268. package/src/utils/operationMutex.ts +181 -0
  269. package/src/utils/result.ts +57 -0
  270. package/src/utils/schemas.ts +190 -0
  271. package/src/utils/storeHelpers.ts +236 -0
  272. package/src/utils/typeGuards.ts +129 -0
  273. package/src/utils/validation.ts +174 -0
  274. package/src/utils/walletStateHelpers.ts +104 -0
  275. package/src/utils/walletUtils.ts +107 -0
@@ -0,0 +1,271 @@
1
+ /**
2
+ * Tests for useWalletManager hook
3
+ *
4
+ * Tests wallet lifecycle management hook
5
+ */
6
+
7
+ import { WalletSetupService } from '../../services/walletSetupService'
8
+ import { getWalletStore } from '../../store/walletStore'
9
+ import type { NetworkConfigs } from '../../types'
10
+
11
+ // Mock stores and services
12
+ jest.mock('../../store/walletStore', () => ({
13
+ getWalletStore: jest.fn(),
14
+ }))
15
+
16
+ jest.mock('../../services/walletSetupService', () => ({
17
+ WalletSetupService: {
18
+ initializeWallet: jest.fn(),
19
+ hasWallet: jest.fn(),
20
+ initializeFromMnemonic: jest.fn(),
21
+ deleteWallet: jest.fn(),
22
+ getMnemonic: jest.fn(),
23
+ createNewWallet: jest.fn(),
24
+ },
25
+ }))
26
+
27
+ jest.mock('../../utils/logger', () => ({
28
+ log: jest.fn(),
29
+ logError: jest.fn(),
30
+ }))
31
+
32
+ // Mock React hooks
33
+ jest.mock('react', () => ({
34
+ useCallback: jest.fn((fn) => fn),
35
+ useMemo: jest.fn((fn) => fn()),
36
+ useState: jest.fn((initial) => [initial, jest.fn()]),
37
+ }))
38
+
39
+ jest.mock('zustand/react/shallow', () => ({
40
+ useShallow: jest.fn((selector) => selector),
41
+ }))
42
+
43
+ describe('useWalletManager', () => {
44
+ let mockWalletStore: any
45
+ const mockNetworkConfigs: NetworkConfigs = {
46
+ ethereum: {
47
+ chainId: 1,
48
+ blockchain: 'ethereum',
49
+ },
50
+ }
51
+
52
+ beforeEach(() => {
53
+ jest.clearAllMocks()
54
+
55
+ mockWalletStore = {
56
+ getState: jest.fn(() => ({
57
+ walletList: [],
58
+ activeWalletId: null,
59
+ })),
60
+ setState: jest.fn(),
61
+ }
62
+ ;(getWalletStore as jest.Mock).mockReturnValue(mockWalletStore)
63
+ })
64
+
65
+ describe('initializeWallet', () => {
66
+ it('should call WalletSetupService.initializeWallet', async () => {
67
+ ;(WalletSetupService.initializeWallet as jest.Mock).mockResolvedValue(
68
+ undefined,
69
+ )
70
+
71
+ const { useWalletManager } = await import('../../hooks/useWalletManager')
72
+
73
+ // Since we can't easily test React hooks in Node, we verify the service calls
74
+ expect(WalletSetupService.initializeWallet).toBeDefined()
75
+ expect(typeof WalletSetupService.initializeWallet).toBe('function')
76
+ })
77
+
78
+ it('should handle initialization errors', async () => {
79
+ const error = new Error('Initialization failed')
80
+ ;(WalletSetupService.initializeWallet as jest.Mock).mockRejectedValue(
81
+ error,
82
+ )
83
+
84
+ expect(WalletSetupService.initializeWallet).toBeDefined()
85
+ await expect(
86
+ WalletSetupService.initializeWallet(mockNetworkConfigs, {}),
87
+ ).rejects.toThrow('Initialization failed')
88
+ })
89
+ })
90
+
91
+ describe('initializeFromMnemonic', () => {
92
+ it('should call WalletSetupService.initializeFromMnemonic', async () => {
93
+ const mnemonic =
94
+ 'word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12'
95
+ ;(
96
+ WalletSetupService.initializeFromMnemonic as jest.Mock
97
+ ).mockResolvedValue(undefined)
98
+
99
+ expect(WalletSetupService.initializeFromMnemonic).toBeDefined()
100
+ await WalletSetupService.initializeFromMnemonic(
101
+ mockNetworkConfigs,
102
+ mnemonic,
103
+ 'test-wallet',
104
+ )
105
+ expect(WalletSetupService.initializeFromMnemonic).toHaveBeenCalledWith(
106
+ mockNetworkConfigs,
107
+ mnemonic,
108
+ 'test-wallet',
109
+ )
110
+ })
111
+
112
+ it('should handle mnemonic import errors', async () => {
113
+ const error = new Error('Invalid mnemonic')
114
+ ;(
115
+ WalletSetupService.initializeFromMnemonic as jest.Mock
116
+ ).mockRejectedValue(error)
117
+
118
+ await expect(
119
+ WalletSetupService.initializeFromMnemonic(
120
+ mockNetworkConfigs,
121
+ 'invalid',
122
+ 'test-wallet',
123
+ ),
124
+ ).rejects.toThrow('Invalid mnemonic')
125
+ })
126
+ })
127
+
128
+ describe('hasWallet', () => {
129
+ it('should check if wallet exists', async () => {
130
+ ;(WalletSetupService.hasWallet as jest.Mock).mockResolvedValue(true)
131
+
132
+ const result = await WalletSetupService.hasWallet('test-wallet')
133
+ expect(result).toBe(true)
134
+ expect(WalletSetupService.hasWallet).toHaveBeenCalledWith('test-wallet')
135
+ })
136
+
137
+ it('should return false if wallet does not exist', async () => {
138
+ ;(WalletSetupService.hasWallet as jest.Mock).mockResolvedValue(false)
139
+
140
+ const result = await WalletSetupService.hasWallet('non-existent')
141
+ expect(result).toBe(false)
142
+ })
143
+ })
144
+
145
+ describe('deleteWallet', () => {
146
+ it('should delete wallet and update store', async () => {
147
+ ;(WalletSetupService.deleteWallet as jest.Mock).mockResolvedValue(
148
+ undefined,
149
+ )
150
+ mockWalletStore.getState.mockReturnValue({
151
+ walletList: [
152
+ { identifier: 'test-wallet', exists: true, isActive: true },
153
+ ],
154
+ activeWalletId: 'test-wallet',
155
+ })
156
+
157
+ await WalletSetupService.deleteWallet('test-wallet')
158
+ expect(WalletSetupService.deleteWallet).toHaveBeenCalledWith(
159
+ 'test-wallet',
160
+ )
161
+ })
162
+
163
+ it('should handle delete errors', async () => {
164
+ const error = new Error('Delete failed')
165
+ ;(WalletSetupService.deleteWallet as jest.Mock).mockRejectedValue(error)
166
+
167
+ await expect(
168
+ WalletSetupService.deleteWallet('test-wallet'),
169
+ ).rejects.toThrow('Delete failed')
170
+ })
171
+ })
172
+
173
+ describe('getMnemonic', () => {
174
+ it('should retrieve mnemonic phrase', async () => {
175
+ const mnemonic =
176
+ 'word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12'
177
+ ;(WalletSetupService.getMnemonic as jest.Mock).mockResolvedValue(mnemonic)
178
+
179
+ const result = await WalletSetupService.getMnemonic('test-wallet')
180
+ expect(result).toBe(mnemonic)
181
+ expect(WalletSetupService.getMnemonic).toHaveBeenCalledWith('test-wallet')
182
+ })
183
+
184
+ it('should return null if mnemonic not found', async () => {
185
+ ;(WalletSetupService.getMnemonic as jest.Mock).mockResolvedValue(null)
186
+
187
+ const result = await WalletSetupService.getMnemonic('non-existent')
188
+ expect(result).toBeNull()
189
+ })
190
+
191
+ it('should handle get mnemonic errors', async () => {
192
+ const error = new Error('Failed to get mnemonic')
193
+ ;(WalletSetupService.getMnemonic as jest.Mock).mockRejectedValue(error)
194
+
195
+ await expect(
196
+ WalletSetupService.getMnemonic('test-wallet'),
197
+ ).rejects.toThrow('Failed to get mnemonic')
198
+ })
199
+ })
200
+
201
+ describe('createWallet', () => {
202
+ it('should create new wallet', async () => {
203
+ ;(WalletSetupService.hasWallet as jest.Mock).mockResolvedValue(false)
204
+ ;(WalletSetupService.createNewWallet as jest.Mock).mockResolvedValue(
205
+ undefined,
206
+ )
207
+
208
+ await WalletSetupService.createNewWallet(mockNetworkConfigs, 'new-wallet')
209
+ expect(WalletSetupService.createNewWallet).toHaveBeenCalledWith(
210
+ mockNetworkConfigs,
211
+ 'new-wallet',
212
+ )
213
+ })
214
+
215
+ it('should throw error if wallet already exists', async () => {
216
+ ;(WalletSetupService.hasWallet as jest.Mock).mockResolvedValue(true)
217
+
218
+ // This would be handled in the hook, but we verify the service
219
+ expect(WalletSetupService.hasWallet).toBeDefined()
220
+ })
221
+ })
222
+
223
+ describe('refreshWalletList', () => {
224
+ it('should refresh wallet list with known identifiers', async () => {
225
+ ;(WalletSetupService.hasWallet as jest.Mock).mockImplementation((id) => {
226
+ return Promise.resolve(id === 'wallet-1' || id === 'wallet-2')
227
+ })
228
+
229
+ mockWalletStore.getState.mockReturnValue({
230
+ walletList: [],
231
+ activeWalletId: 'wallet-1',
232
+ })
233
+
234
+ // Verify hasWallet can be called for multiple wallets
235
+ const wallet1Exists = await WalletSetupService.hasWallet('wallet-1')
236
+ const wallet2Exists = await WalletSetupService.hasWallet('wallet-2')
237
+ const wallet3Exists = await WalletSetupService.hasWallet('wallet-3')
238
+
239
+ expect(wallet1Exists).toBe(true)
240
+ expect(wallet2Exists).toBe(true)
241
+ expect(wallet3Exists).toBe(false)
242
+ })
243
+
244
+ it('should handle refresh errors', async () => {
245
+ const error = new Error('Refresh failed')
246
+ ;(WalletSetupService.hasWallet as jest.Mock).mockRejectedValue(error)
247
+
248
+ await expect(WalletSetupService.hasWallet('test-wallet')).rejects.toThrow(
249
+ 'Refresh failed',
250
+ )
251
+ })
252
+ })
253
+
254
+ describe('wallet list state', () => {
255
+ it('should return wallet list from store', () => {
256
+ const walletList = [
257
+ { identifier: 'wallet-1', exists: true, isActive: true },
258
+ { identifier: 'wallet-2', exists: true, isActive: false },
259
+ ]
260
+
261
+ mockWalletStore.getState.mockReturnValue({
262
+ walletList,
263
+ activeWalletId: 'wallet-1',
264
+ })
265
+
266
+ const state = mockWalletStore.getState()
267
+ expect(state.walletList).toEqual(walletList)
268
+ expect(state.activeWalletId).toBe('wallet-1')
269
+ })
270
+ })
271
+ })
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Tests for useWdkApp hook
3
+ *
4
+ * Tests hook logic without DOM rendering
5
+ */
6
+
7
+ import type { WdkAppContextValue } from '../../provider/WdkAppProvider'
8
+ import { AppStatus, InitializationStatus } from '../../utils/initializationState'
9
+
10
+ describe('useWdkApp', () => {
11
+ it('should have correct error message when context is null', () => {
12
+ const errorMessage = 'useWdkApp must be used within WdkAppProvider'
13
+ expect(errorMessage).toBe('useWdkApp must be used within WdkAppProvider')
14
+ })
15
+
16
+ it('should validate context value structure', () => {
17
+ const mockContextValue: WdkAppContextValue = {
18
+ status: AppStatus.READY,
19
+ workletStatus: InitializationStatus.WORKLET_READY,
20
+ workletState: {
21
+ isReady: true,
22
+ isLoading: false,
23
+ error: null,
24
+ },
25
+ walletState: {
26
+ status: 'ready',
27
+ identifier: 'test-wallet',
28
+ error: null,
29
+ },
30
+ isInitializing: false,
31
+ isReady: true,
32
+ activeWalletId: 'test-wallet',
33
+ loadingWalletId: null,
34
+ walletExists: true,
35
+ error: null,
36
+ retry: jest.fn(),
37
+ }
38
+
39
+ // Validate structure
40
+ expect(mockContextValue).toHaveProperty('status')
41
+ expect(mockContextValue).toHaveProperty('workletStatus')
42
+ expect(mockContextValue).toHaveProperty('workletState')
43
+ expect(mockContextValue).toHaveProperty('walletState')
44
+ expect(mockContextValue).toHaveProperty('isInitializing')
45
+ expect(mockContextValue).toHaveProperty('isReady')
46
+ expect(mockContextValue).toHaveProperty('activeWalletId')
47
+ expect(mockContextValue).toHaveProperty('loadingWalletId')
48
+ expect(mockContextValue).toHaveProperty('walletExists')
49
+ expect(mockContextValue).toHaveProperty('error')
50
+ expect(mockContextValue).toHaveProperty('retry')
51
+ expect(typeof mockContextValue.retry).toBe('function')
52
+ expect(mockContextValue.status).toBe(AppStatus.READY)
53
+ expect(mockContextValue.workletStatus).toBe(InitializationStatus.WORKLET_READY)
54
+ expect(mockContextValue.isReady).toBe(true)
55
+ expect(mockContextValue.workletState.isReady).toBe(true)
56
+ expect(mockContextValue.walletState.status).toBe('ready')
57
+ expect(mockContextValue.activeWalletId).toBe('test-wallet')
58
+ })
59
+ })
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Tests for useWorklet hook
3
+ *
4
+ * Tests worklet interaction hook
5
+ */
6
+
7
+ import { WorkletLifecycleService } from '../../services/workletLifecycleService'
8
+ import { getWorkletStore } from '../../store/workletStore'
9
+
10
+ // Mock stores and services
11
+ jest.mock('../../store/workletStore', () => ({
12
+ getWorkletStore: jest.fn(),
13
+ }))
14
+
15
+ jest.mock('../../services/workletLifecycleService', () => ({
16
+ WorkletLifecycleService: {
17
+ initializeWDK: jest.fn(),
18
+ generateEntropyAndEncrypt: jest.fn(),
19
+ getMnemonicFromEntropy: jest.fn(),
20
+ getSeedAndEntropyFromMnemonic: jest.fn(),
21
+ initializeWorklet: jest.fn(),
22
+ reset: jest.fn(),
23
+ clearError: jest.fn(),
24
+ },
25
+ }))
26
+
27
+ // Mock React hooks
28
+ jest.mock('zustand/react/shallow', () => ({
29
+ useShallow: jest.fn((selector) => selector),
30
+ }))
31
+
32
+ describe('useWorklet', () => {
33
+ let mockWorkletStore: any
34
+
35
+ beforeEach(() => {
36
+ jest.clearAllMocks()
37
+
38
+ mockWorkletStore = jest.fn((selector: any) => {
39
+ const state = {
40
+ isWorkletStarted: true,
41
+ isInitialized: true,
42
+ isLoading: false,
43
+ error: null,
44
+ hrpc: null,
45
+ worklet: null,
46
+ workletStartResult: null,
47
+ wdkInitResult: null,
48
+ encryptedSeed: null,
49
+ encryptionKey: null,
50
+ networkConfigs: null,
51
+ }
52
+ return selector ? selector(state) : state
53
+ })
54
+
55
+ ;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
56
+ })
57
+
58
+ describe('state subscription', () => {
59
+ it('should subscribe to worklet store state', () => {
60
+ const store = getWorkletStore()
61
+ const state = store((s: any) => s)
62
+
63
+ expect(state).toBeDefined()
64
+ expect(state.isWorkletStarted).toBe(true)
65
+ expect(state.isInitialized).toBe(true)
66
+ expect(state.isLoading).toBe(false)
67
+ expect(state.error).toBeNull()
68
+ })
69
+
70
+ it('should return all state properties', () => {
71
+ const store = getWorkletStore()
72
+ const state = store((s: any) => ({
73
+ isWorkletStarted: s.isWorkletStarted,
74
+ isInitialized: s.isInitialized,
75
+ isLoading: s.isLoading,
76
+ error: s.error,
77
+ hrpc: s.hrpc,
78
+ worklet: s.worklet,
79
+ workletStartResult: s.workletStartResult,
80
+ wdkInitResult: s.wdkInitResult,
81
+ encryptedSeed: s.encryptedSeed,
82
+ encryptionKey: s.encryptionKey,
83
+ networkConfigs: s.networkConfigs,
84
+ }))
85
+
86
+ expect(state).toHaveProperty('isWorkletStarted')
87
+ expect(state).toHaveProperty('isInitialized')
88
+ expect(state).toHaveProperty('isLoading')
89
+ expect(state).toHaveProperty('error')
90
+ expect(state).toHaveProperty('hrpc')
91
+ expect(state).toHaveProperty('worklet')
92
+ expect(state).toHaveProperty('workletStartResult')
93
+ expect(state).toHaveProperty('wdkInitResult')
94
+ expect(state).toHaveProperty('encryptedSeed')
95
+ expect(state).toHaveProperty('encryptionKey')
96
+ expect(state).toHaveProperty('networkConfigs')
97
+ })
98
+ })
99
+
100
+ describe('service method delegation', () => {
101
+ it('should delegate initializeWDK to WorkletLifecycleService', () => {
102
+ expect(WorkletLifecycleService.initializeWDK).toBeDefined()
103
+ expect(typeof WorkletLifecycleService.initializeWDK).toBe('function')
104
+ })
105
+
106
+ it('should delegate generateEntropyAndEncrypt to WorkletLifecycleService', () => {
107
+ expect(WorkletLifecycleService.generateEntropyAndEncrypt).toBeDefined()
108
+ expect(typeof WorkletLifecycleService.generateEntropyAndEncrypt).toBe('function')
109
+ })
110
+
111
+ it('should delegate getMnemonicFromEntropy to WorkletLifecycleService', () => {
112
+ expect(WorkletLifecycleService.getMnemonicFromEntropy).toBeDefined()
113
+ expect(typeof WorkletLifecycleService.getMnemonicFromEntropy).toBe('function')
114
+ })
115
+
116
+ it('should delegate getSeedAndEntropyFromMnemonic to WorkletLifecycleService', () => {
117
+ expect(WorkletLifecycleService.getSeedAndEntropyFromMnemonic).toBeDefined()
118
+ expect(typeof WorkletLifecycleService.getSeedAndEntropyFromMnemonic).toBe('function')
119
+ })
120
+
121
+ it('should delegate initializeWorklet to WorkletLifecycleService', () => {
122
+ expect(WorkletLifecycleService.initializeWorklet).toBeDefined()
123
+ expect(typeof WorkletLifecycleService.initializeWorklet).toBe('function')
124
+ })
125
+
126
+ it('should delegate reset to WorkletLifecycleService', () => {
127
+ expect(WorkletLifecycleService.reset).toBeDefined()
128
+ expect(typeof WorkletLifecycleService.reset).toBe('function')
129
+ })
130
+
131
+ it('should delegate clearError to WorkletLifecycleService', () => {
132
+ expect(WorkletLifecycleService.clearError).toBeDefined()
133
+ expect(typeof WorkletLifecycleService.clearError).toBe('function')
134
+ })
135
+ })
136
+
137
+ describe('error state', () => {
138
+ it('should handle error state', () => {
139
+ mockWorkletStore = jest.fn((selector: any) => {
140
+ const state = {
141
+ isWorkletStarted: false,
142
+ isInitialized: false,
143
+ isLoading: false,
144
+ error: 'Worklet initialization failed',
145
+ hrpc: null,
146
+ worklet: null,
147
+ workletStartResult: null,
148
+ wdkInitResult: null,
149
+ encryptedSeed: null,
150
+ encryptionKey: null,
151
+ networkConfigs: null,
152
+ }
153
+ return selector ? selector(state) : state
154
+ })
155
+
156
+ ;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
157
+
158
+ const store = getWorkletStore()
159
+ const state = store((s: any) => s)
160
+
161
+ expect(state.error).toBe('Worklet initialization failed')
162
+ expect(state.isInitialized).toBe(false)
163
+ })
164
+ })
165
+
166
+ describe('loading state', () => {
167
+ it('should handle loading state', () => {
168
+ mockWorkletStore = jest.fn((selector: any) => {
169
+ const state = {
170
+ isWorkletStarted: false,
171
+ isInitialized: false,
172
+ isLoading: true,
173
+ error: null,
174
+ hrpc: null,
175
+ worklet: null,
176
+ workletStartResult: null,
177
+ wdkInitResult: null,
178
+ encryptedSeed: null,
179
+ encryptionKey: null,
180
+ networkConfigs: null,
181
+ }
182
+ return selector ? selector(state) : state
183
+ })
184
+
185
+ ;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
186
+
187
+ const store = getWorkletStore()
188
+ const state = store((s: any) => s)
189
+
190
+ expect(state.isLoading).toBe(true)
191
+ expect(state.isInitialized).toBe(false)
192
+ })
193
+ })
194
+ })
195
+
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Tests for WdkAppProvider
3
+ *
4
+ * Tests validation logic without rendering DOM components
5
+ */
6
+
7
+ import { validateNetworkConfigs, validateTokenConfigs, validateBalanceRefreshInterval } from '../../utils/validation'
8
+ import type { NetworkConfigs, TokenConfigs } from '../../types'
9
+ import { mockSecureStorage } from '../../__mocks__/secureStorage'
10
+
11
+ describe('WdkAppProvider validation', () => {
12
+ const mockNetworkConfigs: NetworkConfigs = {
13
+ ethereum: {
14
+ chainId: 1,
15
+ blockchain: 'ethereum',
16
+ },
17
+ }
18
+
19
+ const mockTokenConfigs: TokenConfigs = {
20
+ ethereum: {
21
+ native: {
22
+ symbol: 'ETH',
23
+ name: 'Ethereum',
24
+ decimals: 18,
25
+ address: null,
26
+ },
27
+ tokens: [],
28
+ },
29
+ }
30
+
31
+ it('should validate networkConfigs', () => {
32
+ expect(() => validateNetworkConfigs(mockNetworkConfigs)).not.toThrow()
33
+ expect(() => validateNetworkConfigs({} as NetworkConfigs)).toThrow()
34
+ })
35
+
36
+ it('should validate tokenConfigs', () => {
37
+ expect(() => validateTokenConfigs(mockTokenConfigs)).not.toThrow()
38
+ expect(() => validateTokenConfigs({} as TokenConfigs)).toThrow()
39
+ })
40
+
41
+ it('should validate balanceRefreshInterval', () => {
42
+ expect(() => validateBalanceRefreshInterval(30000)).not.toThrow()
43
+ expect(() => validateBalanceRefreshInterval(0)).not.toThrow()
44
+ expect(() => validateBalanceRefreshInterval(-1)).toThrow()
45
+ expect(() => validateBalanceRefreshInterval(NaN)).toThrow()
46
+ })
47
+
48
+ it('should validate secureStorage has required methods', () => {
49
+ const requiredMethods = ['authenticate', 'hasWallet', 'setEncryptionKey', 'setEncryptedSeed', 'getAllEncrypted']
50
+
51
+ for (const method of requiredMethods) {
52
+ expect(typeof mockSecureStorage[method as keyof typeof mockSecureStorage]).toBe('function')
53
+ }
54
+ })
55
+
56
+ it('should detect missing secureStorage methods', () => {
57
+ const invalidStorage = {
58
+ authenticate: jest.fn(),
59
+ // Missing other methods
60
+ }
61
+
62
+ const requiredMethods = ['hasWallet', 'setEncryptionKey', 'setEncryptedSeed', 'getAllEncrypted']
63
+ for (const method of requiredMethods) {
64
+ expect(typeof (invalidStorage as any)[method]).not.toBe('function')
65
+ }
66
+ })
67
+ })