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