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