2021-02-28 11:28:46 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { Route, useParams } from 'react-router-dom';
|
2021-02-28 11:28:46 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import createMenuLayout from '../../src/common/MenuLayout';
|
|
|
|
import { NonReachableServer, NotFoundServer, ReachableServer, SelectedServer } from '../../src/servers/data';
|
2021-12-30 12:02:31 +03:00
|
|
|
import { NoMenuLayout } from '../../src/common/NoMenuLayout';
|
2021-03-06 11:38:48 +03:00
|
|
|
import { SemVer } from '../../src/utils/helpers/version';
|
2021-02-28 11:28:46 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useParams: jest.fn().mockReturnValue({}),
|
|
|
|
useLocation: jest.fn().mockReturnValue({}),
|
|
|
|
}));
|
|
|
|
|
2021-02-28 11:28:46 +03:00
|
|
|
describe('<MenuLayout />', () => {
|
|
|
|
const ServerError = jest.fn();
|
|
|
|
const C = jest.fn();
|
2022-02-05 15:37:49 +03:00
|
|
|
const MenuLayout = createMenuLayout(C, C, C, C, C, C, C, C, ServerError, C, C, C);
|
2021-02-28 11:28:46 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const createWrapper = (selectedServer: SelectedServer) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useParams as any).mockReturnValue({ serverId: 'abc123' });
|
|
|
|
|
2022-03-11 18:03:15 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<MenuLayout
|
2022-03-11 18:07:17 +03:00
|
|
|
sidebarNotPresent={jest.fn()}
|
2022-03-11 18:17:44 +03:00
|
|
|
sidebarPresent={jest.fn()}
|
2022-03-11 18:03:15 +03:00
|
|
|
selectServer={jest.fn()}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
/>,
|
|
|
|
);
|
2021-02-28 11:28:46 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2021-02-28 11:28:46 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[ null, NoMenuLayout ],
|
|
|
|
[ Mock.of<NotFoundServer>({ serverNotFound: true }), ServerError ],
|
|
|
|
])('returns error when server is not found', (selectedServer, ExpectedComp) => {
|
|
|
|
const wrapper = createWrapper(selectedServer);
|
|
|
|
const comp = wrapper.find(ExpectedComp);
|
|
|
|
|
|
|
|
expect(comp).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error if server is not reachable', () => {
|
|
|
|
const wrapper = createWrapper(Mock.of<NonReachableServer>()).dive();
|
|
|
|
const serverError = wrapper.find(ServerError);
|
|
|
|
|
|
|
|
expect(serverError).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-02-08 00:17:57 +03:00
|
|
|
[ '2.5.0' as SemVer, 9 ],
|
|
|
|
[ '2.6.0' as SemVer, 10 ],
|
|
|
|
[ '2.7.0' as SemVer, 10 ],
|
|
|
|
[ '2.8.0' as SemVer, 11 ],
|
|
|
|
[ '2.10.0' as SemVer, 11 ],
|
|
|
|
[ '3.0.0' as SemVer, 12 ],
|
2021-02-28 11:28:46 +03:00
|
|
|
])('has expected amount of routes based on selected server\'s version', (version, expectedAmountOfRoutes) => {
|
|
|
|
const selectedServer = Mock.of<ReachableServer>({ version });
|
|
|
|
const wrapper = createWrapper(selectedServer).dive();
|
|
|
|
const routes = wrapper.find(Route);
|
|
|
|
|
|
|
|
expect(routes).toHaveLength(expectedAmountOfRoutes);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(routes.findWhere((element) => element.prop('index'))).toHaveLength(1);
|
2021-02-28 11:28:46 +03:00
|
|
|
});
|
|
|
|
});
|