lundi 3 juillet 2017

Testing method in react component don't work

I have simple component in React. I want to test method in this component when user click button. I have test for that but finally don't pass.

Component:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';

class TestInvokingMethod extends Component {
    onClick() {
    }
    render() {
        return (
            <div>
                <input id='buttonTest' type='button' value={10} onClick={this.onClick} />
            </div>
        );
    }
}

export default TestInvokingMethod;

And test for that:

import React from 'react';
import { shallow } from 'enzyme';
import TestInvokingMethod from '../../components/TestComponent/TestInvokeMethod';

const component = shallow(
    <TestInvokingMethod />
);

test('Testing invoke method', () => {

    const mockFn = jest.fn();

    component.instance().onClick = mockFn;  
    component.update();
    component.find('#buttonTest').simulate('click');

    expect(component.instance().onClick.mock.calls.length).toBe(1);
});

Aucun commentaire:

Enregistrer un commentaire