-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathinitialize-editor.js
58 lines (51 loc) · 1.79 KB
/
initialize-editor.js
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
47
48
49
50
51
52
53
54
55
56
57
58
/**
* External dependencies
*/
import { render, fireEvent } from '@testing-library/react-native';
import { v4 as uuid } from 'uuid';
/**
* WordPress dependencies
*/
import { createElement, cloneElement } from '@wordpress/element';
// eslint-disable-next-line no-restricted-imports
import { initializeEditor as internalInitializeEditor } from '@wordpress/edit-post';
/**
* Internal dependencies
*/
import { waitForStoreResolvers } from './wait-for-store-resolvers';
/**
* Initialize an editor for test assertions.
*
* @param {Object} props Properties passed to the editor component.
* @param {string} props.initialHtml String of block editor HTML to parse and render.
* @param {Object} [options] Configuration options for the editor.
* @param {import('react').ReactNode} [options.component] A specific editor component to render.
* @return {import('@testing-library/react-native').RenderAPI} A Testing Library screen.
*/
export async function initializeEditor( props, { component } = {} ) {
const uniqueId = uuid();
const postId = `post-id-${ uniqueId }`;
const postType = 'post';
return waitForStoreResolvers( () => {
const { screenWidth = 320, ...rest } = props || {};
const editorElement = component
? createElement( component, { postType, postId } )
: internalInitializeEditor( uniqueId, postType, postId );
const screen = render(
cloneElement( editorElement, {
initialTitle: 'test',
...rest,
} )
);
// A layout event must be explicitly dispatched in BlockList component,
// otherwise the inner blocks are not rendered.
fireEvent( screen.getByTestId( 'block-list-wrapper' ), 'layout', {
nativeEvent: {
layout: {
width: screenWidth,
},
},
} );
return screen;
} );
}