javascript - How to test: onClick event - Jest Enzyeme - Stack Overflow

I am new to unit testing and I just ran a test coverage. This is the only line missing at the moment.Ho

I am new to unit testing and I just ran a test coverage. This is the only line missing at the moment.

How could I test this line and make sure 100 % is covered

   export default class Normalize extends Component {

 constructor(props) {
super(props)
this.state = {}
 //    this.handleChange = this.handleChange.bind(this)
}

 ponentDidMount() {
 this.props.normalizeData(null)    
}

 render () {
return (
  <div id='normalize-container'>
    <div id='normalize-header'>
    </div>
    <br /><br />
    <h3 className='normalized-header'>{this.props.newResponse ? 'Raw Data' : 'Normalized Data'}</h3><br/>
    <div>
      {this.props.newResponse ? null :
        this.props.THead ?
          <div>
            {!this.props.datasourceCatalog ? 
              <div id='next-button-modal'>
                {/*<button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={this.props.nextTab}><img src={nextImg}/></button>*/}
                <button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={(e) => {this.props.toggleModal(); this.props.nextTab();}}>
                  <FontAwesome
                  className='fa-angle-right'
                  name='view-externallink-img'
                  size='2x'/>                  
                </button>

                <h4>Proceed To Shape</h4>
              </div> : 
            null}

            <div className='normalize-table-container'>
              <div className="data-table-wrapper">
                <table>
                  <thead dangerouslySetInnerHTML={{__html: this.props.THead}} />
                  <tbody dangerouslySetInnerHTML={{__html: this.props.TBody}} />
                </table>
              </div>
            </div>
          </div>
        : null
      }
    </div>
  </div>

Using React JS - jest and enzyme

Testing file :

// jest mock functions (mocks this.props.func)
const normalizeData =  jest.fn();
const toggleModal =  jest.fn();
const nextTab =  jest.fn();
const onClick =  jest.fn();

// defining this.props
const baseProps = {
normalizeData,
newResponse:{},
THead:{},
TBody:{},
datasourceCatalog:{},
toggleModal,
nextTab,
onClick,

describe(' Normalize Test', () => {
let wrapper;
let tree;

beforeEach(() => wrapper = shallow(<Normalize  {...baseProps} />));

it(' Should render with all of the props', () => { 

Render with all props are working - but just need to make sure how to test the line above, on click with 2 props.

Thank you

I am new to unit testing and I just ran a test coverage. This is the only line missing at the moment.

How could I test this line and make sure 100 % is covered

   export default class Normalize extends Component {

 constructor(props) {
super(props)
this.state = {}
 //    this.handleChange = this.handleChange.bind(this)
}

 ponentDidMount() {
 this.props.normalizeData(null)    
}

 render () {
return (
  <div id='normalize-container'>
    <div id='normalize-header'>
    </div>
    <br /><br />
    <h3 className='normalized-header'>{this.props.newResponse ? 'Raw Data' : 'Normalized Data'}</h3><br/>
    <div>
      {this.props.newResponse ? null :
        this.props.THead ?
          <div>
            {!this.props.datasourceCatalog ? 
              <div id='next-button-modal'>
                {/*<button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={this.props.nextTab}><img src={nextImg}/></button>*/}
                <button title='Proceed To Shape' className='request-button' id='next-btn-ready-modal' onClick={(e) => {this.props.toggleModal(); this.props.nextTab();}}>
                  <FontAwesome
                  className='fa-angle-right'
                  name='view-externallink-img'
                  size='2x'/>                  
                </button>

                <h4>Proceed To Shape</h4>
              </div> : 
            null}

            <div className='normalize-table-container'>
              <div className="data-table-wrapper">
                <table>
                  <thead dangerouslySetInnerHTML={{__html: this.props.THead}} />
                  <tbody dangerouslySetInnerHTML={{__html: this.props.TBody}} />
                </table>
              </div>
            </div>
          </div>
        : null
      }
    </div>
  </div>

Using React JS - jest and enzyme

Testing file :

// jest mock functions (mocks this.props.func)
const normalizeData =  jest.fn();
const toggleModal =  jest.fn();
const nextTab =  jest.fn();
const onClick =  jest.fn();

// defining this.props
const baseProps = {
normalizeData,
newResponse:{},
THead:{},
TBody:{},
datasourceCatalog:{},
toggleModal,
nextTab,
onClick,

describe(' Normalize Test', () => {
let wrapper;
let tree;

beforeEach(() => wrapper = shallow(<Normalize  {...baseProps} />));

it(' Should render with all of the props', () => { 

Render with all props are working - but just need to make sure how to test the line above, on click with 2 props.

Thank you

Share edited Feb 9, 2019 at 16:22 user 9191 asked Feb 9, 2019 at 14:44 user 9191user 9191 7475 gold badges15 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Something like this should work:

it('should call toggleModal and nextTab functions on button click', () => {
  // Reset info from possible previous calls of these mock functions:
  baseProps.toggleModal.mockClear();
  baseProps.nextTab.mockClear();

  // Remove props that would end up hiding the button
  wrapper.setProps({
    newResponse: null,
    datasourceCatalog: null
  });

  // Find the button and call the onClick handler
  wrapper.find('#next-btn-ready-modal').simulate('click');

  // Test to make sure prop functions were called via simulating the button click
  expect(baseProps.toggleModal).toHaveBeenCalled();
  expect(baseProps.nextTab).toHaveBeenCalled();
})

Note: you can also split these into separate tests, one to test each call separately.

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

相关推荐

  • javascript - How to test: onClick event - Jest Enzyeme - Stack Overflow

    I am new to unit testing and I just ran a test coverage. This is the only line missing at the moment.Ho

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信