如何从<Redirect to =“ some-url” />获取组件

时间:2019-08-01 10:08:19

标签: reactjs typescript jestjs enzyme

我正在尝试为react项目中的路由器重定向编写jest + enzyme测试。如何从重定向标记中提供的URL获取组件?

目前,我正在测试,返回的标签为:<Redirect to={"/variants-products"} />。很好,但是我也想从/ variant-products获取组件,以执行快照测试。

这是我的退货。

return (
    <Route
      {...rest}

      render={props => {

        let scen, order;
        if (Scenario === undefined) {
         const { scenario, order_id } = queryString.parse(props.location.search);
         scen = scenario, order = order_id;
        } else { // for testing
          scen = Scenario, order = Order 
          if (vInfo === null) {visitorInfo.visitor.login = vInfo}
        }

        switch (scen) {
          case "success":
            return <Redirect to={`/checkout/order/${order}/confirmed`} />;
          case "paid_access":
            return <Redirect to={"/variants-products"} />;
          case "auth_access":
            return <Component {...props} />;
          case "auth_paid_access":
            return visitorInfo.visitor.login ? (
              <Redirect to={"/variants-products"} />
            ) : (
              <Component {...props} />
            );
          default:
            return (
              <Text color="white">
                No get parameter scenario: ?scenario=paid_access
              </Text>
            );
        }
      }}
    /> 
  );

在测试中,我目前正在测试“ paid_access”情况。

describe("pages after redirect should match their URLs", () => {

    it("should switch to paid_access", () => {
        const wrapper = mount(
            <Visitor.Provider>
                <MemoryRouter>
                    <CheckRoute scenario="paid_access" />
                </MemoryRouter>
            </Visitor.Provider> 
        );

        expect(wrapper).toMatchSnapshot();
        console.log(wrapper.debug());
    })
})

但是“ wrapper.debug()”仅返回当前组件,而不重定向到/ variant-products

<Provider>
      <MemoryRouter>
        <Router history={{...}}>
          <CheckRoute scenario="paid_access">
            <Route render={[Function: render]}>
              <Redirect to="/variants-products">
                <Lifecycle onMount={[Function: onMount]} onUpdate={[Function: onUpdate]} to="/variants-products" />
              </Redirect>
            </Route>
          </CheckRoute>
        </Router>
      </MemoryRouter>
    </Provider>

请帮助!我需要从/ variant-products渲染组件。

0 个答案:

没有答案