Skip to content

Commit 1b2d591

Browse files
committed
chore: update packages and remove redundant code
1 parent cffb78b commit 1b2d591

8 files changed

Lines changed: 54 additions & 429 deletions

File tree

app/actions/money/index.test.ts

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from './index';
55
import Engine from '../../core/Engine';
66
import Logger from '../../util/Logger';
7-
import ToastService from '../../core/ToastService';
87
import { selectPrimaryMoneyAccount } from '../../selectors/moneyAccountController';
98
import 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-
3829
jest.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+
4240
const mockUpgradeAccount = Engine.context.MoneyAccountUpgradeController
4341
.upgradeAccount as jest.Mock;
4442
const mockSelectPrimaryMoneyAccount =
4543
selectPrimaryMoneyAccount as unknown as jest.Mock;
4644
const mockLogError = Logger.error as jest.Mock;
4745
const mockLogLog = Logger.log as jest.Mock;
48-
const mockShowToast = ToastService.showToast as jest.Mock;
4946

5047
const ADDRESS = '0x1111111111111111111111111111111111111111' as const;
5148
const 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);

app/actions/money/index.ts

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import type { RootState } from '../../reducers';
55
import { selectPrimaryMoneyAccount } from '../../selectors/moneyAccountController';
66
import Engine from '../../core/Engine';
77
import Logger from '../../util/Logger';
8-
import ToastService from '../../core/ToastService';
9-
import { ToastVariants } from '../../component-library/components/Toast/Toast.types';
8+
import { whenMoneyAccountUpgradeReady } from '../../core/Engine/controllers/money-account-upgrade-controller-init';
109

1110
const LOG_PREFIX = '[upgradeMoneyAccount]';
1211

@@ -17,18 +16,6 @@ export const __resetUpgradesInFlightForTesting = () => {
1716
upgradesInFlight.clear();
1817
};
1918

20-
const showToast = (label: string, description?: string) => {
21-
if (!ToastService.toastRef?.current) {
22-
return;
23-
}
24-
ToastService.showToast({
25-
variant: ToastVariants.Plain,
26-
labelOptions: [{ label, isBold: true }],
27-
descriptionOptions: description ? { description } : undefined,
28-
hasNoTimeout: false,
29-
});
30-
};
31-
3219
export const upgradeMoneyAccount =
3320
(): ThunkAction<void, RootState, unknown, AnyAction> =>
3421
(_dispatch, getState) => {
@@ -47,28 +34,15 @@ export const upgradeMoneyAccount =
4734
return;
4835
}
4936

50-
Logger.log(LOG_PREFIX, 'firing upgradeAccount', { address });
51-
52-
// TODO: remove this toast before general release. It only exists to make it
53-
// clear what's happening in testing
54-
Logger.log(LOG_PREFIX, 'upgrading money account');
55-
showToast('Upgrading money account…', address);
56-
5737
upgradesInFlight.add(address);
58-
Engine.context.MoneyAccountUpgradeController.upgradeAccount(address)
59-
.then(() => {
60-
Logger.log(LOG_PREFIX, 'upgradeAccount resolved', { address });
61-
showToast('Money account upgrade complete');
62-
})
38+
whenMoneyAccountUpgradeReady()
39+
.then(() =>
40+
Engine.context.MoneyAccountUpgradeController.upgradeAccount(address),
41+
)
6342
.catch((error: unknown) => {
64-
if (error instanceof Error) {
65-
Logger.log(`${LOG_PREFIX} failed`, error);
66-
showToast('Money account upgrade failed', error.message);
67-
} else {
68-
Logger.error(`${LOG_PREFIX} failed`, new Error(String(error)));
69-
console.log(error);
70-
showToast('Money account upgrade failed', 'Unknown error');
71-
}
43+
const wrapped =
44+
error instanceof Error ? error : new Error(String(error));
45+
Logger.error(wrapped, `${LOG_PREFIX} failed`);
7246
})
7347
.finally(() => {
7448
upgradesInFlight.delete(address);

0 commit comments

Comments
 (0)