Files
blockly/tests/mocha/mutator_test.js
Beka Westberg f64c934f04 feat: refactor event serialization to use static fromJson method (#6614)
* feat: add new path for deserialization of events

* chore: add tests for round-tripping events

* chore: add static fromJson methods to all events

* chore: add inline docs to new static methods

* chore: add deprecation warnings

* chore: cleanup

* chore: add deprecation and docs to abstract

* chore: format

* chore: cleanup from rebase

* chore: update docs comment
2022-11-14 11:07:36 -08:00

70 lines
2.3 KiB
JavaScript

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.declareModuleId('Blockly.test.mutator');
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
import {createRenderedBlock, defineMutatorBlocks} from './test_helpers/block_definitions.js';
import {assertEventFired, assertEventNotFired} from './test_helpers/events.js';
suite('Mutator', function() {
setup(function() {
sharedTestSetup.call(this);
});
suite('Firing change event', function() {
setup(function() {
this.workspace = Blockly.inject('blocklyDiv', {});
defineMutatorBlocks();
});
teardown(function() {
Blockly.Extensions.unregister('xml_mutator');
Blockly.Extensions.unregister('jso_mutator');
sharedTestTeardown.call(this);
});
test('No change', function() {
const block = createRenderedBlock(this.workspace, 'xml_block');
block.mutator.setVisible(true);
const mutatorWorkspace = block.mutator.getWorkspace();
// Trigger mutator change listener.
createRenderedBlock(mutatorWorkspace, 'checkbox_block');
assertEventNotFired(this.eventsFireStub, Blockly.Events.BlockChange,
{element: 'mutation'});
});
test('XML', function() {
const block = createRenderedBlock(this.workspace, 'xml_block');
block.mutator.setVisible(true);
const mutatorWorkspace = block.mutator.getWorkspace();
mutatorWorkspace.getBlockById('check_block')
.setFieldValue('TRUE', 'CHECK');
chai.assert.isTrue(
this.eventsFireStub.getCalls().some(
({args}) =>
args[0].type === Blockly.Events.BLOCK_CHANGE &&
args[0].element === 'mutation' &&
/<mutation.*><\/mutation>/.test(args[0].newValue)));
});
test('JSO', function() {
const block = createRenderedBlock(this.workspace, 'jso_block');
block.mutator.setVisible(true);
const mutatorWorkspace = block.mutator.getWorkspace();
mutatorWorkspace.getBlockById('check_block')
.setFieldValue('TRUE', 'CHECK');
assertEventFired(this.eventsFireStub, Blockly.Events.BlockChange,
{
element: 'mutation',
newValue: '{"hasInput":true}',
}, this.workspace.id, block.id);
});
});
});