Fixed most tests using react-router-dom hooks

This commit is contained in:
Alejandro Celaya
2022-02-07 22:17:57 +01:00
parent 97024d828e
commit c4e928ff09
22 changed files with 177 additions and 175 deletions

View File

@@ -1,34 +1,31 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { History, Location } from 'history';
import { match } from 'react-router'; // eslint-disable-line @typescript-eslint/no-unused-vars
import { Route } from 'react-router-dom';
import { Route, useParams } from 'react-router-dom';
import { Mock } from 'ts-mockery';
import createMenuLayout from '../../src/common/MenuLayout';
import { NonReachableServer, NotFoundServer, ReachableServer, SelectedServer } from '../../src/servers/data';
import { NoMenuLayout } from '../../src/common/NoMenuLayout';
import { SemVer } from '../../src/utils/helpers/version';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: jest.fn().mockReturnValue({}),
useLocation: jest.fn().mockReturnValue({}),
}));
describe('<MenuLayout />', () => {
const ServerError = jest.fn();
const C = jest.fn();
const MenuLayout = createMenuLayout(C, C, C, C, C, C, C, C, ServerError, C, C, C);
let wrapper: ShallowWrapper;
const createWrapper = (selectedServer: SelectedServer) => {
wrapper = shallow(
<MenuLayout
selectServer={jest.fn()}
selectedServer={selectedServer}
history={Mock.all<History>()}
location={Mock.all<Location>()}
match={Mock.of<match<{ serverId: string }>>({
params: { serverId: 'abc123' },
})}
/>,
);
(useParams as any).mockReturnValue({ serverId: 'abc123' });
wrapper = shallow(<MenuLayout selectServer={jest.fn()} selectedServer={selectedServer} />);
return wrapper;
};
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it.each([
@@ -49,17 +46,18 @@ describe('<MenuLayout />', () => {
});
it.each([
[ '2.5.0' as SemVer, 8 ],
[ '2.6.0' as SemVer, 9 ],
[ '2.7.0' as SemVer, 9 ],
[ '2.8.0' as SemVer, 10 ],
[ '2.10.0' as SemVer, 10 ],
[ '3.0.0' as SemVer, 11 ],
[ '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 ],
])('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);
expect(routes.findWhere((element) => element.prop('index'))).toHaveLength(1);
});
});