calendar icon indicating copy to clipboard operation
calendar copied to clipboard

Jest + enzyme + find('Calendar')

Open rpellerin opened this issue 7 years ago • 0 comments

Hi, Since 9.9.0, when I run the following piece of code:

import React from 'react'
import moment from 'moment'
import Calendar from 'rc-calendar'
import { mount } from 'enzyme'

class Cal extends React.PureComponent {
  state = {
    date: moment(this.props.date),
  }

  onChange = date => this.setState({ date: moment(date) })

  render() {
    const { date } = this.state
    return (
      <Calendar
        className="my-calendar"
        onChange={this.onChange}
        onSelect={() => null}
        showWeekNumber
        defaultValue={moment()}
        selectedValue={date}
        value={date}
      />
    )
  }
}

describe('Calendar', () => {
  let wrapper
  let rc_calendar

  beforeEach(() => {
    wrapper = mount(<Cal date="2016-01-01T00:00:00.000Z" />)
    rc_calendar = wrapper.find('Calendar')
  })

  it('updates value on change', () => {
    expect(rc_calendar.prop('selectedValue')).toEqual(moment('2016-01-01T00:00:00.000Z'))
})


I get the following error:

 FAIL  fail.test.js
  Calendar
     updates value on change (107ms)

  Calendar › updates value on change

    Method “props” is meant to be run on 1 node. 0 found instead.

      37 | 
      38 |   it('updates value on change', () => {
    > 39 |     expect(rc_calendar.prop('selectedValue')).toEqual(moment('2016-01-01T00:00:00.000Z'))
         |                        ^
      40 |     expect(rc_calendar.prop('value')).toEqual(moment('2016-01-01T00:00:00.000Z'))
      41 | 
      42 |     rc_calendar.prop('onChange')('2010-05-05T00:00:00.000Z')

      at ReactWrapper.single (node_modules/enzyme/build/ReactWrapper.js:1700:17)
      at ReactWrapper.props (node_modules/enzyme/build/ReactWrapper.js:993:21)
      at ReactWrapper.prop (node_modules/enzyme/build/ReactWrapper.js:1187:21)
      at Object.it (fail.test.js:39:24)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.512s
Ran all test suites matching fail.test.js/i.

How come .find('Calendar') does not work?

This was perfectly working prior to 9.9.0. I had a looked at the diff and nothing that might affect this behavior has changed. Any clue? Thanks!

rpellerin avatar Jan 15 '19 23:01 rpellerin