vendredi 5 mars 2021

How to test function with input's ref parameter?

I have such function:

export const clearInput = (ref: RefObject<HTMLInputElement>) => { 
    if (null !== ref.current) {
        ref.current.value = ''
    }
};

I completely don't know how to test it in react-testing-library / jest. This is my current code

import React, { RefObject, useRef } from 'react'
import { clearInput, isInputTextMatch } from '../input';
import { fireEvent, render, waitForElement,  } from '@testing-library/react'
import { debug } from 'console';


const Component = () => {
    const inputRef = useRef<HTMLInputElement>(null);
    return (
        <input ref={inputRef} data-testid="Input" value="example" />
    )
}


describe('clearInput helper', () => {
    test('If used function, input value should be clear', async () => {
        const { findByTestId } = render(<Component />);

        const inputNode = await waitForElement(() => 
            findByTestId('Input')
        )

        fireEvent.change(inputNode, {target: { value: ""}})
        
    });
})

Thanks in advanced

Aucun commentaire:

Enregistrer un commentaire