diff --git a/src/servers/helpers/ServerForm.tsx b/src/servers/helpers/ServerForm.tsx index f13b5b4a..5c781069 100644 --- a/src/servers/helpers/ServerForm.tsx +++ b/src/servers/helpers/ServerForm.tsx @@ -23,7 +23,7 @@ export const ServerForm: FC = ({ onSubmit, initialValues, child }, [initialValues]); return ( -
+ Name URL diff --git a/test/servers/EditServer.test.tsx b/test/servers/EditServer.test.tsx index abd60589..818578bc 100644 --- a/test/servers/EditServer.test.tsx +++ b/test/servers/EditServer.test.tsx @@ -1,46 +1,65 @@ -import { mount, ReactWrapper } from 'enzyme'; +import { fireEvent, render, screen } from '@testing-library/react'; import { Mock } from 'ts-mockery'; import { useNavigate } from 'react-router-dom'; import { EditServer as editServerConstruct } from '../../src/servers/EditServer'; -import { ServerForm } from '../../src/servers/helpers/ServerForm'; -import { ReachableServer } from '../../src/servers/data'; +import { ReachableServer, SelectedServer } from '../../src/servers/data'; jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() })); describe('', () => { - let wrapper: ReactWrapper; const ServerError = jest.fn(); const editServerMock = jest.fn(); const navigate = jest.fn(); - const selectedServer = Mock.of({ + const defaultSelectedServer = Mock.of({ id: 'abc123', - name: 'name', - url: 'url', - apiKey: 'apiKey', + name: 'the_name', + url: 'the_url', + apiKey: 'the_api_key', }); const EditServer = editServerConstruct(ServerError); + const setUp = (selectedServer: SelectedServer = defaultSelectedServer) => render( + , + ); beforeEach(() => { (useNavigate as any).mockReturnValue(navigate); - - wrapper = mount( - , - ); }); afterEach(jest.clearAllMocks); - afterEach(() => wrapper?.unmount()); - it('renders components', () => { - expect(wrapper.find(ServerForm)).toHaveLength(1); + it('renders nothing if selected server is not reachable', () => { + setUp(Mock.all()); + + expect(screen.queryByText('Edit')).not.toBeInTheDocument(); + expect(screen.queryByText('Cancel')).not.toBeInTheDocument(); + expect(screen.queryByText('Save')).not.toBeInTheDocument(); + }); + + it('renders server title', () => { + setUp(); + expect(screen.getByText(`Edit "${defaultSelectedServer.name}"`)).toBeInTheDocument(); + }); + + it('display the server info in the form components', () => { + setUp(); + + expect(screen.getByDisplayValue('the_name')).toBeInTheDocument(); + expect(screen.getByDisplayValue('the_url')).toBeInTheDocument(); + expect(screen.getByDisplayValue('the_api_key')).toBeInTheDocument(); }); it('edits server and redirects to it when form is submitted', () => { - const form = wrapper.find(ServerForm); + setUp(); - form.simulate('submit', {}); + fireEvent.change(screen.getByDisplayValue('the_name'), { target: { value: 'the_new_name' } }); + fireEvent.change(screen.getByDisplayValue('the_url'), { target: { value: 'the_new_url' } }); + fireEvent.submit(screen.getByRole('form')); - expect(editServerMock).toHaveBeenCalledTimes(1); + expect(editServerMock).toHaveBeenCalledWith('abc123', { + name: 'the_new_name', + url: 'the_new_url', + apiKey: 'the_api_key', + }); expect(navigate).toHaveBeenCalledWith(-1); }); });