2018-08-23 17:26:42 +03:00
|
|
|
import React 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';
|
2018-12-18 00:32:51 +03:00
|
|
|
import importServersBtnConstruct from '../../../src/servers/helpers/ImportServersBtn';
|
2020-08-22 12:20:27 +03:00
|
|
|
import ServersImporter from '../../../src/servers/services/ServersImporter';
|
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();
|
2020-08-22 12:20:27 +03:00
|
|
|
const serversImporterMock = Mock.of<ServersImporter>({
|
2019-04-19 11:29:49 +03:00
|
|
|
importServersFromFile: jest.fn().mockResolvedValue([]),
|
2020-08-22 12:20:27 +03:00
|
|
|
});
|
|
|
|
const click = jest.fn();
|
2018-08-23 17:26:42 +03:00
|
|
|
const fileRef = {
|
2020-08-22 12:20:27 +03:00
|
|
|
current: Mock.of<HTMLInputElement>({ click }),
|
2018-08-23 17:26:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-08-22 12:20:27 +03:00
|
|
|
jest.clearAllMocks();
|
2018-08-23 17:26:42 +03:00
|
|
|
|
2018-12-18 00:32:51 +03:00
|
|
|
const ImportServersBtn = importServersBtnConstruct(serversImporterMock);
|
|
|
|
|
2018-08-23 17:26:42 +03:00
|
|
|
wrapper = shallow(
|
2020-08-22 09:10:31 +03:00
|
|
|
<ImportServersBtn createServers={createServersMock} fileRef={fileRef} onImport={onImportMock} />,
|
2018-08-23 17:26:42 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
afterEach(() => wrapper.unmount());
|
|
|
|
|
|
|
|
it('renders a button, a tooltip and a file input', () => {
|
|
|
|
expect(wrapper.find('#importBtn')).toHaveLength(1);
|
|
|
|
expect(wrapper.find(UncontrolledTooltip)).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.create-server__csv-select')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers click on file ref when button is clicked', () => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
it('imports servers when file input changes', (done) => {
|
2018-08-23 17:26:42 +03:00
|
|
|
const file = wrapper.find('.create-server__csv-select');
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
file.simulate('change', { target: { files: [ '' ] } });
|
2018-08-23 17:26:42 +03:00
|
|
|
|
|
|
|
setImmediate(() => {
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(serversImporterMock.importServersFromFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(createServersMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onImportMock).toHaveBeenCalledTimes(1);
|
2018-08-23 17:26:42 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|