diff --git a/tests/mocha/.eslintrc.json b/tests/mocha/.eslintrc.json index 0b9db3e2c..2b1d412ba 100644 --- a/tests/mocha/.eslintrc.json +++ b/tests/mocha/.eslintrc.json @@ -7,6 +7,8 @@ "globals": { "chai": false, "sinon": false, + "addBlockTypeToCleanup": true, + "addMessageToCleanup": true, "assertArrayEquals": true, "assertDeprecationWarningCall": true, "assertEventEquals": true, diff --git a/tests/mocha/astnode_test.js b/tests/mocha/astnode_test.js index e4302232e..c5169936e 100644 --- a/tests/mocha/astnode_test.js +++ b/tests/mocha/astnode_test.js @@ -91,9 +91,6 @@ suite('ASTNode', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['input_statement']; - delete Blockly.Blocks['field_input']; - delete Blockly.Blocks['value_input']; }); suite('HelperFunctions', function() { @@ -284,7 +281,6 @@ suite('ASTNode', function() { "helpUrl": "", "nextStatement": null }]); - var noNextConnection = this.workspace.newBlock('top_connection'); var fieldAndInputs = this.workspace.newBlock('fields_and_input'); var twoFields = this.workspace.newBlock('two_fields'); @@ -308,16 +304,6 @@ suite('ASTNode', function() { this.blocks.secondBlock = secondBlock; this.blocks.outputNextBlock = outputNextBlock; }); - teardown(function() { - delete Blockly.Blocks['output_next']; - delete Blockly.Blocks['fields_and_input2']; - delete Blockly.Blocks['two_fields']; - delete Blockly.Blocks['fields_and_input']; - delete Blockly.Blocks['top_connection']; - delete Blockly.Blocks['start_block']; - delete Blockly.Blocks['dummy_input']; - delete Blockly.Blocks['dummy_inputValue']; - }); suite('Next', function() { setup(function() { this.singleBlockWorkspace = new Blockly.Workspace(); diff --git a/tests/mocha/block_test.js b/tests/mocha/block_test.js index 6ed4cfb89..532125196 100644 --- a/tests/mocha/block_test.js +++ b/tests/mocha/block_test.js @@ -45,10 +45,6 @@ suite('Blocks', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['empty_block']; - delete Blockly.Blocks['stack_block']; - delete Blockly.Blocks['row_block']; - delete Blockly.Blocks['statement_block']; }); function createTestBlocks(workspace, isRow) { @@ -328,9 +324,6 @@ suite('Blocks', function() { }, ]); }); - teardown(function() { - delete Blockly.Blocks['value_block']; - }); suite('Value', function() { setup(function() { @@ -1235,7 +1228,6 @@ suite('Blocks', function() { teardown(function() { Blockly.Events.enable(); workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['variable_block']; }); suite('Connecting and Disconnecting', function() { test('Connect Block to Next', function() { diff --git a/tests/mocha/comment_test.js b/tests/mocha/comment_test.js index af61c35ec..55824823b 100644 --- a/tests/mocha/comment_test.js +++ b/tests/mocha/comment_test.js @@ -14,7 +14,6 @@ suite('Comments', function() { "args0": [] }, ]); - this.workspace = Blockly.inject('blocklyDiv', { comments: true, scrollbars: true @@ -27,7 +26,6 @@ suite('Comments', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['empty_block']; }); suite('Visibility and Editability', function() { setup(function() { diff --git a/tests/mocha/cursor_test.js b/tests/mocha/cursor_test.js index 647ec762d..f689526f2 100644 --- a/tests/mocha/cursor_test.js +++ b/tests/mocha/cursor_test.js @@ -74,8 +74,6 @@ suite('Cursor', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['input_statement']; - delete Blockly.Blocks['field_input']; }); test('Next - From a Previous skip over next connection and block', function() { diff --git a/tests/mocha/event_test.js b/tests/mocha/event_test.js index 67828f5c9..f88aafb93 100644 --- a/tests/mocha/event_test.js +++ b/tests/mocha/event_test.js @@ -28,8 +28,6 @@ suite('Events', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['field_variable_test_block']; - delete Blockly.Blocks['simple_test_block']; }); function createSimpleTestBlock(workspace) { diff --git a/tests/mocha/extensions_test.js b/tests/mocha/extensions_test.js index 1a189df5c..5c5cc23f5 100644 --- a/tests/mocha/extensions_test.js +++ b/tests/mocha/extensions_test.js @@ -8,15 +8,10 @@ suite('Extensions', function() { setup(function() { sharedTestSetup.call(this); this.workspace = new Blockly.Workspace(); - this.blockTypesCleanup_ = []; this.extensionsCleanup_ = []; }); teardown(function() { sharedTestTeardown.call(this); - for (let i = 0; i < this.blockTypesCleanup_.length; i++) { - var blockType = this.blockTypesCleanup_[i]; - delete Blockly.Blocks[blockType]; - } for (let i = 0; i < this.extensionsCleanup_.length; i++) { var extension = this.extensionsCleanup_[i]; delete Blockly.Extensions.ALL_[extension]; @@ -26,7 +21,6 @@ suite('Extensions', function() { test('Definition before and after block type', function() { this.extensionsCleanup_.push('extensions_test_before'); this.extensionsCleanup_.push('extensions_test_after'); - this.blockTypesCleanup_.push('extension_test_block'); chai.assert.isUndefined(Blockly.Extensions.ALL_['extensions_test_before']); var beforeCallback = sinon.spy(); @@ -58,9 +52,6 @@ suite('Extensions', function() { }); test('Parent tooltip when inline', function() { - this.blockTypesCleanup_.push('test_parent_tooltip_when_inline'); - this.blockTypesCleanup_.push('test_parent'); - var defaultTooltip = "defaultTooltip"; var parentTooltip = "parentTooltip"; Blockly.defineBlocksWithJsonArray([ @@ -114,7 +105,6 @@ suite('Extensions', function() { suite('Mixin', function() { test('Basic', function() { this.extensionsCleanup_.push('mixin_test'); - this.blockTypesCleanup_.push('test_block_mixin'); var testMixin = { field: 'FIELD', @@ -129,6 +119,7 @@ suite('Extensions', function() { chai.assert.typeOf(Blockly.Extensions.ALL_['mixin_test'], 'function'); + Blockly.defineBlocksWithJsonArray([{ "type": "test_block_mixin", "message0": "test_block_mixin", @@ -144,7 +135,6 @@ suite('Extensions', function() { suite('Mutator', function() { test('Basic', function() { this.extensionsCleanup_.push('mutator_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", @@ -182,7 +172,6 @@ suite('Extensions', function() { test('With helper function', function() { this.extensionsCleanup_.push('extensions_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", @@ -214,7 +203,6 @@ suite('Extensions', function() { test('No dialog', function() { this.extensionsCleanup_.push('mutator_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", @@ -249,8 +237,6 @@ suite('Extensions', function() { suite('Error cases', function() { test('Missing extension', function() { - this.blockTypesCleanup_.push('missing_extension_block'); - Blockly.defineBlocksWithJsonArray([{ "type": "missing_extension_block", "message0": "missing_extension_block", @@ -266,7 +252,6 @@ suite('Extensions', function() { test('Mixin overwrites local value', function() { this.extensionsCleanup_.push('mixin_bad_inputList'); - this.blockTypesCleanup_.push('test_block_bad_inputList'); var TEST_MIXIN_BAD_INPUTLIST = { inputList: 'bad inputList' // Defined in constructor @@ -291,7 +276,6 @@ suite('Extensions', function() { test('Mixin overwrites prototype', function() { this.extensionsCleanup_.push('mixin_bad_colour_'); - this.blockTypesCleanup_.push('test_block_bad_colour'); var TEST_MIXIN_BAD_COLOUR = { colour_: 'bad colour_' // Defined on prototype @@ -316,7 +300,6 @@ suite('Extensions', function() { test('Use mutator as extension', function() { this.extensionsCleanup_.push('mutator_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", @@ -348,7 +331,6 @@ suite('Extensions', function() { test('Use mutator mixin as extension', function() { this.extensionsCleanup_.push('mutator_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", @@ -380,7 +362,6 @@ suite('Extensions', function() { test('Use extension as mutator', function() { this.extensionsCleanup_.push('extensions_test'); - this.blockTypesCleanup_.push('mutator_test_block'); Blockly.defineBlocksWithJsonArray([{ "type": "mutator_test_block", diff --git a/tests/mocha/field_test.js b/tests/mocha/field_test.js index 69583f312..76f2ef0ac 100644 --- a/tests/mocha/field_test.js +++ b/tests/mocha/field_test.js @@ -351,11 +351,9 @@ suite('Abstract Fields', function() { }); suite('W/ Msg References', function() { setup(function() { + addMessageToCleanup(this.sharedCleanup, 'TOOLTIP'); Blockly.Msg['TOOLTIP'] = 'test tooltip'; }); - teardown(function() { - delete Blockly.Msg['TOOLTIP']; - }); test('JS Constructor', function() { var field = new Blockly.Field('value', null, { tooltip: '%{BKY_TOOLTIP}', @@ -378,6 +376,7 @@ suite('Abstract Fields', function() { workspaceTeardown.call(this, this.workspace); }); test('Before Append', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -393,9 +392,9 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip'); - delete Blockly.Blocks['tooltip']; }); test('After Append', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -411,9 +410,9 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip'); - delete Blockly.Blocks['tooltip']; }); test('After Block Creation', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -429,9 +428,9 @@ suite('Abstract Fields', function() { var field = block.getField('TOOLTIP'); field.setTooltip('tooltip'); chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip'); - delete Blockly.Blocks['tooltip']; }); test('Dynamic Function', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -451,9 +450,9 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, block.tooltipFunc); - delete Blockly.Blocks['tooltip']; }); test('Element', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -472,9 +471,9 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, block.element); - delete Blockly.Blocks['tooltip']; }); test('Null', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -490,9 +489,9 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, block); - delete Blockly.Blocks['tooltip']; }); test('Undefined', function() { + addBlockTypeToCleanup(this.sharedCleanup, 'tooltip'); Blockly.Blocks['tooltip'] = { init: function() { var field = new Blockly.FieldTextInput('default'); @@ -507,7 +506,6 @@ suite('Abstract Fields', function() { ).children[0], this.workspace); var field = block.getField('TOOLTIP'); chai.assert.equal(field.getClickTarget_().tooltip, block); - delete Blockly.Blocks['tooltip']; }); }); }); diff --git a/tests/mocha/field_variable_test.js b/tests/mocha/field_variable_test.js index c266bbf30..f34488d8f 100644 --- a/tests/mocha/field_variable_test.js +++ b/tests/mocha/field_variable_test.js @@ -321,10 +321,6 @@ suite('Variable Fields', function() { 'field_variable_test_block'); this.variableField = this.variableBlock.getField('VAR'); }); - teardown(function() { - this.variableBlock.dispose(); - delete Blockly.Blocks['field_variable_test_block']; - }); test('Rename & Keep Old ID', function() { this.workspace.renameVariableById('id1', 'name2'); chai.assert.equal(this.variableField.getText(), 'name2'); diff --git a/tests/mocha/flyout_test.js b/tests/mocha/flyout_test.js index ed7894bd4..840e48379 100644 --- a/tests/mocha/flyout_test.js +++ b/tests/mocha/flyout_test.js @@ -27,7 +27,6 @@ suite('Flyout', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['basic_block']; }); suite('createFlyoutInfo_', function() { diff --git a/tests/mocha/generator_test.js b/tests/mocha/generator_test.js index 2fc0ef6ee..9250557ab 100644 --- a/tests/mocha/generator_test.js +++ b/tests/mocha/generator_test.js @@ -78,11 +78,6 @@ suite('Generator', function() { }; }); - teardown(function() { - delete Blockly.Blocks['stack_block']; - delete Blockly.Blocks['row_block']; - }); - var testCase = [ [Blockly.Dart, 'Dart'], [Blockly.JavaScript, 'JavaScript'], @@ -115,7 +110,7 @@ suite('Generator', function() { suite('Nested block', function() { setup(function() { - Blockly.defineBlocksWithJsonArray([ { + Blockly.defineBlocksWithJsonArray([{ "type": "test_loop_block", "message0": "Repeat Loop", "message1": "%1", @@ -142,10 +137,6 @@ suite('Generator', function() { }; }); - teardown(function() { - delete Blockly.Blocks['test_loop_block']; - }); - testCase.forEach(function(testCase) { var generator = testCase[0]; var name = testCase[1]; diff --git a/tests/mocha/gesture_test.js b/tests/mocha/gesture_test.js index 82d0c943f..89e159d10 100644 --- a/tests/mocha/gesture_test.js +++ b/tests/mocha/gesture_test.js @@ -46,14 +46,13 @@ suite('Gesture', function() { setup(function() { sharedTestSetup.call(this); - defineBasicBlockWithField(); + defineBasicBlockWithField(this.sharedCleanup); var toolbox = document.getElementById('gesture-test-toolbox'); this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox}); }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Block['test_field_block']; }); test('Constructor', function() { diff --git a/tests/mocha/input_test.js b/tests/mocha/input_test.js index ba0206cef..c135bfe97 100644 --- a/tests/mocha/input_test.js +++ b/tests/mocha/input_test.js @@ -7,13 +7,11 @@ suite('Inputs', function() { setup(function() { sharedTestSetup.call(this); - Blockly.defineBlocksWithJsonArray([ - { - "type": "empty_block", - "message0": "", - "args0": [] - }, - ]); + Blockly.defineBlocksWithJsonArray([{ + "type": "empty_block", + "message0": "", + "args0": [] + }]); this.workspace = Blockly.inject('blocklyDiv'); this.block = Blockly.Xml.domToBlock(Blockly.Xml.textToDom( @@ -32,7 +30,6 @@ suite('Inputs', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['empty_block']; }); suite('Insert Field At', function() { suite('Index Bounds', function() { diff --git a/tests/mocha/insertion_marker_test.js b/tests/mocha/insertion_marker_test.js index 071043cfc..eb028b8eb 100644 --- a/tests/mocha/insertion_marker_test.js +++ b/tests/mocha/insertion_marker_test.js @@ -41,9 +41,6 @@ suite('InsertionMarkers', function() { }); teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['stack_block']; - delete Blockly.Blocks['row_block']; - delete Blockly.Blocks['statement_block']; }); suite('Code Generation', function() { setup(function() { diff --git a/tests/mocha/json_test.js b/tests/mocha/json_test.js index 8943ff7b8..d787d72bd 100644 --- a/tests/mocha/json_test.js +++ b/tests/mocha/json_test.js @@ -8,32 +8,22 @@ suite('JSON Block Definitions', function() { setup(function() { sharedTestSetup.call(this); this.workspace_ = new Blockly.Workspace(); - this.blockTypes_ = []; - this.messages_ = []; }); teardown(function() { sharedTestTeardown.call(this); - for (var i = 0, blockType; (blockType = this.blockTypes_[i]); i++) { - delete Blockly.Blocks[blockType]; - } - for (var i = 0, message; (message = this.messages_[i]); i++) { - delete Blockly.Msg[message]; - } }); suite('defineBlocksWithJsonArray', function() { test('Basic block', function() { /** Ensure a block can be instantiated from a JSON definition. */ var BLOCK_TYPE = 'test_json_minimal'; - this.blockTypes_.push(BLOCK_TYPE); - var workspace = this.workspace_; var block; assertNoWarnings(() => { Blockly.defineBlocksWithJsonArray([{ "type": BLOCK_TYPE }]); - block = new Blockly.Block(workspace, BLOCK_TYPE); + block = new Blockly.Block(this.workspace_, BLOCK_TYPE); }); chai.assert.isNotNull(block); @@ -43,8 +33,6 @@ suite('JSON Block Definitions', function() { test('Null or undefined type id', function() { var BLOCK_TYPE1 = 'test_json_before_bad_blocks'; var BLOCK_TYPE2 = 'test_json_after_bad_blocks'; - this.blockTypes_.push(BLOCK_TYPE1); - this.blockTypes_.push(BLOCK_TYPE2); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]); @@ -67,8 +55,6 @@ suite('JSON Block Definitions', function() { test('Null item', function() { var BLOCK_TYPE1 = 'test_block_before_null'; var BLOCK_TYPE2 = 'test_block_after_null'; - this.blockTypes_.push(BLOCK_TYPE1); - this.blockTypes_.push(BLOCK_TYPE2); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]); @@ -96,8 +82,6 @@ suite('JSON Block Definitions', function() { test('Undefined item', function() { var BLOCK_TYPE1 = 'test_block_before_undefined'; var BLOCK_TYPE2 = 'test_block_after_undefined'; - this.blockTypes_.push(BLOCK_TYPE1); - this.blockTypes_.push(BLOCK_TYPE2); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]); chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]); @@ -123,7 +107,6 @@ suite('JSON Block Definitions', function() { test('message0 creates input', function() { var BLOCK_TYPE = 'test_json_message0'; - this.blockTypes_.push(BLOCK_TYPE); var MESSAGE0 = 'message0'; Blockly.defineBlocksWithJsonArray([{ "type": BLOCK_TYPE, @@ -141,7 +124,6 @@ suite('JSON Block Definitions', function() { test('message1 and message0 creates two inputs', function() { /** Ensure message1 creates a new input. */ var BLOCK_TYPE = 'test_json_message1'; - this.blockTypes_.push(BLOCK_TYPE); var MESSAGE0 = 'message0'; var MESSAGE1 = 'message1'; Blockly.defineBlocksWithJsonArray([{ @@ -166,12 +148,11 @@ suite('JSON Block Definitions', function() { test('Message string is dereferenced', function() { var BLOCK_TYPE = 'test_json_message0_i18n'; - this.blockTypes_.push(BLOCK_TYPE); var MESSAGE0 = '%{BKY_MESSAGE}'; var MESSAGE = 'message'; + addMessageToCleanup(this.sharedCleanup, 'MESSAGE'); Blockly.Msg['MESSAGE'] = MESSAGE; - this.messages_.push('MESSAGE'); Blockly.defineBlocksWithJsonArray([{ "type": BLOCK_TYPE, "message0": MESSAGE0 @@ -187,7 +168,6 @@ suite('JSON Block Definitions', function() { test('Dropdown', function() { var BLOCK_TYPE = 'test_json_dropdown'; - this.blockTypes_.push(BLOCK_TYPE); var FIELD_NAME = 'FIELD_NAME'; var LABEL0 = 'LABEL0'; var VALUE0 = 'VALUE0'; @@ -226,11 +206,10 @@ suite('JSON Block Definitions', function() { test('Dropdown with images', function() { var BLOCK_TYPE = 'test_json_dropdown'; - this.blockTypes_.push(BLOCK_TYPE); var FIELD_NAME = 'FIELD_NAME'; var IMAGE1_ALT_TEXT = 'Localized message.'; + addMessageToCleanup(this.sharedCleanup, 'ALT_TEXT'); Blockly.Msg['ALT_TEXT'] = IMAGE1_ALT_TEXT; - this.messages_.push('ALT_TEXT'); var IMAGE0 = { 'width': 12, 'height': 34, diff --git a/tests/mocha/navigation_modify_test.js b/tests/mocha/navigation_modify_test.js index 5d9c67731..a4badf953 100644 --- a/tests/mocha/navigation_modify_test.js +++ b/tests/mocha/navigation_modify_test.js @@ -24,9 +24,9 @@ suite('Insert/Modify', function() { '' + '' + ''; - defineStackBlock(); - defineRowBlock(); - defineStatementBlock(); + defineStackBlock(this.sharedCleanup); + defineRowBlock(this.sharedCleanup); + defineStatementBlock(this.sharedCleanup); var toolbox = document.getElementById('toolbox-connections'); this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox}); @@ -44,9 +44,6 @@ suite('Insert/Modify', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['stack_block']; - delete Blockly.Blocks['row_block']; - delete Blockly.Blocks['statement_block']; }); suite('Marked Connection', function() { diff --git a/tests/mocha/navigation_test.js b/tests/mocha/navigation_test.js index d1c295fd9..9e63cbf45 100644 --- a/tests/mocha/navigation_test.js +++ b/tests/mocha/navigation_test.js @@ -55,7 +55,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); function testToolboxSelectMethodCalled(ws, mockEvent, keyCode, selectMethodName) { @@ -137,7 +136,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); // Should be a no-op @@ -228,7 +226,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); test('Previous', function() { @@ -341,7 +338,6 @@ suite('Navigation', function() { }); teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); test('Action does not exist', function() { var block = this.workspace.getTopBlocks()[0]; @@ -452,7 +448,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['field_block']; }); test('Perform valid action for read only', function() { @@ -506,7 +501,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); test('Insert from flyout with a valid connection marked', function() { @@ -611,8 +605,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['inline_block']; - delete Blockly.Blocks['basic_block']; }); test('Connect cursor on previous into stack', function() { @@ -682,7 +674,6 @@ suite('Navigation', function() { teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks['basic_block']; }); test('Delete block - has parent ', function() { diff --git a/tests/mocha/test_helpers.js b/tests/mocha/test_helpers.js index d28d60ee7..b14bb7ad9 100644 --- a/tests/mocha/test_helpers.js +++ b/tests/mocha/test_helpers.js @@ -130,11 +130,66 @@ function createEventsFireStubFireImmediately_(clock) { return stub; } +/** + * Adds message to shared cleanup object so that it is cleaned from + * Blockly.Messages global in sharedTestTeardown. + * @param {!Object} sharedCleanupObj The shared cleanup object created in + * sharedTestSetup. + * @param {string} message The message to add to shared cleanup object. + */ +function addMessageToCleanup(sharedCleanupObj, message) { + sharedCleanupObj.messagesCleanup_.push(message); +} + +/** + * Adds block type to shared cleanup object so that it is cleaned from + * Blockly.Blocks global in sharedTestTeardown. + * @param {!Object} sharedCleanupObj The shared cleanup object created in + * sharedTestSetup. + * @param {string} blockType The block type to add to shared cleanup object. + */ +function addBlockTypeToCleanup(sharedCleanupObj, blockType) { + sharedCleanupObj.blockTypesCleanup_.push(blockType); +} + +/** + * Wraps Blockly.defineBlocksWithJsonArray using stub in order to keep track of + * block types passed in to method on shared cleanup object so they are cleaned + * from Blockly.Blocks global in sharedTestTeardown. + * @param {!Object} sharedCleanupObj The shared cleanup object created in + * sharedTestSetup. + * @private + */ +function wrapDefineBlocksWithJsonArrayWithCleanup_(sharedCleanupObj) { + var stub = sinon.stub(Blockly, 'defineBlocksWithJsonArray'); + stub.callsFake(function(jsonArray) { + if (jsonArray) { + jsonArray.forEach((jsonBlock) => { + if (jsonBlock) { + addBlockTypeToCleanup(sharedCleanupObj, jsonBlock['type']); + } + }); + } + // Calls original method. + stub.wrappedMethod.call(this, ...arguments); + }); +} + /** * Shared setup method that sets up fake timer for clock so that pending * setTimeout calls can be cleared in test teardown along with other common * stubs. Should be called in setup of outermost suite using * sharedTestSetup.call(this). + * The sinon fake timer defined on this.clock_ should not be reset in tests to + * avoid causing issues with cleanup in sharedTestTeardown. + * + * Stubs created in this setup (unless disabled by options passed): + * - Blockly.Events.fire - this.eventsFireStub - wraps fire event to trigger + * fireNow_ call immediately, rather than on timeout + * - Blockly.defineBlocksWithJsonArray - thin wrapper that adds logic to keep + * track of block types defined so that they can be undefined in + * sharedTestTeardown and calls original method. + * * @param {Object} options Options to enable/disable setup * of certain stubs. */ @@ -147,6 +202,14 @@ function sharedTestSetup(options = {}) { // Stubs event firing unless passed option "fireEventsNow: false" this.eventsFireStub = createEventsFireStubFireImmediately_(this.clock); } + this.sharedCleanup = { + blockTypesCleanup_: [], + messagesCleanup_: [] + + }; + this.blockTypesCleanup_ = this.sharedCleanup.blockTypesCleanup_; + this.messagesCleanup_ = this.sharedCleanup.messagesCleanup_; + wrapDefineBlocksWithJsonArrayWithCleanup_(this.sharedCleanup); } /** @@ -187,6 +250,15 @@ function sharedTestTeardown() { // Restore all stubbed methods. this.sharedSetupSandbox_.restore(); sinon.restore(); + + var blockTypes = this.sharedCleanup.blockTypesCleanup_; + for (let i = 0; i < blockTypes.length; i++) { + delete Blockly.Blocks[blockTypes[i]]; + } + var messages = this.sharedCleanup.messagesCleanup_; + for (let i = 0; i < messages.length; i++) { + delete Blockly.Msg[messages[i]]; + } } } @@ -397,7 +469,7 @@ function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties, assertXmlProperties_(eventArg, xmlProperties); } -function defineStackBlock() { +function defineStackBlock(sharedCleanupObj) { Blockly.defineBlocksWithJsonArray([{ "type": "stack_block", "message0": "", @@ -406,7 +478,7 @@ function defineStackBlock() { }]); } -function defineRowBlock() { +function defineRowBlock(sharedCleanupObj) { Blockly.defineBlocksWithJsonArray([{ "type": "row_block", "message0": "%1", @@ -420,7 +492,7 @@ function defineRowBlock() { }]); } -function defineStatementBlock() { +function defineStatementBlock(sharedCleanupObj) { Blockly.defineBlocksWithJsonArray([{ "type": "statement_block", "message0": "%1", @@ -437,7 +509,7 @@ function defineStatementBlock() { "helpUrl": "" }]); } -function defineBasicBlockWithField() { +function defineBasicBlockWithField(sharedCleanupObj) { Blockly.defineBlocksWithJsonArray([{ "type": "test_field_block", "message0": "%1", diff --git a/tests/mocha/theme_test.js b/tests/mocha/theme_test.js index c81e5048c..1220bfcc5 100644 --- a/tests/mocha/theme_test.js +++ b/tests/mocha/theme_test.js @@ -20,7 +20,7 @@ suite('Theme', function() { Blockly.registry.typeMap_['theme'] = {}; }); - function defineThemeTestBlocks() { + function defineThemeTestBlocks(sharedCleanupObj) { Blockly.defineBlocksWithJsonArray([{ "type": "stack_block", "message0": "", @@ -40,12 +40,6 @@ suite('Theme', function() { }]); } - function undefineThemeTestBlocks() { - delete Blockly.Blocks['stack_block']; - delete Blockly.Blocks['row_block']; - } - - function createBlockStyles() { return { "styleOne": { @@ -123,7 +117,7 @@ suite('Theme', function() { }); test('Set Theme', function() { - defineThemeTestBlocks(); + defineThemeTestBlocks(this.sharedCleanup); try { var blockStyles = createBlockStyles(); var workspace = new Blockly.WorkspaceSvg(new Blockly.Options({})); @@ -154,7 +148,6 @@ suite('Theme', function() { workspace.id, null); } finally { workspaceTeardown.call(this, workspace); - undefineThemeTestBlocks(); } }); diff --git a/tests/mocha/toolbox_test.js b/tests/mocha/toolbox_test.js index 968e65516..ead0ffcbd 100644 --- a/tests/mocha/toolbox_test.js +++ b/tests/mocha/toolbox_test.js @@ -13,7 +13,6 @@ suite('Toolbox', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['row_block']; }); suite('init', function() { diff --git a/tests/mocha/variables_test.js b/tests/mocha/variables_test.js index c3e653323..2ed765cdb 100644 --- a/tests/mocha/variables_test.js +++ b/tests/mocha/variables_test.js @@ -26,7 +26,6 @@ suite('Variables', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['get_var_block']; }); /** diff --git a/tests/mocha/workspace_svg_test.js b/tests/mocha/workspace_svg_test.js index 981927359..27b4b07f3 100644 --- a/tests/mocha/workspace_svg_test.js +++ b/tests/mocha/workspace_svg_test.js @@ -28,8 +28,6 @@ suite('WorkspaceSvg', function() { teardown(function() { sharedTestTeardown.call(this); - delete Blockly.Blocks['simple_test_block']; - delete Blockly.Blocks['test_val_in']; }); test('dispose of WorkspaceSvg without dom throws no error', function() { @@ -144,7 +142,6 @@ suite('WorkspaceSvg', function() { // because it holds the variable map. // Normally the main workspace disposes of the flyout workspace. workspaceTeardown.call(this, this.targetWorkspace); - delete Blockly.Blocks['get_var_block']; }); test('Trivial Flyout is True', function() { diff --git a/tests/mocha/workspace_test.js b/tests/mocha/workspace_test.js index 401850868..e9bc9ece9 100644 --- a/tests/mocha/workspace_test.js +++ b/tests/mocha/workspace_test.js @@ -34,7 +34,6 @@ function testAWorkspace() { }); teardown(function() { - delete Blockly.Blocks['get_var_block']; // Clear Blockly.Event state. Blockly.Events.setGroup(false); Blockly.Events.disabled_ = 0; diff --git a/tests/mocha/xml_test.js b/tests/mocha/xml_test.js index 8ac9e378e..1c5ba4796 100644 --- a/tests/mocha/xml_test.js +++ b/tests/mocha/xml_test.js @@ -36,7 +36,6 @@ suite('XML', function() { "args0": [] }, ]); - this.blockTypes_ = ['empty_block']; this.complexXmlText = [ '', ' ', @@ -67,10 +66,6 @@ suite('XML', function() { }); teardown(function() { sharedTestTeardown.call(this); - for (var i = 0; i < this.blockTypes_.length; i++) { - delete Blockly.Blocks[this.blockTypes_[i]]; - } - this.blockTypes_.length = 0; }); suite('textToDom', function() { test('Basic', function() { @@ -99,7 +94,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_angle_test_block'); var block = new Blockly.Block(this.workspace, 'field_angle_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -117,7 +111,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_checkbox_test_block'); var block = new Blockly.Block(this.workspace, 'field_checkbox_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -135,7 +128,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_colour_test_block'); var block = new Blockly.Block(this.workspace, 'field_colour_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -166,7 +158,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_dropdown_test_block'); var block = new Blockly.Block(this.workspace, 'field_dropdown_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -187,7 +178,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_image_test_block'); var block = new Blockly.Block(this.workspace, 'field_image_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block); @@ -205,7 +195,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_label_test_block'); var block = new Blockly.Block(this.workspace, 'field_label_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block); @@ -223,7 +212,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_label_serializable_test_block'); var block = new Blockly.Block(this.workspace, 'field_label_serializable_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -241,7 +229,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_number_test_block'); var block = new Blockly.Block(this.workspace, 'field_number_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -259,7 +246,6 @@ suite('XML', function() { } ], }]); - this.blockTypes_.push('field_text_input_test_block'); var block = new Blockly.Block(this.workspace, 'field_text_input_test_block'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; @@ -278,7 +264,6 @@ suite('XML', function() { } ] }]); - this.blockTypes_.push('field_variable_test_block'); }); test('Variable Trivial', function() { this.workspace.createVariable('name1', '', 'id1'); @@ -404,7 +389,6 @@ suite('XML', function() { } ] }]); - this.blockTypes_.push('field_variable_test_block'); }); teardown(function() { workspaceTeardown.call(this, this.workspace); @@ -485,35 +469,7 @@ suite('XML', function() { "name": "VALUE" } ] - }, - { - "type": "math_change", - "message0": "%1 %2", - "args0": [ - { - "type": "field_variable", - "name": "VAR" - }, - { - "type": "input_value", - "name": "DELTA", - "check": "Number" - } - ] - }, - { - "type": "math_number", - "message0": "%1", - "args0": [{ - "type": "field_number", - "name": "NUM", - "value": 0 - }], - "output": "Number" }]); - Array.prototype.push.apply( - this.blockTypes_, - ['variables_get', 'variables_set', 'math_change', 'math_number']); }); teardown(function() { workspaceTeardown.call(this, this.workspace); @@ -680,7 +636,6 @@ suite('XML', function() { } ] }]); - this.blockTypes_.push('field_variable_test_block'); }); teardown(function() { workspaceTeardown.call(this, this.workspace); @@ -758,7 +713,8 @@ suite('XML', function() { }); suite('appendDomToWorkspace', function() { setup(function() { - Blockly.Blocks.test_block = { + addBlockTypeToCleanup(this.sharedCleanup,'test_block'); + Blockly.Blocks['test_block'] = { init: function() { this.jsonInit({ message0: 'test', @@ -769,7 +725,6 @@ suite('XML', function() { }); teardown(function() { workspaceTeardown.call(this, this.workspace); - delete Blockly.Blocks.test_block; }); test('Headless', function() { var dom = Blockly.Xml.textToDom(