Skip to content

Commit

Permalink
fix(dev-tools): Have runSerializationTestSuite accept Blockly
Browse files Browse the repository at this point in the history
Have runSerializationTestSuite accept a second argument which is
the Blockly object to use for XML serialization/deserialization.

This works around an issue in blockly-samples that (following
the deletion of the alias for blockly in webpack.config.js)
causes tests using this function to fail due to having two copies
of jsdom loaded, where each will reject DOM objects created by
the other.  See
google#2229 (comment)
for more details.
  • Loading branch information
cpcallen committed Mar 5, 2024
1 parent 76d3ce8 commit 8de739f
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions plugins/block-dynamic-connection/test/dynamic_if.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly');
const Blockly = require('blockly/node');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down Expand Up @@ -641,5 +641,5 @@ suite('If block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly');
const Blockly = require('blockly/node');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down Expand Up @@ -540,5 +540,5 @@ suite('List create block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly');
const Blockly = require('blockly/node');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down Expand Up @@ -542,5 +542,5 @@ suite('Text join block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
4 changes: 2 additions & 2 deletions plugins/block-plus-minus/test/if.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ suite('If block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('JSON', function () {
Expand Down Expand Up @@ -222,7 +222,7 @@ suite('If block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite('controls_if', 1, 1, 'IF', assertIfBlockStructure);
Expand Down
4 changes: 2 additions & 2 deletions plugins/block-plus-minus/test/list_create.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ suite('List create block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -299,7 +299,7 @@ suite('List create block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite(
Expand Down
4 changes: 2 additions & 2 deletions plugins/block-plus-minus/test/procedures.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ suite('Procedure blocks', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -514,7 +514,7 @@ suite('Procedure blocks', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Adding and removing inputs', function () {
Expand Down
4 changes: 2 additions & 2 deletions plugins/block-plus-minus/test/text_join.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ suite('Text join block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -275,7 +275,7 @@ suite('Text join block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite('text_join', 2, 0, 'ADD', assertTextJoinBlockStructure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ suite('Procedures', function () {
},
},
];
testHelpers.runSerializationTestSuite(xmlTestCases, globalThis.clock);
testHelpers.runSerializationTestSuite(xmlTestCases, Blockly);

const jsonTestCases = [
{
Expand Down Expand Up @@ -2448,5 +2448,5 @@ suite('Procedures', function () {
},
},
];
testHelpers.runSerializationTestSuite(jsonTestCases, globalThis.clock);
testHelpers.runSerializationTestSuite(jsonTestCases, Blockly);
});
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ suite('BlockTemplate', function () {
},
// TODO add additional test cases.
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);

// TODO add any other relevant tests
});
26 changes: 15 additions & 11 deletions plugins/dev-tools/src/block_test_helpers.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ export const runCodeGenerationTestSuites = (testSuites) => {
/**
* Runs serialization test suite.
* @param {!Array<!SerializationTestCase>} testCases The test cases to run.
* @param {?Blockly} blockly The instance of Blockly to use for the
* tests. Optional, but must be supplied and must correspond to
* the instance used to create this.workspace if that would not
* otherwise be the case.
*/
export const runSerializationTestSuite = (testCases) => {
export const runSerializationTestSuite = (testCases, blockly = Blockly) => {
/**
* Creates test callback for xmlToBlock test.
* @param {!SerializationTestCase} testCase The test case information.
Expand All @@ -169,14 +173,14 @@ export const runSerializationTestSuite = (testCases) => {
return function () {
let block;
if (testCase.json) {
block = Blockly.serialization.blocks.append(
block = blockly.serialization.blocks.append(
testCase.json,
this.workspace,
{recordUndo: true},
);
} else {
block = Blockly.Xml.domToBlock(
Blockly.utils.xml.textToDom(testCase.xml),
block = blockly.Xml.domToBlock(
blockly.utils.xml.textToDom(testCase.xml),
this.workspace,
);
}
Expand All @@ -192,23 +196,23 @@ export const runSerializationTestSuite = (testCases) => {
const createRoundTripTestCallback = (testCase) => {
return function () {
if (testCase.json) {
const block = Blockly.serialization.blocks.append(
const block = blockly.serialization.blocks.append(
testCase.json,
this.workspace,
{recordUndo: true},
);
if (globalThis.clock) globalThis.clock.runAll();
const generatedJson = Blockly.serialization.blocks.save(block);
const generatedJson = blockly.serialization.blocks.save(block);
const expectedJson = testCase.expectedJson || testCase.json;
assert.deepEqual(generatedJson, expectedJson);
} else {
const block = Blockly.Xml.domToBlock(
Blockly.utils.xml.textToDom(testCase.xml),
const block = blockly.Xml.domToBlock(
blockly.utils.xml.textToDom(testCase.xml),
this.workspace,
);
if (globalThis.clock) globalThis.clock.runAll();
const generatedXml = Blockly.Xml.domToPrettyText(
Blockly.Xml.blockToDom(block),
const generatedXml = blockly.Xml.domToPrettyText(
blockly.Xml.blockToDom(block),
);
const expectedXml = testCase.expectedXml || testCase.xml;
assert.equal(generatedXml, expectedXml);
Expand All @@ -221,7 +225,7 @@ export const runSerializationTestSuite = (testCases) => {
});
suite('serialization round-trip', function () {
setup(function () {
sinon.stub(Blockly.utils.idGenerator.TEST_ONLY, 'genUid').returns('1');
sinon.stub(blockly.utils.idGenerator.TEST_ONLY, 'genUid').returns('1');
});

teardown(function () {
Expand Down

0 comments on commit 8de739f

Please sign in to comment.