Skip to content

Commit

Permalink
fix(multichain): fix btc balances tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccharly committed Jul 18, 2024
1 parent 5f716ac commit 2739b48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
33 changes: 19 additions & 14 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
BalancesController as MultichainBalancesController,
BTC_AVG_BLOCK_TIME,
} from './lib/accounts/BalancesController';
import { BalancesTracker as MultichainBalancesTracker } from './lib/accounts/BalancesTracker';
import { deferredPromise } from './lib/util';
import MetaMaskController from './metamask-controller';

Expand Down Expand Up @@ -2254,13 +2255,18 @@ describe('MetaMaskController', () => {
},
};
let localMetamaskController;
let spyBalancesTrackerUpdateBalance;

beforeEach(() => {
jest.useFakeTimers();
jest.spyOn(MultichainBalancesController.prototype, 'updateBalances');
jest
.spyOn(MultichainBalancesController.prototype, 'updateBalance')
.mockResolvedValue();
spyBalancesTrackerUpdateBalance = jest.spyOn(
MultichainBalancesTracker.prototype,
'updateBalance',
).mockResolvedValue();
localMetamaskController = new MetaMaskController({
showUserConfirmation: noop,
encryptor: mockEncryptor,
Expand Down Expand Up @@ -2298,30 +2304,29 @@ describe('MetaMaskController', () => {
).toHaveBeenCalled();
});

it('calls updateBalances after the interval has passed', async () => {
it.only('calls updateBalances after the interval has passed', async () => {
// 1st call is during startup:
// updatesBalances is going to call updateBalance for the only non-EVM
// account that we have
expect(
localMetamaskController.multichainBalancesController.updateBalances,
).toHaveBeenCalledTimes(1);
expect(
localMetamaskController.multichainBalancesController.updateBalance,
).toHaveBeenCalledTimes(1);
expect(
localMetamaskController.multichainBalancesController.updateBalance,
).toHaveBeenCalledWith(mockNonEvmAccount);
expect(spyBalancesTrackerUpdateBalance).toHaveBeenCalledTimes(1);
expect(spyBalancesTrackerUpdateBalance).toHaveBeenCalledWith(
mockNonEvmAccount.id,
);

// Wait for "block time", so balances will have to be refreshed
jest.advanceTimersByTime(BTC_AVG_BLOCK_TIME);

// 2nd call because balances are considered "outdated":
expect(
localMetamaskController.multichainBalancesController.updateBalance,
).toHaveBeenCalledTimes(2); // 1 (startup) + 1 (refresh)
expect(
localMetamaskController.multichainBalancesController.updateBalance,
).toHaveBeenLastCalledWith(mockNonEvmAccount);
// Check that we tried to fetch the balances more than once
// NOTE: For now, this method might be called a lot more than just twice, but this
// method has some internal logic to prevent fetching the balance too often if we
// consider the balance to be "up-to-date"
expect(spyBalancesTrackerUpdateBalance.mock.calls.length).toBeGreaterThan(1);
expect(spyBalancesTrackerUpdateBalance).toHaveBeenLastCalledWith(
mockNonEvmAccount.id,
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ const mockBtcAccount = {
methods: [BtcMethod.SendMany],
};
const mockBitcoinWalletSnapSend = jest.fn().mockReturnValue(mockBtcAccount);
const mockMultichainUpdateBalance = jest.fn().mockReturnValue({
[mockBtcAccount.address]: {
[`${MultichainNetworks.BITCOIN_TESTNET}/slip44:0`]: {
amount: '0.00000000',
unit: 'BTC',
},
},
});
const mockSetAccountLabel = jest.fn().mockReturnValue({ type: 'TYPE' });

jest.mock('../../../store/actions', () => ({
forceUpdateMetamaskState: jest.fn(),
setAccountLabel: (address: string, label: string) =>
mockSetAccountLabel(address, label),
multichainUpdateBalance: (accountId: string) =>
mockMultichainUpdateBalance(accountId),
}));

jest.mock(
Expand Down Expand Up @@ -85,6 +95,7 @@ describe('CreateBtcAccount', () => {
newAccountName,
),
);
await waitFor(() => expect(mockMultichainUpdateBalance).toHaveBeenCalled());
await waitFor(() => expect(onActionComplete).toHaveBeenCalled());
});

Expand Down

0 comments on commit 2739b48

Please sign in to comment.