Can't set input element value in Enzyme
Can't set input element value in Enzyme
In my test, I have this:
let fromInput = wrapper.find('#starting-address').getElement();
fromInput.value = 'Foo';
However, I'm getting an error:
TypeError: Cannot add property value, object is not extensible.
I'm trying to test a button I have which, on clicking, should clear the value of the input element (id: 'starting-address'). I was planning on asserting that fromInput.value === 'foo'
before simulating the click, and fromImput.value === ''
after clicking.
fromInput.value === 'foo'
fromImput.value === ''
mount
shallow
I'm using
mount
– Fabian
Jul 4 at 3:17
mount
1 Answer
1
You need to use instance() instead:
const formInput = wrapper.find('#starting-address').instance();
formInput.value = 'Foo';
Check out this answer, for more details.
The docs says it can only be called on a wrapper instance that is also the root instance though?
– Fabian
Jul 5 at 5:24
I think this is b/c of
shallow()
( as in they example). If you do mount()
- I don't see the reason why not to use it.– Alex
Jul 5 at 7:59
shallow()
mount()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you
mount
ed orshallow
rendered the component?– Alex
Jul 3 at 18:05