import React from 'react'; import { shallow, ShallowWrapper } from 'enzyme'; import { Mock } from 'ts-mockery'; import ShlinkVersions, { ShlinkVersionsProps } from '../../src/common/ShlinkVersions'; import { NonReachableServer, NotFoundServer, ReachableServer } from '../../src/servers/data'; describe('', () => { let wrapper: ShallowWrapper; const createWrapper = (props: ShlinkVersionsProps) => { wrapper = shallow(); return wrapper; }; afterEach(() => wrapper?.unmount()); it.each([ [ '1.2.3', Mock.of({ printableVersion: 'foo' }), 'v1.2.3', 'foo' ], [ 'foo', Mock.of({ printableVersion: '1.2.3' }), 'latest', '1.2.3' ], [ 'latest', Mock.of({ printableVersion: 'latest' }), 'latest', 'latest' ], [ '5.5.0', Mock.of({ printableVersion: '0.2.8' }), 'v5.5.0', '0.2.8' ], [ 'not-semver', Mock.of({ printableVersion: 'something' }), 'latest', 'something' ], ])( 'displays expected versions when selected server is reachable', (clientVersion, selectedServer, expectedClientVersion, expectedServerVersion) => { const wrapper = createWrapper({ clientVersion, selectedServer }); const links = wrapper.find('VersionLink'); const serverLink = links.at(0); const clientLink = links.at(1); expect(serverLink.prop('project')).toEqual('shlink'); expect(serverLink.prop('version')).toEqual(expectedServerVersion); expect(clientLink.prop('project')).toEqual('shlink-web-client'); expect(clientLink.prop('version')).toEqual(expectedClientVersion); }, ); it.each([ [ '1.2.3', null ], [ '1.2.3', Mock.of({ serverNotFound: true }) ], [ '1.2.3', Mock.of({ serverNotReachable: true }) ], ])('displays only client version when selected server is not reachable', (clientVersion, selectedServer) => { const wrapper = createWrapper({ clientVersion, selectedServer }); const links = wrapper.find('VersionLink'); expect(links).toHaveLength(1); expect(links.at(0).prop('project')).toEqual('shlink-web-client'); }); });