Jest Enzyme测试失败,未定义

时间:2018-11-17 15:01:07

标签: javascript reactjs jestjs enzyme

在下面的代码中,我正在使用React,Jest和Enzyme来确认传递到组件中的prop具有已知值。被测组件由受控的INPUT元素组成。当我将其中一个测试值更改为FAIL(失败)时,“ Received”(接收到的)值将以未定义而不是错误的值返回。我不知道为什么。我测试输入元素的值不正确吗?

import React from "react"
import { shallow, mount, render } from 'enzyme';
import { MyAddressForm } from "../components/addressForm"

const testText = "fieldValue"

const testObject = {
  firstName: {testText},
  lastName: 'Smith'
}

const wrapper = shallow(<MyAddressForm shipTo={testObject}  />)

var fields = []

for (var field in testObject) {
  if (testObject.hasOwnProperty(field)) {
    fields.push(field)
  }
}

var theTest = (fieldName) => {
  let selector = `input[name="${fieldName}"]`
  expect(wrapper.find(selector).prop('value').text).toEqual(testText)
}

test.each(fields)(
  '%s',
  fieldName => theTest(fieldName)
)

1 个答案:

答案 0 :(得分:0)

因为将firstName设置为对象,而不是testText的值!

正确的测试功能应为:

var theTest = (fieldName) => {
  let selector = `input[name="${fieldName}"]`
  expect(wrapper.find(selector).prop('value')).toEqual(testText)
}