Suppose I have a (rather contrived) controller and view
const useController = () => {
const setId = useSetRecoilState(idState)
return {
setId
}
}
const MyComponent = () => {
const { setId } = useController()
return (
<button onClick={() => setId(1)}>Click me!</button>
)
}
What is the correct pattern for testing
describe('my component', () => {
test.todo('when click button, state updated to 1')
})
This pattern comes up a lot for me when I have a component that sets state and I want to verify that the component is actually updating the state (say, via an <input>
and that I haven't forgotten to add the recoil setter into the onChange
event handler.
I can imagine rendering that component under test inside a custom wrapper that sets the whole state as the value of an INPUT or something and then in my test I get that INPUT's value and I have the state, but is there a better way?
I'm essentially trying to spy on the atom's setter I guess.
Edit To clarify, I'm following what even the recoil testing library says: if you're testing a hook that is just for one component, you should test them together. So my "unit tests" treat the unit as "controller-as-hook" and component (the view).