javascript - Simulating a Div Click on Enzyme and React - Stack Overflow

I'm learning Enzyme, and I've been starting to write some tests on an application a team has

I'm learning Enzyme, and I've been starting to write some tests on an application a team has written. I'm attempting to simulate clicking an element. The application basically have a list, and whenever you click on it, (an image of) a check-mark appears. If you click again, (an image of) no check mark appears. The application does this by changing the state whenever you click the element, which then determines which image to render.

It works on the actual application, but somehow Enzyme is failing. Is there something I'm missing with respect to Enzyme?

Below is some simplified code. Here is the class:

class RecipeInfo extends Component {

  constructor(props) {
    super(props);
    this.state = {};
    this.doneClick = this.doneClick.bind(this);
  }

  doneClick(event) {
    let index = event.target.name;
    let state = {};
    state[index] = !this.state[index];
    this.setState(state)
  }

  renderIngredients(ingredients) {
   let quantities = ingredients.quantity;
   let lines = [];
   for(let i = 0; i < quantities.length; i++){
     lines.push(
      <div className='flex-body-ingredients' key={i}>
        <div onClick={this.doneClick} id={i} >
        {this.state[i] ? 
          (<img className='empty-check' src="/assets/success.png" alt="success" name={i} />)
          : (<img className='empty-check' src="/assets/oval.png" name={i} alt="oval" />)}
        </div>
      </div>
       )
     }
     return lines.map((line) => line)
   }

  render() {
    return (
      {this.renderIngredients(ingredients)}
    )
  }
}

function mapStateToProps(state) {
  return {
    recipe: state.recipes.selectedRecipe
  }
}
export default connect(mapStateToProps, actions)(RecipeInfo);

And below is the test I just wrote:

describe('Recipe Info', () => {
  const recipeInfo = mount(<Provider store={createRecipeStore}><RecipeInfo/></Provider>);

  it('shows a click and then hides the click when clicking an ingredient', () => {
    const notChecked = recipeInfo.find('[alt="oval"]').first();
    expect(notChecked).toBeDefined();

    recipeInfo.find('.flex-body-ingredients').first().childAt(0).simulate('click');

    const checked = recipeInfo.find('[alt="success"]').first();
    expect(checked).toBeDefined();
  });

});

When I run the test, and I console log the element, I see the following:

<div id="0"><img class="empty-check" src="/assets/oval.png" name="0" alt="oval"></div>

This never changes after the click.

I'm learning Enzyme, and I've been starting to write some tests on an application a team has written. I'm attempting to simulate clicking an element. The application basically have a list, and whenever you click on it, (an image of) a check-mark appears. If you click again, (an image of) no check mark appears. The application does this by changing the state whenever you click the element, which then determines which image to render.

It works on the actual application, but somehow Enzyme is failing. Is there something I'm missing with respect to Enzyme?

Below is some simplified code. Here is the class:

class RecipeInfo extends Component {

  constructor(props) {
    super(props);
    this.state = {};
    this.doneClick = this.doneClick.bind(this);
  }

  doneClick(event) {
    let index = event.target.name;
    let state = {};
    state[index] = !this.state[index];
    this.setState(state)
  }

  renderIngredients(ingredients) {
   let quantities = ingredients.quantity;
   let lines = [];
   for(let i = 0; i < quantities.length; i++){
     lines.push(
      <div className='flex-body-ingredients' key={i}>
        <div onClick={this.doneClick} id={i} >
        {this.state[i] ? 
          (<img className='empty-check' src="/assets/success.png" alt="success" name={i} />)
          : (<img className='empty-check' src="/assets/oval.png" name={i} alt="oval" />)}
        </div>
      </div>
       )
     }
     return lines.map((line) => line)
   }

  render() {
    return (
      {this.renderIngredients(ingredients)}
    )
  }
}

function mapStateToProps(state) {
  return {
    recipe: state.recipes.selectedRecipe
  }
}
export default connect(mapStateToProps, actions)(RecipeInfo);

And below is the test I just wrote:

describe('Recipe Info', () => {
  const recipeInfo = mount(<Provider store={createRecipeStore}><RecipeInfo/></Provider>);

  it('shows a click and then hides the click when clicking an ingredient', () => {
    const notChecked = recipeInfo.find('[alt="oval"]').first();
    expect(notChecked).toBeDefined();

    recipeInfo.find('.flex-body-ingredients').first().childAt(0).simulate('click');

    const checked = recipeInfo.find('[alt="success"]').first();
    expect(checked).toBeDefined();
  });

});

When I run the test, and I console log the element, I see the following:

<div id="0"><img class="empty-check" src="/assets/oval.png" name="0" alt="oval"></div>

This never changes after the click.

Share Improve this question edited Apr 23, 2017 at 0:48 Hadoren asked Apr 23, 2017 at 0:31 HadorenHadoren 1651 gold badge2 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I figured out the problem. It was that I wasn't passing an event handler into the simulation.

I had to change it to:

recipeInfo.find('.flex-body-ingredients').first().childAt(0).simulate('click', {target: {name: 0}});

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

相关推荐

  • javascript - Simulating a Div Click on Enzyme and React - Stack Overflow

    I'm learning Enzyme, and I've been starting to write some tests on an application a team has

    2天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信