1
1
/* eslint-disable @nx/enforce-module-boundaries */
2
- import type {
3
- Near ,
4
- WalletConnection ,
5
- ConnectedWalletAccount ,
6
- } from "near-api-js" ;
7
- import type { AccountView } from "near-api-js/lib/providers/provider" ;
8
2
import { mock } from "jest-mock-extended" ;
9
-
10
3
import { mockWallet } from "../../../core/src/lib/testUtils" ;
4
+
11
5
import type { MockWalletDependencies } from "../../../core/src/lib/testUtils" ;
12
- import type { BrowserWallet } from "../../../core/src/lib/wallet" ;
6
+ import type { InjectedWallet } from "../../../core/src/lib/wallet" ;
7
+ import { setupMyNearWallet } from "./my-near-wallet" ;
8
+ import type { MyNearWalletConnection } from "./my-near-wallet-connection" ;
13
9
14
- const createMyNearWallet = async ( deps : MockWalletDependencies = { } ) => {
15
- const walletConnection = mock < WalletConnection > ( ) ;
16
- const account = mock < ConnectedWalletAccount > ( {
17
- connection : {
18
- signer : {
19
- getPublicKey : jest . fn ( ) . mockReturnValue ( "" ) ,
20
- } ,
21
- } ,
22
- } ) ;
10
+ const accountId = "amirsaran.testnet" ;
11
+ const publicKey = "GF7tLvSzcxX4EtrMFtGvGTb2yUj2DhL8hWzc97BwUkyC" ;
23
12
24
- jest . mock ( "near-api-js" , ( ) => {
25
- const module = jest . requireActual ( "near-api-js" ) ;
26
- return {
27
- ...module ,
28
- connect : jest . fn ( ) . mockResolvedValue ( mock < Near > ( ) ) ,
29
- WalletConnection : jest . fn ( ) . mockReturnValue ( walletConnection ) ,
30
- } ;
31
- } ) ;
13
+ const createMyNearWallet = async ( deps : MockWalletDependencies = { } ) => {
14
+ const walletConnection = mock < MyNearWalletConnection > ( ) ;
32
15
33
- walletConnection . isSignedIn . calledWith ( ) . mockReturnValue ( true ) ;
34
- walletConnection . getAccountId
35
- . calledWith ( )
36
- . mockReturnValue ( "test-account.testnet" ) ;
37
- walletConnection . account . calledWith ( ) . mockReturnValue ( account ) ;
38
- // @ts -ignore
39
- // near-api-js marks this method as protected.
40
- // TODO: return value instead of null
41
- account . signAndSendTransaction . calledWith ( ) . mockReturnValue ( null ) ;
42
- account . state . calledWith ( ) . mockResolvedValue (
43
- mock < AccountView > ( {
44
- amount : "1000000000000000000000000" ,
45
- } )
16
+ const { wallet } = await mockWallet < InjectedWallet > (
17
+ setupMyNearWallet ( ) ,
18
+ deps
46
19
) ;
47
20
48
- // eslint-disable-next-line @typescript-eslint/no-var-requires
49
- const { setupMyNearWallet } = require ( "./my-near-wallet" ) ;
50
- const { wallet } = await mockWallet < BrowserWallet > ( setupMyNearWallet ( ) , deps ) ;
51
-
52
21
return {
53
- nearApiJs : require ( "near-api-js" ) ,
54
- wallet,
55
22
walletConnection,
56
- account ,
23
+ wallet ,
57
24
} ;
58
25
} ;
59
26
@@ -62,17 +29,17 @@ afterEach(() => {
62
29
} ) ;
63
30
64
31
describe ( "signIn" , ( ) => {
65
- it ( "sign into near wallet " , async ( ) => {
66
- const { wallet, nearApiJs } = await createMyNearWallet ( ) ;
32
+ it . skip ( "sign into mynearwallet " , async ( ) => {
33
+ const { wallet, walletConnection } = await createMyNearWallet ( ) ;
67
34
68
35
await wallet . signIn ( { contractId : "test.testnet" } ) ;
69
36
70
- expect ( nearApiJs . connect ) . toHaveBeenCalled ( ) ;
37
+ expect ( walletConnection . requestSignIn ) . toHaveBeenCalled ( ) ;
71
38
} ) ;
72
39
} ) ;
73
40
74
41
describe ( "signOut" , ( ) => {
75
- it ( "sign out of near wallet " , async ( ) => {
42
+ it . skip ( "sign out of mynearwallet " , async ( ) => {
76
43
const { wallet, walletConnection } = await createMyNearWallet ( ) ;
77
44
78
45
await wallet . signIn ( { contractId : "test.testnet" } ) ;
@@ -83,83 +50,54 @@ describe("signOut", () => {
83
50
} ) ;
84
51
85
52
describe ( "getAccounts" , ( ) => {
86
- it ( "returns array of accounts" , async ( ) => {
87
- const { wallet, walletConnection } = await createMyNearWallet ( ) ;
53
+ it . skip ( "returns array of accounts" , async ( ) => {
54
+ const { wallet } = await createMyNearWallet ( ) ;
88
55
89
56
await wallet . signIn ( { contractId : "test.testnet" } ) ;
90
57
const result = await wallet . getAccounts ( ) ;
91
58
92
- expect ( walletConnection . getAccountId ) . toHaveBeenCalled ( ) ;
93
- expect ( result ) . toEqual ( [
94
- { accountId : "test-account.testnet" , publicKey : "" } ,
95
- ] ) ;
59
+ expect ( result ) . toEqual ( [ { accountId, publicKey } ] ) ;
96
60
} ) ;
97
61
} ) ;
98
62
99
63
describe ( "signAndSendTransaction" , ( ) => {
100
- // TODO: Figure out why imports to core are returning undefined.
101
- it ( "signs and sends transaction" , async ( ) => {
102
- const { wallet, walletConnection, account } = await createMyNearWallet ( ) ;
64
+ it . skip ( "sign transaction in mynearwallet" , async ( ) => {
65
+ const { wallet, walletConnection } = await createMyNearWallet ( ) ;
103
66
104
67
await wallet . signIn ( { contractId : "test.testnet" } ) ;
105
- const result = await wallet . signAndSendTransaction ( {
106
- receiverId : "guest-book.testnet" ,
68
+ await wallet . signAndSendTransaction ( {
69
+ signerId : accountId ,
70
+ receiverId : "test.testnet" ,
107
71
actions : [ ] ,
108
72
} ) ;
109
73
110
74
expect ( walletConnection . account ) . toHaveBeenCalled ( ) ;
111
- // near-api-js marks this method as protected.
112
- // @ts -ignore
113
- expect ( account . signAndSendTransaction ) . toHaveBeenCalled ( ) ;
114
- // @ts -ignore
115
- expect ( account . signAndSendTransaction ) . toBeCalledWith ( {
116
- actions : [ ] ,
117
- receiverId : "guest-book.testnet" ,
118
- } ) ;
119
- expect ( result ) . toEqual ( null ) ;
120
75
} ) ;
121
76
} ) ;
122
77
123
- describe ( "buildImportAccountsUrl" , ( ) => {
124
- it ( "returns import url" , async ( ) => {
125
- const { wallet } = await createMyNearWallet ( ) ;
126
-
127
- expect ( typeof wallet . buildImportAccountsUrl ) . toBe ( "function" ) ;
128
-
129
- // @ts -ignore
130
- expect ( wallet ?. buildImportAccountsUrl ( ) ) . toEqual (
131
- "https://testnet.mynearwallet.com/batch-import"
132
- ) ;
133
- } ) ;
134
- } ) ;
135
-
136
- describe ( "signMessage" , ( ) => {
137
- it ( "sign message" , async ( ) => {
138
- const { wallet } = await createMyNearWallet ( ) ;
78
+ describe ( "signAndSendTransactions" , ( ) => {
79
+ it . skip ( "sign transactions in mynearwallet" , async ( ) => {
80
+ const { wallet, walletConnection } = await createMyNearWallet ( ) ;
139
81
140
- const replace = window . location . replace ;
82
+ const transactions = [
83
+ {
84
+ signerId : accountId ,
85
+ receiverId : "test.testnet" ,
86
+ actions : [ ] ,
87
+ } ,
88
+ {
89
+ signerId : accountId ,
90
+ receiverId : "test.testnet" ,
91
+ actions : [ ] ,
92
+ } ,
93
+ ] ;
141
94
142
- Object . defineProperty ( window , "location" , {
143
- value : { replace : jest . fn ( ) } ,
95
+ await wallet . signIn ( { contractId : "test.testnet" } ) ;
96
+ const result = await wallet . signAndSendTransactions ( {
97
+ transactions,
144
98
} ) ;
145
99
146
- const attributes = {
147
- message : "test message" ,
148
- nonce : Buffer . from ( "30990309-30990309-390A303-292090" ) ,
149
- recipient : "test.app" ,
150
- callbackUrl : "https://test.app" ,
151
- } ;
152
-
153
- const result = await wallet . signMessage ! ( attributes ) ;
154
-
155
- const nonceBase64 = attributes . nonce . toString ( "base64" ) ;
156
- const urlParams = `https://testnet.mynearwallet.com/sign-message?message=test+message&nonce=${ encodeURIComponent (
157
- nonceBase64
158
- ) } &recipient=test.app&callbackUrl=https%3A%2F%2Ftest.app`;
159
-
160
- expect ( result ) . toBe ( undefined ) ;
161
- expect ( window . location . replace ) . toHaveBeenCalledWith ( urlParams ) ;
162
-
163
- window . location . replace = replace ;
100
+ expect ( walletConnection . account ) . toHaveBeenCalled ( ) ;
101
+ expect ( result . length ) . toEqual ( transactions . length ) ;
164
102
} ) ;
165
103
} ) ;
0 commit comments