From 8b69b61c566d3c96e44761ca19d60cfb467d6a4b Mon Sep 17 00:00:00 2001 From: gregdyke Date: Fri, 3 Jun 2022 19:37:21 +0100 Subject: [PATCH] fix: json hooks for lists blocks not needing extra state (#6177) --- blocks/lists.js | 63 ++++++++++++++++++----- tests/mocha/blocks/lists_test.js | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 12 deletions(-) diff --git a/blocks/lists.js b/blocks/lists.js index f320440df87..4bfd6fae3c1 100644 --- a/blocks/lists.js +++ b/blocks/lists.js @@ -649,10 +649,23 @@ blocks['lists_setIndex'] = { this.updateAt_(isAt); }, - // This block does not need JSO serialization hooks (saveExtraState and - // loadExtraState) because the state of this object is already encoded in the - // dropdown values. - // XML hooks are kept for backwards compatibility. + /** + * Returns the state of this block as a JSON serializable object. + * This block does not need to serialize any specific state as it is already + * encoded in the dropdown values, but must have an implementation to avoid + * the backward compatible XML mutations being serialized. + * @return {null} The state of this block. + */ + saveExtraState: function() { + return null; + }, + + /** + * Applies the given state to this block. + * No extra state is needed or expected as it is already encoded in the + * dropdown values. + */ + loadExtraState: function() {}, /** * Create or delete an input for the numeric index. @@ -761,10 +774,23 @@ blocks['lists_getSublist'] = { this.updateAt_(2, isAt2); }, - // This block does not need JSO serialization hooks (saveExtraState and - // loadExtraState) because the state of this object is already encoded in the - // dropdown values. - // XML hooks are kept for backwards compatibility. + /** + * Returns the state of this block as a JSON serializable object. + * This block does not need to serialize any specific state as it is already + * encoded in the dropdown values, but must have an implementation to avoid + * the backward compatible XML mutations being serialized. + * @return {null} The state of this block. + */ + saveExtraState: function() { + return null; + }, + + /** + * Applies the given state to this block. + * No extra state is needed or expected as it is already encoded in the + * dropdown values. + */ + loadExtraState: function() {}, /** * Create or delete an input for a numeric index. @@ -945,10 +971,23 @@ blocks['lists_split'] = { this.updateType_(xmlElement.getAttribute('mode')); }, - // This block does not need JSO serialization hooks (saveExtraState and - // loadExtraState) because the state of this object is already encoded in the - // dropdown values. - // XML hooks are kept for backwards compatibility. + /** + * Returns the state of this block as a JSON serializable object. + * This block does not need to serialize any specific state as it is already + * encoded in the dropdown values, but must have an implementation to avoid + * the backward compatible XML mutations being serialized. + * @return {null} The state of this block. + */ + saveExtraState: function() { + return null; + }, + + /** + * Applies the given state to this block. + * No extra state is needed or expected as it is already encoded in the + * dropdown values. + */ + loadExtraState: function() {}, }; // Register provided blocks. diff --git a/tests/mocha/blocks/lists_test.js b/tests/mocha/blocks/lists_test.js index b7d51672023..35532a89ec2 100644 --- a/tests/mocha/blocks/lists_test.js +++ b/tests/mocha/blocks/lists_test.js @@ -106,4 +106,89 @@ suite('Lists', function() { ]; runSerializationTestSuite(testCases); }); + + /** + * Test cases for serialization where JSON hooks should have null + * implementation to avoid serializing xml mutations in json. + * @param {!Object} serializedJson basic serialized json + * @param {!string} xmlMutation xml mutation that should be ignored/not reserialized in round trip + * @return {Array} test cases + */ + function makeTestCasesForBlockNotNeedingExtraState_(serializedJson, xmlMutation) { + return [ + { + title: 'JSON not requiring mutations', + json: serializedJson, + assertBlockStructure: (block) => { + chai.assert.equal(block.type, serializedJson.type); + }, + }, + { + title: + 'JSON with XML extra state', + json: { + ...serializedJson, + "extraState": xmlMutation, + }, + expectedJson: serializedJson, + assertBlockStructure: (block) => {}, + }, + ]; + } + + suite('ListsSetIndex', function() { + /** + * Test cases for serialization tests. + * @type {Array} + */ + const testCases = makeTestCasesForBlockNotNeedingExtraState_( + { + "type": "lists_setIndex", + "id": "1", + "fields": { + "MODE": "SET", + "WHERE": "FROM_START", + }, + }, + "" + ); + runSerializationTestSuite(testCases); + }); + + suite('ListsGetSubList', function() { + /** + * Test cases for serialization tests. + * @type {Array} + */ + const testCases = makeTestCasesForBlockNotNeedingExtraState_( + { + "type": "lists_getSublist", + "id": "1", + "fields": { + "WHERE1": "FROM_START", + "WHERE2": "FROM_START", + }, + }, + "" + ); + runSerializationTestSuite(testCases); + }); + + suite('ListsSplit', function() { + /** + * Test cases for serialization tests. + * @type {Array} + */ + const testCases = makeTestCasesForBlockNotNeedingExtraState_( + { + "type": "lists_split", + "id": "1", + "fields": { + "MODE": "SPLIT", + }, + }, + "" + ); + runSerializationTestSuite(testCases); + }); });