2020-08-29 21:20:45 +03:00
|
|
|
import { mount, ReactWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2020-03-15 15:43:12 +03:00
|
|
|
import { EditServer as editServerConstruct } from '../../src/servers/EditServer';
|
|
|
|
import { ServerForm } from '../../src/servers/helpers/ServerForm';
|
2020-08-29 21:20:45 +03:00
|
|
|
import { ReachableServer } from '../../src/servers/data';
|
2020-03-15 15:43:12 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
2020-03-15 15:43:12 +03:00
|
|
|
describe('<EditServer />', () => {
|
2020-08-29 21:20:45 +03:00
|
|
|
let wrapper: ReactWrapper;
|
2020-03-15 15:43:12 +03:00
|
|
|
const ServerError = jest.fn();
|
|
|
|
const editServerMock = jest.fn();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2020-08-29 21:20:45 +03:00
|
|
|
const selectedServer = Mock.of<ReachableServer>({
|
2020-03-15 15:43:12 +03:00
|
|
|
id: 'abc123',
|
|
|
|
name: 'name',
|
|
|
|
url: 'url',
|
|
|
|
apiKey: 'apiKey',
|
2020-08-29 21:20:45 +03:00
|
|
|
});
|
2022-02-08 00:17:57 +03:00
|
|
|
const EditServer = editServerConstruct(ServerError);
|
2020-03-15 15:43:12 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
2020-03-15 15:43:12 +03:00
|
|
|
|
|
|
|
wrapper = mount(
|
2022-02-08 00:17:57 +03:00
|
|
|
<EditServer editServer={editServerMock} selectedServer={selectedServer} selectServer={jest.fn()} />,
|
2020-03-15 15:43:12 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2020-08-29 21:20:45 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2020-03-15 15:43:12 +03:00
|
|
|
|
|
|
|
it('renders components', () => {
|
|
|
|
expect(wrapper.find(ServerForm)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('edits server and redirects to it when form is submitted', () => {
|
|
|
|
const form = wrapper.find(ServerForm);
|
|
|
|
|
|
|
|
form.simulate('submit', {});
|
|
|
|
|
|
|
|
expect(editServerMock).toHaveBeenCalledTimes(1);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2020-03-15 15:43:12 +03:00
|
|
|
});
|
|
|
|
});
|