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

89 lines
2.9 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';
2018-12-18 00:32:51 +03:00
import importServersBtnConstruct from '../../../src/servers/helpers/ImportServersBtn';
import ServersImporter from '../../../src/servers/services/ServersImporter';
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();
2018-08-23 17:26:42 +03:00
const fileRef = {
current: Mock.of<HTMLInputElement>({ click }),
2018-08-23 17:26:42 +03:00
};
2021-10-22 19:53:00 +03:00
const ImportServersBtn = importServersBtnConstruct(serversImporterMock);
const createWrapper = (className?: string, children?: ReactNode) => {
2018-08-23 17:26:42 +03:00
wrapper = shallow(
2021-10-22 19:53:00 +03:00
<ImportServersBtn
createServers={createServersMock}
className={className}
fileRef={fileRef}
children={children}
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([
[ undefined, '' ],
[ 'foo', 'foo' ],
[ 'bar', 'bar' ],
])('allows a class name to be provided', (providedClassName, expectedClassName) => {
const wrapper = createWrapper(providedClassName);
expect(wrapper.find('#importBtn').prop('className')).toEqual(expectedClassName);
});
it.each([
[ undefined, true ],
[ 'foo', false ],
[ 'bar', false ],
])('has expected text', (children, expectToHaveDefaultText) => {
const wrapper = createWrapper(undefined, 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');
2021-10-31 14:07:38 +03:00
await file.simulate('change', { target: { files: [ '' ] } }); // eslint-disable-line @typescript-eslint/await-thenable
2018-08-23 17:26:42 +03:00
2021-10-31 14:07:38 +03:00
expect(importServersFromFile).toHaveBeenCalledTimes(1);
expect(createServersMock).toHaveBeenCalledTimes(1);
expect(onImportMock).toHaveBeenCalledTimes(1);
2018-08-23 17:26:42 +03:00
});
});