-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimal test for useBlockPreview hook
- Loading branch information
1 parent
b91c00f
commit 7dcfacf
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/block-editor/src/components/block-preview/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
registerBlockType, | ||
unregisterBlockType, | ||
createBlock, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockPreview } from '../'; | ||
|
||
describe( 'useBlockPreview', () => { | ||
beforeAll( () => { | ||
registerBlockType( 'core/test-block', { | ||
save: () => <div>Test block save view</div>, | ||
edit: () => <div>Test block edit view</div>, | ||
category: 'text', | ||
title: 'test block', | ||
} ); | ||
} ); | ||
|
||
afterAll( () => { | ||
unregisterBlockType( 'core/test-block' ); | ||
} ); | ||
|
||
function BlockPreviewComponent( { blocks, className } ) { | ||
const blockPreviewProps = useBlockPreview( { | ||
blocks, | ||
props: { className }, | ||
} ); | ||
return <div { ...blockPreviewProps } />; | ||
} | ||
|
||
it( 'will render a block preview with minimal nesting', () => { | ||
const blocks = []; | ||
blocks.push( createBlock( 'core/test-block' ) ); | ||
|
||
const { container } = render( | ||
<BlockPreviewComponent | ||
className="test-container-classname" | ||
blocks={ blocks } | ||
/> | ||
); | ||
|
||
// Test block and block contents are rendered. | ||
const previewedBlock = screen.queryByLabelText( 'Block: test block' ); | ||
const previewedBlockContents = screen.getByText( | ||
'Test block edit view' | ||
); | ||
expect( previewedBlockContents ).toBeInTheDocument(); | ||
|
||
// Ensure the block preview class names are merged with the component's class name. | ||
expect( container.firstChild.className ).toBe( | ||
'test-container-classname block-editor-block-preview__live-content components-disabled' | ||
); | ||
|
||
// Ensure there is no nesting between the parent component and rendered blocks. | ||
expect( container.firstChild.firstChild ).toBe( previewedBlock ); | ||
} ); | ||
} ); |