简体   繁体   English

当存在用于组分的酶提纯时,“在反应元素的道具中未发现流动性”

[英]'flow property not found in props of react element' when is present for enzyme rendering of component

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)
  })
})

The tests pass, but it is giving me the following flow error on my 'MyComponent' declaration which refers to the rendering line in my test, despite name definitely being passed in as part of the defaultProps object that is passed into the component: 测试通过了,但是尽管我的name肯定是作为传递到组件的defaultProps对象的一部分传入的,但它在我的“ MyComponent”声明中给出了以下流错误,该声明引用了测试中的渲染行:

property 'name' Property not found in props of react element 'MyComponent' 属性'名称'在反应元素'MyComponent'的道具中找不到属性

So, if I completely removed the second test, there was no flow error in the above as written. 因此,如果我完全删除了第二项测试,那么上面编写的流程没有错误。

I think the problem was that whenever I was passing something to the render() in my test file, flow only checked the overriding props on the component instead of all of them. 我认为问题在于,每当我将某些内容传递到测试文件中的render()时,flow只会检查组件上的替代道具,而不是全部。

Rewriting my test render function like so solved my problem: 像这样重写我的测试渲染功能解决了我的问题:

const render = (overrideProps) => {
  const props = {
    ...defaultProps,
    ...overrideProps
  }

  return shallow(<MyComponent {...props} />)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM