2020-08-22 14:41:54 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
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';
|
2021-10-17 20:13:06 +03:00
|
|
|
import { ServerWithId } from '../../src/servers/data';
|
2021-12-30 23:21:30 +03:00
|
|
|
import { DuplicatedServerModal } from '../../src/servers/helpers/DuplicatedServerModal';
|
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();
|
2021-12-30 23:21:30 +03:00
|
|
|
const goBack = jest.fn();
|
|
|
|
const historyMock = Mock.of<History>({ push, goBack });
|
2021-10-17 20:13:06 +03:00
|
|
|
const servers = { foo: Mock.all<ServerWithId>() };
|
2020-08-22 18:58:44 +03:00
|
|
|
const createWrapper = (serversImported = false, importFailed = false) => {
|
|
|
|
const useStateFlagTimeout = jest.fn()
|
|
|
|
.mockReturnValueOnce([ serversImported, () => '' ])
|
2021-12-30 23:21:30 +03:00
|
|
|
.mockReturnValueOnce([ importFailed, () => '' ])
|
|
|
|
.mockReturnValue([]);
|
2020-08-22 18:58:44 +03:00
|
|
|
const CreateServer = createServerConstruct(ImportServersBtn, useStateFlagTimeout);
|
2018-12-18 00:32:51 +03:00
|
|
|
|
2021-10-17 20:13:06 +03:00
|
|
|
wrapper = shallow(<CreateServer createServer={createServerMock} history={historyMock} servers={servers} />);
|
2020-03-15 12:33:23 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2021-12-30 23:21:30 +03:00
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => 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-12-21 19:54:05 +03:00
|
|
|
expect(wrapper.find('ImportResult')).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-12-21 19:54:05 +03:00
|
|
|
const result = wrapper.find('ImportResult');
|
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);
|
2020-12-21 19:54:05 +03:00
|
|
|
const result = wrapper.find('ImportResult');
|
2020-08-22 18:58:44 +03:00
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result.prop('type')).toEqual('error');
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
|
2021-12-30 23:21:30 +03:00
|
|
|
it('creates server data 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
|
|
|
|
2021-12-30 23:21:30 +03:00
|
|
|
expect(wrapper.find(DuplicatedServerModal).prop('serverData')).not.toBeDefined();
|
2020-03-15 13:59:07 +03:00
|
|
|
form.simulate('submit', {});
|
2021-12-30 23:21:30 +03:00
|
|
|
expect(wrapper.find(DuplicatedServerModal).prop('serverData')).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves server and redirects on modal save', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
wrapper.find(ServerForm).simulate('submit', {});
|
|
|
|
wrapper.find(DuplicatedServerModal).simulate('save');
|
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
|
|
|
});
|
2021-12-30 23:21:30 +03:00
|
|
|
|
|
|
|
it('goes back on modal discard', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
wrapper.find(DuplicatedServerModal).simulate('discard');
|
|
|
|
|
|
|
|
expect(goBack).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|