Browse Source

test(clightning): add and update unit tests for components

master
jamaljsr 5 years ago
parent
commit
54b4f9e958
  1. 59
      src/components/designer/lightning/ConnectTab.tsx
  2. 78
      src/components/designer/lightning/LightningDetails.spec.tsx
  3. 7
      src/components/designer/lightning/actions/OpenChannelModal.spec.tsx
  4. 6
      src/components/terminal/OpenTerminalButton.spec.tsx
  5. 1
      src/i18n/locales/en-US.json
  6. 1
      src/i18n/locales/es.json

59
src/components/designer/lightning/ConnectTab.tsx

@ -52,41 +52,44 @@ const ConnectTab: React.FC<Props> = ({ node }) => {
const nodeState = useStoreState(s => s.lightning.nodes[node.name]);
const lnUrl = nodeState && nodeState.info ? nodeState.info.rpcUrl : '';
const info = useMemo((): ConnectionInfo | undefined => {
if (node.status !== Status.Started) return;
if (node.implementation === 'LND') {
const lnd = node as LndNode;
return {
restUrl: `https://127.0.0.1:${lnd.ports.rest}`,
restDocsUrl: 'https://api.lightning.community/rest/',
grpcUrl: `127.0.0.1:${lnd.ports.grpc}`,
grpcDocsUrl: 'https://api.lightning.community/',
credentials: {
admin: lnd.paths.adminMacaroon,
readOnly: lnd.paths.readonlyMacaroon,
cert: lnd.paths.tlsCert,
},
};
} else if (node.implementation === 'c-lightning') {
const cln = node as CLightningNode;
return {
restUrl: `http://127.0.0.1:${cln.ports.rest}`,
restDocsUrl: 'https://github.com/Ride-The-Lightning/c-lightning-REST',
credentials: {
admin: cln.paths.macaroon,
},
};
const info = useMemo((): ConnectionInfo => {
if (node.status === Status.Started) {
if (node.implementation === 'LND') {
const lnd = node as LndNode;
return {
restUrl: `https://127.0.0.1:${lnd.ports.rest}`,
restDocsUrl: 'https://api.lightning.community/rest/',
grpcUrl: `127.0.0.1:${lnd.ports.grpc}`,
grpcDocsUrl: 'https://api.lightning.community/',
credentials: {
admin: lnd.paths.adminMacaroon,
readOnly: lnd.paths.readonlyMacaroon,
cert: lnd.paths.tlsCert,
},
};
} else if (node.implementation === 'c-lightning') {
const cln = node as CLightningNode;
return {
restUrl: `http://127.0.0.1:${cln.ports.rest}`,
restDocsUrl: 'https://github.com/Ride-The-Lightning/c-lightning-REST',
credentials: {
admin: cln.paths.macaroon,
},
};
}
}
return {
restUrl: '',
restDocsUrl: '',
credentials: {},
} as ConnectionInfo;
}, [node]);
if (node.status !== Status.Started) {
return <>{l('notStarted')}</>;
}
if (!info) {
return <>{l('unsupported', { implementation: node.implementation })}</>;
}
const { restUrl, grpcUrl, credentials } = info;
const hosts: DetailValues = [
[l('grpcHost'), grpcUrl, grpcUrl],

78
src/components/designer/lightning/LightningDetails.spec.tsx

@ -1,7 +1,8 @@
import React from 'react';
import { shell } from 'electron';
import { fireEvent, wait, waitForElement } from '@testing-library/dom';
import { Status } from 'shared/types';
import { LightningNode, Status } from 'shared/types';
import { Network } from 'types';
import * as files from 'utils/files';
import {
defaultStateBalances,
@ -15,10 +16,17 @@ import LightningDetails from './LightningDetails';
jest.mock('utils/files');
describe('LightningDetails', () => {
let network: Network;
let node: LightningNode;
const renderComponent = (status?: Status) => {
const network = getNetwork(1, 'test network', status);
if (status === Status.Error) {
network.nodes.lightning.forEach(n => (n.errorMsg = 'test-error'));
if (status !== undefined) {
network.status = status;
network.nodes.bitcoin.forEach(n => (n.status = status));
network.nodes.lightning.forEach(n => {
n.status = status;
n.errorMsg = status === Status.Error ? 'test-error' : undefined;
});
}
const initialState = {
network: {
@ -30,7 +38,6 @@ describe('LightningDetails', () => {
},
},
};
const node = network.nodes.lightning[0];
const cmp = <LightningDetails node={node} />;
const result = renderWithProviders(cmp, { initialState });
return {
@ -39,6 +46,11 @@ describe('LightningDetails', () => {
};
};
beforeEach(() => {
network = getNetwork(1, 'test network');
node = network.nodes.lightning[0];
});
describe('with node Stopped', () => {
it('should display Node Type', async () => {
const { findByText, node } = renderComponent();
@ -122,6 +134,21 @@ describe('LightningDetails', () => {
lightningServiceMock.getChannels.mockResolvedValue([]);
});
it('should display the sync warning', async () => {
lightningServiceMock.getInfo.mockResolvedValue(
defaultStateInfo({
alias: 'my-node',
pubkey: 'abcdef',
syncedToChain: false,
}),
);
const { findByText } = renderComponent(Status.Started);
fireEvent.click(await findByText('Info'));
expect(
await findByText('Not in sync with then chain. Mine a block'),
).toBeInTheDocument();
});
it('should display correct Status', async () => {
const { findByText, node } = renderComponent(Status.Started);
fireEvent.click(await findByText('Info'));
@ -221,6 +248,29 @@ describe('LightningDetails', () => {
expect(from).toEqual(node.name);
});
describe('c-lightning', () => {
beforeEach(() => {
node = network.nodes.lightning[2];
});
it('should display the REST Host', async () => {
const { getByText, findByText } = renderComponent(Status.Started);
fireEvent.click(await findByText('Connect'));
expect(getByText('REST Host')).toBeInTheDocument();
expect(getByText('http://127.0.0.1:8183')).toBeInTheDocument();
});
it('should open API Doc links in the browser', async () => {
shell.openExternal = jest.fn().mockResolvedValue(true);
const { getByText, findByText } = renderComponent(Status.Started);
fireEvent.click(await findByText('Connect'));
await wait(() => fireEvent.click(getByText('REST')));
expect(shell.openExternal).toBeCalledWith(
'https://github.com/Ride-The-Lightning/c-lightning-REST',
);
});
});
describe('connect options', () => {
const toggle = (container: HTMLElement, value: string) => {
fireEvent.click(
@ -228,6 +278,13 @@ describe('LightningDetails', () => {
);
};
it('should not fail with undefined node state', async () => {
lightningServiceMock.getInfo.mockResolvedValue(undefined as any);
const { queryByText, findByText } = renderComponent(Status.Started);
fireEvent.click(await findByText('Connect'));
expect(queryByText('http://127.0.0.1:8183')).toBeNull();
});
it('should display hex values for paths', async () => {
mockFiles.read.mockResolvedValue('test-hex');
const { findByText, container, getAllByText } = renderComponent(Status.Started);
@ -297,6 +354,17 @@ describe('LightningDetails', () => {
expect(getByText('Unable to create LND Connect url')).toBeInTheDocument();
expect(getByText('lndc-error')).toBeInTheDocument();
});
it('should properly handle an unknown implementation', async () => {
node.implementation = '' as any;
const { getByText, queryByText, findByText, container } = renderComponent(
Status.Started,
);
fireEvent.click(await findByText('Connect'));
await wait(() => toggle(container, 'base64'));
expect(getByText('API Docs')).toBeInTheDocument();
expect(queryByText('TLS Cert')).not.toBeInTheDocument();
});
});
});

7
src/components/designer/lightning/actions/OpenChannelModal.spec.tsx

@ -111,13 +111,12 @@ describe('OpenChannelModal', () => {
it('should do nothing if an invalid node is selected', async () => {
const { getByText, getByLabelText, store } = await renderComponent();
const btn = getByText('Open Channel');
await wait(() => {
store.getActions().modals.showOpenChannel({ from: 'invalid', to: 'invalid' });
store.getActions().modals.showOpenChannel({ from: 'invalid', to: 'invalid2' });
});
fireEvent.change(getByLabelText('Capacity (sats)'), { target: { value: '1000' } });
await wait(() => fireEvent.click(btn));
expect(btn).toBeInTheDocument();
await wait(() => fireEvent.click(getByText('Open Channel')));
expect(getByText('Open Channel')).toBeInTheDocument();
});
describe('with form submitted', () => {

6
src/components/terminal/OpenTerminalButton.spec.tsx

@ -34,6 +34,12 @@ describe('OpenTerminalButton', () => {
expect(help).toBeInTheDocument();
});
it('should render c-lightning help text', () => {
const { getByText } = renderComponent(n => n.nodes.lightning[2]);
const help = getByText("Run 'lightning-cli' commands directly on the node");
expect(help).toBeInTheDocument();
});
it('should send an ipc message when the button is clicked', async () => {
const ipcMock = injections.ipc as jest.Mock;
ipcMock.mockResolvedValue(true);

1
src/i18n/locales/en-US.json

@ -92,7 +92,6 @@
"cmps.designer.lightning.connect.LndConnect.encodeError": "Unable to create LND Connect url",
"cmps.designer.lightning.ActionsTab.notStarted": "Node needs to be started to perform actions on it",
"cmps.designer.lightning.ConnectTab.notStarted": "Node needs to be started to view connection info",
"cmps.designer.lightning.ConnectTab.unsupported": "{{implementation}} nodes are not supported yet",
"cmps.designer.lightning.ConnectTab.grpcHost": "GRPC Host",
"cmps.designer.lightning.ConnectTab.restHost": "REST Host",
"cmps.designer.lightning.ConnectTab.p2pLnUrl": "P2P LN Url",

1
src/i18n/locales/es.json

@ -92,7 +92,6 @@
"cmps.designer.lightning.connect.LndConnect.encodeError": "No se puede crear la URL de LND Connect",
"cmps.designer.lightning.ActionsTab.notStarted": "Nodo debe iniciarse para realizar acciones en él",
"cmps.designer.lightning.ConnectTab.notStarted": "Se necesita iniciar el nodo para ver la información de conexión",
"cmps.designer.lightning.ConnectTab.unsupported": "Los {{implementación}} no son compatibles todavía",
"cmps.designer.lightning.ConnectTab.grpcHost": "Host GRPC",
"cmps.designer.lightning.ConnectTab.restHost": "Host REST",
"cmps.designer.lightning.ConnectTab.p2pLnUrl": "P2P LN Url",

Loading…
Cancel
Save