【问题标题】:How to access props value component in test file (Jest and Enzyme)如何访问测试文件中的 props 值组件(Jest 和 Enzyme)
【发布时间】:2020-10-09 08:51:46
【问题描述】:

如何在测试文件(Jest 和 Enzyme)中访问组件中的 props 值? 这是我的组件,我想在测试文件(Jest 和 Enzyme)中访问 Pin 组件中的 prop 值,可以吗?

<View style={styles.containerForm}>
  <Text style={styles.textDescriptionGreyFont}>Enter Code</Text>
  <Pin testID={'input_pin'} count={4} value={otp} setCode={(code) => setOtpCode(code)} />
  <View style={styles.containerResendTimer}>
    {renderResend()}
    <Text style={styles.textDescriptionThemaFont}>{renderTimer()}</Text>
  </View>
  <ButtonFull
    testID={'submit_otp'}
    isDisabled={false}
    buttonColor={fullFilled ? color.thema : color.disabledButton}
    onPress={() => submitOtp()}
    title={submitting ? 'Loading ...' : 'Submit'}
  />
</View>

【问题讨论】:

  • 你的意思是this.props
  • 我的错,我想在 jest 和酵素中从 pin 组件访问 prop

标签: react-native jestjs enzyme


【解决方案1】:

你可以像这样找到 Pin 组件的 prop 值

const wrapper = shallow(<Component .... />);
expect(wrapper.find(Pin).props().testID).toBe('input_pin');

【讨论】:

  • 如果我想从 pin 组件中检查 prop 值怎么办?假设来自 pin 组件的 prop 值应该是空字符串 ('')
  • prop value 是什么意思,以下是哪一个 prop value?我在组件 setOtpCode(code)} /> 上只看到 4 个道具
  • 如果你想检查它是否是一个空字符串,那么就这样做 expect(wrapper.find(Pin).props().testID).toBe('');
  • 在组件引脚中,有一个道具计数对吗?如何检查道具计数组件引脚的值?
  • 如果我像这样创建 expect(appWrapper.find('[id="input_pin"]').props('count')).toBe('') 它会抛出错误,方法“ props”意味着在 1 个节点上运行。找到了 0 个。
【解决方案2】:

我找到了解决办法,

expect(appWrapper.find('Pin').prop('value')).toBe('')
expect(appWrapper.find('ButtonFull').prop('isDisabled')).toBe(true)

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 2017-06-29
    • 2017-02-12
    • 2021-01-08
    • 1970-01-01
    • 2019-04-20
    • 2020-05-10
    • 1970-01-01
    相关资源
    最近更新 更多