node.jsreactjsmocha.jschaienzyme

enzyme mocha AssertionError: expected 0 to equal 21


Writing some unit tests for an app and hitting a wall in the describe block.

/* eslint-env mocha */
const React = require('react')
const chai = require('chai')
const { expect } = chai
const Search = require('../js/Search')
const ShowCard = require('../js/ShowCard')
const enzyme = require('enzyme')
const { shallow } = enzyme
const data = require('../public/data')

describe('<Search />', () => {
  it('should render as many shows as there are data for', () => {
    const wrapper = shallow(<Search />)
    expect(wrapper.find(ShowCard).length).to.equal(data.shows.length)
    console.log(wrapper.debug())
  })
})

The code in the Search component is rendering ShowCard like this:

<div className='shows'>
  {data.shows
    .filter((show) => `${show.title} ${show.description}`.toUpperCase().indexOf(this.state.searchTerm.toUpperCase()) >= 0)
    .map((show, index) => (
      <ShowCard {...show} key={index} id={index} />
  ))}
</div>

(wrapper.find(ShowCard).length) should equal (data.shows.length), but it's giving this error:

  <Search /> should render as many shows as there are data for:

  AssertionError: expected 0 to equal 21
  + expected - actual

  -0
  +21

  at Context.<anonymous> (App.spec.js:19:45)

According to the above error, the problem starts at the expectation equal(data.shows.length) but I see nothing wrong with that. Can anyone point me in the right direction?


Solution

  • wow, how embarrassing. i had the state of the Search constructor's input value set to "default search term" - thus preventing any search results from appearing until that string was manually removed from the input.

    replacing with an empty string solved the problem. all tests now pass.