Skip to content

Commit

Permalink
chore: add tests for bumping pasted blocks to the correct location
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Aug 1, 2023
1 parent b27d580 commit 1282dea
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
9 changes: 5 additions & 4 deletions core/clipboard/block_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
eventUtils.disable();
let block;
try {
block = append(copyData.blockState, workspace, {
recordUndo: true,
}) as BlockSvg;
block = append(copyData.blockState, workspace) as BlockSvg;
moveBlockToNotConflict(block);
} finally {
eventUtils.enable();
Expand All @@ -53,10 +51,13 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
/**
* Moves the given block to a location where it does not: (1) overlap exactly
* with any other blocks, or (2) look like it is connected to any other blocks.
*
* Exported for testing.
*
* @param block The block to move to an unambiguous location.
* @internal
*/
function moveBlockToNotConflict(block: BlockSvg) {
export function moveBlockToNotConflict(block: BlockSvg) {
const workspace = block.workspace;
const snapRadius = config.snapRadius;
const coord = block.getRelativeToSurfaceXY();
Expand Down
4 changes: 3 additions & 1 deletion core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ export function appendInternal(
const block = appendPrivate(state, workspace, {parentConnection, isShadow});

eventUtils.enable();
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block));
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block));
}
eventUtils.setGroup(existingGroup);
eventUtils.setRecordUndo(prevRecordUndo);

Expand Down
86 changes: 84 additions & 2 deletions tests/mocha/clipboard_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2019 Google LLC
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -10,11 +10,15 @@ import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
import {
assertEventFired,
createChangeListenerSpy,
} from './test_helpers/events.js';

suite('Clipboard', function () {
setup(function () {
this.clock = sharedTestSetup.call(this, {fireEventsNow: false}).clock;
this.workspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
this.workspace = Blockly.inject('blocklyDiv');
});

teardown(function () {
Expand All @@ -32,4 +36,82 @@ suite('Clipboard', function () {

Blockly.clipboard.registry.unregister('test-paster');
});

suite('pasting blocks', function () {
test('pasting blocks fires a create event', function () {
const eventSpy = createChangeListenerSpy(this.workspace);
const block = Blockly.serialization.blocks.append(
{
'type': 'controls_if',
'id': 'blockId',
},
this.workspace,
);
const data = block.toCopyData();
this.clock.runAll();
eventSpy.resetHistory();

Blockly.clipboard.paste(data, this.workspace);
this.clock.runAll();

assertEventFired(
eventSpy,
Blockly.Events.BlockCreate,
{'recordUndo': true, 'type': Blockly.Events.BLOCK_CREATE},
this.workspace.id,
);
});

suite('pasted blocks are placed in unambiguous locations', function () {
test('pasted blocks are bumped to not overlap', function () {
const block = Blockly.serialization.blocks.append(
{
'type': 'controls_if',
'x': 38,
'y': 13,
},
this.workspace,
);
const data = block.toCopyData();

const newBlock = Blockly.clipboard.paste(data, this.workspace);
chai.assert.deepEqual(
newBlock.getRelativeToSurfaceXY(),
new Blockly.utils.Coordinate(66, 69),
);
});

test('pasted blocks are bumped to be outside the connection snap radius', function () {
Blockly.serialization.workspaces.load(
{
'blocks': {
'languageVersion': 0,
'blocks': [
{
'type': 'controls_if',
'id': 'sourceBlockId',
'x': 38,
'y': 13,
},
{
'type': 'logic_compare',
'x': 113,
'y': 63,
},
],
},
},
this.workspace,
);
this.clock.runAll(); // Update the connection DB.
const data = this.workspace.getBlockById('sourceBlockId').toCopyData();

const newBlock = Blockly.clipboard.paste(data, this.workspace);
chai.assert.deepEqual(
newBlock.getRelativeToSurfaceXY(),
new Blockly.utils.Coordinate(94, 125),
);
});
});
});
});
11 changes: 4 additions & 7 deletions tests/mocha/test_helpers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,10 @@ export function assertEventFired(
expectedWorkspaceId,
expectedBlockId,
) {
expectedProperties = Object.assign(
{
workspaceId: expectedWorkspaceId,
blockId: expectedBlockId,
},
expectedProperties,
);
const baseProps = {};
if (expectedWorkspaceId) baseProps.workspaceId = expectedWorkspaceId;
if (expectedBlockId) baseProps.blockId = expectedBlockId;
expectedProperties = Object.assign(baseProps, expectedProperties);
const expectedEvent = sinon.match
.instanceOf(instanceType)
.and(sinon.match(expectedProperties));
Expand Down

0 comments on commit 1282dea

Please sign in to comment.