如何使用 Jest、React-Native 酶模拟单元测试中的事件

问题描述 投票:0回答:3

我正在尝试弄清楚如何在 React-Native 应用程序中使用 Jest 测试“onPress”事件,以便我可以确保调用正确的函数。

我浏览了文档和谷歌,但在 React-Native 中找不到解决方案。

这是我发现应该适用于 React-Native 的内容

enzyme
:

const mockFunc = jest.fn();
const component = mount(<MyComponent onPress={mockFunc} />);
component.simulate('press');
expect(mockFunc).toHaveBeenCalled();

但这行不通。好像

mount
不起作用,我得到这个输出:

ReferenceError:文档未定义

我尝试使用

shallow
代替,但是当我查看函数的输出时
TouchableOpacity
没有被渲染……你猜对了,它也不起作用。不知道该怎么做。

有人找到在 React-Native 上测试事件的方法吗?

谢谢

javascript unit-testing react-native jestjs enzyme
3个回答
14
投票

Enzyme 不支持 React-Native,因为它的渲染方式不同并且不使用 DOM。这就是为什么您会收到错误

ReferenceError: document is not defined
。您可以查看本期了解更多信息。 React 团队目前正在努力在
.find()
中公开一个
react-test-renderer
方法来模拟组件上的操作。然后它应该适用于 React/React-native 而不需要 DOM 环境。

有一个 hack 你可以做(这就是我们在我们公司所做的),它正在呈现一个自定义组件,该组件扩展

TouchableOpacity
并映射
onClick
以调用
onPress
。像这样的东西:

const mockPressable = (name) => {
  const RealComponent = require.requireActual(name);

  class Component extends RealComponent {

    render() {
      return React.createElement(
        RealComponent.displayName || RealComponent.name,
        { ...this.props, onClick: this.props.onPress },
        this.props.children
      );
    }

  }

  return Component;
};


jest.mock('TouchableOpacity', () => mockPressable('TouchableOpacity'));

在你的测试代码中,你调用

component.simulate('click')
.

这是一个 hack,我不确定这样做的后果是什么,但它对我们的用例有效。


11
投票

你应该使用

shallow
,然后调用
.dive()

const mockFunc = jest.fn();
const component = shallow(<MyComponent onPress={mockFunc} />);    
component.dive().simulate('press');
expect(mockFunc).toHaveBeenCalled();

6
投票

我能够像您在 React Native 的问题中描述的那样运行测试。这是我的配置:

package.json

"scripts": {
  ...
  "test": "node_modules/jest/bin/jest.js",
}

"devDependencies": {
  ...
  "enzyme": "^3.1.0",
  "enzyme-adapter-react-16": "^1.0.1",
  "enzyme-to-json": "^3.1.2",
  "jest": "^21.2.1",
  "jest-enzyme": "^4.0.0",
  "jest-expo": "~21.0.0",
}

"jest": {
  "preset": "jest-expo",
  "setupFiles": [
    "./test/jestSetup.js"
  ],
  "snapshotSerializers": [
    "./node_modules/enzyme-to-json/serializer"
  ]
}

测试/jestSetup.js

import { configure, shallow, render, mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

configure( { adapter: new Adapter() } )

// enzyme
global.shallow = shallow
global.render = render
global.mount = mount

示例组件:

import React from 'react'
import { Button } from 'react-native'

const CancelButton = ( props ) =>
  <Button
    { ...props }
    onPress={ () => { props.navigation.goBack() } }
    title="Cancel"
  />

export { CancelButton }

示例测试

import React from 'react'
import { CancelButton } from '../CancelButton'

test( 'onPress', () => {
  const goBackFunc = jest.fn()

  const navigation = {
    goBack: goBackFunc,
  }

  const component = shallow(
    <CancelButton
      navigation={ navigation }
    />
  )

  component.simulate( 'press' )
  expect( goBackFunc ).toHaveBeenCalled()
} )

.babelrc

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.