|
|
@ -1,11 +1,13 @@ |
|
|
|
import React from 'react'; |
|
|
|
import { fireEvent, wait } from '@testing-library/dom'; |
|
|
|
import fsExtra from 'fs-extra'; |
|
|
|
import { fireEvent, wait, waitForElement } from '@testing-library/dom'; |
|
|
|
import { createMemoryHistory } from 'history'; |
|
|
|
import { Status } from 'types'; |
|
|
|
import { initChartFromNetwork } from 'utils/chart'; |
|
|
|
import { getNetwork, injections, renderWithProviders } from 'utils/tests'; |
|
|
|
import NetworkView from './NetworkView'; |
|
|
|
|
|
|
|
const fsMock = fsExtra as jest.Mocked<typeof fsExtra>; |
|
|
|
const lndServiceMock = injections.lndService as jest.Mocked<typeof injections.lndService>; |
|
|
|
const bitcoindServiceMock = injections.bitcoindService as jest.Mocked< |
|
|
|
typeof injections.bitcoindService |
|
|
@ -135,24 +137,79 @@ describe('NetworkView Component', () => { |
|
|
|
const input = await findByDisplayValue('test network'); |
|
|
|
fireEvent.change(input, { target: { value: 'new network name' } }); |
|
|
|
fireEvent.click(getByText('Save')); |
|
|
|
await wait(() => { |
|
|
|
expect(store.getState().network.networkById(1).name).toBe('new network name'); |
|
|
|
}); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
expect(store.getState().network.networkById(1).name).toBe('new network name'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should display an error if renaming fails', async () => { |
|
|
|
const { getByLabelText, getByText, findByDisplayValue, store } = renderComponent( |
|
|
|
'1', |
|
|
|
); |
|
|
|
const { getByLabelText, getByText, findByDisplayValue } = renderComponent('1'); |
|
|
|
fireEvent.mouseOver(getByLabelText('icon: more')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
fireEvent.click(getByText('Rename')); |
|
|
|
const input = await findByDisplayValue('test network'); |
|
|
|
fireEvent.change(input, { target: { value: '' } }); |
|
|
|
fireEvent.click(getByText('Save')); |
|
|
|
await wait(() => { |
|
|
|
expect(getByText('Failed to rename the network')).toBeInTheDocument(); |
|
|
|
}); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
expect(getByText('Failed to rename the network')).toBeInTheDocument(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
describe('delete network', () => { |
|
|
|
beforeEach(jest.useFakeTimers); |
|
|
|
afterEach(jest.useRealTimers); |
|
|
|
|
|
|
|
it('should show the confirm modal', async () => { |
|
|
|
const { getByLabelText, getByText } = renderComponent('1'); |
|
|
|
fireEvent.mouseOver(getByLabelText('icon: more')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
fireEvent.click(getByText('Delete')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
expect( |
|
|
|
getByText('Are you sure you want to delete this network?'), |
|
|
|
).toBeInTheDocument(); |
|
|
|
expect(getByText('Yes')).toBeInTheDocument(); |
|
|
|
expect(getByText('Cancel')).toBeInTheDocument(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should delete the network', async () => { |
|
|
|
const { getByLabelText, getByText, getAllByText, store } = renderComponent( |
|
|
|
'1', |
|
|
|
Status.Started, |
|
|
|
); |
|
|
|
const path = store.getState().network.networks[0].path; |
|
|
|
fireEvent.mouseOver(getByLabelText('icon: more')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
fireEvent.click(getByText('Delete')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
// antd creates two modals in the DOM for some silly reason. Need to click one
|
|
|
|
fireEvent.click(getAllByText('Yes')[0]); |
|
|
|
// wait for the error notification to be displayed
|
|
|
|
await waitForElement(() => getByLabelText('icon: check-circle-o')); |
|
|
|
expect( |
|
|
|
getByText("The network 'test network' and its data has been deleted!"), |
|
|
|
).toBeInTheDocument(); |
|
|
|
expect(fsMock.remove).toBeCalledWith(expect.stringContaining(path)); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should display an error if the delete fails', async () => { |
|
|
|
// antd Modal.confirm logs a console error when onOk fails
|
|
|
|
// this supresses those errors from being displayed in test runs
|
|
|
|
const oldConsoleErr = console.error; |
|
|
|
console.error = () => {}; |
|
|
|
fsMock.remove = jest.fn().mockRejectedValue(new Error('cannot delete')); |
|
|
|
const { getByLabelText, getByText, getAllByText, store } = renderComponent('1'); |
|
|
|
fireEvent.mouseOver(getByLabelText('icon: more')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
fireEvent.click(getByText('Delete')); |
|
|
|
await wait(() => jest.runOnlyPendingTimers()); |
|
|
|
// antd creates two modals in the DOM for some silly reason. Need to click one
|
|
|
|
fireEvent.click(getAllByText('Yes')[0]); |
|
|
|
// wait for the error notification to be displayed
|
|
|
|
await waitForElement(() => getByLabelText('icon: close-circle-o')); |
|
|
|
expect(getByText('cannot delete')).toBeInTheDocument(); |
|
|
|
expect(store.getState().network.networks).toHaveLength(1); |
|
|
|
expect(store.getState().designer.allCharts[1]).toBeDefined(); |
|
|
|
console.error = oldConsoleErr; |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|