1+ import { beforeEach , beforeAll , afterAll , describe , expect , it , vi } from 'vitest' ;
12import { postRequestToWallet } from './components/communication/postRequestToWallet' ;
23import { KeyManager } from './components/key/KeyManager' ;
34import { MWPClient } from './MWPClient' ;
@@ -16,30 +17,30 @@ import { ScopedAsyncStorage } from ':core/storage/ScopedAsyncStorage';
1617import { fetchRPCRequest } from ':core/util/utils' ;
1718import { Wallets } from ':core/wallet' ;
1819
19- jest . mock ( ':core/util/utils' , ( ) => {
20- const actual = jest . requireActual ( ':core/util/utils' ) ;
20+ vi . mock ( ':core/util/utils' , async ( ) => {
21+ const actual = await vi . importActual ( ':core/util/utils' ) ;
2122 return {
2223 ...actual ,
23- fetchRPCRequest : jest . fn ( ) ,
24+ fetchRPCRequest : vi . fn ( ) ,
2425 } ;
2526} ) ;
2627
27- jest . mock ( './components/communication/postRequestToWallet' ) ;
28+ vi . mock ( './components/communication/postRequestToWallet' ) ;
2829
29- jest . mock ( 'expo-web-browser' , ( ) => ( {
30- openAuthSessionAsync : jest . fn ( ) ,
31- dismissBrowser : jest . fn ( ) ,
30+ vi . mock ( 'expo-web-browser' , ( ) => ( {
31+ openAuthSessionAsync : vi . fn ( ) ,
32+ dismissBrowser : vi . fn ( ) ,
3233} ) ) ;
3334
34- jest . mock ( './components/key/KeyManager' ) ;
35- const storageStoreSpy = jest . spyOn ( ScopedAsyncStorage . prototype , 'storeObject' ) ;
36- const storageClearSpy = jest . spyOn ( ScopedAsyncStorage . prototype , 'clear' ) ;
35+ vi . mock ( './components/key/KeyManager' ) ;
36+ const storageStoreSpy = vi . spyOn ( ScopedAsyncStorage . prototype , 'storeObject' ) ;
37+ const storageClearSpy = vi . spyOn ( ScopedAsyncStorage . prototype , 'clear' ) ;
3738
38- jest . mock ( ':core/cipher/cipher' , ( ) => ( {
39- decryptContent : jest . fn ( ) ,
40- encryptContent : jest . fn ( ) ,
41- exportKeyToHexString : jest . fn ( ) ,
42- importKeyFromHexString : jest . fn ( ) ,
39+ vi . mock ( ':core/cipher/cipher' , ( ) => ( {
40+ decryptContent : vi . fn ( ) ,
41+ encryptContent : vi . fn ( ) ,
42+ exportKeyToHexString : vi . fn ( ) ,
43+ importKeyFromHexString : vi . fn ( ) ,
4344} ) ) ;
4445
4546const mockCryptoKey = { } as CryptoKey ;
@@ -64,7 +65,7 @@ const mockWallet = Wallets.CoinbaseSmartWallet;
6465describe ( 'MWPClient' , ( ) => {
6566 let client : MWPClient ;
6667 let mockMetadata : AppMetadata ;
67- let mockKeyManager : jest . Mocked < KeyManager > ;
68+ let mockKeyManager : ReturnType < typeof vi . mocked < KeyManager > > ;
6869
6970 beforeEach ( async ( ) => {
7071 mockMetadata = {
@@ -73,18 +74,18 @@ describe('MWPClient', () => {
7374 customScheme : 'myapp://' ,
7475 } ;
7576
76- ( postRequestToWallet as jest . Mock ) . mockResolvedValue ( mockSuccessResponse ) ;
77+ ( postRequestToWallet as ReturnType < typeof vi . fn > ) . mockResolvedValue ( mockSuccessResponse ) ;
7778
7879 mockKeyManager = new KeyManager ( {
7980 wallet : mockWallet ,
80- } ) as jest . Mocked < KeyManager > ;
81- ( KeyManager as jest . Mock ) . mockImplementation ( ( ) => mockKeyManager ) ;
81+ } ) as ReturnType < typeof vi . mocked < KeyManager > > ;
82+ ( KeyManager as ReturnType < typeof vi . fn > ) . mockImplementation ( ( ) => mockKeyManager ) ;
8283 storageStoreSpy . mockReset ( ) ;
8384
84- ( importKeyFromHexString as jest . Mock ) . mockResolvedValue ( mockCryptoKey ) ;
85- ( exportKeyToHexString as jest . Mock ) . mockResolvedValueOnce ( '0xPublicKey' ) ;
85+ ( importKeyFromHexString as ReturnType < typeof vi . fn > ) . mockResolvedValue ( mockCryptoKey ) ;
86+ ( exportKeyToHexString as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( '0xPublicKey' ) ;
8687 mockKeyManager . getSharedSecret . mockResolvedValue ( mockCryptoKey ) ;
87- ( encryptContent as jest . Mock ) . mockResolvedValueOnce ( encryptedData ) ;
88+ ( encryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( encryptedData ) ;
8889
8990 client = await MWPClient . createInstance ( {
9091 metadata : mockMetadata ,
@@ -106,8 +107,21 @@ describe('MWPClient', () => {
106107 } ) ;
107108
108109 describe ( 'handshake' , ( ) => {
110+ it ( 'should throw an error if failure in response.content' , async ( ) => {
111+ const mockResponse : RPCResponseMessage = {
112+ id : '1-2-3-4-5' ,
113+ requestId : '1-2-3-4-5-6' ,
114+ sender : '0xPublicKey' ,
115+ content : { failure : mockError } ,
116+ timestamp : new Date ( ) ,
117+ } ;
118+ ( postRequestToWallet as ReturnType < typeof vi . fn > ) . mockResolvedValue ( mockResponse ) ;
119+
120+ await expect ( client . handshake ( ) ) . rejects . toThrowError ( mockError ) ;
121+ } ) ;
122+
109123 it ( 'should perform a successful handshake' , async ( ) => {
110- ( decryptContent as jest . Mock ) . mockResolvedValueOnce ( {
124+ ( decryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( {
111125 result : {
112126 value : [ '0xAddress' ] ,
113127 } ,
@@ -130,26 +144,13 @@ describe('MWPClient', () => {
130144 expect ( storageStoreSpy ) . toHaveBeenCalledWith ( 'walletCapabilities' , mockCapabilities ) ;
131145 expect ( storageStoreSpy ) . toHaveBeenCalledWith ( 'accounts' , [ '0xAddress' ] ) ;
132146
133- expect ( client . request ( { method : 'eth_requestAccounts' } ) ) . resolves . toEqual ( [ '0xAddress' ] ) ;
134- } ) ;
135-
136- it ( 'should throw an error if failure in response.content' , async ( ) => {
137- const mockResponse : RPCResponseMessage = {
138- id : '1-2-3-4-5' ,
139- requestId : '1-2-3-4-5' ,
140- sender : '0xPublicKey' ,
141- content : { failure : mockError } ,
142- timestamp : new Date ( ) ,
143- } ;
144- ( postRequestToWallet as jest . Mock ) . mockResolvedValue ( mockResponse ) ;
145-
146- await expect ( client . handshake ( ) ) . rejects . toThrowError ( mockError ) ;
147+ await expect ( client . request ( { method : 'eth_requestAccounts' } ) ) . resolves . toEqual ( [ '0xAddress' ] ) ;
147148 } ) ;
148149 } ) ;
149150
150151 describe ( 'request' , ( ) => {
151152 beforeAll ( ( ) => {
152- jest . spyOn ( ScopedAsyncStorage . prototype , 'loadObject' ) . mockImplementation ( async ( key ) => {
153+ vi . spyOn ( ScopedAsyncStorage . prototype , 'loadObject' ) . mockImplementation ( async ( key ) => {
153154 switch ( key ) {
154155 case 'accounts' :
155156 return [ '0xAddress' ] ;
@@ -162,7 +163,7 @@ describe('MWPClient', () => {
162163 } ) ;
163164
164165 afterAll ( ( ) => {
165- jest . spyOn ( ScopedAsyncStorage . prototype , ' loadObject' ) . mockRestore ( ) ;
166+ vi . mocked ( ScopedAsyncStorage . prototype . loadObject ) . mockRestore ( ) ;
166167 } ) ;
167168
168169 it ( 'should perform a successful request' , async ( ) => {
@@ -171,7 +172,7 @@ describe('MWPClient', () => {
171172 params : [ '0xMessage' , '0xAddress' ] ,
172173 } ;
173174
174- ( decryptContent as jest . Mock ) . mockResolvedValueOnce ( {
175+ ( decryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( {
175176 result : {
176177 value : '0xSignature' ,
177178 } ,
@@ -212,7 +213,7 @@ describe('MWPClient', () => {
212213 params : [ ] ,
213214 } ;
214215
215- ( decryptContent as jest . Mock ) . mockResolvedValueOnce ( {
216+ ( decryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( {
216217 result : {
217218 value : '0xSignature' ,
218219 } ,
@@ -252,7 +253,7 @@ describe('MWPClient', () => {
252253 params : [ '0xMessage' , '0xAddress' ] ,
253254 } ;
254255
255- ( decryptContent as jest . Mock ) . mockResolvedValueOnce ( {
256+ ( decryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( {
256257 result : {
257258 error : mockError ,
258259 } ,
@@ -267,7 +268,7 @@ describe('MWPClient', () => {
267268 params : [ { chainId : '0x1' } ] ,
268269 } ;
269270
270- ( decryptContent as jest . Mock ) . mockResolvedValueOnce ( {
271+ ( decryptContent as ReturnType < typeof vi . fn > ) . mockResolvedValueOnce ( {
271272 result : {
272273 value : null ,
273274 } ,
0 commit comments