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

76 lines
2.3 KiB
TypeScript
Raw Normal View History

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();
const serversImporterMock = Mock.of<ServersImporter>({
importServersFromFile: jest.fn().mockResolvedValue([]),
});
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) => {
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}
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);
});
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
});
it('imports servers when file input changes', (done) => {
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');
file.simulate('change', { target: { files: [ '' ] } });
2018-08-23 17:26:42 +03:00
setImmediate(() => {
expect(serversImporterMock.importServersFromFile).toHaveBeenCalledTimes(1);
expect(createServersMock).toHaveBeenCalledTimes(1);
expect(onImportMock).toHaveBeenCalledTimes(1);
2018-08-23 17:26:42 +03:00
done();
});
});
});