All files / testutils parseSlotContents.ts

100% Statements 9/9
75% Branches 3/4
100% Functions 4/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 472x 2x                 2x       17x     17x         17x                           2x       17x       17x    
import { defineComponent, VNode } from 'vue';
import { mount } from '@vue/test-utils';
import { GlobalMountOptions } from '@vue/test-utils/dist/types';
 
/**
 * Helper component for parseSlotContents().
 *
 * This component renders nothing, but exposes the slot contents as a VNode array through the
 * getContents() method.
 */
const TestComponent = defineComponent( {
	name: 'TestComponent',
	setup( props, { slots } ) {
		function getContents() {
			return slots.default?.();
		}
 
		return {
			getContents
		};
	},
	render() {
		return '';
	}
} );
 
/**
 * Parse a Vue template string to a VNode array.
 *
 * This is used to allow tests to be written more clearly and concisely, by expressing the VNode
 * input in template form, rather than as VNode objects.
 *
 * @param slotContents Vue template string
 * @param components Components used in the template string
 * @return VNodes representing the given string
 */
export default function parseSlotContents(
	slotContents: string,
	components?: GlobalMountOptions['components']
): VNode[] | undefined {
	const wrapper = mount( TestComponent, {
		slots: { default: slotContents },
		global: { components }
	} );
	return wrapper.vm.getContents();
}