Enzyme test simulating 'change' not changing the value
I created a simple app here: https://github.com/odedw/react-input-mask-enzyme
What I'm seeing is that in my test I call:
component
.find('input')
.simulate('change', {target: {value: '1234567890'}})
And afterwards I assert on the value:
expect('(123) 456 7890').toEqual(component.find('input').prop('value'));
The assert fails and the value stays undefined. Any idea?
Same problem here. Using Jest + Enzyme to tests some components with react-input-mask, to ensure the masks are ok.
Hope this should work , it is working for me component .find('input') .simulate('change', {target: {value: '1234567890'}}) expect('(123) 456 7890').toEqual(component.find('input').instance().value);
For those that are still trying, according with @uiyuvi , you can do:
import { mount } from 'enzyme'
import MockAdapter from 'axios-mock-adapter'
...
test('Testing async function', async () => {
// Just in case of you're going to do async calls
const mock = new MockAdapter(*axios.instance*)
mock.onGet(...).reply(*response*)
const spy = jest.spyOn(*axios.instance*, 'get')
const component = mount(<Component />)
const searchBtn = component.find('[aria-label="search-client"]').at(1)
const inputMask = component.find('#cpf-input').at(1)
inputMask.simulate('change', { target: { value: '999.999.999-99' } })
expect('999.999.999-99').toEqual(inputCpf.instance().value)
searchBtn.simulate('click')
// Just in case of you're going to do async calls
await wait(() => expect(spy).toHaveBeenCalledTimes(1))
})
Obs: In case of you have more than one node and your input mask component is inside the 2nd or whatever, you'll need at() attrib.
I was getting the following message:
Method “simulate” is meant to be run on 1 node. 0 found instead.
And after at() attrib I was able to solve.
Lemme know if I'm wrong...
Facing the same issue while testing with jest and react-testing-library.
Even on updating the value, its returning undefined
@Yugank16 Having same problem. Did you find any solution ?
@junaid1840 I used react-text-mask instead.