1 year ago

#74463

test-img

Pratheek R

How to test a function in parent component that is passed as props to child component

I have a function onConfirm in parent component which I want to test using jest and react-testing-library, but this is passed as prop to the child component. How can I test this function in parent component.

    const onConfirm: any = (): void => {
    let requestdata: any,
        urlToHit = '';
    if (props.operation === 'editActiveInactive') {
        ....
        if (...) {
            urlToHit = 'url1';
        } else {
            urlToHit = 'url2';
        }
    }
    if (...) {
        ....
    }
    fetch(urlToHit, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(requestdata),
    })
        .then(function (response): any {
            if (!response.ok) {
                return response.json().then((json) => {
                    throw json;
                });
            }
        })
        .then(function (): void {
            ....;
        })
        .catch(function (error): any {
            if (...) {
                if (...) {
                    .....
                }
                setTimeout(() => {
                    ....
                }, 800);
            }
            console.error(error);
        });
};

reactjs

react-testing-library

jest-fetch-mock

0 Answers

Your Answer

Accepted video resources