From 95a8eaa173687da35ed6d3ef2998eceb8a9a10c9 Mon Sep 17 00:00:00 2001 From: David Calhoun <438664+dcalhoun@users.noreply.github.com> Date: Wed, 23 Jun 2021 16:40:27 -0500 Subject: [PATCH] Test block insertion after blurring post title field Relates to #32831. Adds tests asserting that block inserter inserts new blocks in the correct position after blurring the post title field. --- .../visual-editor/test/index.native.js | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 packages/edit-post/src/components/visual-editor/test/index.native.js diff --git a/packages/edit-post/src/components/visual-editor/test/index.native.js b/packages/edit-post/src/components/visual-editor/test/index.native.js new file mode 100644 index 00000000000000..01839689852faa --- /dev/null +++ b/packages/edit-post/src/components/visual-editor/test/index.native.js @@ -0,0 +1,93 @@ +/** + * External dependencies + */ +import { initializeEditor, fireEvent } from 'test/helpers'; + +/** + * WordPress dependencies + */ +import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks'; +import { registerCoreBlocks } from '@wordpress/block-library'; + +beforeAll( () => { + // Register all core blocks + registerCoreBlocks(); +} ); + +afterAll( () => { + // Clean up registered blocks + getBlockTypes().forEach( ( block ) => { + unregisterBlockType( block.name ); + } ); +} ); + +const initialHtml = ` + +
First example paragraph.
+ + + +Second example paragraph.
+ +`; + +describe( 'when title is focused', () => { + it( 'new blocks are inserted after the title', async () => { + const screen = await initializeEditor( { + initialHtml, + } ); + + // Focus first block + fireEvent.press( screen.getByA11yLabel( /Paragraph Block. Row 1/ ) ); + + // Focus title + fireEvent( + screen.getAllByA11yLabel( 'Post title. test' )[ 0 ], + 'select' + ); + + // Add new Heading block + fireEvent.press( screen.getByA11yLabel( 'Add block' ) ); + fireEvent.press( screen.getByText( 'Heading' ) ); + + expect( screen.getByA11yLabel( /Heading Block. Row 1/ ) ).toBeDefined(); + expect( + screen.getByA11yLabel( /Paragraph Block. Row 2/ ) + ).toBeDefined(); + expect( + screen.getByA11yLabel( /Paragraph Block. Row 3/ ) + ).toBeDefined(); + } ); +} ); + +describe( 'when title is no longer focused', () => { + it( 'new blocks are inserted after the currently focused block', async () => { + const screen = await initializeEditor( { + initialHtml, + } ); + + // Focus first block + fireEvent.press( screen.getByA11yLabel( /Paragraph Block. Row 1/ ) ); + + // Focus title + fireEvent( + screen.getAllByA11yLabel( 'Post title. test' )[ 0 ], + 'select' + ); + + // Focus last block + fireEvent.press( screen.getByA11yLabel( /Paragraph Block. Row 2/ ) ); + + // Add new Heading block + fireEvent.press( screen.getByA11yLabel( 'Add block' ) ); + fireEvent.press( screen.getByText( 'Heading' ) ); + + expect( + screen.getByA11yLabel( /Paragraph Block. Row 1/ ) + ).toBeDefined(); + expect( + screen.getByA11yLabel( /Paragraph Block. Row 2/ ) + ).toBeDefined(); + expect( screen.getByA11yLabel( /Heading Block. Row 3/ ) ).toBeDefined(); + } ); +} );