React Native Jest Enzyme Find and Press Component

时间:2017-02-25 11:52:47

标签: unit-testing testing tdd jestjs enzyme

我正在尝试冒险,并试图在我的个人项目中获得100%的代码覆盖率。我无法解释如何使用jestenzymereact-native来模拟点击事件的文档。

<Screen.TopBar>
    <Toolbar
        leftElement="arrow-back"
        onLeftElementPress={() => router.pop()}
        centerElement={pageName}
    />
</Screen.TopBar>

这只是它的顶部,但我试图模拟该元素的点击。

const wrapper = mount(
    <MockProvider store={store}>
        <ThemeProvider uiTheme={uiTheme}>
            <Category />
        </ThemeProvider>
    </MockProvider>
);
expect(wrapper.find('Toolbar').length).toBe(1);

我收到错误:

ReferenceError: document is not defined

  at Object.renderIntoDocument (node_modules/react-dom/lib/ReactTestUtils.js:73:15)
  at renderWithOptions (node_modules/enzyme/build/react-compat.js:187:26)
  at new ReactWrapper (node_modules/enzyme/build/ReactWrapper.js:94:59)
  at mount (node_modules/enzyme/build/mount.js:19:10)
  at Object.<anonymous> (app/screens/category/tests/Category.test.js:32:30)

所以我的问题是如何选择工具栏以便我可以点击工具栏组件中的按钮。

1 个答案:

答案 0 :(得分:1)

挂载仅适用于react-dom,而不适用于react-native