1
1
import type { Network } from '@ethersproject/providers' ;
2
- import type { AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller' ;
2
+ import type {
3
+ AccountsControllerGetAccountAction ,
4
+ AccountsControllerGetSelectedAccountAction ,
5
+ AccountsControllerSelectedAccountChangeEvent ,
6
+ } from '@metamask/accounts-controller' ;
3
7
import type { ApprovalControllerMessenger } from '@metamask/approval-controller' ;
4
8
import { ApprovalController } from '@metamask/approval-controller' ;
5
9
import { ControllerMessenger } from '@metamask/base-controller' ;
@@ -146,6 +150,10 @@ jest.mock('uuid', () => {
146
150
* @param args.mockNetworkClientConfigurationsByNetworkClientId - Used to construct
147
151
* mock versions of network clients and ultimately mock the
148
152
* `NetworkController:getNetworkClientById` action.
153
+ * @param args.getAccount - Used to construct mock versions of the
154
+ * `AccountsController:getAccount` action.
155
+ * @param args.getSelectedAccount - Used to construct mock versions of the
156
+ * `AccountsController:getSelectedAccount` action.
149
157
* @param args.defaultSelectedAccount - The default selected account to use in
150
158
* @returns A collection of test controllers and mocks.
151
159
*/
@@ -157,6 +165,8 @@ function setupController({
157
165
getERC721OwnerOf,
158
166
getERC1155BalanceOf,
159
167
getERC1155TokenURI,
168
+ getAccount,
169
+ getSelectedAccount,
160
170
mockNetworkClientConfigurationsByNetworkClientId = { } ,
161
171
defaultSelectedAccount = OWNER_ACCOUNT ,
162
172
} : {
@@ -185,6 +195,14 @@ function setupController({
185
195
ReturnType < AssetsContractControllerGetERC1155TokenURIAction [ 'handler' ] > ,
186
196
Parameters < AssetsContractControllerGetERC1155TokenURIAction [ 'handler' ] >
187
197
> ;
198
+ getAccount ?: jest . Mock <
199
+ ReturnType < AccountsControllerGetAccountAction [ 'handler' ] > ,
200
+ Parameters < AccountsControllerGetAccountAction [ 'handler' ] > | [ null ]
201
+ > ;
202
+ getSelectedAccount ?: jest . Mock <
203
+ ReturnType < AccountsControllerGetSelectedAccountAction [ 'handler' ] > ,
204
+ Parameters < AccountsControllerGetSelectedAccountAction [ 'handler' ] >
205
+ > ;
188
206
mockNetworkClientConfigurationsByNetworkClientId ?: Record <
189
207
NetworkClientId ,
190
208
NetworkClientConfiguration
@@ -209,19 +227,15 @@ function setupController({
209
227
getNetworkClientById ,
210
228
) ;
211
229
212
- const mockGetAccount = jest
213
- . fn ( )
214
- . mockReturnValue ( defaultSelectedAccount ?? OWNER_ACCOUNT ) ;
215
-
230
+ const mockGetAccount =
231
+ getAccount ?? jest . fn ( ) . mockReturnValue ( defaultSelectedAccount ) ;
216
232
messenger . registerActionHandler (
217
233
'AccountsController:getAccount' ,
218
234
mockGetAccount ,
219
235
) ;
220
236
221
- const mockGetSelectedAccount = jest
222
- . fn ( )
223
- . mockReturnValue ( defaultSelectedAccount ?? OWNER_ACCOUNT ) ;
224
-
237
+ const mockGetSelectedAccount =
238
+ getSelectedAccount ?? jest . fn ( ) . mockReturnValue ( defaultSelectedAccount ) ;
225
239
messenger . registerActionHandler (
226
240
'AccountsController:getSelectedAccount' ,
227
241
mockGetSelectedAccount ,
@@ -637,10 +651,13 @@ describe('NftController', () => {
637
651
triggerPreferencesStateChange,
638
652
triggerSelectedAccountChange,
639
653
} = setupController ( {
654
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
655
+ getERC721OwnerOf : jest . fn ( ) . mockResolvedValue ( OWNER_ADDRESS ) ,
640
656
getERC721TokenURI : jest
641
657
. fn ( )
642
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
643
- getERC721OwnerOf : jest . fn ( ) . mockImplementation ( ( ) => OWNER_ADDRESS ) ,
658
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
659
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
660
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
644
661
} ) ;
645
662
triggerSelectedAccountChange ( OWNER_ACCOUNT ) ;
646
663
triggerPreferencesStateChange ( {
@@ -716,10 +733,13 @@ describe('NftController', () => {
716
733
triggerPreferencesStateChange,
717
734
triggerSelectedAccountChange,
718
735
} = setupController ( {
736
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
737
+ getERC721OwnerOf : jest . fn ( ) . mockResolvedValue ( OWNER_ADDRESS ) ,
719
738
getERC721TokenURI : jest
720
739
. fn ( )
721
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
722
- getERC721OwnerOf : jest . fn ( ) . mockImplementation ( ( ) => OWNER_ADDRESS ) ,
740
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
741
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
742
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
723
743
} ) ;
724
744
triggerSelectedAccountChange ( OWNER_ACCOUNT ) ;
725
745
triggerPreferencesStateChange ( {
@@ -795,10 +815,13 @@ describe('NftController', () => {
795
815
triggerPreferencesStateChange,
796
816
triggerSelectedAccountChange,
797
817
} = setupController ( {
818
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
819
+ getERC721OwnerOf : jest . fn ( ) . mockResolvedValue ( OWNER_ADDRESS ) ,
798
820
getERC721TokenURI : jest
799
821
. fn ( )
800
- . mockImplementation ( ( ) => 'ipfs://testtokenuri.com' ) ,
801
- getERC721OwnerOf : jest . fn ( ) . mockImplementation ( ( ) => OWNER_ADDRESS ) ,
822
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
823
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
824
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
802
825
} ) ;
803
826
triggerSelectedAccountChange ( OWNER_ACCOUNT ) ;
804
827
triggerPreferencesStateChange ( {
@@ -874,10 +897,13 @@ describe('NftController', () => {
874
897
triggerPreferencesStateChange,
875
898
triggerSelectedAccountChange,
876
899
} = setupController ( {
900
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
901
+ getERC721OwnerOf : jest . fn ( ) . mockResolvedValue ( OWNER_ADDRESS ) ,
877
902
getERC721TokenURI : jest
878
903
. fn ( )
879
- . mockImplementation ( ( ) => 'ipfs://testtokenuri.com' ) ,
880
- getERC721OwnerOf : jest . fn ( ) . mockImplementation ( ( ) => OWNER_ADDRESS ) ,
904
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
905
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
906
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
881
907
} ) ;
882
908
883
909
triggerSelectedAccountChange ( OWNER_ACCOUNT ) ;
@@ -955,13 +981,17 @@ describe('NftController', () => {
955
981
triggerPreferencesStateChange,
956
982
triggerSelectedAccountChange,
957
983
} = setupController ( {
984
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
985
+ getERC721OwnerOf : jest
986
+ . fn ( )
987
+ . mockRejectedValue ( new Error ( 'Not an ERC721 contract' ) ) ,
988
+ getERC1155BalanceOf : jest . fn ( ) . mockResolvedValue ( new BN ( 1 ) ) ,
958
989
getERC721TokenURI : jest
959
990
. fn ( )
960
991
. mockRejectedValue ( new Error ( 'Not an ERC721 contract' ) ) ,
961
992
getERC1155TokenURI : jest
962
993
. fn ( )
963
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
964
- getERC1155BalanceOf : jest . fn ( ) . mockImplementation ( ( ) => new BN ( 1 ) ) ,
994
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
965
995
} ) ;
966
996
967
997
triggerSelectedAccountChange ( OWNER_ACCOUNT ) ;
@@ -1042,13 +1072,17 @@ describe('NftController', () => {
1042
1072
1043
1073
const { nftController, messenger, triggerPreferencesStateChange } =
1044
1074
setupController ( {
1075
+ getAccount : jest . fn ( ) . mockReturnValue ( OWNER_ACCOUNT ) ,
1076
+ getERC721OwnerOf : jest
1077
+ . fn ( )
1078
+ . mockRejectedValue ( new Error ( 'Not an ERC721 contract' ) ) ,
1079
+ getERC1155BalanceOf : jest . fn ( ) . mockResolvedValue ( new BN ( 1 ) ) ,
1045
1080
getERC721TokenURI : jest
1046
1081
. fn ( )
1047
1082
. mockRejectedValue ( new Error ( 'Not an ERC721 contract' ) ) ,
1048
1083
getERC1155TokenURI : jest
1049
1084
. fn ( )
1050
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
1051
- getERC1155BalanceOf : jest . fn ( ) . mockImplementation ( ( ) => new BN ( 1 ) ) ,
1085
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
1052
1086
} ) ;
1053
1087
triggerPreferencesStateChange ( {
1054
1088
...getDefaultPreferencesState ( ) ,
@@ -1134,18 +1168,12 @@ describe('NftController', () => {
1134
1168
triggerPreferencesStateChange,
1135
1169
triggerSelectedAccountChange,
1136
1170
} = setupController ( {
1137
- getERC721OwnerOf : jest
1138
- . fn ( )
1139
- . mockImplementation ( ( ) => SECOND_OWNER_ADDRESS ) ,
1171
+ getERC721OwnerOf : jest . fn ( ) . mockResolvedValue ( SECOND_OWNER_ADDRESS ) ,
1140
1172
getERC721TokenURI : jest
1141
1173
. fn ( )
1142
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
1143
- getERC721AssetName : jest
1144
- . fn ( )
1145
- . mockImplementation ( ( ) => 'testERC721Name' ) ,
1146
- getERC721AssetSymbol : jest
1147
- . fn ( )
1148
- . mockImplementation ( ( ) => 'testERC721Symbol' ) ,
1174
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
1175
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
1176
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
1149
1177
} ) ;
1150
1178
1151
1179
const requestId = 'approval-request-id-1' ;
@@ -1241,13 +1269,9 @@ describe('NftController', () => {
1241
1269
getERC721OwnerOf : jest . fn ( ) . mockImplementation ( ( ) => OWNER_ADDRESS ) ,
1242
1270
getERC721TokenURI : jest
1243
1271
. fn ( )
1244
- . mockImplementation ( ( ) => 'https://testtokenuri.com' ) ,
1245
- getERC721AssetName : jest
1246
- . fn ( )
1247
- . mockImplementation ( ( ) => 'testERC721Name' ) ,
1248
- getERC721AssetSymbol : jest
1249
- . fn ( )
1250
- . mockImplementation ( ( ) => 'testERC721Symbol' ) ,
1272
+ . mockResolvedValue ( 'https://testtokenuri.com' ) ,
1273
+ getERC721AssetName : jest . fn ( ) . mockResolvedValue ( 'testERC721Name' ) ,
1274
+ getERC721AssetSymbol : jest . fn ( ) . mockResolvedValue ( 'testERC721Symbol' ) ,
1251
1275
} ) ;
1252
1276
1253
1277
const requestId = 'approval-request-id-1' ;
0 commit comments