A
A
anriko2021-11-14 12:47:24
React
anriko, 2021-11-14 12:47:24

Why jest and enzyme don't see ref in component?

this is how i add the ref

setinputTaskItemName = (ref) => {
        this.inputTaskItemName = ref;
    };
   <input type="text" className="task-item-name" name="title" placeholder="Enter task item name" required="" ref={this.setinputTaskItemName} />


this is how I test

it('should render Input', () => {
        expect(component.find(".task-item-name").at(0)).toHaveLength(1);
        expect(component.instance().inputTaskItemName).toBeTruthy();
    })

here is the error
● FormTaskItem New › should render Input

TypeError: component.ref is not a function

● FormTaskItem New › should render Input

expect(received).toBeTruthy()

Received: undefined

49 | it('should render Input', () => {
50 | expect(component.find(".task-item-name").at(0)).toHaveLength(1);
> 51 | expect(component.instance ().inputTaskItemName).toBeTruthy();
| ^
52 | })
53 |

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anriko, 2021-11-14
@anriko

changed ref like this

constructor(props) {
        super(props);
        this.inputTaskItemName = React.createRef();
        this.inputTaskItemText = React.createRef();

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question