44} from './index' ;
55import Engine from '../../core/Engine' ;
66import Logger from '../../util/Logger' ;
7- import ToastService from '../../core/ToastService' ;
87import { selectPrimaryMoneyAccount } from '../../selectors/moneyAccountController' ;
98import type { RootState } from '../../reducers' ;
109
@@ -27,25 +26,23 @@ jest.mock('../../util/Logger', () => ({
2726 } ,
2827} ) ) ;
2928
30- jest . mock ( '../../core/ToastService' , ( ) => ( {
31- __esModule : true ,
32- default : {
33- showToast : jest . fn ( ) ,
34- toastRef : { current : { } } ,
35- } ,
36- } ) ) ;
37-
3829jest . mock ( '../../selectors/moneyAccountController' , ( ) => ( {
3930 selectPrimaryMoneyAccount : jest . fn ( ) ,
4031} ) ) ;
4132
33+ jest . mock (
34+ '../../core/Engine/controllers/money-account-upgrade-controller-init' ,
35+ ( ) => ( {
36+ whenMoneyAccountUpgradeReady : jest . fn ( ( ) => Promise . resolve ( ) ) ,
37+ } ) ,
38+ ) ;
39+
4240const mockUpgradeAccount = Engine . context . MoneyAccountUpgradeController
4341 . upgradeAccount as jest . Mock ;
4442const mockSelectPrimaryMoneyAccount =
4543 selectPrimaryMoneyAccount as unknown as jest . Mock ;
4644const mockLogError = Logger . error as jest . Mock ;
4745const mockLogLog = Logger . log as jest . Mock ;
48- const mockShowToast = ToastService . showToast as jest . Mock ;
4946
5047const ADDRESS = '0x1111111111111111111111111111111111111111' as const ;
5148const OTHER_ADDRESS = '0x2222222222222222222222222222222222222222' as const ;
@@ -59,57 +56,18 @@ describe('upgradeMoneyAccount', () => {
5956 beforeEach ( ( ) => {
6057 jest . clearAllMocks ( ) ;
6158 __resetUpgradesInFlightForTesting ( ) ;
62- ( ToastService as unknown as { toastRef : unknown } ) . toastRef = {
63- current : { } ,
64- } ;
6559 dispatch = jest . fn ( ) ;
6660 getState = jest . fn ( ) . mockReturnValue ( { } as RootState ) ;
6761 mockUpgradeAccount . mockResolvedValue ( undefined ) ;
6862 } ) ;
6963
70- it ( 'calls MoneyAccountUpgradeController.upgradeAccount with the primary money account address' , ( ) => {
71- mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
72-
73- upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
74-
75- expect ( mockUpgradeAccount ) . toHaveBeenCalledWith ( ADDRESS ) ;
76- } ) ;
77-
78- it ( 'logs and shows a toast when the upgrade is fired' , ( ) => {
79- mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
80-
81- upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
82-
83- expect ( mockLogLog ) . toHaveBeenCalledWith (
84- expect . stringContaining ( 'upgradeMoneyAccount' ) ,
85- 'firing upgradeAccount' ,
86- { address : ADDRESS } ,
87- ) ;
88- expect ( mockShowToast ) . toHaveBeenCalledWith (
89- expect . objectContaining ( {
90- labelOptions : [ { label : 'Upgrading money account…' , isBold : true } ] ,
91- } ) ,
92- ) ;
93- } ) ;
94-
95- it ( 'logs and shows a success toast when upgradeAccount resolves' , async ( ) => {
64+ it ( 'calls MoneyAccountUpgradeController.upgradeAccount with the primary money account address' , async ( ) => {
9665 mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
9766
9867 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
9968 await flushPromises ( ) ;
10069
101- expect ( mockLogLog ) . toHaveBeenCalledWith (
102- expect . stringContaining ( 'upgradeMoneyAccount' ) ,
103- 'upgradeAccount resolved' ,
104- { address : ADDRESS } ,
105- ) ;
106- expect ( mockShowToast ) . toHaveBeenLastCalledWith (
107- expect . objectContaining ( {
108- labelOptions : [
109- { label : 'Money account upgrade complete' , isBold : true } ,
110- ] ,
111- } ) ,
112- ) ;
70+ expect ( mockUpgradeAccount ) . toHaveBeenCalledWith ( ADDRESS ) ;
11371 } ) ;
11472
11573 it ( 'skips the call and logs when there is no primary money account' , ( ) => {
@@ -119,7 +77,6 @@ describe('upgradeMoneyAccount', () => {
11977
12078 expect ( mockUpgradeAccount ) . not . toHaveBeenCalled ( ) ;
12179 expect ( mockLogLog ) . toHaveBeenCalled ( ) ;
122- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
12380 } ) ;
12481
12582 it ( 'skips the call when the primary money account address is not a strict hex string' , ( ) => {
@@ -135,7 +92,7 @@ describe('upgradeMoneyAccount', () => {
13592 ) ;
13693 } ) ;
13794
138- it ( 'catches rejected upgradeAccount promises, logs, and shows an error toast ' , async ( ) => {
95+ it ( 'catches rejected upgradeAccount promises and logs ' , async ( ) => {
13996 mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
14097 const error = new Error ( 'boom' ) ;
14198 mockUpgradeAccount . mockRejectedValueOnce ( error ) ;
@@ -144,15 +101,9 @@ describe('upgradeMoneyAccount', () => {
144101 await flushPromises ( ) ;
145102
146103 expect ( mockLogError ) . toHaveBeenCalledWith ( error , expect . any ( String ) ) ;
147- expect ( mockShowToast ) . toHaveBeenLastCalledWith (
148- expect . objectContaining ( {
149- labelOptions : [ { label : 'Money account upgrade failed' , isBold : true } ] ,
150- descriptionOptions : { description : 'boom' } ,
151- } ) ,
152- ) ;
153104 } ) ;
154105
155- it ( 'wraps non-Error rejections and shows a generic error toast ' , async ( ) => {
106+ it ( 'wraps non-Error rejections' , async ( ) => {
156107 mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
157108 mockUpgradeAccount . mockRejectedValueOnce ( 'string rejection' ) ;
158109
@@ -163,30 +114,15 @@ describe('upgradeMoneyAccount', () => {
163114 expect . any ( Error ) ,
164115 expect . any ( String ) ,
165116 ) ;
166- expect ( mockShowToast ) . toHaveBeenLastCalledWith (
167- expect . objectContaining ( {
168- labelOptions : [ { label : 'Money account upgrade failed' , isBold : true } ] ,
169- descriptionOptions : { description : 'Unknown error' } ,
170- } ) ,
171- ) ;
172117 } ) ;
173118
174- it ( 'skips showing toasts when the toast ref is not mounted' , ( ) => {
175- ( ToastService as unknown as { toastRef : unknown } ) . toastRef = null ;
176- mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
177-
178- upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
179-
180- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
181- expect ( mockUpgradeAccount ) . toHaveBeenCalledWith ( ADDRESS ) ;
182- } ) ;
183-
184- it ( 'deduplicates concurrent upgrades for the same address' , ( ) => {
119+ it ( 'deduplicates concurrent upgrades for the same address' , async ( ) => {
185120 mockSelectPrimaryMoneyAccount . mockReturnValue ( { address : ADDRESS } ) ;
186121 mockUpgradeAccount . mockReturnValueOnce ( new Promise ( ( ) => undefined ) ) ;
187122
188123 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
189124 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
125+ await flushPromises ( ) ;
190126
191127 expect ( mockUpgradeAccount ) . toHaveBeenCalledTimes ( 1 ) ;
192128 expect ( mockLogLog ) . toHaveBeenCalledWith (
@@ -202,11 +138,12 @@ describe('upgradeMoneyAccount', () => {
202138 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
203139 await flushPromises ( ) ;
204140 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
141+ await flushPromises ( ) ;
205142
206143 expect ( mockUpgradeAccount ) . toHaveBeenCalledTimes ( 2 ) ;
207144 } ) ;
208145
209- it ( 'does not deduplicate upgrades for different addresses' , ( ) => {
146+ it ( 'does not deduplicate upgrades for different addresses' , async ( ) => {
210147 mockUpgradeAccount . mockReturnValue ( new Promise ( ( ) => undefined ) ) ;
211148
212149 mockSelectPrimaryMoneyAccount . mockReturnValueOnce ( { address : ADDRESS } ) ;
@@ -215,6 +152,7 @@ describe('upgradeMoneyAccount', () => {
215152 address : OTHER_ADDRESS ,
216153 } ) ;
217154 upgradeMoneyAccount ( ) ( dispatch , getState , undefined ) ;
155+ await flushPromises ( ) ;
218156
219157 expect ( mockUpgradeAccount ) . toHaveBeenCalledTimes ( 2 ) ;
220158 expect ( mockUpgradeAccount ) . toHaveBeenNthCalledWith ( 1 , ADDRESS ) ;
0 commit comments