Ser*_*nov 5 typescript reactjs redux react-testing-library redux-dynamic-modules
我有一个小型应用程序redux-dynamic-modules,用作存储,react-router用于路由,所有组件都是带有useState、useHistory、useLocation、useSelector等钩子的函数组件useDispatch。
我想设置react-testing-library来测试组件,正如文档中所述,我需要设置一个自定义render().
所以这是我的index.tsx
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { Provider } from "react-redux";
import { store } from "./redux/store";
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
Run Code Online (Sandbox Code Playgroud)
这是我的 App.tsx
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import { SearchPage } from "./pages/search/search-page";
import { LayoutComponent as Layout } from "./components/shared/layout/layout";
import { DynamicModuleLoader } from "redux-dynamic-modules";
import { reduxSearch } from "./redux/search";
import { store } from "./redux/store";
import { paths } from "./lib/constants/paths";
const App = () => {
return (
<>
<Router>
<DynamicModuleLoader
modules={[reduxSearch()]}
createStore={() => store}
>
<Layout>
<Route exact path={paths.search}>
<SearchPage />
</Route>
</Layout>
</DynamicModuleLoader>
</Router>
</>
);
};
export default App;
Run Code Online (Sandbox Code Playgroud)
最后,test-utils.tsx我为自定义渲染器创建了:
import React, { ReactElement, ReactNode } from "react";
import { render as rtlRender, RenderOptions } from "@testing-library/react";
import { DynamicModuleLoader } from "redux-dynamic-modules";
import { Provider } from "react-redux";
import { reduxSearch } from "../redux/search";
import { store, history } from "../redux/store";
import { Router } from "react-router-dom";
export interface WrapperProps {
children: ReactElement;
}
const render = (ui: ReactElement, renderOptions?: RenderOptions) => {
const Wrapper = ({ children }: WrapperProps): ReactElement => {
return (
<Provider store={store}>
<Router history={history}>
<DynamicModuleLoader
modules={[reduxSearch()]}
createStore={() => store}
>
{children}
</DynamicModuleLoader>
</Router>
</Provider>
);
};
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
};
// re-export everything
export * from "@testing-library/react";
// override render method
export { render };
):
Run Code Online (Sandbox Code Playgroud)
我似乎遵循提供的文档和示例,但我在 TSLint 控制台的这一行中收到此错误return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });(wrapper带下划线)
Run Code Online (Sandbox Code Playgroud)Overload 1 of 2, '(ui: ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>, options?: Pick<...> | undefined): RenderResult<...>', gave the following error. Type '({ children }: WrapperProps) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | ... | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<...>)>' is not assignable to type 'ComponentClass<{}, any> | FunctionComponent<{}> | undefined'. Type '({ children }: WrapperProps) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | ... | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<...>)>' is not assignable to type 'FunctionComponent<{}>'. Types of parameters '__0' and 'props' are incompatible. Type '{ children?: ReactNode; }' is not assignable to type 'WrapperProps'. Types of property 'children' are incompatible. Type 'ReactNode' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>'. Overload 2 of 2, '(ui: ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>, options: RenderOptions<...>): RenderResult<...>', gave the following error. Type '({ children }: WrapperProps) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | ... | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<...>)>' is not assignable to type 'ComponentClass<{}, any> | FunctionComponent<{}> | undefined'. Type '({ children }: WrapperProps) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | ... | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<...>)>' is not assignable to type 'FunctionComponent<{}>'.```
设置此功能以测试连接的组件的正确方法是什么?
小智 5
您收到该错误是因为您的道具与包装器属性的预期不兼容。您可以通过添加 React 的功能组件类型(其中包含以下定义)轻松修复它children:
import React, { ReactElement, ReactNode } from "react";
import { render as rtlRender, RenderOptions } from "@testing-library/react";
import { DynamicModuleLoader } from "redux-dynamic-modules";
import { Provider } from "react-redux";
import { reduxSearch } from "../redux/search";
import { store, history } from "../redux/store";
import { Router } from "react-router-dom";
const render = (ui: ReactElement, renderOptions?: RenderOptions) => {
const Wrapper : React.FC = ({ children }) => {
return (
<Provider store={store}>
<Router history={history}>
<DynamicModuleLoader
modules={[reduxSearch()]}
createStore={() => store}
>
{children}
</DynamicModuleLoader>
</Router>
</Provider>
);
};
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
};
// re-export everything
export * from "@testing-library/react";
// override render method
export { render };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2664 次 |
| 最近记录: |