如何修复:TypeError:projects.map不是函数

时间:2019-06-13 12:07:07

标签: reactjs jestjs enzyme

我对测试数组有问题。请检查截图 https://ibb.co/0Dx26vf

  • 我尝试安装其他版本的Jest,但我得到的却是相同的 错误。
  • 我在不使用npm run jest的情况下使用CRA(这不是问题)
    beforeEach(() => {
      dashboard.find('.review-number').simulate('click')
    })

    it('Call the `addFeedback` callback', () => {
      expect(mockRemove).toHaveBeenCalledWith('1', '1')
    })
  })

这里是组件:

          <Row>
            {projects && projects.map((project, i) => {
              return (

                <Col sm={4} key={i}>
                  <div className='mainBoxItem'>
                    <Link to={'/project/' + project.id} >
                      <div className='overflow-hidden'>
                        <img src={project.userImage} alt='Car' />
                      </div>
                      <h3>{project.carType} {project.carModel}</h3>
                    </Link>
                    <div className='flex'>
                      <p>{project.authorFirstName}</p>
                      <em>{moment(project.createdAt.toDate()).calendar()}</em>
                    </div>
                    <span className='car-price'>{project.carPrice} {project.Currency} / <em>Per Day</em></span>
                    <span className='review-number' onClick={() => this.addFeedback(project.id, project.feedback)}>+1 ({project.feedback})</span>
                  </div>
                </Col>
              )
            })}
          </Row>
        </Container>```


  [1]: https://i.stack.imgur.com/LLSxx.png

1 个答案:

答案 0 :(得分:0)

我刚刚解决了这个问题

在这里您可以检查我的考试

import Adapter from 'enzyme-adapter-react-16'
import { shallow, configure } from 'enzyme'

import { Dashboard } from './index'

configure({ adapter: new Adapter() });

describe('Dashboard', () => {
  const mockRemove = jest.fn()
  const props = { addFeedback: mockRemove, projects: [] }
  const dashboard = shallow(<Dashboard {...props} />)
  console.log(dashboard.debug())
  it('render Dashboard', () => {
    expect(dashboard).toMatchSnapshot()
  })

  describe('Add feedback on click', () => {
    beforeEach(() => {
      dashboard.find('.review-number').simulate('click')

      it('Call the `addFeedback` callback', () => {
        expect(mockRemove).toHaveBeenCalled()
      })
    })
  })
})

您需要将<Dashboard {...props} />道具阵列发送到您的组件