1

I am trying to get my head around testing my Wordpress Block Plugin with Jest. I am specifically struggling how to mock calls to wp.data.

See the following function:

import apiFetch from '@wordpress/api-fetch';

export const initPageLink = async (useShortlink) => {
    const postId = wp.data.select('core/editor').getCurrentPostId();

    if (!postId) {
        throw new Error('No post ID available.');
    }

    if (useShortlink) {
        const post = await apiFetch({ path: `/wp/v2/posts/${postId}?context=edit` });
        const shortlink = `${post.link.replace(/\/[^\/]*$/, '')}/?p=${post.id}`;
        return shortlink;
    } else {
        const post = await apiFetch({ path: `/wp/v2/posts/${postId}` });
        return post.link;
    }
};

What is the best way to mock apiFetch and then specifically calls to wp.data in a jest test file.

Thanks Sina

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.