2020-08-22 13:41:54 +02:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-07 22:17:57 +01:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2018-12-17 22:32:51 +01:00
|
|
|
import createServerConstruct from '../../src/servers/CreateServer';
|
2020-03-15 11:59:07 +01:00
|
|
|
import { ServerForm } from '../../src/servers/helpers/ServerForm';
|
2021-10-17 19:13:06 +02:00
|
|
|
import { ServerWithId } from '../../src/servers/data';
|
2022-01-01 12:20:09 +01:00
|
|
|
import { DuplicatedServersModal } from '../../src/servers/helpers/DuplicatedServersModal';
|
2018-08-24 23:19:49 +02:00
|
|
|
|
2022-02-07 22:17:57 +01:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
2018-08-24 23:19:49 +02:00
|
|
|
describe('<CreateServer />', () => {
|
2020-08-22 13:41:54 +02:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const ImportServersBtn = () => null;
|
2019-04-19 10:29:49 +02:00
|
|
|
const createServerMock = jest.fn();
|
2022-02-07 22:17:57 +01:00
|
|
|
const navigate = jest.fn();
|
2021-10-17 19:13:06 +02:00
|
|
|
const servers = { foo: Mock.all<ServerWithId>() };
|
2020-08-22 17:58:44 +02:00
|
|
|
const createWrapper = (serversImported = false, importFailed = false) => {
|
2022-02-07 22:17:57 +01:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2020-08-22 17:58:44 +02:00
|
|
|
const useStateFlagTimeout = jest.fn()
|
2022-03-26 12:17:42 +01:00
|
|
|
.mockReturnValueOnce([serversImported, () => ''])
|
|
|
|
.mockReturnValueOnce([importFailed, () => ''])
|
2021-12-30 21:21:30 +01:00
|
|
|
.mockReturnValue([]);
|
2020-08-22 17:58:44 +02:00
|
|
|
const CreateServer = createServerConstruct(ImportServersBtn, useStateFlagTimeout);
|
2018-12-17 22:32:51 +01:00
|
|
|
|
2022-02-07 22:17:57 +01:00
|
|
|
wrapper = shallow(<CreateServer createServer={createServerMock} servers={servers} />);
|
2020-03-15 10:33:23 +01:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2021-12-30 21:21:30 +01:00
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-08-24 23:19:49 +02:00
|
|
|
|
|
|
|
it('renders components', () => {
|
2020-03-15 10:33:23 +01:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
2020-03-15 11:59:07 +01:00
|
|
|
expect(wrapper.find(ServerForm)).toHaveLength(1);
|
2020-12-21 17:54:05 +01:00
|
|
|
expect(wrapper.find('ImportResult')).toHaveLength(0);
|
2018-08-24 23:19:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows success message when imported is true', () => {
|
2020-03-15 10:33:23 +01:00
|
|
|
const wrapper = createWrapper(true);
|
2020-12-21 17:54:05 +01:00
|
|
|
const result = wrapper.find('ImportResult');
|
2020-03-15 10:33:23 +01:00
|
|
|
|
2020-08-22 17:58:44 +02: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 17:54:05 +01:00
|
|
|
const result = wrapper.find('ImportResult');
|
2020-08-22 17:58:44 +02:00
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result.prop('type')).toEqual('error');
|
2018-08-24 23:19:49 +02:00
|
|
|
});
|
|
|
|
|
2022-01-01 12:20:09 +01:00
|
|
|
it('creates server data when form is submitted', () => {
|
2020-03-15 10:33:23 +01:00
|
|
|
const wrapper = createWrapper();
|
2020-03-15 11:59:07 +01:00
|
|
|
const form = wrapper.find(ServerForm);
|
2018-08-25 23:39:27 +02:00
|
|
|
|
2022-01-01 12:20:09 +01:00
|
|
|
expect(wrapper.find(DuplicatedServersModal).prop('duplicatedServers')).toEqual([]);
|
2020-03-15 11:59:07 +01:00
|
|
|
form.simulate('submit', {});
|
2022-01-01 12:20:09 +01:00
|
|
|
expect(wrapper.find(DuplicatedServersModal).prop('duplicatedServers')).toEqual([{}]);
|
2021-12-30 21:21:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('saves server and redirects on modal save', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
wrapper.find(ServerForm).simulate('submit', {});
|
2022-01-01 12:20:09 +01:00
|
|
|
wrapper.find(DuplicatedServersModal).simulate('save');
|
2018-08-24 23:19:49 +02:00
|
|
|
|
2019-04-19 10:29:49 +02:00
|
|
|
expect(createServerMock).toHaveBeenCalledTimes(1);
|
2022-02-07 22:17:57 +01:00
|
|
|
expect(navigate).toHaveBeenCalledTimes(1);
|
2018-08-24 23:19:49 +02:00
|
|
|
});
|
2021-12-30 21:21:30 +01:00
|
|
|
|
|
|
|
it('goes back on modal discard', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
2022-01-01 12:20:09 +01:00
|
|
|
wrapper.find(DuplicatedServersModal).simulate('discard');
|
2021-12-30 21:21:30 +01:00
|
|
|
|
2022-02-07 22:17:57 +01:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2021-12-30 21:21:30 +01:00
|
|
|
});
|
2018-08-24 23:19:49 +02:00
|
|
|
});
|