woocommerce/plugins/woocommerce-admin/tests/js/util/index.js

24 lines
713 B
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { screen } from '@testing-library/react';
/**
* For use with react-testing-library, like getText but allows the text to reside in multiple elements
*
* @param {Object} query - Original query.
*
* @return {Array} - Array of two arrays, first including truthy values, and second including falsy.
*/
const withMarkup = ( query ) => ( text ) =>
query( ( content, node ) => {
const hasText = ( domNode ) => domNode.textContent === text;
const childrenDontHaveText = Array.from( node.children ).every(
( child ) => ! hasText( child )
);
return hasText( node ) && childrenDontHaveText;
} );
export const getByTextWithMarkup = withMarkup( screen.getByText );