Enzyme: is there a simulate example that passes in the `args` param?

Created on 25 Jan 2016  路  3Comments  路  Source: enzymejs/enzyme

Most helpful comment

Are you asking it to be documented better? Or just want to see what an example is? This would be an example:

// given this component
class Component {
  foo(e) {
    console.log(e.pageX);
  }

  render() {
    return (
      <div onClick={foo} />
    );
  }
}
// test
const wrapper = shallow(Component);
const div = wrapper.find('div');

div.simulate('click', { pageX: 200 });

All 3 comments

Are you asking it to be documented better? Or just want to see what an example is? This would be an example:

// given this component
class Component {
  foo(e) {
    console.log(e.pageX);
  }

  render() {
    return (
      <div onClick={foo} />
    );
  }
}
// test
const wrapper = shallow(Component);
const div = wrapper.find('div');

div.simulate('click', { pageX: 200 });

@blainekasten thanks that works. But because of the issue with jsdom (https://github.com/tmpvar/jsdom/issues/961), I was trying to see if there was a way to pass in the dataset objecte.

import React from 'react/addons';

// botton component
let Btn = React.createClass({

  _go(e){
    console.log('dataset = ', e.currentTarget.dataset);
  },

  render(){
    return <button id="btn" onClick={this._go} data-name="foo">go</button>;
  }
});


// tests

import { mount } from 'enzyme';
let wrapper = mount(<Btn />);
let btn = wrapper.find('#btn');

btn.simulate('click', {
  // attempt 1
  dataset: {
    name: 'foo'
  },
 // attempt 3
  target: {
    dataset: {
      name: 'foo'
    }
  },
 // attempt 3
  currentTarget: {
    dataset: {
      name: 'foo'
    }
  }
});

// results in
// dataset =  undefined

i'm switching to use e.currentTarget.getAttribute instead

Was this page helpful?
0 / 5 - 0 ratings