2021-10-22 20:03:12 +03:00
|
|
|
import { ReactNode } from 'react';
|
2020-08-22 12:20:27 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2018-08-23 17:26:42 +03:00
|
|
|
import { UncontrolledTooltip } from 'reactstrap';
|
2020-08-22 12:20:27 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-01-01 14:20:09 +03:00
|
|
|
import importServersBtnConstruct, { 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 />', () => {
|
2020-08-22 12:20:27 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2019-04-19 11:29:49 +03:00
|
|
|
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 });
|
2020-08-22 12:20:27 +03:00
|
|
|
const click = jest.fn();
|
2022-01-01 14:20:09 +03:00
|
|
|
const fileRef = { current: Mock.of<HTMLInputElement>({ click }) };
|
2021-10-22 19:53:00 +03:00
|
|
|
const ImportServersBtn = importServersBtnConstruct(serversImporterMock);
|
2022-01-01 14:20:09 +03:00
|
|
|
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
|
2022-01-01 14:20:09 +03:00
|
|
|
servers={{}}
|
|
|
|
{...props}
|
2021-10-22 19:53:00 +03:00
|
|
|
fileRef={fileRef}
|
2022-01-01 14:20:09 +03:00
|
|
|
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([
|
|
|
|
[ undefined, '' ],
|
|
|
|
[ 'foo', 'foo' ],
|
|
|
|
[ 'bar', 'bar' ],
|
|
|
|
])('allows a class name to be provided', (providedClassName, expectedClassName) => {
|
2022-01-01 14:20:09 +03:00
|
|
|
const wrapper = createWrapper({ className: providedClassName });
|
2021-10-22 19:53:00 +03:00
|
|
|
|
|
|
|
expect(wrapper.find('#importBtn').prop('className')).toEqual(expectedClassName);
|
|
|
|
});
|
|
|
|
|
2021-10-22 20:03:12 +03:00
|
|
|
it.each([
|
|
|
|
[ undefined, true ],
|
|
|
|
[ 'foo', false ],
|
|
|
|
[ 'bar', false ],
|
|
|
|
])('has expected text', (children, expectToHaveDefaultText) => {
|
2022-01-01 14:20:09 +03:00
|
|
|
const wrapper = createWrapper({ children });
|
2021-10-22 20:03:12 +03:00
|
|
|
|
|
|
|
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-26 00:39:27 +03:00
|
|
|
|
2018-08-23 17:26:42 +03:00
|
|
|
btn.simulate('click');
|
|
|
|
|
2020-08-22 12:20:27 +03:00
|
|
|
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');
|
2018-08-26 00:39:27 +03:00
|
|
|
|
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);
|
2022-01-01 14:20:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[ '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
|
|
|
});
|
|
|
|
});
|