首页 文章

'flow property not found in props of react element'何时存在用于组分的酶呈现

提问于
浏览
1

My Component:

// @flow
import React from 'react'

type Props = {
  close: Function,
  name: string
}

const MyComponent = ({ close, name }: Props) => (
  <div className='click' onClick={close}>
    {name}
  </div>
)

export default MyComponent

My Enzyme Test:

// @flow
import React from 'react'
import assert from 'assert'
import { shallow } from 'enzyme'
import sinon from 'sinon'

import MyComponent from 'client/apps/spaces/components/slideouts/record-settings/myc'

const defaultProps = {
  close: () => {},
  name: 'My Name'
}

const render = (props) => shallow(<MyComponent {...defaultProps} {...props} />)

describe('<MyComponent />', () => {
  it('renders the name', () => {
    const component = render()

    assert.equal(component.find('.click').text(), 'My Name')
  })

  it('calls close on Click', () => {
    const close = sinon.spy()
    const component = render({ close })
    const clickableDiv = component.find('.click')
    clickableDiv.simulate('click')

    assert(close.calledOnce)
  })
})

测试通过,但它在我的'MyComponent'声明中给出了以下流错误,该声明引用了我的测试中的渲染行,尽管 name 肯定是作为传递到组件的 defaultProps 对象的一部分传入的:

property'name'在反应元素'MyComponent'的道具中找不到属性

1 回答

  • 2

    因此,如果我完全删除了第二个测试,则上面写的没有流错误 .

    我认为问题在于,每当我在测试文件中传递某些东西时,流程只检查组件上的重写道具而不是所有这些道具 .

    像我这样重写我的测试渲染函数解决了我的问题:

    const render = (overrideProps) => {
      const props = {
        ...defaultProps,
        ...overrideProps
      }
    
      return shallow(<MyComponent {...props} />)
    }
    

相关问题