Browse Source

test(channels): fix errors in unit tests

feat/auto-update
jamaljsr 5 years ago
parent
commit
af13947098
  1. 2
      src/components/designer/NetworkDesigner.spec.tsx
  2. 3
      src/components/designer/NetworkDesigner.tsx
  3. 20
      src/components/designer/lnd/LndDeposit.spec.tsx
  4. 6
      src/components/designer/lnd/LndDetails.tsx
  5. 55
      src/lib/lnd/lndProxyClient.spec.ts
  6. 76
      src/lib/lnd/lndService.spec.ts
  7. 4
      src/store/models/lnd.spec.ts

2
src/components/designer/NetworkDesigner.spec.tsx

@ -31,7 +31,7 @@ describe('NetworkDesigner Component', () => {
return renderWithProviders(cmp, { initialState });
};
it('should render the designer component', () => {
it('should render the designer component', async () => {
const { getByText } = renderComponent();
expect(getByText('lnd-1')).toBeInTheDocument();
expect(getByText('lnd-2')).toBeInTheDocument();

3
src/components/designer/NetworkDesigner.tsx

@ -27,6 +27,7 @@ interface Props {
const NetworkDesigner: React.FC<Props> = ({ network, updateStateDelay = 3000 }) => {
const { setActiveId, ...callbacks } = useStoreActions(s => s.designer);
const { showOpenChannel } = useStoreActions(s => s.modals);
const { openChannel } = useStoreState(s => s.modals);
// update the redux store with the current network's chart
useEffect(() => {
setActiveId(network.id);
@ -53,7 +54,7 @@ const NetworkDesigner: React.FC<Props> = ({ network, updateStateDelay = 3000 })
callbacks={callbacks}
/>
<Sidebar network={network} chart={chart} onOpenChannel={showOpenChannel} />
<OpenChannelModal network={network} />
{openChannel.visible && <OpenChannelModal network={network} />}
</Styled.Designer>
);
};

20
src/components/designer/lnd/LndDeposit.spec.tsx

@ -25,7 +25,7 @@ describe('LndDeposit', () => {
return {
...result,
input: result.container.querySelector('input') as HTMLInputElement,
btn: result.getByText('Send').parentElement as HTMLElement,
btn: result.getByText('Deposit').parentElement as HTMLElement,
};
};
@ -48,7 +48,7 @@ describe('LndDeposit', () => {
it('should render label', () => {
const { getByText } = renderComponent();
expect(getByText('Deposit BTC')).toBeInTheDocument();
expect(getByText('Deposit Funds (satoshis)')).toBeInTheDocument();
});
it('should render button', () => {
@ -63,14 +63,14 @@ describe('LndDeposit', () => {
expect(input).toBeInstanceOf(HTMLInputElement);
});
it('should use a default value of 1 for the input', () => {
it('should use a default value of 100000 for the input', () => {
const { input } = renderComponent();
expect(input.value).toEqual('1');
expect(input.value).toEqual('100000');
});
it('should deposit funds when the button is clicked', async () => {
const { input, btn, getByText } = renderComponent();
const amount = 5;
const amount = '250000';
fireEvent.change(input, { target: { value: amount } });
fireEvent.click(btn);
await waitForElement(() => getByText('Deposit successful'));
@ -78,7 +78,7 @@ describe('LndDeposit', () => {
expect(bitcoindServiceMock.sendFunds).toBeCalledWith(
expect.anything(),
'bc1aaaa',
amount,
0.0025,
);
});
@ -90,12 +90,4 @@ describe('LndDeposit', () => {
fireEvent.click(btn);
expect(await findByText(/connection failed/)).toBeInTheDocument();
});
it('should display an error if blocks is below 1', async () => {
const { input, btn, findByText } = renderComponent();
const numBlocks = -5;
fireEvent.change(input, { target: { value: numBlocks } });
fireEvent.click(btn);
expect(await findByText(/must be a positve number/)).toBeInTheDocument();
});
});

6
src/components/designer/lnd/LndDetails.tsx

@ -25,10 +25,6 @@ const LndDetails: React.FC<Props> = ({ node, onOpenChannel }) => {
);
const { nodes } = useStoreState(s => s.lnd);
if (getInfoAsync.loading) {
return <Loader />;
}
const details: DetailValues = [
{ label: 'Node Type', value: node.type },
{ label: 'Implementation', value: node.implementation },
@ -56,6 +52,8 @@ const LndDetails: React.FC<Props> = ({ node, onOpenChannel }) => {
{ label: 'Synced to Chain', value: `${syncedToChain}` },
);
}
} else if (getInfoAsync.loading) {
return <Loader />;
}
return (

55
src/lib/lnd/lndProxyClient.spec.ts

@ -0,0 +1,55 @@
import { ipcChannels } from 'shared';
import { IpcSender } from 'lib/ipc/ipcService';
import { getNetwork } from 'utils/tests';
import lndProxyClient from './lndProxyClient';
describe('LndService', () => {
const node = getNetwork().nodes.lightning[0];
let actualIpc: IpcSender;
beforeEach(() => {
actualIpc = lndProxyClient.ipc;
// mock the ipc dependency
lndProxyClient.ipc = jest.fn();
});
afterEach(() => {
// restore the actual ipc implementation
lndProxyClient.ipc = actualIpc;
});
it('should call the getInfo ipc', () => {
lndProxyClient.getInfo(node);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.getInfo, { node });
});
it('should call the getWalletBalance ipc', () => {
lndProxyClient.getWalletBalance(node);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.walletBalance, { node });
});
it('should call the getNewAddress ipc', () => {
lndProxyClient.getNewAddress(node);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.newAddress, { node });
});
it('should call the listPeers ipc', () => {
lndProxyClient.listPeers(node);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.listPeers, { node });
});
it('should call the connectPeer ipc', () => {
const req = { addr: { pubkey: 'abcdef', host: 'lnd-1' } };
lndProxyClient.connectPeer(node, req);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.connectPeer, { node, req });
});
it('should call the openChannel ipc', () => {
const req = {
nodePubkeyString: 'asdf',
localFundingAmount: '1000',
};
lndProxyClient.openChannel(node, req);
expect(lndProxyClient.ipc).toBeCalledWith(ipcChannels.openChannel, { node, req });
});
});

76
src/lib/lnd/lndService.spec.ts

@ -1,52 +1,76 @@
import { IpcSender } from 'lib/ipc/ipcService';
import { getNetwork } from 'utils/tests';
import lndProxyClient from './lndProxyClient';
import lndService from './lndService';
jest.mock('./lndProxyClient');
describe('LndService', () => {
const node = getNetwork().nodes.lightning[0];
let actualIpc: IpcSender;
const [node, node2] = getNetwork().nodes.lightning;
beforeEach(() => {
actualIpc = lndService.ipc;
// mock the ipc dependency
lndService.ipc = jest.fn();
it('should get node info', async () => {
const expected = { identityPubkey: 'asdf' };
lndProxyClient.getInfo = jest.fn().mockResolvedValue(expected);
const actual = await lndService.getInfo(node);
expect(actual).toEqual(expected);
});
afterEach(() => {
// restore the actual ipc implementation
lndService.ipc = actualIpc;
it('should get wallet balance', async () => {
const expected = { confirmedBalance: '1000' };
lndProxyClient.getWalletBalance = jest.fn().mockResolvedValue(expected);
const actual = await lndService.getWalletBalance(node);
expect(actual).toEqual(expected);
});
it('should call the getInfo ipc', () => {
lndService.getInfo(node);
expect(lndService.ipc).toBeCalledWith('get-info', { node });
it('should get new address', async () => {
const expected = { address: 'abcdef' };
lndProxyClient.getNewAddress = jest.fn().mockResolvedValue(expected);
const actual = await lndService.getNewAddress(node);
expect(actual).toEqual(expected);
});
it('should call the getWalletBalance ipc', () => {
lndService.getWalletBalance(node);
expect(lndService.ipc).toBeCalledWith('wallet-balance', { node });
});
describe('openChannel', () => {
it('should open the channel successfully', async () => {
lndProxyClient.getInfo = jest.fn().mockResolvedValue({ identityPubkey: 'asdf' });
lndProxyClient.listPeers = jest.fn().mockResolvedValue({
peers: [{ pubKey: 'asdf' }],
});
const expected = { fundingTxidStr: 'xyz' };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected);
const actual = await lndService.openChannel(node, node2, '1000');
expect(actual).toEqual(expected);
expect(lndProxyClient.getInfo).toBeCalledTimes(1);
expect(lndProxyClient.listPeers).toBeCalledTimes(1);
expect(lndProxyClient.connectPeer).toBeCalledTimes(0);
});
it('should call the getNewAddress ipc', () => {
lndService.getNewAddress(node);
expect(lndService.ipc).toBeCalledWith('new-address', { node });
it('should connect peer then open the channel', async () => {
lndProxyClient.getInfo = jest.fn().mockResolvedValue({ identityPubkey: 'asdf' });
lndProxyClient.listPeers = jest.fn().mockResolvedValue({
peers: [{ pubKey: 'fdsa' }],
});
const expected = { fundingTxidStr: 'xyz' };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected);
const actual = await lndService.openChannel(node, node2, '1000');
expect(actual).toEqual(expected);
expect(lndProxyClient.getInfo).toBeCalledTimes(1);
expect(lndProxyClient.listPeers).toBeCalledTimes(1);
expect(lndProxyClient.connectPeer).toBeCalledTimes(1);
});
});
describe('waitUntilOnline', () => {
it('should return true when successful', async () => {
const ipc = lndService.ipc as jest.Mock;
ipc.mockResolvedValue({});
lndProxyClient.getInfo = jest.fn().mockResolvedValue({});
const result = await lndService.waitUntilOnline(node);
expect(result).toBe(true);
expect(ipc).toBeCalledTimes(1);
expect(lndProxyClient.getInfo).toBeCalledTimes(1);
});
it('should return false on failure', async () => {
const ipc = lndService.ipc as jest.Mock;
ipc.mockRejectedValue(new Error());
lndProxyClient.getInfo = jest.fn().mockRejectedValue(new Error());
const result = await lndService.waitUntilOnline(node, 0.5, 1);
expect(result).toBe(false);
expect(ipc).toBeCalledTimes(5);
expect(lndProxyClient.getInfo).toBeCalledTimes(5);
});
});
});

4
src/store/models/lnd.spec.ts

@ -74,7 +74,7 @@ describe('LND Model', () => {
it('should be able to deposit funds using the backend bitcoin node', async () => {
const { depositFunds } = store.getActions().lnd;
await depositFunds({ node, amount: 1 });
await depositFunds({ node, sats: '50000' });
const nodeState = store.getState().lnd.nodes[node.name];
expect(nodeState.walletBalance).toBeTruthy();
const balances = nodeState.walletBalance as LND.WalletBalanceResponse;
@ -86,7 +86,7 @@ describe('LND Model', () => {
it('should be able to deposit funds using the first bitcoin node', async () => {
const { depositFunds } = store.getActions().lnd;
const modifiednode = { ...node, backendName: 'not-valid' };
await depositFunds({ node: modifiednode, amount: 1 });
await depositFunds({ node: modifiednode, sats: '50000' });
const nodeState = store.getState().lnd.nodes[node.name];
expect(nodeState.walletBalance).toBeTruthy();
const balances = nodeState.walletBalance as LND.WalletBalanceResponse;

Loading…
Cancel
Save