Вот решение для модульного тестирования, основанное на объяснении @ skyboyer.
index.tsx
:
import React, { useState } from 'react';
export function SearchProviders({ changeRoute }) {
const [speciality, setspeciality] = useState('Hospital');
const onInputChange = () => {
setspeciality(speciality.toUpperCase());
};
return (
<div>
<span>{speciality}</span>
<input id="speciality" onChange={onInputChange}></input>
</div>
);
}
index.test.tsx
:
import { SearchProviders } from './';
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
describe('60135675', () => {
let wrapper: ShallowWrapper;
beforeEach(() => {
const props = { changeRoute: jest.fn() };
wrapper = shallow(<SearchProviders {...props}></SearchProviders>);
});
it('should render', () => {
expect(wrapper.exists()).toBeTruthy();
expect(wrapper.find('span').text()).toBe('Hospital');
});
it('should handle input change', () => {
wrapper.find('#speciality').simulate('change');
expect(wrapper.find('span').text()).toBe('HOSPITAL');
});
});
Результаты модульного теста со 100% покрытием:
PASS stackoverflow/60135675/index.test.tsx (6.152s)
60135675
✓ should render (11ms)
✓ should handle input change (2ms)
-----------|---------|----------|---------|---------|-------------------
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: 8.217s
Исходный код: https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60135675