|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import SimulationDesignerTab from './SimulationDesignerTab'; |
| 4 | +import { Status } from 'shared/types'; |
| 5 | +import { injections, renderWithProviders, testManagedImages } from 'utils/tests'; |
| 6 | +import { createNetwork } from 'utils/network'; |
| 7 | +import { defaultRepoState } from 'utils/constants'; |
| 8 | +import { initChartFromNetwork } from 'utils/chart'; |
| 9 | + |
| 10 | +const mockDockerService = injections.dockerService as jest.Mocked< |
| 11 | + typeof injections.dockerService |
| 12 | +>; |
| 13 | + |
| 14 | +describe('SimulationDesignerTab', () => { |
| 15 | + const renderComponent = (status?: Status, simStatus?: Status, noSim?: boolean) => { |
| 16 | + const network = createNetwork({ |
| 17 | + id: 1, |
| 18 | + name: 'test network', |
| 19 | + description: 'network description', |
| 20 | + lndNodes: 2, |
| 21 | + clightningNodes: 0, |
| 22 | + eclairNodes: 0, |
| 23 | + litdNodes: 0, |
| 24 | + bitcoindNodes: 2, |
| 25 | + tapdNodes: 0, |
| 26 | + repoState: defaultRepoState, |
| 27 | + managedImages: testManagedImages, |
| 28 | + customImages: [], |
| 29 | + status, |
| 30 | + }); |
| 31 | + |
| 32 | + if (!noSim) { |
| 33 | + network.simulation = { |
| 34 | + networkId: 1, |
| 35 | + source: network.nodes.lightning[0], |
| 36 | + destination: network.nodes.lightning[1], |
| 37 | + intervalSecs: 10, |
| 38 | + amountMsat: 1000000, |
| 39 | + status: simStatus ?? Status.Stopped, |
| 40 | + }; |
| 41 | + } |
| 42 | + const chart = initChartFromNetwork(network); |
| 43 | + |
| 44 | + const initialState = { |
| 45 | + network: { |
| 46 | + networks: [network], |
| 47 | + }, |
| 48 | + modals: { |
| 49 | + addSimulation: { |
| 50 | + visible: false, |
| 51 | + }, |
| 52 | + }, |
| 53 | + designer: { |
| 54 | + allCharts: { |
| 55 | + [network.id]: chart, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + const result = renderWithProviders(<SimulationDesignerTab network={network} />, { |
| 61 | + initialState, |
| 62 | + }); |
| 63 | + |
| 64 | + return { |
| 65 | + ...result, |
| 66 | + network, |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + describe('Simulation Designer Tab', () => { |
| 71 | + it('should render the simulation designer tab', async () => { |
| 72 | + const { getByText, store } = renderComponent(Status.Started, Status.Started, true); |
| 73 | + expect(getByText('No simulations created yet')).toBeInTheDocument(); |
| 74 | + |
| 75 | + const addSimulationBtn = getByText('Add a new Simulation'); |
| 76 | + expect(addSimulationBtn).toBeInTheDocument(); |
| 77 | + fireEvent.click(addSimulationBtn); |
| 78 | + await waitFor(() => { |
| 79 | + expect(store.getState().modals.addSimulation.visible).toBe(true); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should show add simulation button if no simulation is defined', async () => { |
| 84 | + const { getByText } = renderComponent(Status.Started, Status.Started, true); |
| 85 | + expect(getByText('No simulations created yet')).toBeInTheDocument(); |
| 86 | + |
| 87 | + const addSimulationBtn = getByText('Add a new Simulation'); |
| 88 | + expect(addSimulationBtn).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('Start Simulation', () => { |
| 93 | + it('should start simulation successfully', async () => { |
| 94 | + const { getByText } = renderComponent(Status.Started); |
| 95 | + expect(getByText('Start')).toBeInTheDocument(); |
| 96 | + |
| 97 | + fireEvent.click(getByText('Start')); |
| 98 | + await waitFor(() => { |
| 99 | + expect(mockDockerService.startSimulation).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should show error if simulation fails to start', async () => { |
| 104 | + // Simulation should fail to start because the network is not started, |
| 105 | + // which means the lightning nodes are not running. |
| 106 | + const { getByText } = renderComponent(Status.Stopped); |
| 107 | + fireEvent.click(getByText('Start')); |
| 108 | + |
| 109 | + await waitFor(() => { |
| 110 | + expect(getByText('Unable to start the simulation')).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('Stop Simulation', () => { |
| 116 | + it('should stop simulation successfully', async () => { |
| 117 | + const { getByText } = renderComponent(Status.Started, Status.Started); |
| 118 | + expect(getByText('Stop')).toBeInTheDocument(); |
| 119 | + fireEvent.click(getByText('Stop')); |
| 120 | + await waitFor(() => { |
| 121 | + expect(mockDockerService.stopSimulation).toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should show error if simulation fails to stop', async () => { |
| 126 | + mockDockerService.stopSimulation.mockRejectedValue(new Error('simulation-error')); |
| 127 | + const { getByText } = renderComponent(Status.Started, Status.Started); |
| 128 | + fireEvent.click(getByText('Stop')); |
| 129 | + |
| 130 | + await waitFor(() => { |
| 131 | + expect(getByText('Unable to stop the simulation')).toBeInTheDocument(); |
| 132 | + expect(getByText('simulation-error')).toBeInTheDocument(); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('Remove Simulation', () => { |
| 138 | + it('should remove simulation', async () => { |
| 139 | + const { getByText, getByRole } = renderComponent(Status.Started); |
| 140 | + fireEvent.click(getByRole('remove')); |
| 141 | + await waitFor(() => { |
| 142 | + expect(getByText('Remove Simulation')).toBeInTheDocument(); |
| 143 | + }); |
| 144 | + |
| 145 | + fireEvent.click(getByText('Remove')); |
| 146 | + await waitFor(() => { |
| 147 | + expect(mockDockerService.removeSimulation).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should show error if simulation fails to remove', async () => { |
| 152 | + mockDockerService.removeSimulation.mockRejectedValue(new Error('simulation-error')); |
| 153 | + const { getByRole, getByText } = renderComponent(Status.Started); |
| 154 | + fireEvent.click(getByRole('remove')); |
| 155 | + await waitFor(() => { |
| 156 | + expect(getByText('Remove Simulation')).toBeInTheDocument(); |
| 157 | + expect(getByText('Remove')).toBeInTheDocument(); |
| 158 | + expect(getByText('Cancel')).toBeInTheDocument(); |
| 159 | + }); |
| 160 | + fireEvent.click(getByText('Remove')); |
| 161 | + await waitFor(() => { |
| 162 | + expect(getByText('Failed to remove simulation')).toBeInTheDocument(); |
| 163 | + expect(getByText('simulation-error')).toBeInTheDocument(); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should return early if no simulation is defined', async () => { |
| 168 | + const { getByText, getByRole, network } = renderComponent( |
| 169 | + Status.Started, |
| 170 | + Status.Stopped, |
| 171 | + ); |
| 172 | + const sim = { |
| 173 | + networkId: 1, |
| 174 | + source: network.nodes.lightning[0], |
| 175 | + destination: network.nodes.lightning[1], |
| 176 | + intervalSecs: 10, |
| 177 | + amountMsat: 1000000, |
| 178 | + status: Status.Stopped, |
| 179 | + }; |
| 180 | + |
| 181 | + // This is an unlikely scenario, as the start button is not |
| 182 | + // visible if no simulation is defined. |
| 183 | + network.simulation = undefined; |
| 184 | + expect(getByText('Start')).toBeInTheDocument(); |
| 185 | + fireEvent.click(getByText('Start')); |
| 186 | + expect(getByText('No simulations created yet')).toBeInTheDocument(); |
| 187 | + |
| 188 | + network.simulation = { ...sim, status: Status.Started }; |
| 189 | + await waitFor(() => { |
| 190 | + expect(getByText('Stop')).toBeInTheDocument(); |
| 191 | + }); |
| 192 | + network.simulation = undefined; |
| 193 | + expect(getByText('Stop')).toBeInTheDocument(); |
| 194 | + fireEvent.click(getByText('Stop')); |
| 195 | + expect(getByText('No simulations created yet')).toBeInTheDocument(); |
| 196 | + |
| 197 | + network.simulation = { ...sim, status: Status.Stopped }; |
| 198 | + await waitFor(() => { |
| 199 | + expect(getByText('Start')).toBeInTheDocument(); |
| 200 | + }); |
| 201 | + expect(getByRole('remove')).toBeInTheDocument(); |
| 202 | + fireEvent.click(getByRole('remove')); |
| 203 | + await waitFor(() => { |
| 204 | + expect(getByText('Remove Simulation')).toBeInTheDocument(); |
| 205 | + }); |
| 206 | + |
| 207 | + network.simulation = undefined; |
| 208 | + fireEvent.click(getByText('Remove')); |
| 209 | + await waitFor(() => { |
| 210 | + expect(mockDockerService.removeSimulation).not.toHaveBeenCalled(); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments