javascript - Why do enzyme not find 'tr' in a table? - Stack Overflow

I'm writting some test using enzyme but I have some weird behavior. Here is my test : import React

I'm writting some test using enzyme but I have some weird behavior. Here is my test :

  import React from 'react'
  import { TypeTable } from 'routes/Type/ponents/TypeTable'
  import { shallow } from 'enzyme'
  import { Table } from 'react-toolbox'

  // ...

  let _props, _wrapper

  beforeEach(() => {
    _props = {
      getTypes: sinon.spy(),
      types: [
        { name: 'type 1'},
        { name: 'type 2'}
      ]
    }
    _wrapper = shallow(<TypeTable {..._props} />)
  })

  it('Should render a <Table>', () => {
    expect(_wrapper.is(Table)).to.be.true
  })

  it('should render 2 rows', () => {
    expect(_wrapper.find('tbody').find('tr')).to.have.length(2)
  })

The first test is working. The second one is not working (the assertion is failing : expected { Object (root, unrendered, ...) } to have a length of 2 but got 0)

But in my second test, if I print the content of my _wrapper using console.log(_wrapper.html()) I get

'<table data-react-toolbox="table">
  <thead>
    <tr>
      <th>name</th>
    </tr>
  </thead>
  <tbody>
    <tr data-react-toolbox-table="row">
      <td>type 1</td>
    </tr>
    <tr data-react-toolbox-table="row">
      <td>type 2</td>
    </tr>
  </tbody>
</table>'

Which seems to be ok and which contains several tr.

Did I missed something ?

I'm writting some test using enzyme but I have some weird behavior. Here is my test :

  import React from 'react'
  import { TypeTable } from 'routes/Type/ponents/TypeTable'
  import { shallow } from 'enzyme'
  import { Table } from 'react-toolbox'

  // ...

  let _props, _wrapper

  beforeEach(() => {
    _props = {
      getTypes: sinon.spy(),
      types: [
        { name: 'type 1'},
        { name: 'type 2'}
      ]
    }
    _wrapper = shallow(<TypeTable {..._props} />)
  })

  it('Should render a <Table>', () => {
    expect(_wrapper.is(Table)).to.be.true
  })

  it('should render 2 rows', () => {
    expect(_wrapper.find('tbody').find('tr')).to.have.length(2)
  })

The first test is working. The second one is not working (the assertion is failing : expected { Object (root, unrendered, ...) } to have a length of 2 but got 0)

But in my second test, if I print the content of my _wrapper using console.log(_wrapper.html()) I get

'<table data-react-toolbox="table">
  <thead>
    <tr>
      <th>name</th>
    </tr>
  </thead>
  <tbody>
    <tr data-react-toolbox-table="row">
      <td>type 1</td>
    </tr>
    <tr data-react-toolbox-table="row">
      <td>type 2</td>
    </tr>
  </tbody>
</table>'

Which seems to be ok and which contains several tr.

Did I missed something ?

Share Improve this question asked Oct 24, 2016 at 12:28 ThomasThiebaudThomasThiebaud 12k6 gold badges56 silver badges78 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

shallow will not "render" subponents. It is used to test a single ponent and not its children. I think that using mount instead of shallow will let you test what you want.

Shallow rendering is useful to constrain yourself to testing a ponent as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child ponents.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745360086a4624317.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信