2020-08-22 14:41:54 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
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';
|
2022-01-01 14:20:09 +03:00
|
|
|
import { DuplicatedServersModal } from '../../src/servers/helpers/DuplicatedServersModal';
|
2018-08-25 00:19:49 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
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();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
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) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2020-08-22 18:58:44 +03:00
|
|
|
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
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
wrapper = shallow(<CreateServer createServer={createServerMock} 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
|
|
|
});
|
|
|
|
|
2022-01-01 14:20:09 +03:00
|
|
|
it('creates server data 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
|
|
|
|
2022-01-01 14:20:09 +03:00
|
|
|
expect(wrapper.find(DuplicatedServersModal).prop('duplicatedServers')).toEqual([]);
|
2020-03-15 13:59:07 +03:00
|
|
|
form.simulate('submit', {});
|
2022-01-01 14:20:09 +03:00
|
|
|
expect(wrapper.find(DuplicatedServersModal).prop('duplicatedServers')).toEqual([{}]);
|
2021-12-30 23:21:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('saves server and redirects on modal save', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
wrapper.find(ServerForm).simulate('submit', {});
|
2022-01-01 14:20:09 +03:00
|
|
|
wrapper.find(DuplicatedServersModal).simulate('save');
|
2018-08-25 00:19:49 +03:00
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(createServerMock).toHaveBeenCalledTimes(1);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).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();
|
|
|
|
|
2022-01-01 14:20:09 +03:00
|
|
|
wrapper.find(DuplicatedServersModal).simulate('discard');
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2021-12-30 23:21:30 +03:00
|
|
|
});
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|