Есть два способа проверить этот случай. Например,
index.tsx
:
import React, { Component } from 'react';
export default class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
someValue: '',
};
}
render() {
return (
<>
<input type="text" onChange={(e) => this.setState({ someValue: e.target.value })}></input>
</>
);
}
}
index.test.tsx
:
import MyComponent from './';
import { shallow } from 'enzyme';
import React from 'react';
describe('61597604', () => {
it('should pass', () => {
const wrapper = shallow(<MyComponent></MyComponent>);
const mEvent = { target: { value: 'ok' } };
wrapper.find('input').simulate('change', mEvent);
expect(wrapper.state()).toEqual({ someValue: 'ok' });
});
it('should pass 2', () => {
const wrapper = shallow(<MyComponent></MyComponent>);
const input = wrapper.find('input').getElement();
const mEvent = { target: { value: 'ok' } };
input.props.onChange(mEvent);
expect(wrapper.state()).toEqual({ someValue: 'ok' });
});
});
Результаты модульного теста со 100% покрытием:
PASS stackoverflow/61597604/index.test.tsx (8.059s)
61597604
✓ should pass (13ms)
✓ should pass 2 (1ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.tsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 9.213s