1
- import { useEffect , useState } from 'react' ;
2
- import { useProvider } from '@ant-design/web3' ;
3
- import { fireEvent , render } from '@testing-library/react' ;
1
+ import React from 'react' ;
2
+ import { ConnectButton , Connector } from '@ant-design/web3' ;
3
+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
4
4
import { afterAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
5
5
6
- import { TronWeb3ConfigProvider } from '../index' ;
7
- import { xrender } from './utils' ;
8
-
9
- const mockCreateConnectionInstance = vi . fn ( ) ;
6
+ import { TronWeb3ConfigProvider } from '../' ;
7
+ import { OkxTronWallet , TronlinkWallet } from '../../wallets' ;
10
8
11
9
describe ( 'TronWeb3ConfigProvider' , ( ) => {
12
- beforeEach ( ( ) => {
13
- mockCreateConnectionInstance . mockClear ( ) ;
14
-
15
- vi . resetAllMocks ( ) ;
16
- } ) ;
17
-
18
10
afterAll ( ( ) => {
11
+ vi . restoreAllMocks ( ) ;
19
12
vi . resetModules ( ) ;
20
13
} ) ;
21
14
22
- const mockedData = vi . hoisted ( ( ) => {
23
- const mockAddress = 'TUguNkmfvjeHanGyQZLGJfj28w1tMtvNDT' ;
24
- const balance = 10002 ;
25
-
26
- const mockedDisconnect = vi . fn ( ) ;
27
-
28
- return {
29
- address : {
30
- value : mockAddress ,
31
- } ,
32
- balance,
33
- mockedDisconnect,
34
- } ;
35
- } ) ;
36
-
37
- vi . mock ( '@tronweb3/tronwallet-adapter-react-hooks' , async ( ) => {
38
- const { remember } = await import ( './utils' ) ;
39
-
40
- const address = mockedData . address . value ;
41
-
42
- const ConnectionProvider : React . FC <
43
- React . PropsWithChildren < { endpoint : string ; config : any } >
44
- > = ( { children, endpoint, config = { commitment : 'confirmed' } } ) => {
45
- useEffect ( ( ) => {
46
- mockCreateConnectionInstance ( endpoint , config ?. commitment ) ;
47
- } , [ endpoint , config ] ) ;
48
-
49
- return (
50
- < div >
51
- < div className = "endpoint" > { endpoint } </ div >
52
- < div className = "commitment" > { config ?. commitment } </ div >
53
- { children }
54
- </ div >
55
- ) ;
56
- } ;
57
-
58
- const WalletProvider : React . FC < React . PropsWithChildren > = ( { children } ) => < > { children } </ > ;
59
-
60
- const connectedRef = remember ( false ) ;
61
- const currentWalletRef = remember < any > ( null ) ;
62
-
63
- return {
64
- useWallet : ( ) => {
65
- // provide a state to emit re-render
66
- const [ , setConnected ] = useState ( connectedRef . value ) ;
67
- const [ , setCurrentWallet ] = useState ( currentWalletRef . value ) ;
68
-
69
- return {
70
- address,
71
- connected : connectedRef . value ,
72
- connect : ( ) => {
73
- connectedRef . value = true ;
74
- setConnected ( true ) ;
75
- } ,
76
- select : ( _wallet : any ) => {
77
- currentWalletRef . value = _wallet ;
78
- setCurrentWallet ( _wallet ) ;
79
- } ,
80
- disconnect : ( ) => {
81
- mockedData . mockedDisconnect ( ) ;
82
- } ,
83
- wallet : currentWalletRef . value ,
84
- } ;
85
- } ,
86
- ConnectionProvider,
87
- WalletProvider,
88
- } ;
89
- } ) ;
90
-
91
15
it ( 'mount correctly' , ( ) => {
92
16
const App = ( ) => (
93
- < TronWeb3ConfigProvider >
17
+ < TronWeb3ConfigProvider wallets = { [ TronlinkWallet ] } >
94
18
< div className = "content" > test</ div >
95
19
</ TronWeb3ConfigProvider >
96
20
) ;
@@ -99,80 +23,95 @@ describe('TronWeb3ConfigProvider', () => {
99
23
expect ( baseElement . querySelector ( '.content' ) ?. textContent ) . toBe ( 'test' ) ;
100
24
} ) ;
101
25
102
- it ( 'available show account address' , async ( ) => {
103
- const { useWallet } = await import ( '@tronweb3/tronwallet-adapter-react-hooks' ) ;
104
- const connectRunned = vi . fn ( ) ;
105
-
106
- const Address : React . FC = ( ) => {
107
- const { account } = useProvider ( ) ;
108
- return < div className = "address" > { account ?. address } </ div > ;
26
+ it ( 'show address' , async ( ) => {
27
+ const Address = ( ) => {
28
+ return < div className = "address" > TUguNkmfvjeHanGyQZLGJfj28w1tMtvNDT</ div > ;
109
29
} ;
110
30
111
31
const App = ( ) => {
112
- const { connect } = useWallet ( ) ;
113
32
return (
114
- < TronWeb3ConfigProvider >
115
- < div >
116
- < div className = "content" > test</ div >
117
- < button
118
- className = "connect"
119
- onClick = { async ( ) => {
120
- await connect ( ) ;
121
- connectRunned ( ) ;
122
- } }
123
- >
124
- Connect
125
- </ button >
126
- < Address />
127
- </ div >
33
+ < TronWeb3ConfigProvider wallets = { [ TronlinkWallet ] } >
34
+ < Connector >
35
+ < ConnectButton className = "connect" />
36
+ </ Connector >
37
+ < Address />
128
38
</ TronWeb3ConfigProvider >
129
39
) ;
130
40
} ;
131
41
132
- const { selector } = xrender ( App ) ;
133
- expect ( selector ( '.content' ) ?. textContent ) . toBe ( 'test' ) ;
134
-
135
- const connectBtn = selector ( '.connect' ) ! ;
136
- const address = selector ( '.address' ) ;
137
-
138
- expect ( connectBtn ) . not . toBeNull ( ) ;
139
-
140
- // default address is empty
141
- expect ( address ?. textContent ) . toBe ( '' ) ;
142
-
143
- fireEvent . click ( connectBtn ) ;
42
+ const { baseElement } = render ( < App /> ) ;
43
+ const modalBtn = baseElement . querySelector ( '.connect' ) as HTMLButtonElement ;
44
+ fireEvent . click ( modalBtn ) ;
45
+ await vi . waitFor ( ( ) => {
46
+ const connectBtn = baseElement . querySelector ( '.ant-list-item' ) ! ;
47
+ fireEvent . click ( connectBtn ) ;
48
+ expect ( connectBtn ) . toBeTruthy ( ) ;
49
+ } ) ;
144
50
145
51
await vi . waitFor ( ( ) => {
146
- expect ( connectRunned ) . toBeCalled ( ) ;
147
- expect ( address ?. textContent ) . toBe ( mockedData . address . value ) ;
52
+ expect ( baseElement . querySelector ( '.address' ) ?. textContent ) . toBe (
53
+ 'TUguNkmfvjeHanGyQZLGJfj28w1tMtvNDT' ,
54
+ ) ;
148
55
} ) ;
149
56
} ) ;
150
57
151
- it ( 'available disconnect' , ( ) => {
152
- const CustomConnector : React . FC = ( ) => {
153
- const { disconnect } = useProvider ( ) ;
58
+ it ( 'disconnect' , async ( ) => {
59
+ const App = ( ) => {
154
60
return (
155
- < div >
156
- < button onClick = { async ( ) => await disconnect ?.( ) } > Disconnect</ button >
157
- </ div >
61
+ < TronWeb3ConfigProvider wallets = { [ TronlinkWallet ] } >
62
+ < Connector >
63
+ < ConnectButton className = "connect" />
64
+ </ Connector >
65
+ </ TronWeb3ConfigProvider >
158
66
) ;
159
67
} ;
160
68
161
- const App : React . FC = ( ) => {
69
+ const { baseElement } = render ( < App /> ) ;
70
+ const modalBtn = baseElement . querySelector ( '.connect' ) as HTMLButtonElement ;
71
+ fireEvent . click ( modalBtn ) ;
72
+ await waitFor ( ( ) => {
73
+ const connectBtn = baseElement . querySelector ( '.ant-list-item' ) ! ;
74
+ expect ( connectBtn ) . toBeTruthy ( ) ;
75
+ fireEvent . click ( connectBtn ) ;
76
+ } ) ;
77
+
78
+ await waitFor ( ( ) => {
79
+ expect ( baseElement . querySelector ( '.connect' ) ?. textContent ) ;
80
+ } ) ;
81
+
82
+ fireEvent . click ( modalBtn ) ;
83
+
84
+ await waitFor ( ( ) => {
85
+ const disconnect = screen . getByText ( 'Disconnect' ) ;
86
+ expect ( disconnect ) . toBeTruthy ( ) ;
87
+ fireEvent . click ( screen . getByText ( 'Disconnect' ) ) ;
88
+ } ) ;
89
+ expect ( baseElement . querySelector ( '.connect' ) ?. textContent ) . toBe ( 'Connect Wallet' ) ;
90
+ } ) ;
91
+
92
+ it ( 'show icon' , async ( ) => {
93
+ const App = ( ) => {
162
94
return (
163
- < TronWeb3ConfigProvider >
164
- < CustomConnector />
95
+ < TronWeb3ConfigProvider wallets = { [ TronlinkWallet , OkxTronWallet ] } >
96
+ < Connector >
97
+ < ConnectButton className = "connect" />
98
+ </ Connector >
165
99
</ TronWeb3ConfigProvider >
166
100
) ;
167
101
} ;
168
102
169
- const { selector } = xrender ( App ) ;
170
-
171
- const btn = selector ( 'button' ) ! ;
172
-
173
- expect ( btn ?. textContent ) . toBe ( 'Disconnect' ) ;
174
- fireEvent . click ( btn ) ;
175
-
176
- expect ( mockedData . mockedDisconnect ) . toBeCalled ( ) ;
103
+ const { baseElement } = render ( < App /> ) ;
104
+ const modalBtn = baseElement . querySelector ( '.connect' ) as HTMLButtonElement ;
105
+ fireEvent . click ( modalBtn ) ;
106
+ await waitFor ( ( ) => {
107
+ const icons = baseElement . querySelector ( '.ant-web3-icon-okx-wallet-colorful' ) ! ;
108
+ expect ( icons ) . toBeTruthy ( ) ;
109
+ } ) ;
110
+ await waitFor ( ( ) => {
111
+ const items = baseElement . querySelectorAll ( '.ant-list-item' ) ! ;
112
+ expect ( items . length ) . toBe ( 2 ) ;
113
+ const item = baseElement . querySelector ( '.ant-web3-connect-modal-img' ) ! ;
114
+ expect ( item . getAttribute ( 'src' ) ) . toBeTruthy ( ) ;
115
+ } ) ;
177
116
} ) ;
178
117
} ) ;
0 commit comments