I wrote a functional component using the useState hook, the component itself works fine but I'm having trouble testing it. I don't understand how to create a initial state in my component. I've tried multiple ways of mocking useState, none of which worked:
describe('<Component/>', () => {
beforeEach() = > {
jest.spyOn(Readt, 'useState').mockImplementation( () => React.useState(['test']));
//Also tried this:
//React['useState'] = jest.fn().mockImplementation( () => React.useState(['test']));
}
it('should match snapshot', () => {
expect(wrapper).toMatchSnapshot()
}
}
I've seen this post too but was unable to get it to work for me
How to set initial state for useState Hook in jest and enzyme?
I've also read that your not supposed to mock out useState in your unit test but then how do you set an initial state for your components if you don't mock it out?
Thanks.
EDIT: In regards to an answer below:
Passing in a prop doesn't change the state I'm trying to test.
I have a button which when clicked (one state) renders an input box which, when the user types in (another state) and presses the okay button (yet another state) creates a button based on the content in the input box.
The props which is passed helps renders a list of buttons but it has no control over what the user types into the input box. I'm trying to set a state in the input box and then simulate the click on the okay button and test that. I'm not trying to test reacts native useState functionality. I'm just trying to use what enzyme already has 'wrapper.setState' but how do I do that with useState?
Ideally I'd like to set up my component, set up the state in which the user has typed into the input box and then test to see if the ok button does what it's supposed to do. Without simulating all the clicks before that.