1
1
import React from "react"
2
-
3
- import { act , render } from "@testing-library/react-native"
2
+ import { it } from "@jest/globals"
3
+ import { MockedResponse } from "@apollo/client/testing"
4
+ import { act , render , waitFor } from "@testing-library/react-native"
4
5
5
6
import { HomeScreen } from "../../app/screens/home-screen"
6
7
import { ContextForScreen } from "./helper"
8
+ import {
9
+ AccountLevel ,
10
+ HomeAuthedDocument ,
11
+ HomeUnauthedDocument ,
12
+ Network ,
13
+ } from "@app/graphql/generated"
14
+
15
+ let currentMocks : MockedResponse [ ] = [ ]
16
+
17
+ jest . mock ( "@app/utils/helper" , ( ) => ( {
18
+ ...jest . requireActual ( "@app/utils/helper" ) ,
19
+ isIos : true ,
20
+ } ) )
21
+
22
+ jest . mock ( "@app/hooks" , ( ) => {
23
+ const actual = jest . requireActual ( "@app/hooks" )
24
+
25
+ return {
26
+ ...actual ,
27
+ usePriceConversion : ( ) => ( {
28
+ convertMoneyAmount : ( { amount } : { amount : number } ) => ( {
29
+ amount,
30
+ currency : "DisplayCurrency" ,
31
+ currencyCode : "USD" ,
32
+ } ) ,
33
+ } ) ,
34
+ }
35
+ } )
36
+
37
+ jest . mock ( "@app/graphql/mocks" , ( ) => ( {
38
+ __esModule : true ,
39
+ get default ( ) {
40
+ return currentMocks
41
+ } ,
42
+ } ) )
7
43
8
44
jest . mock ( "@react-native-firebase/app-check" , ( ) => {
9
45
return ( ) => ( {
@@ -22,11 +58,238 @@ jest.mock("react-native-config", () => {
22
58
}
23
59
} )
24
60
25
- it ( "HomeAuthed" , async ( ) => {
26
- render (
27
- < ContextForScreen >
28
- < HomeScreen />
29
- </ ContextForScreen > ,
61
+ export const generateHomeMock = ( {
62
+ level,
63
+ network,
64
+ btcBalance,
65
+ usdBalance,
66
+ } : {
67
+ level : AccountLevel
68
+ network : Network
69
+ btcBalance : number
70
+ usdBalance : number
71
+ } ) : MockedResponse [ ] => {
72
+ return [
73
+ {
74
+ request : { query : HomeUnauthedDocument } ,
75
+ result : {
76
+ data : {
77
+ __typename : "Query" ,
78
+ globals : {
79
+ __typename : "Globals" ,
80
+ network,
81
+ } ,
82
+ currencyList : [ ] ,
83
+ } ,
84
+ } ,
85
+ } ,
86
+ {
87
+ request : { query : HomeAuthedDocument } ,
88
+ result : {
89
+ data : {
90
+ me : {
91
+ __typename : "User" ,
92
+ id : "user-id" ,
93
+ defaultAccount : {
94
+ __typename : "ConsumerAccount" ,
95
+ id : "account-id" ,
96
+ level,
97
+ defaultWalletId : "btc-wallet" ,
98
+ wallets : [
99
+ {
100
+ __typename : "BTCWallet" ,
101
+ id : "btc-wallet" ,
102
+ balance : btcBalance ,
103
+ walletCurrency : "BTC" ,
104
+ } ,
105
+ {
106
+ __typename : "UsdWallet" ,
107
+ id : "usd-wallet" ,
108
+ balance : usdBalance ,
109
+ walletCurrency : "USD" ,
110
+ } ,
111
+ ] ,
112
+ transactions : {
113
+ __typename : "TransactionConnection" ,
114
+ edges : [ ] ,
115
+ pageInfo : {
116
+ __typename : "PageInfo" ,
117
+ hasNextPage : false ,
118
+ hasPreviousPage : false ,
119
+ startCursor : null ,
120
+ endCursor : null ,
121
+ } ,
122
+ } ,
123
+ pendingIncomingTransactions : [ ] ,
124
+ } ,
125
+ } ,
126
+ } ,
127
+ } ,
128
+ } ,
129
+ ]
130
+ }
131
+
132
+ type ConvertButtonCase = {
133
+ description : string
134
+ isIos : boolean
135
+ level : AccountLevel
136
+ network : Network
137
+ btcBalance : number
138
+ usdBalance : number
139
+ expectConvertButton : boolean
140
+ }
141
+
142
+ const iosCases : ConvertButtonCase [ ] = [
143
+ {
144
+ description : "iOS + mainnet + ONE + no balance --> hidden" ,
145
+ isIos : true ,
146
+ level : AccountLevel . One ,
147
+ network : Network . Mainnet ,
148
+ btcBalance : 0 ,
149
+ usdBalance : 0 ,
150
+ expectConvertButton : false ,
151
+ } ,
152
+ {
153
+ description : "iOS + mainnet + ONE + has balance --> shown" ,
154
+ isIos : true ,
155
+ level : AccountLevel . One ,
156
+ network : Network . Mainnet ,
157
+ btcBalance : 1000 ,
158
+ usdBalance : 0 ,
159
+ expectConvertButton : true ,
160
+ } ,
161
+ {
162
+ description : "iOS + mainnet + TWO + no balance --> shown" ,
163
+ isIos : true ,
164
+ level : AccountLevel . Two ,
165
+ network : Network . Mainnet ,
166
+ btcBalance : 0 ,
167
+ usdBalance : 0 ,
168
+ expectConvertButton : true ,
169
+ } ,
170
+ {
171
+ description : "iOS + mainnet + THREE + no balance --> shown" ,
172
+ isIos : true ,
173
+ level : AccountLevel . Three ,
174
+ network : Network . Mainnet ,
175
+ btcBalance : 0 ,
176
+ usdBalance : 0 ,
177
+ expectConvertButton : true ,
178
+ } ,
179
+ {
180
+ description : "iOS + signet + ONE + no balance --> shown" ,
181
+ isIos : true ,
182
+ level : AccountLevel . One ,
183
+ network : Network . Signet ,
184
+ btcBalance : 0 ,
185
+ usdBalance : 0 ,
186
+ expectConvertButton : true ,
187
+ } ,
188
+ {
189
+ description : "iOS + regtest + ONE + no balance --> shown" ,
190
+ isIos : true ,
191
+ level : AccountLevel . One ,
192
+ network : Network . Regtest ,
193
+ btcBalance : 0 ,
194
+ usdBalance : 0 ,
195
+ expectConvertButton : true ,
196
+ } ,
197
+ {
198
+ description : "iOS + testnet + ONE + no balance --> shown" ,
199
+ isIos : true ,
200
+ level : AccountLevel . One ,
201
+ network : Network . Testnet ,
202
+ btcBalance : 0 ,
203
+ usdBalance : 0 ,
204
+ expectConvertButton : true ,
205
+ } ,
206
+ ]
207
+
208
+ const androidCases : ConvertButtonCase [ ] = [
209
+ {
210
+ description : "Android + signet + ONE + no balance --> shown" ,
211
+ isIos : false ,
212
+ level : AccountLevel . One ,
213
+ network : Network . Signet ,
214
+ btcBalance : 0 ,
215
+ usdBalance : 0 ,
216
+ expectConvertButton : true ,
217
+ } ,
218
+ {
219
+ description : "Android + regtest + ONE + has balance --> shown" ,
220
+ isIos : false ,
221
+ level : AccountLevel . One ,
222
+ network : Network . Regtest ,
223
+ btcBalance : 0 ,
224
+ usdBalance : 5000 ,
225
+ expectConvertButton : true ,
226
+ } ,
227
+ {
228
+ description : "Android + signet + TWO + has balance --> shown" ,
229
+ isIos : false ,
230
+ level : AccountLevel . Two ,
231
+ network : Network . Signet ,
232
+ btcBalance : 2000 ,
233
+ usdBalance : 0 ,
234
+ expectConvertButton : true ,
235
+ } ,
236
+ {
237
+ description : "Android + regtest + THREE + has balance --> shown" ,
238
+ isIos : false ,
239
+ level : AccountLevel . Three ,
240
+ network : Network . Regtest ,
241
+ btcBalance : 3000 ,
242
+ usdBalance : 3000 ,
243
+ expectConvertButton : true ,
244
+ } ,
245
+ {
246
+ description : "Android + mainnet + ONE + no balance --> shown" ,
247
+ isIos : false ,
248
+ level : AccountLevel . One ,
249
+ network : Network . Mainnet ,
250
+ btcBalance : 0 ,
251
+ usdBalance : 0 ,
252
+ expectConvertButton : true ,
253
+ } ,
254
+ ]
255
+
256
+ describe ( "HomeScreen" , ( ) => {
257
+ beforeEach ( ( ) => {
258
+ currentMocks = [ ]
259
+ jest . clearAllMocks ( )
260
+ } )
261
+
262
+ it ( "HomeAuthed" , async ( ) => {
263
+ render (
264
+ < ContextForScreen >
265
+ < HomeScreen />
266
+ </ ContextForScreen > ,
267
+ )
268
+ await act ( async ( ) => { } )
269
+ } )
270
+
271
+ it . each ( [ ...iosCases , ...androidCases ] satisfies ConvertButtonCase [ ] ) (
272
+ "%s" ,
273
+ async ( { isIos, level, network, btcBalance, usdBalance, expectConvertButton } ) => {
274
+ jest . doMock ( "@app/utils/helper" , ( ) => ( {
275
+ ...jest . requireActual ( "@app/utils/helper" ) ,
276
+ isIos,
277
+ } ) )
278
+
279
+ currentMocks = generateHomeMock ( { level, network, btcBalance, usdBalance } )
280
+
281
+ const { getByTestId } = render (
282
+ < ContextForScreen >
283
+ < HomeScreen />
284
+ </ ContextForScreen > ,
285
+ )
286
+
287
+ if ( expectConvertButton ) {
288
+ await waitFor ( ( ) => expect ( getByTestId ( "transfer" ) ) . toBeTruthy ( ) )
289
+ return
290
+ }
291
+
292
+ await waitFor ( ( ) => expect ( ( ) => getByTestId ( "transfer" ) ) . toThrow ( ) )
293
+ } ,
30
294
)
31
- await act ( async ( ) => { } )
32
295
} )
0 commit comments