简体   繁体   中英

JEST unit testing for $.get request

I have a code in React

$.get('/bin/data', (response) => {
            this.data = response;
        });

This is my test case:

$document = {
            on: jest.fn().mockName('$document.on'),
        };

        expect(window.$).toBeUndefined(); 
        $ = jest.fn().mockName('$').mockReturnValue($document);
        $.get = () => {};
        jest.spyOn($, 'get').mockName('$.get').mockImplementation(response => jest.fn()); // not sure what to do here such that callback function is also covered

        window.$ = $;

Everything is working fine, but the callback function is not getting covered.

Got if solved.

jest.spyOn($, 'get').mockName('$.get').mockImplementation((url, myMockFn) => {
            myMockFn({ name: 'Tom' });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM