|
|
@ -1,30 +1,37 @@ |
|
|
|
import React from 'react'; |
|
|
|
import { Route } from 'react-router'; |
|
|
|
import { fireEvent } from '@testing-library/dom'; |
|
|
|
import { createMemoryHistory } from 'history'; |
|
|
|
import { Status } from 'types'; |
|
|
|
import { getNetwork, injections, renderWithProviders } from 'utils/tests'; |
|
|
|
import NetworkView from './NetworkView'; |
|
|
|
|
|
|
|
describe('NetworkView Component', () => { |
|
|
|
const renderComponent = (route = '/network/1', status?: Status) => { |
|
|
|
const renderComponent = (id: string | undefined, status?: Status) => { |
|
|
|
const initialState = { |
|
|
|
network: { |
|
|
|
networks: [getNetwork(1, 'test network', status)], |
|
|
|
}, |
|
|
|
}; |
|
|
|
// NetworkView needs to be rendered by a route due to
|
|
|
|
// RouteComponentProps not being easily mockable
|
|
|
|
const cmp = <Route path="/network/:id" component={NetworkView} />; |
|
|
|
const route = `/network/${id}`; |
|
|
|
const history = createMemoryHistory({ initialEntries: [route] }); |
|
|
|
const location = { pathname: route, search: '', hash: '', state: undefined }; |
|
|
|
const match = { params: { id }, isExact: true, path: '', url: route }; |
|
|
|
const cmp = <NetworkView history={history} location={location} match={match} />; |
|
|
|
return renderWithProviders(cmp, { initialState, route }); |
|
|
|
}; |
|
|
|
|
|
|
|
it('should not render if the network is not found', () => { |
|
|
|
const { queryByText } = renderComponent('/network/99'); |
|
|
|
const { queryByText } = renderComponent('99'); |
|
|
|
expect(queryByText('test network')).toBeNull(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should not render if the network id is not provided', () => { |
|
|
|
const { queryByText } = renderComponent(undefined); |
|
|
|
expect(queryByText('test network')).toBeNull(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should render the name', () => { |
|
|
|
const { getByText } = renderComponent(); |
|
|
|
const { getByText } = renderComponent('1'); |
|
|
|
expect(getByText('test network')).toBeInTheDocument(); |
|
|
|
}); |
|
|
|
|
|
|
@ -33,13 +40,13 @@ describe('NetworkView Component', () => { |
|
|
|
// mock dockerService.start to throw an error
|
|
|
|
const mockDockerStart = injections.dockerService.start as jest.Mock; |
|
|
|
mockDockerStart.mockRejectedValueOnce(new Error(errorMsg)); |
|
|
|
const { getByText, findByText } = renderComponent('/network/1'); |
|
|
|
const { getByText, findByText } = renderComponent('1'); |
|
|
|
fireEvent.click(getByText('cmps.network-actions.primary-btn-start')); |
|
|
|
expect(await findByText(errorMsg)).toBeInTheDocument(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should change UI when network is started', async () => { |
|
|
|
const { getByText, findByText } = renderComponent(); |
|
|
|
const { getByText, findByText } = renderComponent('1'); |
|
|
|
expect(getByText('cmps.status-tag.status-stopped')).toBeInTheDocument(); |
|
|
|
fireEvent.click(getByText('cmps.network-actions.primary-btn-start')); |
|
|
|
// should switch to starting immediately
|
|
|
@ -49,7 +56,7 @@ describe('NetworkView Component', () => { |
|
|
|
}); |
|
|
|
|
|
|
|
it('should change UI when network is stopped', async () => { |
|
|
|
const { getByText, findByText } = renderComponent('/network/1', Status.Started); |
|
|
|
const { getByText, findByText } = renderComponent('1', Status.Started); |
|
|
|
expect(getByText('cmps.status-tag.status-started')).toBeInTheDocument(); |
|
|
|
fireEvent.click(getByText('cmps.network-actions.primary-btn-stop')); |
|
|
|
// should switch to stopping immediately
|
|
|
@ -59,7 +66,7 @@ describe('NetworkView Component', () => { |
|
|
|
}); |
|
|
|
|
|
|
|
it('should do nothing when network is starting', async () => { |
|
|
|
const { getByText } = renderComponent('/network/1', Status.Starting); |
|
|
|
const { getByText } = renderComponent('1', Status.Starting); |
|
|
|
expect(getByText('cmps.status-tag.status-starting')).toBeInTheDocument(); |
|
|
|
fireEvent.click(getByText('cmps.network-actions.primary-btn-starting')); |
|
|
|
// should switch to stopping immediately
|
|
|
|