shlink-web-client/test/servers/CreateServer.test.tsx

82 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
import { ServerForm } from '../../src/servers/helpers/ServerForm';
2021-10-17 20:13:06 +03:00
import { ServerWithId } from '../../src/servers/data';
import { DuplicatedServerModal } from '../../src/servers/helpers/DuplicatedServerModal';
2018-08-25 00:19:49 +03:00
describe('<CreateServer />', () => {
let wrapper: ShallowWrapper;
const ImportServersBtn = () => null;
const createServerMock = jest.fn();
const push = jest.fn();
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>() };
const createWrapper = (serversImported = false, importFailed = false) => {
const useStateFlagTimeout = jest.fn()
.mockReturnValueOnce([ serversImported, () => '' ])
.mockReturnValueOnce([ importFailed, () => '' ])
.mockReturnValue([]);
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} />);
return wrapper;
};
beforeEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
2018-08-25 00:19:49 +03:00
it('renders components', () => {
const wrapper = createWrapper();
expect(wrapper.find(ServerForm)).toHaveLength(1);
expect(wrapper.find('ImportResult')).toHaveLength(0);
2018-08-25 00:19:49 +03:00
});
it('shows success message when imported is true', () => {
const wrapper = createWrapper(true);
const result = wrapper.find('ImportResult');
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('ImportResult');
expect(result).toHaveLength(1);
expect(result.prop('type')).toEqual('error');
2018-08-25 00:19:49 +03:00
});
it('creates server data form is submitted', () => {
const wrapper = createWrapper();
const form = wrapper.find(ServerForm);
expect(wrapper.find(DuplicatedServerModal).prop('serverData')).not.toBeDefined();
form.simulate('submit', {});
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
expect(createServerMock).toHaveBeenCalledTimes(1);
expect(push).toHaveBeenCalledTimes(1);
2018-08-25 00:19:49 +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
});