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

66 lines
2 KiB
TypeScript
Raw Normal View History

2018-08-25 00:19:49 +03:00
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
2018-08-25 00:19:49 +03:00
import { identity } from 'ramda';
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';
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 historyMock = Mock.of<History>({ push });
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
);
return wrapper;
};
afterEach(() => {
jest.resetAllMocks();
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('Result')).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('Result');
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', () => {
const wrapper = createWrapper();
const form = wrapper.find(ServerForm);
form.simulate('submit', {});
2018-08-25 00:19:49 +03:00
expect(createServerMock).toHaveBeenCalledTimes(1);
expect(push).toHaveBeenCalledTimes(1);
2018-08-25 00:19:49 +03:00
});
});