shlink-web-client/test/servers/ManageServers.test.tsx

94 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-10-23 12:33:32 +03:00
import { Mock } from 'ts-mockery';
import { shallow, ShallowWrapper } from 'enzyme';
import { Button } from 'reactstrap';
import ServersExporter from '../../src/servers/services/ServersExporter';
import { ManageServers as createManageServers } from '../../src/servers/ManageServers';
import { ServersMap, ServerWithId } from '../../src/servers/data';
2022-05-14 17:41:09 +03:00
import { SearchField } from '../../src/utils/SearchField';
2021-10-23 12:33:32 +03:00
import { Result } from '../../src/utils/Result';
describe('<ManageServers />', () => {
const exportServers = jest.fn();
const serversExporter = Mock.of<ServersExporter>({ exportServers });
const ImportServersBtn = () => null;
const ManageServersRow = () => null;
const useTimeoutToggle = jest.fn().mockReturnValue([false, jest.fn()]);
const ManageServers = createManageServers(serversExporter, ImportServersBtn, useTimeoutToggle, ManageServersRow);
2021-10-23 12:33:32 +03:00
let wrapper: ShallowWrapper;
const createServerMock = (value: string, autoConnect = false) => Mock.of<ServerWithId>(
{ id: value, name: value, url: value, autoConnect },
);
const createWrapper = (servers: ServersMap = {}) => {
wrapper = shallow(<ManageServers servers={servers} />);
return wrapper;
};
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it('shows search field which allows searching servers, affecting te amount of rendered rows', () => {
const wrapper = createWrapper({
foo: createServerMock('foo'),
bar: createServerMock('bar'),
baz: createServerMock('baz'),
});
const searchField = wrapper.find(SearchField);
2021-10-23 12:33:32 +03:00
expect(wrapper.find(ManageServersRow)).toHaveLength(3);
expect(wrapper.find('tbody').find('tr')).toHaveLength(0);
searchField.simulate('change', 'foo');
2021-10-23 12:33:32 +03:00
expect(wrapper.find(ManageServersRow)).toHaveLength(1);
expect(wrapper.find('tbody').find('tr')).toHaveLength(0);
searchField.simulate('change', 'ba');
2021-10-23 12:33:32 +03:00
expect(wrapper.find(ManageServersRow)).toHaveLength(2);
expect(wrapper.find('tbody').find('tr')).toHaveLength(0);
searchField.simulate('change', 'invalid');
2021-10-23 12:33:32 +03:00
expect(wrapper.find(ManageServersRow)).toHaveLength(0);
expect(wrapper.find('tbody').find('tr')).toHaveLength(1);
});
it.each([
2022-03-26 14:17:42 +03:00
[createServerMock('foo'), 3],
[createServerMock('foo', true), 4],
2021-10-23 12:33:32 +03:00
])('shows different amount of columns if there are at least one auto-connect server', (server, expectedCols) => {
const wrapper = createWrapper({ server });
const row = wrapper.find(ManageServersRow);
expect(wrapper.find('th')).toHaveLength(expectedCols);
expect(row.prop('hasAutoConnect')).toEqual(server.autoConnect);
});
it.each([
2022-03-26 14:17:42 +03:00
[{}, 1],
[{ foo: createServerMock('foo') }, 2],
2021-10-23 12:33:32 +03:00
])('shows export button if the list of servers is not empty', (servers, expectedButtons) => {
const wrapper = createWrapper(servers);
const exportBtn = wrapper.find(Button);
expect(exportBtn).toHaveLength(expectedButtons);
});
it('allows exporting servers when clicking on button', () => {
const wrapper = createWrapper({ foo: createServerMock('foo') });
const exportBtn = wrapper.find(Button).first();
expect(exportServers).not.toHaveBeenCalled();
exportBtn.simulate('click');
expect(exportServers).toHaveBeenCalled();
});
it('shows an error message if an error occurs while importing servers', () => {
useTimeoutToggle.mockReturnValue([true, jest.fn()]);
2021-10-23 12:33:32 +03:00
const wrapper = createWrapper({ foo: createServerMock('foo') });
const result = wrapper.find(Result);
expect(result).toHaveLength(1);
expect(result.prop('type')).toEqual('error');
});
});