React router 以编程方式在react router中设置路由以进行集成测试

React router 以编程方式在react router中设置路由以进行集成测试,react-router,React Router,我正在尝试为React/Redux应用程序编写一些集成测试,我希望能够将我的整个应用程序视为一个黑盒。在这一点上,我有一个基本的测试设置,但当它加载时,没有匹配的路由,应用程序退出。我的谷歌搜索让我在react router中找到了,但我不清楚如何使用它,而不必在测试中重新定义路由器和路由 这是我的App.tsx文件: import * as React from "react"; import * as ReactRouter from "react-router-dom"; import {

我正在尝试为React/Redux应用程序编写一些集成测试,我希望能够将我的整个应用程序视为一个黑盒。在这一点上,我有一个基本的测试设置,但当它加载时,没有匹配的路由,应用程序退出。我的谷歌搜索让我在react router中找到了
,但我不清楚如何使用它,而不必在测试中重新定义路由器和路由

这是我的App.tsx文件:

import * as React from "react";
import * as ReactRouter from "react-router-dom";
import { SomeContainer } from "./Container";

import "./App.css";

const { BrowserRouter, Route } = ReactRouter;

class App extends React.Component<{}, {}> {
  render() {
    return (
      <BrowserRouter>
        <div className="app">
          <Route
            exact={true}
            path="/segment/:id"
            component={SomeContainer}
          />
        </div>
      </BrowserRouter>
    );
  }
}

export default App;
import*as React from“React”;
从“react router dom”导入*作为ReactRouter;
从“/Container”导入{SomeContainer};
导入“/App.css”;
const{BrowserRouter,Route}=ReactRouter;
类应用程序扩展了React.Component{
render(){
返回(
);
}
}
导出默认应用程序;
还有我的测试:

import { mount } from "enzyme";
import * as React from "react";
import { Provider } from "react-redux";
import configureMockStore from "redux-mock-store";
import App from "./App";

describe("/src/App", () => {
  it("should render", () => {
    const mockStore = configureMockStore([]);
    const store = mockStore({});
    const wrapper = mount(
      <Provider store={store}>
        <App />
      </Provider>
    );

    wrapper.find(".some-css-selector").simulate("click");
    ...make some assertions
  });
});
从“酶”导入{mount};
从“React”导入*作为React;
从“react redux”导入{Provider};
从“redux模拟存储”导入configureMockStore;
从“/App”导入应用程序;
描述(“/src/App”,()=>{
它(“应该呈现”,()=>{
const mockStore=configureMockStore([]);
const store=mockStore({});
常量包装器=装入(
);
find(“.somecss选择器”).simulate(“单击”);
…做出一些断言
});
});
。如果匹配了正确的路由,但当前未找到,则某些css选择器将存在

我是否有办法在测试中指定要加载与
/segment/123
匹配的路由,以便我的路由器呈现
某个容器

或者,我可以将
加载到
中,然后完全跳过路由器,但我希望在测试中尽可能多地封装实际应用程序

感谢您的帮助