【问题标题】:Jest enzyme test router with props带道具的 Jest 酶测试路由器
【发布时间】:2021-04-18 10:01:09
【问题描述】:

我有反应应用程序并开始用单元测试覆盖它。 我的应用文件包含:

export default class App extends Component {
    render() {
        return (
  <Provider store={store}>
    <Router history={history}>      
        <div>
            <Header />
            <Switch>
              <Route exact path="/" component={Home} />
              <Route exact path="/detail/:collectionName/:RecordId/:firmId" component={DetailDialog} />
              <Route exact path="/dashboard" component={Dashboard} />
              <Route path="/auth-login" component={LogIn} />
              <Route path="/auth-register" component={Register} />
              <Route path="/user-manager" component={this.props.decoded.role==='Admin' ? Admin : Home} />
            </Switch>
        </div>
    </Router>
  </Provider>
        );
    }
}

我要做下一个测试:去登录,检查href和通过href检查组件。

it.only('test right component is attached', () => {
        const wrapper = shallow(<Login loginUser={fn}/>);
        const registerLink = wrapper.find("[data-qa='sign-up']").props().href;
        const history = jest.fn();
        const props = {
            decoded: {role: 'Admin'}
        }
        const router = mount(
            <MemoryRouter initialEntries={[registerLink]}>
                <App history={history} {...props} />
            </MemoryRouter>
        );
        expect(router.find(Register)).toHaveLength(1);
    });

直到安装一切正常。但是在安装时我收到下一个错误:

控制台错误 错误:未捕获 [错误:无法在未安装的组件上找到节点。]

我做错了什么?如何挂载组件并检查 Register 是否在其中?

【问题讨论】:

    标签: jestjs enzyme


    【解决方案1】:

    尝试传递 history 属性,而不是 jest.fn(),而是 { push : jest.fn() }

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2020-07-15
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2019-11-15
      • 2019-04-24
      相关资源
      最近更新 更多