Skip to content

Commit

Permalink
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.
  • Loading branch information
Abdulkbk committed Dec 26, 2024
1 parent cd5db70 commit 17f4abb
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 130 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,
Expand All @@ -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 = (
Expand Down Expand Up @@ -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());
});

Expand Down
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,
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/components/designer/bitcoin/BitcoinDetails.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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' });
Expand Down
12 changes: 6 additions & 6 deletions src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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' } });
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
41 changes: 19 additions & 22 deletions src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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...' },
Expand All @@ -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...' },
Expand All @@ -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...' },
Expand All @@ -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...' },
Expand Down
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');
Expand All @@ -31,7 +28,7 @@ describe('Deposit', () => {
};

beforeEach(() => {
bitcoindServiceMock.sendFunds.mockResolvedValue('txid');
bitcoinServiceMock.sendFunds.mockResolvedValue('txid');
lightningServiceMock.getNewAddress.mockResolvedValue({ address: 'bc1aaaa' });
lightningServiceMock.getInfo.mockResolvedValue(
defaultStateInfo({
Expand Down Expand Up @@ -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,
Expand All @@ -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 } });
Expand Down
22 changes: 10 additions & 12 deletions src/components/designer/lightning/actions/OpenChannelModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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);
});

Expand Down Expand Up @@ -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' }),
Expand Down Expand Up @@ -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);
});

Expand Down
Loading

0 comments on commit 17f4abb

Please sign in to comment.