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

Fix move block to position bug; Add test cases; #14924

Merged
merged 2 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ export function* moveBlockToPosition( clientId, fromRootClientId = '', toRootCli
return;
}

// If templateLock is insert we can not remove the block from the parent.
// Given that here we know that we are moving the block to a different parent, the move should not be possible if the condition is true.
if ( templateLock === 'insert' ) {
return;
}

const blockName = yield select(
'core/block-editor',
'getBlockName',
Expand Down
179 changes: 179 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
insertBlock,
insertBlocks,
mergeBlocks,
moveBlockToPosition,
multiSelect,
removeBlock,
removeBlocks,
Expand Down Expand Up @@ -468,6 +469,184 @@ describe( 'actions', () => {
} );
} );

describe( 'moveBlockToPosition', () => {
it( 'should yield MOVE_BLOCK_TO_POSITION action if locking is insert and move is not changing the root block', () => {
const moveBlockToPositionGenerator = moveBlockToPosition(
'chicken',
'ribs',
'ribs',
5
);

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'ribs' ],
selectorName: 'getTemplateLock',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( 'insert' ).value
).toEqual( {
type: 'MOVE_BLOCK_TO_POSITION',
fromRootClientId: 'ribs',
toRootClientId: 'ribs',
clientId: 'chicken',
index: 5,
} );

expect(
moveBlockToPositionGenerator.next().done
).toBe( true );
} );

it( 'should not yield MOVE_BLOCK_TO_POSITION action if locking is all', () => {
const moveBlockToPositionGenerator = moveBlockToPosition(
'chicken',
'ribs',
'ribs',
5
);

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'ribs' ],
selectorName: 'getTemplateLock',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( 'all' )
).toEqual( {
done: true,
value: undefined,
} );
} );

it( 'should not yield MOVE_BLOCK_TO_POSITION action if locking is insert and move is changing the root block', () => {
const moveBlockToPositionGenerator = moveBlockToPosition(
'chicken',
'ribs',
'chicken-ribs',
5
);

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'ribs' ],
selectorName: 'getTemplateLock',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( 'insert' )
).toEqual( {
done: true,
value: undefined,
} );
} );

it( 'should yield MOVE_BLOCK_TO_POSITION action if there is not locking in the original root block and block can be inserted in the destination', () => {
const moveBlockToPositionGenerator = moveBlockToPosition( 'chicken',
'ribs',
'chicken-ribs',
5
);

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'ribs' ],
selectorName: 'getTemplateLock',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'chicken' ],
selectorName: 'getBlockName',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( 'myblock/chicken-block' ).value
).toEqual( {
args: [ 'myblock/chicken-block', 'chicken-ribs' ],
selectorName: 'canInsertBlockType',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( true ).value
).toEqual( {
type: 'MOVE_BLOCK_TO_POSITION',
fromRootClientId: 'ribs',
toRootClientId: 'chicken-ribs',
clientId: 'chicken',
index: 5,
} );

expect(
moveBlockToPositionGenerator.next()
).toEqual( {
done: true,
value: undefined,
} );
} );

it( 'should not yield MOVE_BLOCK_TO_POSITION action if there is not locking in the original root block and block can be inserted in the destination', () => {
const moveBlockToPositionGenerator = moveBlockToPosition( 'chicken',
'ribs',
'chicken-ribs',
5
);

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'ribs' ],
selectorName: 'getTemplateLock',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next().value
).toEqual( {
args: [ 'chicken' ],
selectorName: 'getBlockName',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( 'myblock/chicken-block' ).value
).toEqual( {
args: [ 'myblock/chicken-block', 'chicken-ribs' ],
selectorName: 'canInsertBlockType',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
moveBlockToPositionGenerator.next( false )
).toEqual( {
done: true,
value: undefined,
} );
} );
} );

describe( 'removeBlock', () => {
it( 'should return REMOVE_BLOCKS action', () => {
const clientId = 'myclientid';
Expand Down