From e1ad6274302cbe2ea60d3148302bfc72f64c432b Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 1 Mar 2022 20:09:29 +0000 Subject: [PATCH] fix: split event helpers into a new file --- tests/deps.mocha.js | 21 +-- tests/mocha/block_create_event_test.js | 3 +- tests/mocha/comment_test.js | 3 +- tests/mocha/event_test.js | 5 +- tests/mocha/gesture_test.js | 3 +- tests/mocha/jso_deserialization_test.js | 3 +- tests/mocha/test_helpers/events.js | 205 +++++++++++++++++++++++ tests/mocha/test_helpers/test_helpers.js | 197 ---------------------- tests/mocha/theme_test.js | 3 +- tests/mocha/trashcan_test.js | 3 +- tests/mocha/workspace_svg_test.js | 3 +- tests/mocha/zoom_controls_test.js | 3 +- 12 files changed, 235 insertions(+), 217 deletions(-) create mode 100644 tests/mocha/test_helpers/events.js diff --git a/tests/deps.mocha.js b/tests/deps.mocha.js index 710740359..4ed69ef49 100644 --- a/tests/deps.mocha.js +++ b/tests/deps.mocha.js @@ -1,17 +1,17 @@ goog.addDependency('../../tests/mocha/.mocharc.js', [], []); goog.addDependency('../../tests/mocha/astnode_test.js', ['Blockly.test.astNode'], ['Blockly.ASTNode', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/block_change_event_test.js', ['Blockly.test.blockChangeEvent'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/block_create_event_test.js', ['Blockly.test.blockCreateEvent'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/block_create_event_test.js', ['Blockly.test.blockCreateEvent'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.events'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/block_json_test.js', ['Blockly.test.blockJson'], ['Blockly.Input'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/block_test.js', ['Blockly.test.blocks'], ['Blockly.ConnectionType', 'Blockly.Events.utils', 'Blockly.blocks', 'Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/comment_test.js', ['Blockly.test.comments'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/comment_test.js', ['Blockly.test.comments'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.events'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/connection_checker_test.js', ['Blockly.test.connectionChecker'], ['Blockly.ConnectionType', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/connection_db_test.js', ['Blockly.test.connectionDb'], ['Blockly.ConnectionType', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/connection_test.js', ['Blockly.test.connection'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/contextmenu_items_test.js', ['Blockly.test.contextMenuItem'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/cursor_test.js', ['Blockly.test.cursor'], ['Blockly.ASTNode', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/dropdowndiv_test.js', ['Blockly.test.dropdown'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/event_test.js', ['Blockly.test.event'], ['Blockly.ASTNode', 'Blockly.Events.utils', 'Blockly.WorkspaceComment', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/event_test.js', ['Blockly.test.event'], ['Blockly.ASTNode', 'Blockly.Events.utils', 'Blockly.WorkspaceComment', 'Blockly.test.helpers', 'Blockly.test.helpers.events'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/extensions_test.js', ['Blockly.test.extensions'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/field_angle_test.js', ['Blockly.test.fieldAngle'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.fields'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/field_checkbox_test.js', ['Blockly.test.fieldCheckbox'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.fields'], {'lang': 'es6', 'module': 'goog'}); @@ -28,10 +28,10 @@ goog.addDependency('../../tests/mocha/field_textinput_test.js', ['Blockly.test.f goog.addDependency('../../tests/mocha/field_variable_test.js', ['Blockly.test.fieldVariable'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.fields'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/flyout_test.js', ['Blockly.test.flyout'], ['Blockly.test.helpers', 'Blockly.test.helpers.toolbox_definitions'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/generator_test.js', ['Blockly.test.generator'], ['Blockly.Dart', 'Blockly.JavaScript', 'Blockly.Lua', 'Blockly.PHP', 'Blockly.Python', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/gesture_test.js', ['Blockly.test.gesture'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/gesture_test.js', ['Blockly.test.gesture'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.events', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/input_test.js', ['Blockly.test.input'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/insertion_marker_test.js', ['Blockly.test.insertionMarker'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/jso_deserialization_test.js', ['Blockly.test.jsoDeserialization'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/jso_deserialization_test.js', ['Blockly.test.jsoDeserialization'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.events'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/jso_serialization_test.js', ['Blockly.test.jsoSerialization'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es8', 'module': 'goog'}); goog.addDependency('../../tests/mocha/json_test.js', ['Blockly.test.json'], ['Blockly.test.helpers', 'Blockly.test.helpers.warnings'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/keydown_test.js', ['Blockly.test.keydown'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); @@ -47,25 +47,26 @@ goog.addDependency('../../tests/mocha/shortcut_registry_test.js', ['Blockly.test goog.addDependency('../../tests/mocha/test_helpers/block_definitions.js', ['Blockly.test.helpers.blockDefinitions'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/code_generation.js', ['Blockly.test.helpers.codeGeneration'], ['Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/common.js', ['Blockly.test.helpers.common'], [], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/test_helpers/events.js', ['Blockly.test.helpers.events'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/fields.js', ['Blockly.test.helpers.fields'], ['Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/procedures.js', ['Blockly.test.helpers.procedures'], ['Blockly.ConnectionType'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/serialization.js', ['Blockly.test.helpers.serialization'], ['Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/test_helpers/test_helpers.js', ['Blockly.test.helpers'], ['Blockly.Events.utils', 'Blockly.blocks', 'Blockly.test.helpers.common', 'Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/test_helpers/test_helpers.js', ['Blockly.test.helpers'], ['Blockly.Events.utils', 'Blockly.blocks'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/toolbox_definitions.js', ['Blockly.test.helpers.toolbox_definitions'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/user_input.js', ['Blockly.test.helpers.userInput'], ['Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/warnings.js', ['Blockly.test.helpers.warnings'], ['Blockly.Events.utils', 'Blockly.blocks', 'Blockly.test.helpers.common', 'Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/workspace.js', ['Blockly.test.helpers.workspace'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.warnings'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/theme_test.js', ['Blockly.test.theme'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/theme_test.js', ['Blockly.test.theme'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.events'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/toolbox_test.js', ['Blockly.test.toolbox'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.toolbox_definitions'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/tooltip_test.js', ['Blockly.test.tooltip'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/trashcan_test.js', ['Blockly.test.trashcan'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/trashcan_test.js', ['Blockly.test.trashcan'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.events', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/utils_test.js', ['Blockly.test.utils'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/variable_map_test.js', ['Blockly.test.variableMap'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/variable_model_test.js', ['Blockly.test.variableModel'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/variables_test.js', ['Blockly.test.variables'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/widget_div_test.js', ['Blockly.test.widgetDiv'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/workspace_comment_test.js', ['Blockly.test.workspaceComment'], ['Blockly.WorkspaceComment', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/workspace_svg_test.js', ['Blockly.test.workspaceSvg'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.workspace'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/workspace_svg_test.js', ['Blockly.test.workspaceSvg'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.events', 'Blockly.test.helpers.workspace'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/workspace_test.js', ['Blockly.test.workspace'], ['Blockly.test.helpers', 'Blockly.test.helpers.workspace'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/xml_test.js', ['Blockly.test.xml'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/zoom_controls_test.js', ['Blockly.test.zoomControls'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/zoom_controls_test.js', ['Blockly.test.zoomControls'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.events', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); diff --git a/tests/mocha/block_create_event_test.js b/tests/mocha/block_create_event_test.js index 69debed96..d7e6fe27f 100644 --- a/tests/mocha/block_create_event_test.js +++ b/tests/mocha/block_create_event_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.blockCreateEvent'); +const {assertEventFired} = goog.require('Blockly.test.helpers.events'); const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); suite('Block Create Event', function() { diff --git a/tests/mocha/comment_test.js b/tests/mocha/comment_test.js index 94a496637..a0979dad5 100644 --- a/tests/mocha/comment_test.js +++ b/tests/mocha/comment_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.comments'); +const {assertEventFired} = goog.require('Blockly.test.helpers.events'); const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); suite('Comments', function() { diff --git a/tests/mocha/event_test.js b/tests/mocha/event_test.js index 70ae131b7..3eae28604 100644 --- a/tests/mocha/event_test.js +++ b/tests/mocha/event_test.js @@ -6,9 +6,10 @@ goog.module('Blockly.test.event'); -const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventEquals, assertNthCallEventArgEquals, assertVariableValues, createFireChangeListenerSpy, createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); const {ASTNode} = goog.require('Blockly.ASTNode'); +const {assertVariableValues, createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventEquals, assertNthCallEventArgEquals, createFireChangeListenerSpy} = goog.require('Blockly.test.helpers.events'); +const eventUtils = goog.require('Blockly.Events.utils'); goog.require('Blockly.WorkspaceComment'); diff --git a/tests/mocha/gesture_test.js b/tests/mocha/gesture_test.js index 12a36cfe6..749cbef74 100644 --- a/tests/mocha/gesture_test.js +++ b/tests/mocha/gesture_test.js @@ -6,7 +6,8 @@ goog.module('Blockly.test.gesture'); -const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired} = goog.require('Blockly.test.helpers.events'); const {defineBasicBlockWithField} = goog.require('Blockly.test.helpers.blockDefinitions'); const {dispatchPointerEvent} = goog.require('Blockly.test.helpers.userInput'); diff --git a/tests/mocha/jso_deserialization_test.js b/tests/mocha/jso_deserialization_test.js index 02eace4af..ddbdc96a8 100644 --- a/tests/mocha/jso_deserialization_test.js +++ b/tests/mocha/jso_deserialization_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.jsoDeserialization'); +const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired} = goog.require('Blockly.test.helpers.events'); const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventFired, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); suite('JSO Deserialization', function() { diff --git a/tests/mocha/test_helpers/events.js b/tests/mocha/test_helpers/events.js new file mode 100644 index 000000000..3b3b4563d --- /dev/null +++ b/tests/mocha/test_helpers/events.js @@ -0,0 +1,205 @@ +/** + * @license + * Copyright 2019 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +goog.module('Blockly.test.helpers.events'); + + +/** + * Creates spy for workspace fireChangeListener + * @param {!Blockly.Workspace} workspace The workspace to spy fireChangeListener + * calls on. + * @return {!SinonSpy} The created spy. + */ +function createFireChangeListenerSpy(workspace) { + return sinon.spy(workspace, 'fireChangeListener'); +} +exports.createFireChangeListenerSpy = createFireChangeListenerSpy; + +/** + * Asserts whether the given xml property has the expected property. + * @param {!Node} xmlValue The xml value to check. + * @param {!Node|string} expectedValue The expected value. + * @param {string=} message Optional message to use in assert message. + * @private + */ +function assertXmlPropertyEqual_(xmlValue, expectedValue, message) { + const value = Blockly.Xml.domToText(xmlValue); + if (expectedValue instanceof Node) { + expectedValue = Blockly.Xml.domToText(expectedValue); + } + chai.assert.equal(value, expectedValue, message); +} + +/** + * Asserts that the given object has the expected xml properties. + * @param {Object} obj The object to check. + * @param {Object} expectedXmlProperties The expected xml + * properties. + * @private + */ +function assertXmlProperties_(obj, expectedXmlProperties) { + Object.keys(expectedXmlProperties).map((key) => { + const value = obj[key]; + const expectedValue = expectedXmlProperties[key]; + if (expectedValue === undefined) { + chai.assert.isUndefined(value, + 'Expected ' + key + ' property to be undefined'); + return; + } + chai.assert.exists(value, 'Expected ' + key + ' property to exist'); + assertXmlPropertyEqual_(value, expectedValue, 'Checking property ' + key); + }); +} + +/** + * Whether given key indicates that the property is xml. + * @param {string} key The key to check. + * @return {boolean} Whether the given key is for xml property. + * @private + */ +function isXmlProperty_(key) { + return key.toLowerCase().endsWith('xml'); +} + +/** + * Asserts that the given event has the expected values. + * @param {!Blockly.Events.Abstract} event The event to check. + * @param {string} expectedType Expected type of event fired. + * @param {string} expectedWorkspaceId Expected workspace id of event fired. + * @param {?string} expectedBlockId Expected block id of event fired. + * @param {!Object} expectedProperties Map of of additional expected + * properties to check on fired event. + * @param {boolean=} [isUiEvent=false] Whether the event is a UI event. + * @param {string=} message Optional message to prepend assert messages. + */ +function assertEventEquals(event, expectedType, + expectedWorkspaceId, expectedBlockId, expectedProperties, isUiEvent = false, message) { + let prependMessage = message ? message + ' ' : ''; + prependMessage += 'Event fired '; + chai.assert.equal(event.type, expectedType, + prependMessage + 'type'); + chai.assert.equal(event.workspaceId, expectedWorkspaceId, + prependMessage + 'workspace id'); + chai.assert.equal(event.blockId, expectedBlockId, + prependMessage + 'block id'); + Object.keys(expectedProperties).map((key) => { + const value = event[key]; + const expectedValue = expectedProperties[key]; + if (expectedValue === undefined) { + chai.assert.isUndefined(value, prependMessage + key); + return; + } + chai.assert.exists(value, prependMessage + key); + if (isXmlProperty_(key)) { + assertXmlPropertyEqual_(value, expectedValue, + prependMessage + key); + } else { + chai.assert.equal(value, expectedValue, + prependMessage + key); + } + }); + if (isUiEvent) { + chai.assert.isTrue(event.isUiEvent); + } else { + chai.assert.isFalse(event.isUiEvent); + } +} +exports.assertEventEquals = assertEventEquals; + +/** + * Asserts that an event with the given values was fired. + * @param {!SinonSpy|!SinonSpyCall} spy The spy or spy call to use. + * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance + * type of event fired. + * @param {!Object} expectedProperties Map of of expected properties + * to check on fired event. + * @param {string} expectedWorkspaceId Expected workspace id of event fired. + * @param {?string=} expectedBlockId Expected block id of event fired. + */ +function assertEventFired(spy, instanceType, expectedProperties, + expectedWorkspaceId, expectedBlockId) { + expectedProperties = Object.assign({ + type: instanceType.prototype.type, + workspaceId: expectedWorkspaceId, + blockId: expectedBlockId, + }, expectedProperties); + const expectedEvent = + sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties)); + sinon.assert.calledWith(spy, expectedEvent); +} +exports.assertEventFired = assertEventFired; + +/** + * Asserts that an event with the given values was not fired. + * @param {!SpyCall} spy The spy to use. + * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance + * type of event fired. + * @param {!Object} expectedProperties Map of of expected properties + * to check on fired event. + * @param {string=} expectedWorkspaceId Expected workspace id of event fired. + * @param {?string=} expectedBlockId Expected block id of event fired. + */ +function assertEventNotFired(spy, instanceType, expectedProperties, + expectedWorkspaceId, expectedBlockId) { + expectedProperties.type = instanceType.prototype.type; + if (expectedWorkspaceId !== undefined) { + expectedProperties.workspaceId = expectedWorkspaceId; + } + if (expectedBlockId !== undefined) { + expectedProperties.blockId = expectedBlockId; + } + const expectedEvent = + sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties)); + sinon.assert.neverCalledWith(spy, expectedEvent); +} +exports.assertEventNotFired = assertEventNotFired; + +/** + * Filters out xml properties from given object based on key. + * @param {Object} properties The properties to filter. + * @return {Array>} A list containing split non + * xml properties and xml properties. [Object, Object] + * @private + */ +function splitByXmlProperties_(properties) { + const xmlProperties = {}; + const nonXmlProperties = {}; + Object.keys(properties).forEach((key) => { + if (isXmlProperty_(key)) { + xmlProperties[key] = properties[key]; + return false; + } else { + nonXmlProperties[key] = properties[key]; + } + }); + return [nonXmlProperties, xmlProperties]; +} + +/** + * Asserts that the event passed to the nth call of the given spy has the + * expected values. Assumes that the event is passed as the first argument. + * @param {!SinonSpy} spy The spy to use. + * @param {number} n Which call to check. + * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance + * type of event fired. + * @param {Object} expectedProperties Map of of expected properties + * to check on fired event. + * @param {string} expectedWorkspaceId Expected workspace id of event fired. + * @param {?string=} expectedBlockId Expected block id of event fired. + */ +function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties, + expectedWorkspaceId, expectedBlockId) { + const nthCall = spy.getCall(n); + const splitProperties = splitByXmlProperties_(expectedProperties); + const nonXmlProperties = splitProperties[0]; + const xmlProperties = splitProperties[1]; + + assertEventFired(nthCall, instanceType, nonXmlProperties, expectedWorkspaceId, + expectedBlockId); + const eventArg = nthCall.firstArg; + assertXmlProperties_(eventArg, xmlProperties); +} +exports.assertNthCallEventArgEquals = assertNthCallEventArgEquals; \ No newline at end of file diff --git a/tests/mocha/test_helpers/test_helpers.js b/tests/mocha/test_helpers/test_helpers.js index c9b3c4a3b..a0b25e0a0 100644 --- a/tests/mocha/test_helpers/test_helpers.js +++ b/tests/mocha/test_helpers/test_helpers.js @@ -6,8 +6,6 @@ goog.module('Blockly.test.helpers'); -const {captureWarnings} = goog.require('Blockly.test.helpers.common'); -const {KeyCodes} = goog.require('Blockly.utils.KeyCodes'); const eventUtils = goog.require('Blockly.Events.utils'); const {Blocks} = goog.require('Blockly.blocks'); @@ -225,198 +223,3 @@ function createGenUidStubWithReturns(returnIds) { return stub; } exports.createGenUidStubWithReturns = createGenUidStubWithReturns; - -/** - * Creates spy for workspace fireChangeListener - * @param {!Blockly.Workspace} workspace The workspace to spy fireChangeListener - * calls on. - * @return {!SinonSpy} The created spy. - */ -function createFireChangeListenerSpy(workspace) { - return sinon.spy(workspace, 'fireChangeListener'); -} -exports.createFireChangeListenerSpy = createFireChangeListenerSpy; - -/** - * Asserts whether the given xml property has the expected property. - * @param {!Node} xmlValue The xml value to check. - * @param {!Node|string} expectedValue The expected value. - * @param {string=} message Optional message to use in assert message. - * @private - */ -function assertXmlPropertyEqual_(xmlValue, expectedValue, message) { - const value = Blockly.Xml.domToText(xmlValue); - if (expectedValue instanceof Node) { - expectedValue = Blockly.Xml.domToText(expectedValue); - } - chai.assert.equal(value, expectedValue, message); -} - -/** - * Asserts that the given object has the expected xml properties. - * @param {Object} obj The object to check. - * @param {Object} expectedXmlProperties The expected xml - * properties. - * @private - */ -function assertXmlProperties_(obj, expectedXmlProperties) { - Object.keys(expectedXmlProperties).map((key) => { - const value = obj[key]; - const expectedValue = expectedXmlProperties[key]; - if (expectedValue === undefined) { - chai.assert.isUndefined(value, - 'Expected ' + key + ' property to be undefined'); - return; - } - chai.assert.exists(value, 'Expected ' + key + ' property to exist'); - assertXmlPropertyEqual_(value, expectedValue, 'Checking property ' + key); - }); -} - -/** - * Whether given key indicates that the property is xml. - * @param {string} key The key to check. - * @return {boolean} Whether the given key is for xml property. - * @private - */ -function isXmlProperty_(key) { - return key.toLowerCase().endsWith('xml'); -} - -/** - * Asserts that the given event has the expected values. - * @param {!Blockly.Events.Abstract} event The event to check. - * @param {string} expectedType Expected type of event fired. - * @param {string} expectedWorkspaceId Expected workspace id of event fired. - * @param {?string} expectedBlockId Expected block id of event fired. - * @param {!Object} expectedProperties Map of of additional expected - * properties to check on fired event. - * @param {boolean=} [isUiEvent=false] Whether the event is a UI event. - * @param {string=} message Optional message to prepend assert messages. - */ -function assertEventEquals(event, expectedType, - expectedWorkspaceId, expectedBlockId, expectedProperties, isUiEvent = false, message) { - let prependMessage = message ? message + ' ' : ''; - prependMessage += 'Event fired '; - chai.assert.equal(event.type, expectedType, - prependMessage + 'type'); - chai.assert.equal(event.workspaceId, expectedWorkspaceId, - prependMessage + 'workspace id'); - chai.assert.equal(event.blockId, expectedBlockId, - prependMessage + 'block id'); - Object.keys(expectedProperties).map((key) => { - const value = event[key]; - const expectedValue = expectedProperties[key]; - if (expectedValue === undefined) { - chai.assert.isUndefined(value, prependMessage + key); - return; - } - chai.assert.exists(value, prependMessage + key); - if (isXmlProperty_(key)) { - assertXmlPropertyEqual_(value, expectedValue, - prependMessage + key); - } else { - chai.assert.equal(value, expectedValue, - prependMessage + key); - } - }); - if (isUiEvent) { - chai.assert.isTrue(event.isUiEvent); - } else { - chai.assert.isFalse(event.isUiEvent); - } -} -exports.assertEventEquals = assertEventEquals; - -/** - * Asserts that an event with the given values was fired. - * @param {!SinonSpy|!SinonSpyCall} spy The spy or spy call to use. - * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance - * type of event fired. - * @param {!Object} expectedProperties Map of of expected properties - * to check on fired event. - * @param {string} expectedWorkspaceId Expected workspace id of event fired. - * @param {?string=} expectedBlockId Expected block id of event fired. - */ -function assertEventFired(spy, instanceType, expectedProperties, - expectedWorkspaceId, expectedBlockId) { - expectedProperties = Object.assign({ - workspaceId: expectedWorkspaceId, - blockId: expectedBlockId, - }, expectedProperties); - const expectedEvent = - sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties)); - sinon.assert.calledWith(spy, expectedEvent); -} -exports.assertEventFired = assertEventFired; - -/** - * Asserts that an event with the given values was not fired. - * @param {!SpyCall} spy The spy to use. - * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance - * type of event fired. - * @param {!Object} expectedProperties Map of of expected properties - * to check on fired event. - * @param {string=} expectedWorkspaceId Expected workspace id of event fired. - * @param {?string=} expectedBlockId Expected block id of event fired. - */ -function assertEventNotFired(spy, instanceType, expectedProperties, - expectedWorkspaceId, expectedBlockId) { - if (expectedWorkspaceId !== undefined) { - expectedProperties.workspaceId = expectedWorkspaceId; - } - if (expectedBlockId !== undefined) { - expectedProperties.blockId = expectedBlockId; - } - const expectedEvent = - sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties)); - sinon.assert.neverCalledWith(spy, expectedEvent); -} -exports.assertEventNotFired = assertEventNotFired; - -/** - * Filters out xml properties from given object based on key. - * @param {Object} properties The properties to filter. - * @return {Array>} A list containing split non - * xml properties and xml properties. [Object, Object] - * @private - */ -function splitByXmlProperties_(properties) { - const xmlProperties = {}; - const nonXmlProperties = {}; - Object.keys(properties).forEach((key) => { - if (isXmlProperty_(key)) { - xmlProperties[key] = properties[key]; - return false; - } else { - nonXmlProperties[key] = properties[key]; - } - }); - return [nonXmlProperties, xmlProperties]; -} - -/** - * Asserts that the event passed to the nth call of the given spy has the - * expected values. Assumes that the event is passed as the first argument. - * @param {!SinonSpy} spy The spy to use. - * @param {number} n Which call to check. - * @param {function(new:Blockly.Events.Abstract)} instanceType Expected instance - * type of event fired. - * @param {Object} expectedProperties Map of of expected properties - * to check on fired event. - * @param {string} expectedWorkspaceId Expected workspace id of event fired. - * @param {?string=} expectedBlockId Expected block id of event fired. - */ -function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties, - expectedWorkspaceId, expectedBlockId) { - const nthCall = spy.getCall(n); - const splitProperties = splitByXmlProperties_(expectedProperties); - const nonXmlProperties = splitProperties[0]; - const xmlProperties = splitProperties[1]; - - assertEventFired(nthCall, instanceType, nonXmlProperties, expectedWorkspaceId, - expectedBlockId); - const eventArg = nthCall.firstArg; - assertXmlProperties_(eventArg, xmlProperties); -} -exports.assertNthCallEventArgEquals = assertNthCallEventArgEquals; \ No newline at end of file diff --git a/tests/mocha/theme_test.js b/tests/mocha/theme_test.js index fa9480624..b909d752e 100644 --- a/tests/mocha/theme_test.js +++ b/tests/mocha/theme_test.js @@ -7,7 +7,8 @@ goog.module('Blockly.test.theme'); const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventFired, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired} = goog.require('Blockly.test.helpers.events'); +const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); suite('Theme', function() { diff --git a/tests/mocha/trashcan_test.js b/tests/mocha/trashcan_test.js index e5ac93f10..fd8ddf17e 100644 --- a/tests/mocha/trashcan_test.js +++ b/tests/mocha/trashcan_test.js @@ -6,7 +6,8 @@ goog.module('Blockly.test.trashcan'); -const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired} = goog.require('Blockly.test.helpers.events'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); const {defineBasicBlockWithField, defineMutatorBlocks, defineRowBlock, defineStackBlock, defineStatementBlock} = goog.require('Blockly.test.helpers.blockDefinitions'); const eventUtils = goog.require('Blockly.Events.utils'); const {simulateClick} = goog.require('Blockly.test.helpers.userInput'); diff --git a/tests/mocha/workspace_svg_test.js b/tests/mocha/workspace_svg_test.js index 012b10541..86778e5d9 100644 --- a/tests/mocha/workspace_svg_test.js +++ b/tests/mocha/workspace_svg_test.js @@ -6,7 +6,8 @@ goog.module('Blockly.test.workspaceSvg'); -const {assertEventFired, assertEventNotFired, assertVariableValues, createFireChangeListenerSpy, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired, createFireChangeListenerSpy} = goog.require('Blockly.test.helpers.events'); +const {assertVariableValues, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers'); const {defineStackBlock} = goog.require('Blockly.test.helpers.blockDefinitions'); const {testAWorkspace} = goog.require('Blockly.test.helpers.workspace'); diff --git a/tests/mocha/zoom_controls_test.js b/tests/mocha/zoom_controls_test.js index 45cf6ac72..052738fd3 100644 --- a/tests/mocha/zoom_controls_test.js +++ b/tests/mocha/zoom_controls_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.zoomControls'); -const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired} = goog.require('Blockly.test.helpers.events'); const eventUtils = goog.require('Blockly.Events.utils'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); const {simulateClick} = goog.require('Blockly.test.helpers.userInput');