react-input-mask icon indicating copy to clipboard operation
react-input-mask copied to clipboard

Enzyme test simulating 'change' not changing the value

Open odedw opened this issue 7 years ago • 6 comments

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?

odedw avatar Dec 18 '18 15:12 odedw

Same problem here. Using Jest + Enzyme to tests some components with react-input-mask, to ensure the masks are ok.

pedrolauro avatar Mar 05 '19 04:03 pedrolauro

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);

uiyuvi avatar Jul 30 '19 15:07 uiyuvi

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...

reubert22 avatar Nov 11 '19 21:11 reubert22

Facing the same issue while testing with jest and react-testing-library. Even on updating the value, its returning undefined

Yugank16 avatar Apr 15 '20 10:04 Yugank16

@Yugank16 Having same problem. Did you find any solution ?

junaid1840 avatar Jul 20 '20 07:07 junaid1840

@junaid1840 I used react-text-mask instead.

Yugank16 avatar Jul 20 '20 07:07 Yugank16