I have a React component that uses TextField
from office-ui-fabric-react
. I'm using Enzyme/Jest to write unit tests. I'm having trouble updating the value of the TextField
.
I've tried used .simulate('change', { target: { value: 'Input Value'} }
.
Here is my simplified version of my component:
const FeedbackForm = (props: IFeedbackFormProps): JSX.Element => {
const [comments, setComments] = React.useState<string>('');
const onCommentChange = (_event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue: string): void => {
setComments(newValue);
};
return (
<TextField
value={comments}
onChange={onCommentChange}
/>
);
};
This is my test:
it('should enable the submit button when the user enters a comment', () => {
const wrapper = shallow(<FeedbackForm {...mockProps} />);
const commentBox = wrapper.find(TextField);
const submitButton = wrapper.find('[type="submit"]');
commentBox.simulate('change', { target: { value: 'test value' } });
// This button gets enabled when the text area is not empty
expect(submitButton.is('[disabled]')).toBe(false);
});
The UI works as expected. Looking at commentBox.debug()
, the component's value
doesn't change.
I've also verified that onCommentChange
gets called when calling commentBox.simulate(...)
.