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

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-08-23 17:26:42 +03:00
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import { UncontrolledTooltip } from 'reactstrap';
import { ImportServersBtnComponent } from '../../../src/servers/helpers/ImportServersBtn';
2018-08-23 17:26:42 +03:00
describe('<ImportServersBtn />', () => {
let wrapper;
const onImportMock = sinon.fake();
const createServersMock = sinon.fake();
const serversImporterMock = {
importServersFromFile: sinon.fake.returns(Promise.resolve([])),
};
const fileRef = {
current: { click: sinon.fake() },
2018-08-23 17:26:42 +03:00
};
beforeEach(() => {
onImportMock.resetHistory();
createServersMock.resetHistory();
serversImporterMock.importServersFromFile.resetHistory();
fileRef.current.click.resetHistory();
wrapper = shallow(
<ImportServersBtnComponent
2018-08-23 17:26:42 +03:00
createServers={createServersMock}
serversImporter={serversImporterMock}
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-23 17:26:42 +03:00
btn.simulate('click');
expect(fileRef.current.click.callCount).toEqual(1);
});
it('imports servers when file input changes', (done) => {
2018-08-23 17:26:42 +03:00
const file = wrapper.find('.create-server__csv-select');
file.simulate('change', { target: { files: [ '' ] } });
2018-08-23 17:26:42 +03:00
setImmediate(() => {
expect(serversImporterMock.importServersFromFile.callCount).toEqual(1);
expect(createServersMock.callCount).toEqual(1);
expect(onImportMock.callCount).toEqual(1);
done();
});
});
});