I am struggling to understand how to update a react component's props while using cypress-react-unit-test.
Here I have a simple controlled input component.
interface MyInputProps {
inputVal: string
onInputChanged(val: string): void
}
export const MyInput = ({ inputVal, onInputChanged }: MyInputProps) => {
return <input
type='text'
value={inputVal}
onChange={e => onInputChanged(e.target.value)}
/>
}
I then want to test that component by giving it an initial value of '', then doing something like cy.get('input').type('a'). I would then expect that the onInputChanged callback would be called.
But this is where I get stuck. What do I do with the updated value ('a')? I can't use hooks or state inside of the tests, so how do I get my component to re-render using the updated value? It seems wrong to do mount(<MyInput inputVal='' />), then mount(<MyInput inputVal='a' />) because then I’m mounting a different component and I’m not testing how the component reacts to props updates.
// MyInput.spec.tsx
describe('My Input', () => {
it('updates when props change', () => {
mount(<MyInput
inputVal=''
onInputChanged={(newVal) => {
/* What do I do here? How do I update the props */
return null
}}
/>);
/* Update props */
/* Re-render somehow */
})
})
inputVal, mount once, have cypress.type('a-new-value'), check the local variable is updated, finally mount a second time and check the input's value. - Ackroydd