2018-08-25 00:19:49 +03:00
|
|
|
import React from 'react';
|
2020-08-22 14:41:54 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2018-08-25 00:19:49 +03:00
|
|
|
import { identity } from 'ramda';
|
2020-08-22 14:41:54 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { History } from 'history';
|
2018-12-18 00:32:51 +03:00
|
|
|
import createServerConstruct from '../../src/servers/CreateServer';
|
2020-03-15 13:59:07 +03:00
|
|
|
import { ServerForm } from '../../src/servers/helpers/ServerForm';
|
2018-08-25 00:19:49 +03:00
|
|
|
|
|
|
|
describe('<CreateServer />', () => {
|
2020-08-22 14:41:54 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const ImportServersBtn = () => null;
|
2019-04-19 11:29:49 +03:00
|
|
|
const createServerMock = jest.fn();
|
2020-08-22 14:41:54 +03:00
|
|
|
const push = jest.fn();
|
|
|
|
const historyMock = Mock.of<History>({ push });
|
2020-08-22 18:58:44 +03:00
|
|
|
const createWrapper = (serversImported = false, importFailed = false) => {
|
|
|
|
const useStateFlagTimeout = jest.fn()
|
|
|
|
.mockReturnValueOnce([ serversImported, () => '' ])
|
|
|
|
.mockReturnValueOnce([ importFailed, () => '' ]);
|
|
|
|
const CreateServer = createServerConstruct(ImportServersBtn, useStateFlagTimeout);
|
2018-12-18 00:32:51 +03:00
|
|
|
|
2018-08-25 00:19:49 +03:00
|
|
|
wrapper = shallow(
|
2020-08-22 09:10:31 +03:00
|
|
|
<CreateServer createServer={createServerMock} resetSelectedServer={identity} history={historyMock} />,
|
2018-08-25 00:19:49 +03:00
|
|
|
);
|
2020-03-15 12:33:23 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
2020-08-22 14:41:54 +03:00
|
|
|
wrapper?.unmount();
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders components', () => {
|
2020-03-15 12:33:23 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
2020-03-15 13:59:07 +03:00
|
|
|
expect(wrapper.find(ServerForm)).toHaveLength(1);
|
2020-08-22 18:58:44 +03:00
|
|
|
expect(wrapper.find('Result')).toHaveLength(0);
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows success message when imported is true', () => {
|
2020-03-15 12:33:23 +03:00
|
|
|
const wrapper = createWrapper(true);
|
2020-08-22 18:58:44 +03:00
|
|
|
const result = wrapper.find('Result');
|
2020-03-15 12:33:23 +03:00
|
|
|
|
2020-08-22 18:58:44 +03:00
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result.prop('type')).toEqual('success');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows error message when import failed', () => {
|
|
|
|
const wrapper = createWrapper(false, true);
|
|
|
|
const result = wrapper.find('Result');
|
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result.prop('type')).toEqual('error');
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('creates server and redirects to it when form is submitted', () => {
|
2020-03-15 12:33:23 +03:00
|
|
|
const wrapper = createWrapper();
|
2020-03-15 13:59:07 +03:00
|
|
|
const form = wrapper.find(ServerForm);
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2020-03-15 13:59:07 +03:00
|
|
|
form.simulate('submit', {});
|
2018-08-25 00:19:49 +03:00
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(createServerMock).toHaveBeenCalledTimes(1);
|
2020-08-22 14:41:54 +03:00
|
|
|
expect(push).toHaveBeenCalledTimes(1);
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
});
|