kafka-ui/kafka-ui-react-app/src/lib/testHelpers.tsx
Mgrdich 71ac16357b
react router migration (#2045)
* remove withRouter HOC from FiltersContainer

* remove withRouter HOC from Topics DetailsContainer

* remove withRouter HOC from Topics TopicsConsumerGroupsContainer

* withRouter HOC from Topics TopicsConsumerGroupsContainer

* minor code refactor in the Details spec

* Routes code modifications to refactor strings representation to functions

* Settings and TopicsConsumer removal of HOC with Router

* Remove withRouter HOC from Overview file

* Remove withRouter HOC from Edit file

* replace Router path with functions instead of strings

* delete CustomParamsContainer and use the simple component in the TopicForm

* remove HOC from DangerZone container

* Remove withRouter HOC from Connect pages like Config , Overview , Tasks

* Remove withRouter HOC from Connect pages like Actions, Details, Edit, New

* Refactor Kafka Connect Codes

* Refactor Topics pages

* Remove HOC from Diff component and minor code refactor

* Route component migration into children instead of renderProps or component param in App Component

* Route component migration into children instead of renderProps or component param in Cluster Component

* Route component migration into children instead of renderProps or component param in Topics Component

* Route component migration into children instead of renderProps or component param in Topic Component

* Route component migration into children instead of renderProps or component param in Topic Component

* minor bug fix in the Overview selector spread

* change Router from component Render to child render
in ConsumerGroups page

* change Router from component Render to child render
in Schemas page

* change Router from component Render to child render
in KsqlDb page

* change Router from component Render to child render
in Connect page

* change Router from component Render to child render
in Connect Details page

* Overview Details styling code modifications

* All written path to paths with functions

* Route Parameters code fix with functions and params with variables

* Updating BreadCrumb Route

* Refactor Redirects

* WIP React Router v6 migration

* Remove unused imports from the file

* Make KsqlDb pages work with relative Routes

* WIP Make Connect pages work and fix the Schema page testing problem

* transforming consumer groups into relative path router

* Transform Topics pages into relative routes

* Transform Topic pages into relative routes

* Minor changes in Connect and KsqlDb test suites relative routes

* Minor changes in Connect and KsqlDb test suites relative routes

* change the Details into relative Routes

* Topics List naviagtion and caching issue fixed in tests suites

* Topic New Naviagation issue fix + tests suites

* Details navigate migrating into relative paths

* Send Message Submit Naviagttion with tests suites

* Topic Edit pages with working routes navigation

* Topic Details and ResetOffsets Pages tests suites and navigations

* Messages Table Tests suites

* BreadCrumbs Routes fixes

* ClusterMenu and Links styling minor code modifications

* ClusterMenu and Links styling minor code modifications

* Minor Code modifications

* Fix Lintter Problems

* fix Code Smells

* create custom useParams hook

* Adding Path tests

* minor code refactors

* Fix the Button Component redundant Props + transforming routes to relative

* Fix linter issues
2022-05-31 12:33:15 +03:00

102 lines
2.8 KiB
TypeScript

import React, { PropsWithChildren, ReactElement } from 'react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { Provider } from 'react-redux';
import { ThemeProvider } from 'styled-components';
import theme from 'theme/theme';
import { render, RenderOptions, screen } from '@testing-library/react';
import { AnyAction, Store } from 'redux';
import { RootState } from 'redux/interfaces';
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from 'redux/reducers';
import mockStoreCreator from 'redux/store/configureStore/mockStoreCreator';
import { MemoryRouterProps } from 'react-router';
interface CustomRenderOptions extends Omit<RenderOptions, 'wrapper'> {
preloadedState?: Partial<RootState>;
store?: Store<Partial<RootState>, AnyAction>;
initialEntries?: MemoryRouterProps['initialEntries'];
}
export function getByTextContent(textMatch: string | RegExp): HTMLElement {
return screen.getByText((content, node) => {
const hasText = (nod: Element) => nod.textContent === textMatch;
const nodeHasText = hasText(node as Element);
const childrenDontHaveText = Array.from(node?.children || []).every(
(child) => !hasText(child)
);
return nodeHasText && childrenDontHaveText;
});
}
interface WithRouterProps {
children: React.ReactNode;
path: string;
}
export const WithRoute: React.FC<WithRouterProps> = ({ children, path }) => {
return (
<Routes>
<Route path={path} element={children} />
</Routes>
);
};
const customRender = (
ui: ReactElement,
{
preloadedState,
store = configureStore<RootState>({
reducer: rootReducer,
preloadedState,
}),
initialEntries,
...renderOptions
}: CustomRenderOptions = {}
) => {
// overrides @testing-library/react render.
const AllTheProviders: React.FC<PropsWithChildren<unknown>> = ({
children,
}) => {
return (
<ThemeProvider theme={theme}>
<Provider store={store}>
<MemoryRouter initialEntries={initialEntries}>
{children}
</MemoryRouter>
</Provider>
</ThemeProvider>
);
};
return render(ui, { wrapper: AllTheProviders, ...renderOptions });
};
export { customRender as render };
export class EventSourceMock {
url: string;
close: () => void;
open: () => void;
error: () => void;
onmessage: () => void;
constructor(url: string) {
this.url = url;
this.open = jest.fn();
this.error = jest.fn();
this.onmessage = jest.fn();
this.close = jest.fn();
}
}
export const getTypeAndPayload = (store: typeof mockStoreCreator) => {
return store.getActions().map(({ type, payload }) => ({ type, payload }));
};
export const getAlertActions = (mockStore: typeof mockStoreCreator) =>
getTypeAndPayload(mockStore).filter((currentAction: AnyAction) =>
currentAction.type.startsWith('alerts')
);