mirror of https://github.com/lukechilds/polar.git
jamaljsr
5 years ago
7 changed files with 375 additions and 63 deletions
@ -0,0 +1,78 @@ |
|||
import React from 'react'; |
|||
import { ILink } from '@mrblenny/react-flow-chart'; |
|||
import { fireEvent } from '@testing-library/dom'; |
|||
import { initChartFromNetwork } from 'utils/chart'; |
|||
import { getNetwork, renderWithProviders } from 'utils/tests'; |
|||
import LinkContextMenu from './LinkContextMenu'; |
|||
|
|||
describe('LinkContextMenu', () => { |
|||
const createChannelLink = (): ILink => ({ |
|||
id: 'alice-carol', |
|||
from: { nodeId: 'alice', portId: 'alice-carol' }, |
|||
to: { nodeId: 'carol', portId: 'alice-carol' }, |
|||
properties: { |
|||
type: 'open-channel', |
|||
capacity: '1000', |
|||
fromBalance: '600', |
|||
toBalance: '400', |
|||
direction: 'ltr', |
|||
status: 'Open', |
|||
}, |
|||
}); |
|||
const createBackendLink = (): ILink => ({ |
|||
id: 'alice-backend1', |
|||
from: { nodeId: 'alice', portId: 'alice-backend1' }, |
|||
to: { nodeId: 'backend1', portId: 'alice-backend1' }, |
|||
properties: { |
|||
type: 'backend', |
|||
}, |
|||
}); |
|||
const renderComponent = (link: ILink) => { |
|||
const network = getNetwork(1, 'test network'); |
|||
const chart = initChartFromNetwork(network); |
|||
chart.links[link.id] = link; |
|||
const initialState = { |
|||
network: { |
|||
networks: [network], |
|||
}, |
|||
designer: { |
|||
activeId: network.id, |
|||
allCharts: { |
|||
[network.id]: chart, |
|||
}, |
|||
}, |
|||
}; |
|||
const cmp = ( |
|||
<LinkContextMenu link={link}> |
|||
<span>test-child</span> |
|||
</LinkContextMenu> |
|||
); |
|||
const result = renderWithProviders(cmp, { initialState }); |
|||
// always open the context menu for all tests
|
|||
fireEvent.contextMenu(result.getByText('test-child')); |
|||
return result; |
|||
}; |
|||
|
|||
it('should display the correct options for an open channel', async () => { |
|||
const { getByText } = renderComponent(createChannelLink()); |
|||
expect(getByText('Close Channel')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should display the correct options for a backend connection', async () => { |
|||
const { getByText } = renderComponent(createBackendLink()); |
|||
expect(getByText('Change Backend')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should not display a menu for an invalid link', async () => { |
|||
const { queryByText } = renderComponent({} as ILink); |
|||
expect(queryByText('Close Channel')).not.toBeInTheDocument(); |
|||
expect(queryByText('Change Backend')).not.toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should not display Close Channel an invalid node', async () => { |
|||
const link = createChannelLink(); |
|||
link.from.nodeId = 'invalid'; |
|||
const { queryByText } = renderComponent(link); |
|||
expect(queryByText('Close Channel')).not.toBeInTheDocument(); |
|||
}); |
|||
}); |
@ -0,0 +1,165 @@ |
|||
import React from 'react'; |
|||
import { fireEvent } from '@testing-library/dom'; |
|||
import { act } from '@testing-library/react'; |
|||
import { ipcChannels } from 'shared'; |
|||
import { Status } from 'shared/types'; |
|||
import { initChartFromNetwork } from 'utils/chart'; |
|||
import { getNetwork, injections, renderWithProviders } from 'utils/tests'; |
|||
import NodeContextMenu from './NodeContextMenu'; |
|||
|
|||
describe('NodeContextMenu', () => { |
|||
const renderComponent = (nodeName: string, status?: Status) => { |
|||
const network = getNetwork(1, 'test network', status); |
|||
const chart = initChartFromNetwork(network); |
|||
if (nodeName === 'invalid') { |
|||
chart.nodes.alice.id = 'invalid'; |
|||
nodeName = 'alice'; |
|||
} |
|||
const initialState = { |
|||
network: { |
|||
networks: [network], |
|||
}, |
|||
designer: { |
|||
activeId: network.id, |
|||
allCharts: { |
|||
[network.id]: chart, |
|||
}, |
|||
}, |
|||
}; |
|||
const cmp = ( |
|||
<NodeContextMenu node={chart.nodes[nodeName]}> |
|||
<span>test-child</span> |
|||
</NodeContextMenu> |
|||
); |
|||
const result = renderWithProviders(cmp, { initialState }); |
|||
// always open the context menu for all tests
|
|||
fireEvent.contextMenu(result.getByText('test-child')); |
|||
return result; |
|||
}; |
|||
|
|||
it('should display the correct options for a started lightning node', async () => { |
|||
const { getByText } = renderComponent('alice', Status.Started); |
|||
expect(getByText('Create Invoice')).toBeInTheDocument(); |
|||
expect(getByText('Pay Invoice')).toBeInTheDocument(); |
|||
expect(getByText('Open Outgoing Channel')).toBeInTheDocument(); |
|||
expect(getByText('Open Incoming Channel')).toBeInTheDocument(); |
|||
expect(getByText('Launch Terminal')).toBeInTheDocument(); |
|||
expect(getByText('Stop')).toBeInTheDocument(); |
|||
expect(getByText('Advanced Options')).toBeInTheDocument(); |
|||
expect(getByText('Remove')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should display the correct options for a stopped lightning node', async () => { |
|||
const { getByText, queryByText } = renderComponent('alice', Status.Stopped); |
|||
expect(queryByText('Create Invoice')).not.toBeInTheDocument(); |
|||
expect(queryByText('Pay Invoice')).not.toBeInTheDocument(); |
|||
expect(queryByText('Open Outgoing Channel')).not.toBeInTheDocument(); |
|||
expect(queryByText('Open Incoming Channel')).not.toBeInTheDocument(); |
|||
expect(queryByText('Launch Terminal')).not.toBeInTheDocument(); |
|||
expect(getByText('Start')).toBeInTheDocument(); |
|||
expect(getByText('Advanced Options')).toBeInTheDocument(); |
|||
expect(getByText('Remove')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should display the correct options for a started bitcoin node', async () => { |
|||
const { getByText } = renderComponent('backend1', Status.Started); |
|||
expect(getByText('Launch Terminal')).toBeInTheDocument(); |
|||
expect(getByText('Stop')).toBeInTheDocument(); |
|||
expect(getByText('Advanced Options')).toBeInTheDocument(); |
|||
expect(getByText('Remove')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should display the correct options for a stopped bitcoin node', async () => { |
|||
const { getByText, queryByText } = renderComponent('backend1', Status.Stopped); |
|||
expect(queryByText('Launch Terminal')).not.toBeInTheDocument(); |
|||
expect(getByText('Start')).toBeInTheDocument(); |
|||
expect(getByText('Advanced Options')).toBeInTheDocument(); |
|||
expect(getByText('Remove')).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should display a menu for an invalid node', async () => { |
|||
const { queryByText } = renderComponent('invalid', Status.Started); |
|||
expect(queryByText('Create Invoice')).not.toBeInTheDocument(); |
|||
expect(queryByText('Pay Invoice')).not.toBeInTheDocument(); |
|||
expect(queryByText('Open Outgoing Channel')).not.toBeInTheDocument(); |
|||
expect(queryByText('Open Incoming Channel')).not.toBeInTheDocument(); |
|||
expect(queryByText('Launch Terminal')).not.toBeInTheDocument(); |
|||
expect(queryByText('Stop')).not.toBeInTheDocument(); |
|||
expect(queryByText('Advanced Options')).not.toBeInTheDocument(); |
|||
expect(queryByText('Remove')).not.toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should show the create invoice modal', async () => { |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.createInvoice.visible).toBe(false); |
|||
fireEvent.click(getByText('Create Invoice')); |
|||
expect(store.getState().modals.createInvoice.visible).toBe(true); |
|||
}); |
|||
|
|||
it('should show the pay invoice modal', async () => { |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.payInvoice.visible).toBe(false); |
|||
fireEvent.click(getByText('Pay Invoice')); |
|||
expect(store.getState().modals.payInvoice.visible).toBe(true); |
|||
}); |
|||
|
|||
it('should show the open outgoing channel modal', async () => { |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.openChannel.visible).toBe(false); |
|||
fireEvent.click(getByText('Open Outgoing Channel')); |
|||
expect(store.getState().modals.openChannel.visible).toBe(true); |
|||
expect(store.getState().modals.openChannel.from).toBe('alice'); |
|||
}); |
|||
|
|||
it('should show the open incoming channel modal', async () => { |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.openChannel.visible).toBe(false); |
|||
fireEvent.click(getByText('Open Incoming Channel')); |
|||
expect(store.getState().modals.openChannel.visible).toBe(true); |
|||
expect(store.getState().modals.openChannel.to).toBe('alice'); |
|||
}); |
|||
|
|||
it('should open the terminal', async () => { |
|||
const ipcMock = injections.ipc as jest.Mock; |
|||
ipcMock.mockResolvedValue(true); |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.openChannel.visible).toBe(false); |
|||
await act(async () => { |
|||
fireEvent.click(getByText('Launch Terminal')); |
|||
}); |
|||
const url = '/terminal/LND/polar-n1-alice'; |
|||
expect(ipcMock).toBeCalledWith(ipcChannels.openWindow, { url }); |
|||
}); |
|||
|
|||
it('should show the start node confirmation modal', async () => { |
|||
const { getByText, findByText } = renderComponent('alice', Status.Stopped); |
|||
fireEvent.click(getByText('Start')); |
|||
expect( |
|||
await findByText('Would you like to start the alice node?'), |
|||
).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should show the stop node confirmation modal', async () => { |
|||
const { getByText, findByText } = renderComponent('alice', Status.Started); |
|||
fireEvent.click(getByText('Stop')); |
|||
expect( |
|||
await findByText('Are you sure you want to stop the alice node?'), |
|||
).toBeInTheDocument(); |
|||
}); |
|||
|
|||
it('should show the advanced options modal', async () => { |
|||
const { getByText, store } = renderComponent('alice', Status.Started); |
|||
expect(store.getState().modals.advancedOptions.visible).toBe(false); |
|||
fireEvent.click(getByText('Advanced Options')); |
|||
expect(store.getState().modals.advancedOptions.visible).toBe(true); |
|||
expect(store.getState().modals.advancedOptions.nodeName).toBe('alice'); |
|||
}); |
|||
|
|||
it('should show the remove node confirmation modal', async () => { |
|||
const { getByText, findByText } = renderComponent('alice', Status.Started); |
|||
fireEvent.click(getByText('Remove')); |
|||
expect( |
|||
await findByText('Are you sure you want to remove alice from the network?'), |
|||
).toBeInTheDocument(); |
|||
}); |
|||
}); |
Loading…
Reference in new issue