shlink-web-client/test/servers/helpers/ImportServersBtn.test.tsx

101 lines
3.2 KiB
TypeScript
Raw Normal View History

import { ReactNode } from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
2018-08-23 17:26:42 +03:00
import { UncontrolledTooltip } from 'reactstrap';
import { Mock } from 'ts-mockery';
2022-05-28 11:47:39 +03:00
import {
ImportServersBtn as createImportServersBtn,
ImportServersBtnProps,
} from '../../../src/servers/helpers/ImportServersBtn';
import { ServersImporter } from '../../../src/servers/services/ServersImporter';
import { DuplicatedServersModal } from '../../../src/servers/helpers/DuplicatedServersModal';
2018-08-23 17:26:42 +03:00
describe('<ImportServersBtn />', () => {
let wrapper: ShallowWrapper;
const onImportMock = jest.fn();
const createServersMock = jest.fn();
2021-10-31 14:07:38 +03:00
const importServersFromFile = jest.fn().mockResolvedValue([]);
const serversImporterMock = Mock.of<ServersImporter>({ importServersFromFile });
const click = jest.fn();
const fileRef = { current: Mock.of<HTMLInputElement>({ click }) };
2022-05-28 11:47:39 +03:00
const ImportServersBtn = createImportServersBtn(serversImporterMock);
const createWrapper = (props: Partial<ImportServersBtnProps & { children: ReactNode }> = {}) => {
2018-08-23 17:26:42 +03:00
wrapper = shallow(
2021-10-22 19:53:00 +03:00
<ImportServersBtn
servers={{}}
{...props}
2021-10-22 19:53:00 +03:00
fileRef={fileRef}
createServers={createServersMock}
2021-10-22 19:53:00 +03:00
onImport={onImportMock}
/>,
2018-08-23 17:26:42 +03:00
);
2021-10-22 19:53:00 +03:00
return wrapper;
};
afterEach(jest.clearAllMocks);
2018-08-23 17:26:42 +03:00
afterEach(() => wrapper.unmount());
it('renders a button, a tooltip and a file input', () => {
2021-10-22 19:53:00 +03:00
const wrapper = createWrapper();
2018-08-23 17:26:42 +03:00
expect(wrapper.find('#importBtn')).toHaveLength(1);
expect(wrapper.find(UncontrolledTooltip)).toHaveLength(1);
2021-10-17 20:13:06 +03:00
expect(wrapper.find('.import-servers-btn__csv-select')).toHaveLength(1);
2018-08-23 17:26:42 +03:00
});
2021-10-22 19:53:00 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[undefined, ''],
['foo', 'foo'],
['bar', 'bar'],
2021-10-22 19:53:00 +03:00
])('allows a class name to be provided', (providedClassName, expectedClassName) => {
const wrapper = createWrapper({ className: providedClassName });
2021-10-22 19:53:00 +03:00
expect(wrapper.find('#importBtn').prop('className')).toEqual(expectedClassName);
});
it.each([
2022-03-26 14:17:42 +03:00
[undefined, true],
['foo', false],
['bar', false],
])('has expected text', (children, expectToHaveDefaultText) => {
const wrapper = createWrapper({ children });
if (expectToHaveDefaultText) {
expect(wrapper.find('#importBtn').html()).toContain('Import from file');
} else {
expect(wrapper.find('#importBtn').html()).toContain(children);
expect(wrapper.find('#importBtn').html()).not.toContain('Import from file');
}
});
2018-08-23 17:26:42 +03:00
it('triggers click on file ref when button is clicked', () => {
2021-10-22 19:53:00 +03:00
const wrapper = createWrapper();
2018-08-23 17:26:42 +03:00
const btn = wrapper.find('#importBtn');
2018-08-23 17:26:42 +03:00
btn.simulate('click');
expect(click).toHaveBeenCalledTimes(1);
2018-08-23 17:26:42 +03:00
});
2021-10-31 14:07:38 +03:00
it('imports servers when file input changes', async () => {
2021-10-22 19:53:00 +03:00
const wrapper = createWrapper();
2021-10-17 20:13:06 +03:00
const file = wrapper.find('.import-servers-btn__csv-select');
await file.simulate('change', { target: { files: [''] } });
2018-08-23 17:26:42 +03:00
2021-10-31 14:07:38 +03:00
expect(importServersFromFile).toHaveBeenCalledTimes(1);
});
it.each([
2022-03-26 14:17:42 +03:00
['discard'],
['save'],
])('invokes callback in DuplicatedServersModal events', (event) => {
const wrapper = createWrapper();
wrapper.find(DuplicatedServersModal).simulate(event);
2021-10-31 14:07:38 +03:00
expect(createServersMock).toHaveBeenCalledTimes(1);
expect(onImportMock).toHaveBeenCalledTimes(1);
2018-08-23 17:26:42 +03:00
});
});