Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add BlockList coverage #50252

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class BlockListItem extends Component {
const {
blockAlignment,
clientId,
index,
isReadOnly,
shouldShowInsertionPointBefore,
shouldShowInsertionPointAfter,
Expand Down Expand Up @@ -134,7 +135,11 @@ export class BlockListItem extends Component {
pointerEvents={ isReadOnly ? 'box-only' : 'auto' }
>
{ shouldShowInsertionPointBefore && (
<BlockInsertionPoint />
<BlockInsertionPoint
testID={ `block-insertion-point-before-row-${
index + 1
}` }
/>
) }
<BlockListBlock
key={ clientId }
Expand All @@ -147,7 +152,11 @@ export class BlockListItem extends Component {
/>
{ ! shouldShowInnerBlockAppender() &&
shouldShowInsertionPointAfter && (
<BlockInsertionPoint />
<BlockInsertionPoint
testID={ `block-insertion-point-after-row-${
index + 1
}` }
/>
) }
</View>
</ReadableContentView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export class BlockList extends Component {
const {
clearSelectedBlock,
blockClientIds,
rootClientId,
title,
header,
isReadOnly,
Expand Down Expand Up @@ -275,6 +276,9 @@ export class BlockList extends Component {
) }
data={ blockClientIds }
keyExtractor={ identity }
listKey={
rootClientId ? `list-${ rootClientId }` : 'list-root'
}
renderItem={ this.renderItem }
CellRendererComponent={ this.getCellRendererComponent }
shouldPreventAutomaticScroll={
Expand Down Expand Up @@ -305,7 +309,7 @@ export class BlockList extends Component {
);
}

renderItem( { item: clientId } ) {
renderItem( { item: clientId, index } ) {
const {
contentResizeMode,
contentStyle,
Expand All @@ -331,6 +335,7 @@ export class BlockList extends Component {
};
return (
<BlockListItem
index={ index }
isStackedHorizontally={ isStackedHorizontally }
rootClientId={ rootClientId }
clientId={ clientId }
Expand Down Expand Up @@ -421,6 +426,7 @@ export default compose( [
Platform.OS === 'ios' && isBlockInsertionPointVisible(),
isReadOnly,
isRootList: rootClientId === undefined,
rootClientId,
isFloatingToolbarVisible,
isStackedHorizontally,
maxWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { withPreferredColorScheme } from '@wordpress/compose';
*/
import styles from './style.scss';

const BlockInsertionPoint = ( { getStylesFromColorScheme } ) => {
const BlockInsertionPoint = ( { getStylesFromColorScheme, testID } ) => {
const lineStyle = getStylesFromColorScheme(
styles.lineStyleAddHere,
styles.lineStyleAddHereDark
Expand All @@ -25,7 +25,7 @@ const BlockInsertionPoint = ( { getStylesFromColorScheme } ) => {
);

return (
<View style={ styles.containerStyleAddHere }>
<View style={ styles.containerStyleAddHere } testID={ testID }>
<View style={ lineStyle }></View>
<Text style={ labelStyle }>{ __( 'ADD BLOCK HERE' ) }</Text>
<View style={ lineStyle }></View>
Expand Down
169 changes: 169 additions & 0 deletions packages/block-editor/src/components/block-list/test/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* External dependencies
*/
import {
addBlock,
getBlock,
fireEvent,
initializeEditor,
screen,
setupCoreBlocks,
triggerBlockListLayout,
within,
} from 'test/helpers';

setupCoreBlocks( [ 'core/paragraph', 'core/group' ] );

describe( 'BlockList', () => {
describe( 'when empty', () => {
beforeEach( async () => {
// Arrange
await initializeEditor();
} );

it( 'renders a post title', async () => {
// Assert
expect( screen.getByPlaceholderText( 'Add title' ) ).toBeTruthy();
} );

it( 'renders a block appender as a content placeholder', async () => {
// Assert
expect(
screen.getByPlaceholderText( /Start writing/ )
).toBeTruthy();
} );

it( 'renders an end-of-list paragraph appender', async () => {
// Assert
expect(
screen.getByLabelText( 'Add paragraph block' )
).toBeTruthy();
} );
} );

describe( 'for inner blocks', () => {
it( 'renders an inner block appender', async () => {
await initializeEditor();
await addBlock( screen, 'Group' );
const groupBlock = await getBlock( screen, 'Group' );
triggerBlockListLayout( groupBlock );

// Assert
expect(
within( groupBlock ).getByTestId( 'appender-button' )
).toBeTruthy();
} );

describe( 'when a non-last block is selected', () => {
beforeEach( async () => {
// Arrange
await initializeEditor();
await addBlock( screen, 'Group' );
const groupBlock = await getBlock( screen, 'Group' );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );
fireEvent.press( screen.getByLabelText( 'Navigate Up' ) );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );
} );

it( 'renders an insertion point before the block', async () => {
// Act
const paragraphBlock = await getBlock( screen, 'Paragraph', {
rowIndex: 1,
} );
fireEvent.press( paragraphBlock );
fireEvent( screen.getByLabelText( 'Add block' ), 'longPress' );
fireEvent.press( screen.getByText( 'Add Block Before ' ) );
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved

// Assert
expect( screen.getByText( 'ADD BLOCK HERE' ) ).toBeTruthy();
expect(
screen.getByTestId( 'block-insertion-point-before-row-1' )
).toBeTruthy();
} );

it( 'renders an insertion point after the block', async () => {
// Act
const paragraphBlock = await getBlock( screen, 'Paragraph', {
rowIndex: 1,
} );
fireEvent.press( paragraphBlock );
fireEvent( screen.getByLabelText( 'Add block' ), 'longPress' );
fireEvent.press( screen.getByText( 'Add Block After ' ) );

// Assert
expect( screen.getByText( 'ADD BLOCK HERE' ) ).toBeTruthy();
expect(
screen.getByTestId( 'block-insertion-point-before-row-2' )
).toBeTruthy();
} );
} );

describe( 'when the last block is selected', () => {
it( 'renders an insertion point before the block', async () => {
// Arrange
await initializeEditor();
await addBlock( screen, 'Group' );
const groupBlock = await getBlock( screen, 'Group' );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );
fireEvent.press( screen.getByLabelText( 'Navigate Up' ) );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );

// Act
const paragraphBlock = await getBlock( screen, 'Paragraph', {
rowIndex: 2,
} );
fireEvent.press( paragraphBlock );
fireEvent( screen.getByLabelText( 'Add block' ), 'longPress' );
fireEvent.press( screen.getByText( 'Add Block Before ' ) );

// Assert
expect( screen.getByText( 'ADD BLOCK HERE' ) ).toBeTruthy();
expect(
screen.getByTestId( 'block-insertion-point-before-row-2' )
).toBeTruthy();
} );

it( 'renders an insertion point after the block', async () => {
// Arrange
await initializeEditor();
await addBlock( screen, 'Group' );
const groupBlock = await getBlock( screen, 'Group' );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );
fireEvent.press( screen.getByLabelText( 'Navigate Up' ) );
fireEvent.press(
within( groupBlock ).getByTestId( 'appender-button' )
);
await addBlock( screen, 'Paragraph', { isPickerOpened: true } );

// Act
const paragraphBlock = await getBlock( screen, 'Paragraph', {
rowIndex: 2,
} );
fireEvent.press( paragraphBlock );
fireEvent( screen.getByLabelText( 'Add block' ), 'longPress' );
fireEvent.press( screen.getByText( 'Add Block After ' ) );

// Assert
expect( screen.getByText( 'ADD BLOCK HERE' ) ).toBeTruthy();
expect(
screen.getByTestId( 'block-insertion-point-after-row-2' )
).toBeTruthy();
} );
} );
} );
} );
1 change: 1 addition & 0 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const coreBlocks = [
column,
cover,
embed,
group,
geriux marked this conversation as resolved.
Show resolved Hide resolved
file,
html,
mediaText,
Expand Down
3 changes: 3 additions & 0 deletions test/native/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,7 @@ module.exports = {
mediaAreaPadding: {
width: 12,
},
defaultAppender: {
marginLeft: 16,
},
};