Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use the new bitcoin factory in tests
Browse files Browse the repository at this point in the history
In this commit, we refactor the test files to use the new bitcoin
factory implementation. We also added a test for the newly created
notImplemented service file for bitcoin nodes.
Abdulkbk committed Jan 27, 2025
1 parent 8db1489 commit 49c1e3d
Showing 20 changed files with 122 additions and 141 deletions.
6 changes: 3 additions & 3 deletions src/components/common/RemoveNode.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { CommonNode, Status } from 'shared/types';
import { BitcoindLibrary, DockerLibrary } from 'types';
import { DockerLibrary } from 'types';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createBitcoindNetworkNode, createLndNetworkNode } from 'utils/network';
import {
getNetwork,
injections,
lightningServiceMock,
bitcoinServiceMock,
renderWithProviders,
suppressConsoleErrors,
tapServiceMock,
@@ -17,7 +18,6 @@ import {
import RemoveNode from './RemoveNode';

const dockerServiceMock = injections.dockerService as jest.Mocked<DockerLibrary>;
const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;

describe('RemoveNode', () => {
const renderComponent = (
@@ -136,7 +136,7 @@ describe('RemoveNode', () => {
beforeEach(() => {
lightningServiceMock.getChannels.mockResolvedValue([]);
lightningServiceMock.waitUntilOnline.mockResolvedValue(Promise.resolve());
bitcoindServiceMock.waitUntilOnline.mockResolvedValue(Promise.resolve());
bitcoinServiceMock.waitUntilOnline.mockResolvedValue(Promise.resolve());
tapServiceMock.waitUntilOnline.mockResolvedValue(Promise.resolve());
});

5 changes: 2 additions & 3 deletions src/components/common/RenameNodeModal.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { Status } from 'shared/types';
import { BitcoindLibrary } from 'types';
import * as asyncUtil from 'utils/async';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import {
injections,
lightningServiceMock,
bitcoinServiceMock,
litdServiceMock,
renderWithProviders,
tapServiceMock,
@@ -22,7 +22,6 @@ const asyncUtilMock = asyncUtil as jest.Mocked<typeof asyncUtil>;
const dockerServiceMock = injections.dockerService as jest.Mocked<
typeof injections.dockerService
>;
const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;

describe('RenameNodeModal', () => {
let unmount: () => void;
@@ -144,7 +143,7 @@ describe('RenameNodeModal', () => {
it('should update the started Backend node name', async () => {
asyncUtilMock.delay.mockResolvedValue(Promise.resolve());
lightningServiceMock.waitUntilOnline.mockResolvedValue();
bitcoindServiceMock.waitUntilOnline.mockResolvedValue();
bitcoinServiceMock.waitUntilOnline.mockResolvedValue();
tapServiceMock.waitUntilOnline.mockResolvedValue();
litdServiceMock.waitUntilOnline.mockResolvedValue();
const { getByText, getByLabelText, store } = await renderComponent(
6 changes: 3 additions & 3 deletions src/components/designer/bitcoin/BitcoinDetails.spec.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { shell } from 'electron';
import { fireEvent, waitFor } from '@testing-library/react';
import { Status } from 'shared/types';
import { bitcoinCredentials, dockerConfigs } from 'utils/constants';
import { getNetwork, injections, renderWithProviders } from 'utils/tests';
import { getNetwork, renderWithProviders, bitcoinServiceMock } from 'utils/tests';
import BitcoindDetails from './BitcoinDetails';

describe('BitcoindDetails', () => {
@@ -98,8 +98,8 @@ describe('BitcoindDetails', () => {
});

describe('with node Started', () => {
const chainMock = injections.bitcoindService.getBlockchainInfo as jest.Mock;
const walletMock = injections.bitcoindService.getWalletInfo as jest.Mock;
const chainMock = bitcoinServiceMock.getBlockchainInfo as jest.Mock;
const walletMock = bitcoinServiceMock.getWalletInfo as jest.Mock;

beforeEach(() => {
chainMock.mockResolvedValue({ blocks: 123, bestblockhash: 'abcdef' });
12 changes: 6 additions & 6 deletions src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@ import { fireEvent, waitFor } from '@testing-library/dom';
import { Status } from 'shared/types';
import {
getNetwork,
injections,
lightningServiceMock,
renderWithProviders,
tapServiceMock,
bitcoinServiceMock,
} from 'utils/tests';
import MineBlocksInput from './MineBlocksInput';

@@ -50,7 +50,7 @@ describe('MineBlocksInput', () => {
});

it('should mine a block when the button is clicked', async () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
const mineMock = bitcoinServiceMock.mine as jest.Mock;
mineMock.mockResolvedValue(true);
const { input, btn, store } = renderComponent();
const numBlocks = 5;
@@ -63,7 +63,7 @@ describe('MineBlocksInput', () => {
});

it('should mine 1 block when a invalid value is specified', async () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
const mineMock = bitcoinServiceMock.mine as jest.Mock;
mineMock.mockResolvedValue(true);
const { input, btn, store } = renderComponent();
fireEvent.change(input, { target: { value: 'asdf' } });
@@ -75,7 +75,7 @@ describe('MineBlocksInput', () => {
});

it('should display an error if mining fails', async () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
const mineMock = bitcoinServiceMock.mine as jest.Mock;
mineMock.mockRejectedValue(new Error('connection failed'));
const { input, btn, findByText } = renderComponent();
const numBlocks = 5;
@@ -93,7 +93,7 @@ describe('MineBlocksInput', () => {
});

it('should display an error if lightning nodes cannot update after mining', async () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
const mineMock = bitcoinServiceMock.mine as jest.Mock;
mineMock.mockResolvedValue(true);
lightningServiceMock.getInfo.mockRejectedValueOnce(new Error('info-error'));
const { input, btn, findByText } = renderComponent(Status.Started);
@@ -104,7 +104,7 @@ describe('MineBlocksInput', () => {
});

it('should display an error if tap nodes cannot update after mining', async () => {
const mineMock = injections.bitcoindService.mine as jest.Mock;
const mineMock = bitcoinServiceMock.mine as jest.Mock;
mineMock.mockResolvedValue(true);
tapServiceMock.listAssets.mockRejectedValueOnce(new Error('info-error'));
const { input, btn, findByText } = renderComponent(Status.Started);
41 changes: 19 additions & 22 deletions src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx
Original file line number Diff line number Diff line change
@@ -2,20 +2,17 @@ import React from 'react';
import { fireEvent } from '@testing-library/dom';
import { waitFor } from '@testing-library/react';
import { Status } from 'shared/types';
import { BitcoindLibrary } from 'types';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork } from 'utils/network';
import {
injections,
renderWithProviders,
suppressConsoleErrors,
testManagedImages,
bitcoinServiceMock,
} from 'utils/tests';
import SendOnChainModal from './SendOnChainModal';

const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;

describe('SendOnChainModal', () => {
let unmount: () => void;

@@ -123,13 +120,13 @@ describe('SendOnChainModal', () => {
});

it('should display the correct balance for the selected backend', async () => {
bitcoindServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 123 } as any);
bitcoindServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 456 } as any);
bitcoindServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 0 } as any);
bitcoinServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 123 } as any);
bitcoinServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 456 } as any);
bitcoinServiceMock.getWalletInfo.mockResolvedValueOnce({ balance: 0 } as any);
const { findByText, changeSelect, store, network } = await renderComponent();
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[1]);
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[2]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[1]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[2]);
expect(await findByText('Balance: 123 BTC')).toBeInTheDocument();
changeSelect('From Bitcoin Node', 'backend2');
expect(await findByText('Balance: 456 BTC')).toBeInTheDocument();
@@ -139,14 +136,14 @@ describe('SendOnChainModal', () => {

describe('with form submitted', () => {
beforeEach(() => {
bitcoindServiceMock.getWalletInfo.mockResolvedValue({ balance: 123 } as any);
bitcoindServiceMock.mine.mockResolvedValue([]);
bitcoindServiceMock.sendFunds.mockResolvedValue('txid123');
bitcoinServiceMock.getWalletInfo.mockResolvedValue({ balance: 123 } as any);
bitcoinServiceMock.mine.mockResolvedValue([]);
bitcoinServiceMock.sendFunds.mockResolvedValue('txid123');
});

it('should send coins successfully', async () => {
const { getByText, getByLabelText, store, network } = await renderComponent();
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[0]);
fireEvent.change(getByLabelText('Amount (BTC)'), { target: { value: '0.001' } });
fireEvent.change(getByLabelText('Destination Onchain Address'), {
target: { value: 'bc1...' },
@@ -156,13 +153,13 @@ describe('SendOnChainModal', () => {
expect(store.getState().modals.sendOnChain.visible).toBe(false);
});
const node = network.nodes.bitcoin[0];
expect(bitcoindServiceMock.sendFunds).toHaveBeenCalledWith(node, 'bc1...', 0.001);
expect(bitcoindServiceMock.mine).toHaveBeenCalledWith(6, node);
expect(bitcoinServiceMock.sendFunds).toHaveBeenCalledWith(node, 'bc1...', 0.001);
expect(bitcoinServiceMock.mine).toHaveBeenCalledWith(6, node);
});

it('should not mine block when the option is unchecked', async () => {
const { getByText, getByLabelText, store, network } = await renderComponent();
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[0]);
fireEvent.change(getByLabelText('Amount (BTC)'), { target: { value: '0.001' } });
fireEvent.change(getByLabelText('Destination Onchain Address'), {
target: { value: 'bc1...' },
@@ -175,13 +172,13 @@ describe('SendOnChainModal', () => {
expect(store.getState().modals.sendOnChain.visible).toBe(false);
});
const node = network.nodes.bitcoin[0];
expect(bitcoindServiceMock.sendFunds).toHaveBeenCalledWith(node, 'bc1...', 0.001);
expect(bitcoindServiceMock.mine).not.toHaveBeenCalled();
expect(bitcoinServiceMock.sendFunds).toHaveBeenCalledWith(node, 'bc1...', 0.001);
expect(bitcoinServiceMock.mine).not.toHaveBeenCalled();
});

it('should display an error when amount is above balance', async () => {
const { getByText, getByLabelText, store, network } = await renderComponent();
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[0]);
fireEvent.change(getByLabelText('Amount (BTC)'), { target: { value: '125' } });
fireEvent.change(getByLabelText('Destination Onchain Address'), {
target: { value: 'bc1...' },
@@ -196,9 +193,9 @@ describe('SendOnChainModal', () => {
});

it('should display an error when sending funds fails', async () => {
bitcoindServiceMock.sendFunds.mockRejectedValue(new Error('error-msg'));
bitcoinServiceMock.sendFunds.mockRejectedValue(new Error('error-msg'));
const { getByText, getByLabelText, store, network } = await renderComponent();
store.getActions().bitcoind.getInfo(network.nodes.bitcoin[0]);
store.getActions().bitcoin.getInfo(network.nodes.bitcoin[0]);
fireEvent.change(getByLabelText('Amount (BTC)'), { target: { value: '0.001' } });
fireEvent.change(getByLabelText('Destination Onchain Address'), {
target: { value: 'bc1...' },
13 changes: 5 additions & 8 deletions src/components/designer/lightning/actions/Deposit.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { BitcoindLibrary } from 'types';
import {
defaultStateInfo,
getNetwork,
injections,
lightningServiceMock,
renderWithProviders,
bitcoinServiceMock,
} from 'utils/tests';
import { Deposit } from './';

const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;

describe('Deposit', () => {
const renderComponent = () => {
const network = getNetwork(1, 'test network');
@@ -31,7 +28,7 @@ describe('Deposit', () => {
};

beforeEach(() => {
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
bitcoinServiceMock.sendFunds.mockResolvedValue('txid');
lightningServiceMock.getNewAddress.mockResolvedValue({ address: 'bc1aaaa' });
lightningServiceMock.getInfo.mockResolvedValue(
defaultStateInfo({
@@ -76,7 +73,7 @@ describe('Deposit', () => {
fireEvent.click(btn);
await waitFor(() => getByText('Deposited 250,000 sats to alice'));
expect(lightningServiceMock.getNewAddress).toBeCalledTimes(1);
expect(bitcoindServiceMock.sendFunds).toBeCalledWith(
expect(bitcoinServiceMock.sendFunds).toBeCalledWith(
expect.anything(),
'bc1aaaa',
0.0025,
@@ -90,15 +87,15 @@ describe('Deposit', () => {
fireEvent.click(btn);
await waitFor(() => getByText('Deposited 1,000,000 sats to alice'));
expect(lightningServiceMock.getNewAddress).toBeCalledTimes(1);
expect(bitcoindServiceMock.sendFunds).toBeCalledWith(
expect(bitcoinServiceMock.sendFunds).toBeCalledWith(
expect.anything(),
'bc1aaaa',
0.01,
);
});

it('should display an error if mining fails', async () => {
bitcoindServiceMock.sendFunds.mockRejectedValue(new Error('connection failed'));
bitcoinServiceMock.sendFunds.mockRejectedValue(new Error('connection failed'));
const { input, btn, findByText } = renderComponent();
const numBlocks = 5;
fireEvent.change(input, { target: { value: numBlocks } });
22 changes: 10 additions & 12 deletions src/components/designer/lightning/actions/OpenChannelModal.spec.tsx
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { fireEvent, waitForElementToBeRemoved } from '@testing-library/dom';
import { waitFor } from '@testing-library/react';
import { Status } from 'shared/types';
import { LightningNodeChannelAsset } from 'lib/lightning/types';
import { BitcoindLibrary, Network } from 'types';
import { Network } from 'types';
import { initChartFromNetwork } from 'utils/chart';
import { defaultRepoState } from 'utils/constants';
import { createNetwork, mapToTapd } from 'utils/network';
@@ -14,17 +14,15 @@ import {
defaultTapAsset,
defaultTapBalance,
getNetwork,
injections,
lightningServiceMock,
renderWithProviders,
suppressConsoleErrors,
tapServiceMock,
testManagedImages,
bitcoinServiceMock,
} from 'utils/tests';
import OpenChannelModal from './OpenChannelModal';

const bitcoindServiceMock = injections.bitcoindService as jest.Mocked<BitcoindLibrary>;

describe('OpenChannelModal', () => {
let unmount: () => void;
let network: Network;
@@ -198,7 +196,7 @@ describe('OpenChannelModal', () => {
unconfirmed: '200',
total: '300',
});
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
bitcoinServiceMock.sendFunds.mockResolvedValue('txid');
});

it('should open a channel successfully', async () => {
@@ -216,7 +214,7 @@ describe('OpenChannelModal', () => {
amount: 1000,
isPrivate: false,
});
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1);
expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1);
});

it('should open a private channel successfully', async () => {
@@ -235,7 +233,7 @@ describe('OpenChannelModal', () => {
amount: 1000,
isPrivate: true,
});
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1);
expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1);
});

it('should open a channel and deposit funds', async () => {
@@ -252,8 +250,8 @@ describe('OpenChannelModal', () => {
amount: 1000,
isPrivate: false,
});
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2);
expect(bitcoindServiceMock.sendFunds).toHaveBeenCalledTimes(1);
expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(2);
expect(bitcoinServiceMock.sendFunds).toHaveBeenCalledTimes(1);
expect(lightningServiceMock.getNewAddress).toHaveBeenCalledTimes(1);
});

@@ -310,7 +308,7 @@ describe('OpenChannelModal', () => {
unconfirmed: '200',
total: '300',
});
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
bitcoinServiceMock.sendFunds.mockResolvedValue('txid');
tapServiceMock.listBalances.mockResolvedValue([
defaultTapBalance({ id: 'abcd', name: 'test asset', balance: '1000' }),
defaultTapBalance({ id: 'efgh', name: 'other asset', balance: '5000' }),
@@ -394,8 +392,8 @@ describe('OpenChannelModal', () => {
'abcd',
1000,
);
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2);
expect(bitcoindServiceMock.sendFunds).toHaveBeenCalledTimes(1);
expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(2);
expect(bitcoinServiceMock.sendFunds).toHaveBeenCalledTimes(1);
expect(lightningServiceMock.getNewAddress).toHaveBeenCalledTimes(1);
});

Loading

0 comments on commit 49c1e3d

Please sign in to comment.