【问题标题】:React Native/Jest TypeError: Cannot read property 'params' of undefined - testing with jestReact Native/Jest TypeError:无法读取未定义的属性“参数” - 用玩笑进行测试
【发布时间】:2018-11-09 16:57:11
【问题描述】:

我正在尝试用 jest 在应用程序中创建一个测试,这是我的一些代码行:

import React, { Component } from 'react';
import {...} from 'react-native';
import jwt_decode from 'jwt-decode';

class CreateProduct extends Component {
constructor(props) {
  super(props);
  this.keyboardHeight = new Animated.Value(0);
  this.imageHeight = new Animated.Value(199);
  this.state = {
    isButtonsHidden: false,
    title: '',
    price: '',
    description: '',
    isDialogVisible: false,
    messageError: '',
  };
}

_goBack = async () => {
  const {state} = this.props.navigation;
  var token = state.params ? state.params.token : undefined;

  this.props.navigation.navigate('MyProducts', {token:token});
}

我想测试一下导航:

this.props.navigation.navigate('MyProducts', {token:token});

现在这是测试的尝试:

describe('Testing navigation', () =>{

  let wrapper = null
  const spyNavigate = jest.fn()
  const props = {
    navigation:{
        navigate: spyNavigate
    }
  }
  const params = {
      token: 'randomToken'
  }

  beforeEach(() => {
    wrapper = shallow(<CreateProduct {...props}/>)
    wrapper.setState({params: params})
  })

  it('should test navigation', () => {
  wrapper.instance()._goBack(params)
  expect(spyNavigate).toHaveBeenCalled()
  })
})

但我收到了this error

我假设我传递const params 的方式有误。你能帮我告诉我模拟令牌以及在屏幕中导航的最佳方式是什么吗?

谢谢。

【问题讨论】:

    标签: react-native jestjs react-native-navigation


    【解决方案1】:

    根本原因是您的_goBackasync。但是在运行expect 之前不要等到它结束。更重要的是:jest 也不会等待 _goBack 完成,所以你甚至看不到错误

    无法读取未定义的属性“参数”

    这是因为您没有在 navigation.params 中模拟 state

    要使用异步代码,有 2 different approaches in Jest:要么从 it() 返回 Promise,要么手动运行 done() 回调(它作为 it() 中的第一个参数传递)。

    我会选择第二个,因为它允许我们在运行 expect 之前等待直到 goBack 完成:

    describe('Testing navigation', () => {
    
      let wrapper = null
      const spyNavigate = jest.fn()
      const props = {
        navigation: {
          navigate: spyNavigate,
          state: {}
        }
      }
      const params = {
        token: 'randomToken'
      }
    
      beforeEach(() => {
        wrapper = shallow(<CreateProduct {...props} />)
        wrapper.setState({ params: params })
      })
    
      it('should test navigation', async () => {
        await wrapper.instance()._goBack(params)
        expect(spyNavigate).toHaveBeenCalled()
      })
    })
    

    或者不使用async/await 它看起来像

      it('should test navigation', () => {
        return wrapper.
            instance()._goBack(params).
            then(() => expect(spyNavigate).toHaveBeenCalled());
      })
    

    看起来很乱

    或者使用done()回调

      it('should test navigation', (done) => {
          wrapper.
            instance()._goBack(params).
            then(() => expect(spyNavigate).toHaveBeenCalled()).
            then(done);
      })
    

    【讨论】:

    • 这对我来说是错误的---> > 84 |期望(spyNavigate).toHaveBeenCalled();
    • @samira 我怀疑您的代码与 OP 在 2018 年的代码完全相同。可能是“shallow() 不会触发 useEffect()”、触发导航时选择器错误或名称不正确间谍的道具。最好通过提供完整的上下文来提出新问题。
    猜你喜欢
    • 1970-01-01
    • 2017-06-27
    • 2023-03-03
    • 1970-01-01
    • 2020-01-08
    • 2017-09-19
    • 2018-11-27
    • 2017-07-30
    • 2019-02-16
    相关资源
    最近更新 更多