diff --git a/src/components/common/RemoveNode.spec.tsx b/src/components/common/RemoveNode.spec.tsx index cf0169aae..491bd824f 100644 --- a/src/components/common/RemoveNode.spec.tsx +++ b/src/components/common/RemoveNode.spec.tsx @@ -1,7 +1,7 @@ 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'; @@ -9,6 +9,7 @@ import { getNetwork, injections, lightningServiceMock, + bitcoinServiceMock, renderWithProviders, suppressConsoleErrors, tapServiceMock, @@ -17,7 +18,6 @@ import { import RemoveNode from './RemoveNode'; const dockerServiceMock = injections.dockerService as jest.Mocked; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; 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()); }); diff --git a/src/components/common/RenameNodeModal.spec.tsx b/src/components/common/RenameNodeModal.spec.tsx index ba18d4154..c77a12800 100644 --- a/src/components/common/RenameNodeModal.spec.tsx +++ b/src/components/common/RenameNodeModal.spec.tsx @@ -1,7 +1,6 @@ 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'; @@ -9,6 +8,7 @@ import { createNetwork } from 'utils/network'; import { injections, lightningServiceMock, + bitcoinServiceMock, litdServiceMock, renderWithProviders, tapServiceMock, @@ -22,7 +22,6 @@ const asyncUtilMock = asyncUtil as jest.Mocked; const dockerServiceMock = injections.dockerService as jest.Mocked< typeof injections.dockerService >; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; 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( diff --git a/src/components/designer/bitcoin/BitcoinDetails.spec.tsx b/src/components/designer/bitcoin/BitcoinDetails.spec.tsx index bd8a20f88..3a4dcc035 100644 --- a/src/components/designer/bitcoin/BitcoinDetails.spec.tsx +++ b/src/components/designer/bitcoin/BitcoinDetails.spec.tsx @@ -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' }); diff --git a/src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx b/src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx index d92b8a061..17a388c19 100644 --- a/src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx +++ b/src/components/designer/bitcoin/actions/MineBlocksInput.spec.tsx @@ -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); diff --git a/src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx b/src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx index acbd96bc9..73d3d6345 100644 --- a/src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx +++ b/src/components/designer/bitcoin/actions/SendOnChainModal.spec.tsx @@ -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; - 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...' }, diff --git a/src/components/designer/lightning/actions/Deposit.spec.tsx b/src/components/designer/lightning/actions/Deposit.spec.tsx index 485c3862e..48a0adc35 100644 --- a/src/components/designer/lightning/actions/Deposit.spec.tsx +++ b/src/components/designer/lightning/actions/Deposit.spec.tsx @@ -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; - 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,7 +87,7 @@ 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, @@ -98,7 +95,7 @@ describe('Deposit', () => { }); 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 } }); diff --git a/src/components/designer/lightning/actions/OpenChannelModal.spec.tsx b/src/components/designer/lightning/actions/OpenChannelModal.spec.tsx index bc0973eac..ee40b07fc 100644 --- a/src/components/designer/lightning/actions/OpenChannelModal.spec.tsx +++ b/src/components/designer/lightning/actions/OpenChannelModal.spec.tsx @@ -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; - 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); }); diff --git a/src/components/designer/link/Channel.spec.tsx b/src/components/designer/link/Channel.spec.tsx index 1a14403f4..4afa6e073 100644 --- a/src/components/designer/link/Channel.spec.tsx +++ b/src/components/designer/link/Channel.spec.tsx @@ -6,17 +6,13 @@ import { LightningNodeChannelAsset } from 'lib/lightning/types'; import { initChartFromNetwork } from 'utils/chart'; import { getNetwork, - injections, lightningServiceMock, renderWithProviders, suppressConsoleErrors, + bitcoinServiceMock, } from 'utils/tests'; import Channel from './Channel'; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked< - typeof injections.bitcoindService ->; - describe('Channel component', () => { const renderComponent = ( status = Status.Stopped, @@ -164,7 +160,7 @@ describe('Channel component', () => { beforeEach(() => { lightningServiceMock.closeChannel.mockResolvedValue(true); lightningServiceMock.getChannels.mockResolvedValue([]); - bitcoindServiceMock.mine.mockResolvedValue(['txid']); + bitcoinServiceMock.mine.mockResolvedValue(['txid']); }); it('should show the close channel modal', async () => { @@ -188,7 +184,7 @@ describe('Channel component', () => { await waitFor(() => getByLabelText('check-circle')); expect(getByText('The channel has been closed')).toBeInTheDocument(); expect(lightningServiceMock.closeChannel).toHaveBeenCalledTimes(1); - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1); }); it('should display an error if the node is not started', async () => { diff --git a/src/components/designer/tap/actions/MintAssetModal.spec.tsx b/src/components/designer/tap/actions/MintAssetModal.spec.tsx index c3c96cfdd..9e35b2b5d 100644 --- a/src/components/designer/tap/actions/MintAssetModal.spec.tsx +++ b/src/components/designer/tap/actions/MintAssetModal.spec.tsx @@ -2,23 +2,20 @@ import React from 'react'; import { fireEvent } from '@testing-library/dom'; import { waitFor } from '@testing-library/react'; import { Status, TapNode } from 'shared/types'; -import { BitcoindLibrary } from 'types'; import { initChartFromNetwork } from 'utils/chart'; import { defaultRepoState } from 'utils/constants'; import { createLitdNetworkNode } from 'utils/network'; import { defaultTapAsset, getNetwork, - injections, lightningServiceMock, renderWithProviders, tapServiceMock, testNodeDocker, + bitcoinServiceMock, } from 'utils/tests'; import MintAssetModal from './MintAssetModal'; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; - describe('MintAssetModal', () => { let unmount: () => void; let node: TapNode; @@ -150,7 +147,7 @@ describe('MintAssetModal', () => { fireEvent.click(getByText('Mint')); await waitFor(() => { expect(tapServiceMock.mintAsset).toHaveBeenCalled(); - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1); }); }); @@ -167,7 +164,7 @@ describe('MintAssetModal', () => { asset: expect.objectContaining({ assetType: 'COLLECTIBLE' }), }), ); - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1); }); }); diff --git a/src/components/designer/tap/actions/SendAssetModal.spec.tsx b/src/components/designer/tap/actions/SendAssetModal.spec.tsx index f1a637690..588f56137 100644 --- a/src/components/designer/tap/actions/SendAssetModal.spec.tsx +++ b/src/components/designer/tap/actions/SendAssetModal.spec.tsx @@ -2,7 +2,6 @@ import React from 'react'; import { fireEvent } from '@testing-library/dom'; import { waitFor } from '@testing-library/react'; import { Status, TapdNode } from 'shared/types'; -import { BitcoindLibrary } from 'types'; import { initChartFromNetwork } from 'utils/chart'; import { defaultRepoState } from 'utils/constants'; import { createLitdNetworkNode } from 'utils/network'; @@ -11,16 +10,14 @@ import { defaultTapAsset, defaultTapBalance, getNetwork, - injections, lightningServiceMock, renderWithProviders, tapServiceMock, testNodeDocker, + bitcoinServiceMock, } from 'utils/tests'; import SendAssetModal from './SendAssetModal'; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; - describe('SendAssetModal', () => { let unmount: () => void; @@ -262,7 +259,7 @@ describe('SendAssetModal', () => { await waitFor(() => { expect(tapServiceMock.sendAsset).toHaveBeenCalled(); - expect(bitcoindServiceMock.mine).toHaveBeenCalled(); + expect(bitcoinServiceMock.mine).toHaveBeenCalled(); expect(tapServiceMock.listBalances).toHaveBeenCalled(); }); @@ -302,7 +299,7 @@ describe('SendAssetModal', () => { fireEvent.click(getByText('Send')); await waitFor(() => { expect(tapServiceMock.sendAsset).toHaveBeenCalled(); - expect(bitcoindServiceMock.mine).toHaveBeenCalled(); + expect(bitcoinServiceMock.mine).toHaveBeenCalled(); }); }); diff --git a/src/components/network/NetworkActions.spec.tsx b/src/components/network/NetworkActions.spec.tsx index 9b1a8becc..5e498198a 100644 --- a/src/components/network/NetworkActions.spec.tsx +++ b/src/components/network/NetworkActions.spec.tsx @@ -6,9 +6,9 @@ import { defaultStateBalances, defaultStateInfo, getNetwork, - injections, lightningServiceMock, renderWithProviders, + bitcoinServiceMock, } from 'utils/tests'; import NetworkActions from './NetworkActions'; @@ -26,7 +26,7 @@ describe('NetworkActions Component', () => { network: { networks: [network], }, - bitcoind: { + bitcoin: { nodes: { '1-backend1': { chainInfo: { @@ -123,7 +123,7 @@ describe('NetworkActions Component', () => { }); it('should mine a block when the Mine button is clicked', async () => { - const mineMock = injections.bitcoindService.mine as jest.Mock; + const mineMock = bitcoinServiceMock.mine as jest.Mock; mineMock.mockResolvedValue(true); const { getByText, store } = renderComponent(Status.Started); fireEvent.click(getByText('Quick Mine')); @@ -134,7 +134,7 @@ describe('NetworkActions Component', () => { }); 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 { getByText, findByText } = renderComponent(Status.Started); fireEvent.click(getByText('Quick Mine')); diff --git a/src/components/network/NetworkView.spec.tsx b/src/components/network/NetworkView.spec.tsx index 9d786db32..1a89ac3a9 100644 --- a/src/components/network/NetworkView.spec.tsx +++ b/src/components/network/NetworkView.spec.tsx @@ -15,6 +15,7 @@ import { renderWithProviders, suppressConsoleErrors, testCustomImages, + bitcoinServiceMock, } from 'utils/tests'; import NetworkView from './NetworkView'; @@ -24,9 +25,6 @@ const fsMock = fsExtra as jest.Mocked; const logMock = log as jest.Mocked; const ipcMock = ipc as jest.Mocked; const dialogMock = electron.remote.dialog as jest.Mocked; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked< - typeof injections.bitcoindService ->; const dockerServiceMock = injections.dockerService as jest.Mocked< typeof injections.dockerService >; @@ -66,7 +64,7 @@ describe('NetworkView Component', () => { 1: initChartFromNetwork(network), }, }, - bitcoind: withBitcoinData ? bitcoinData : undefined, + bitcoin: withBitcoinData ? bitcoinData : undefined, }; const route = `/network/${id}`; const history = createMemoryHistory({ initialEntries: [route] }); @@ -82,7 +80,7 @@ describe('NetworkView Component', () => { beforeEach(() => { lightningServiceMock.waitUntilOnline.mockResolvedValue(); - bitcoindServiceMock.waitUntilOnline.mockResolvedValue(); + bitcoinServiceMock.waitUntilOnline.mockResolvedValue(); dockerServiceMock.getImages.mockResolvedValue([]); }); @@ -202,11 +200,11 @@ describe('NetworkView Component', () => { describe('node state', () => { beforeEach(() => { - bitcoindServiceMock.getBlockchainInfo.mockResolvedValue({ + bitcoinServiceMock.getBlockchainInfo.mockResolvedValue({ blocks: 321, bestblockhash: 'abcdef', } as any); - bitcoindServiceMock.getWalletInfo.mockResolvedValue({ + bitcoinServiceMock.getWalletInfo.mockResolvedValue({ balance: 10, immature_balance: 20, } as any); @@ -219,7 +217,7 @@ describe('NetworkView Component', () => { }); it('should handle an error when fetching bitcoin data on mount', async () => { - bitcoindServiceMock.getBlockchainInfo.mockRejectedValue(new Error('test-err')); + bitcoinServiceMock.getBlockchainInfo.mockRejectedValue(new Error('test-err')); const { findByText } = renderComponent('1', Status.Started, [], false); expect( await findByText('Failed to fetch the bitcoin block height'), @@ -229,7 +227,7 @@ describe('NetworkView Component', () => { it('should not fetch bitcoin data if it is already in the store', async () => { renderComponent('1', Status.Started, [], true); - expect(bitcoindServiceMock.getBlockchainInfo).not.toHaveBeenCalled(); + expect(bitcoinServiceMock.getBlockchainInfo).not.toHaveBeenCalled(); }); }); diff --git a/src/lib/bitcoin/notImplementedService.spec.ts b/src/lib/bitcoin/notImplementedService.spec.ts new file mode 100644 index 000000000..2a5e5af03 --- /dev/null +++ b/src/lib/bitcoin/notImplementedService.spec.ts @@ -0,0 +1,19 @@ +import { getNetwork } from 'utils/tests'; +import service from './notImplementedService'; + +describe('NotImplementedService', () => { + const node = getNetwork().nodes.bitcoin[0]; + node.implementation = 'btcd'; + const msg = (name: string) => `${name} is not implemented for btcd nodes`; + + it('should throw for all methods', () => { + expect(() => service.waitUntilOnline(node)).toThrow(msg('waitUntilOnline')); + expect(() => service.createDefaultWallet(node)).toThrow(msg('createDefaultWallet')); + expect(() => service.getBlockchainInfo(node)).toThrow(msg('getBlockchainInfo')); + expect(() => service.getWalletInfo(node)).toThrow(msg('getWalletInfo')); + expect(() => service.getNewAddress(node)).toThrow(msg('getNewAddress')); + expect(() => service.connectPeers(node)).toThrow(msg('connectPeers')); + expect(() => service.mine(1, node)).toThrow(msg('mine')); + expect(() => service.sendFunds(node, '', 0)).toThrow(msg('sendFunds')); + }); +}); diff --git a/src/lib/lightning/eclair/eclairService.spec.ts b/src/lib/lightning/eclair/eclairService.spec.ts index 54f2ef708..e61d8e8ef 100644 --- a/src/lib/lightning/eclair/eclairService.spec.ts +++ b/src/lib/lightning/eclair/eclairService.spec.ts @@ -1,5 +1,5 @@ import { WalletInfo } from 'bitcoin-core'; -import bitcoindService from 'lib/bitcoin/bitcoindService'; +import bitcoindService from 'lib/bitcoin/bitcoind/bitcoindService'; import { defaultRepoState } from 'utils/constants'; import { defaultStateBalances, defaultStateInfo, getNetwork } from 'utils/tests'; import { eclairService } from './'; @@ -7,7 +7,7 @@ import * as eclairApi from './eclairApi'; import * as ELN from './types'; jest.mock('./eclairApi'); -jest.mock('lib/bitcoin/bitcoindService'); +jest.mock('lib/bitcoin/bitcoind/bitcoindService'); jest.mock('utils/async', () => { const actualAsync = jest.requireActual('utils/async'); return { diff --git a/src/store/models/designer.spec.ts b/src/store/models/designer.spec.ts index 8858460f3..36e2c8950 100644 --- a/src/store/models/designer.spec.ts +++ b/src/store/models/designer.spec.ts @@ -3,7 +3,7 @@ import { waitFor } from '@testing-library/react'; import { notification } from 'antd'; import { createStore } from 'easy-peasy'; import { Status } from 'shared/types'; -import { BitcoindLibrary, DockerLibrary } from 'types'; +import { DockerLibrary } from 'types'; import { initChartFromNetwork } from 'utils/chart'; import { defaultRepoState, LOADING_NODE_ID } from 'utils/constants'; import * as files from 'utils/files'; @@ -14,9 +14,10 @@ import { lightningServiceMock, testNodeDocker, testRepoState, + bitcoinServiceMock, } from 'utils/tests'; import appModel from './app'; -import bitcoindModel from './bitcoind'; +import bitcoinModel from './bitcoin'; import designerModel from './designer'; import lightningModel from './lightning'; import modalsModel from './modals'; @@ -43,7 +44,7 @@ describe('Designer model', () => { app: appModel, network: networkModel, lightning: lightningModel, - bitcoind: bitcoindModel, + bitcoin: bitcoinModel, designer: designerModel, modals: modalsModel, tap: tapModel, @@ -525,8 +526,6 @@ describe('Designer model', () => { describe('onCanvasDrop', () => { const mockDockerService = injections.dockerService as jest.Mocked; - const mockBitcoindService = - injections.bitcoindService as jest.Mocked; const lndLatest = defaultRepoState.images.LND.latest; const btcLatest = defaultRepoState.images.bitcoind.latest; const tapdLatest = defaultRepoState.images.tapd.latest; @@ -723,7 +722,7 @@ describe('Designer model', () => { }); it('should start the node if the network is running', async () => { - mockBitcoindService.waitUntilOnline.mockResolvedValue(); + bitcoinServiceMock.waitUntilOnline.mockResolvedValue(); lightningServiceMock.waitUntilOnline.mockResolvedValue(); const { setStatus } = store.getActions().network; setStatus({ id: firstNetwork().id, status: Status.Started }); diff --git a/src/store/models/lightning.spec.ts b/src/store/models/lightning.spec.ts index 5480a504c..04ece416e 100644 --- a/src/store/models/lightning.spec.ts +++ b/src/store/models/lightning.spec.ts @@ -6,7 +6,6 @@ import { LightningNodeChannel, LightningNodeInfo, } from 'lib/lightning/types'; -import { BitcoindLibrary } from 'types'; import * as asyncUtil from 'utils/async'; import { initChartFromNetwork } from 'utils/chart'; import { @@ -15,23 +14,23 @@ import { injections, lightningServiceMock, mockProperty, + bitcoinServiceMock, } from 'utils/tests'; import appModel from './app'; -import bitcoindModel from './bitcoind'; +import bitcoinModel from './bitcoin'; import designerModel from './designer'; import lightningModel from './lightning'; import networkModel from './network'; jest.mock('utils/async'); const asyncUtilMock = asyncUtil as jest.Mocked; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; describe('Lightning Model', () => { const rootModel = { app: appModel, network: networkModel, lightning: lightningModel, - bitcoind: bitcoindModel, + bitcoin: bitcoinModel, designer: designerModel, }; const network = getNetwork(); @@ -55,7 +54,7 @@ describe('Lightning Model', () => { store = createStore(rootModel, { injections, initialState }); asyncUtilMock.delay.mockResolvedValue(Promise.resolve()); - bitcoindServiceMock.sendFunds.mockResolvedValue('txid'); + bitcoinServiceMock.sendFunds.mockResolvedValue('txid'); lightningServiceMock.getNewAddress.mockResolvedValue({ address: 'bc1aaaa' }); lightningServiceMock.getInfo.mockResolvedValue( defaultStateInfo({ @@ -155,7 +154,7 @@ describe('Lightning Model', () => { await openChannel({ from, to, sats, autoFund: false, isPrivate: false }); expect(lightningServiceMock.getInfo).toHaveBeenCalledTimes(1); expect(lightningServiceMock.openChannel).toHaveBeenCalledTimes(1); - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(1); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(1); }); it('should open a channel and mine on the first bitcoin node', async () => { @@ -174,7 +173,7 @@ describe('Lightning Model', () => { await getInfo(to); await openChannel({ from, to, sats, autoFund: false, isPrivate: false }); const btcNode = store.getState().network.networks[0].nodes.bitcoin[0]; - expect(bitcoindServiceMock.mine).toHaveBeenCalledWith(6, btcNode); + expect(bitcoinServiceMock.mine).toHaveBeenCalledWith(6, btcNode); }); it('should cause some delay waiting for nodes', async () => { diff --git a/src/store/models/lit.spec.ts b/src/store/models/lit.spec.ts index 930719bde..3f11fd8cc 100644 --- a/src/store/models/lit.spec.ts +++ b/src/store/models/lit.spec.ts @@ -16,7 +16,7 @@ import { testManagedImages, } from 'utils/tests'; import appModel from './app'; -import bitcoindModel from './bitcoind'; +import bitcoinModel from './bitcoin'; import designerModel from './designer'; import lightningModel from './lightning'; import litModel from './lit'; @@ -27,7 +27,7 @@ describe('LIT Model', () => { app: appModel, network: networkModel, lightning: lightningModel, - bitcoind: bitcoindModel, + bitcoin: bitcoinModel, designer: designerModel, lit: litModel, }; diff --git a/src/store/models/network.spec.ts b/src/store/models/network.spec.ts index c23ce5cac..745d9a227 100644 --- a/src/store/models/network.spec.ts +++ b/src/store/models/network.spec.ts @@ -10,6 +10,7 @@ import { initChartFromNetwork } from 'utils/chart'; import { defaultRepoState } from 'utils/constants'; import * as files from 'utils/files'; import { + bitcoinServiceMock, getNetwork, injections, lightningServiceMock, @@ -19,7 +20,7 @@ import { testRepoState, } from 'utils/tests'; import appModel from './app'; -import bitcoindModel from './bitcoind'; +import bitcoinModel from './bitcoin'; import designerModel from './designer'; import lightningModel from './lightning'; import litModel from './lit'; @@ -36,9 +37,6 @@ const asyncUtilMock = asyncUtil as jest.Mocked; const filesMock = files as jest.Mocked; const logMock = log as jest.Mocked; const detectPortMock = detectPort as jest.Mock; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked< - typeof injections.bitcoindService ->; const electronMock = electron as jest.Mocked; describe('Network model', () => { @@ -46,7 +44,7 @@ describe('Network model', () => { app: appModel, network: networkModel, lightning: lightningModel, - bitcoind: bitcoindModel, + bitcoin: bitcoinModel, designer: designerModel, tap: tapModel, lit: litModel, @@ -75,7 +73,7 @@ describe('Network model', () => { // always return true immediately filesMock.waitForFile.mockResolvedValue(); lightningServiceMock.waitUntilOnline.mockResolvedValue(); - bitcoindServiceMock.waitUntilOnline.mockResolvedValue(); + bitcoinServiceMock.waitUntilOnline.mockResolvedValue(); litdServiceMock.waitUntilOnline.mockResolvedValue(); }); @@ -577,7 +575,7 @@ describe('Network model', () => { }); it('should set bitcoind node status to error if the node startup fails', async () => { - bitcoindServiceMock.waitUntilOnline.mockRejectedValue(new Error('test-error')); + bitcoinServiceMock.waitUntilOnline.mockRejectedValue(new Error('test-error')); const { start } = store.getActions().network; await start(firstNetwork().id); const { bitcoin } = firstNetwork().nodes; @@ -586,7 +584,7 @@ describe('Network model', () => { }); it('should mine a block on startup', async () => { - bitcoindServiceMock.getBlockchainInfo.mockResolvedValue({ blocks: 0 } as any); + bitcoinServiceMock.getBlockchainInfo.mockResolvedValue({ blocks: 0 } as any); const { start } = store.getActions().network; const network = firstNetwork(); await start(network.id); @@ -594,11 +592,11 @@ describe('Network model', () => { ...firstNetwork().nodes.bitcoin[0], status: Status.Starting, }; - expect(bitcoindServiceMock.mine).toHaveBeenCalledWith(1, btcNode); + expect(bitcoinServiceMock.mine).toHaveBeenCalledWith(1, btcNode); }); it('should not throw when mining a block on startup fails', async () => { - bitcoindServiceMock.mine.mockRejectedValue(new Error('test-error')); + bitcoinServiceMock.mine.mockRejectedValue(new Error('test-error')); const { start } = store.getActions().network; const network = firstNetwork(); await expect(start(network.id)).resolves.not.toThrow(); @@ -606,7 +604,7 @@ describe('Network model', () => { ...firstNetwork().nodes.bitcoin[0], status: Status.Starting, }; - expect(bitcoindServiceMock.mine).toHaveBeenCalledWith(1, btcNode); + expect(bitcoinServiceMock.mine).toHaveBeenCalledWith(1, btcNode); }); it('should not save compose file and networks if all ports are available', async () => { @@ -988,7 +986,7 @@ describe('Network model', () => { const { monitorStartup } = store.getActions().network; await monitorStartup([]); expect(lightningServiceMock.waitUntilOnline).not.toHaveBeenCalled(); - expect(bitcoindServiceMock.waitUntilOnline).not.toHaveBeenCalled(); + expect(bitcoinServiceMock.waitUntilOnline).not.toHaveBeenCalled(); }); it('should fail with an invalid network id', async () => { @@ -1011,8 +1009,8 @@ describe('Network model', () => { const { monitorStartup } = store.getActions().network; await monitorStartup(firstNetwork().nodes.bitcoin); await waitFor(() => { - expect(bitcoindServiceMock.waitUntilOnline).toHaveBeenCalled(); - expect(bitcoindServiceMock.connectPeers).toHaveBeenCalled(); + expect(bitcoinServiceMock.waitUntilOnline).toHaveBeenCalled(); + expect(bitcoinServiceMock.connectPeers).toHaveBeenCalled(); }); }); @@ -1021,7 +1019,7 @@ describe('Network model', () => { const { bitcoin } = firstNetwork().nodes; bitcoin[0].type = 'asdf' as any; await monitorStartup(bitcoin); - expect(bitcoindServiceMock.waitUntilOnline).not.toHaveBeenCalled(); + expect(bitcoinServiceMock.waitUntilOnline).not.toHaveBeenCalled(); }); }); @@ -1089,7 +1087,7 @@ describe('Network model', () => { .network.autoMine({ id: networks[0].id, mode: AutoMineMode.Auto30s }); jest.advanceTimersByTime(65000); - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(2); await store .getActions() @@ -1097,7 +1095,7 @@ describe('Network model', () => { jest.advanceTimersByTime(65000); // the call count is not incremented - expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2); + expect(bitcoinServiceMock.mine).toHaveBeenCalledTimes(2); jest.useRealTimers(); }); diff --git a/src/store/models/tap.spec.ts b/src/store/models/tap.spec.ts index e3efbc5f4..dc2c15413 100644 --- a/src/store/models/tap.spec.ts +++ b/src/store/models/tap.spec.ts @@ -2,7 +2,6 @@ import { createStore } from 'easy-peasy'; import { defaultTapdMintAsset } from 'shared'; import { Status, TapdNode } from 'shared/types'; import * as PTAP from 'lib/tap/types'; -import { BitcoindLibrary } from 'types'; import { initChartFromNetwork } from 'utils/chart'; import { defaultTapAsset, @@ -10,22 +9,21 @@ import { getNetwork, injections, tapServiceMock, + bitcoinServiceMock, } from 'utils/tests'; import appModel from './app'; -import bitcoindModel from './bitcoind'; +import bitcoinModel from './bitcoin'; import designerModel from './designer'; import lightningModel from './lightning'; import networkModel from './network'; import tapModel, { MintAssetPayload } from './tap'; -const bitcoindServiceMock = injections.bitcoindService as jest.Mocked; - describe('TAP Model', () => { const rootModel = { app: appModel, network: networkModel, lightning: lightningModel, - bitcoind: bitcoindModel, + bitcoin: bitcoinModel, designer: designerModel, tap: tapModel, }; @@ -110,7 +108,7 @@ describe('TAP Model', () => { tap[1], expect.objectContaining({ asset: expect.objectContaining({ name: 'my-asset' }) }), ); - expect(bitcoindServiceMock.mine).toHaveBeenCalledWith( + expect(bitcoinServiceMock.mine).toHaveBeenCalledWith( 6, expect.objectContaining({ name: 'backend1' }), ); @@ -136,7 +134,7 @@ describe('TAP Model', () => { expect.objectContaining({ asset: expect.objectContaining({ name: 'my-asset' }) }), ); expect(tapServiceMock.finalizeBatch).not.toHaveBeenCalled(); - expect(bitcoindServiceMock.mine).not.toHaveBeenCalled(); + expect(bitcoinServiceMock.mine).not.toHaveBeenCalled(); }); it('should format the asset amount', async () => { diff --git a/src/types/index.ts b/src/types/index.ts index ba46bab79..270cb076a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -144,17 +144,6 @@ export interface RepoServiceInjection { checkForUpdates: (currState: DockerRepoState) => Promise; } -export interface BitcoindLibrary { - waitUntilOnline: (node: BitcoinNode) => Promise; - createDefaultWallet: (node: BitcoinNode) => Promise; - getBlockchainInfo: (node: BitcoinNode) => Promise; - getWalletInfo: (node: BitcoinNode) => Promise; - getNewAddress: (node: BitcoinNode) => Promise; - connectPeers: (node: BitcoinNode) => Promise; - mine: (numBlocks: number, node: BitcoinNode) => Promise; - sendFunds: (node: BitcoinNode, addr: string, amount: number) => Promise; -} - export interface BitcoinService { waitUntilOnline: (node: BitcoinNode) => Promise; createDefaultWallet: (node: BitcoinNode) => Promise;