diff --git a/core/events/events_var_create.ts b/core/events/events_var_create.ts index 63292bc84..eb8ccfe9f 100644 --- a/core/events/events_var_create.ts +++ b/core/events/events_var_create.ts @@ -53,7 +53,7 @@ export class VarCreate extends VarBase { */ override toJson(): VarCreateJson { const json = super.toJson() as VarCreateJson; - if (!this.varType) { + if (this.varType === undefined) { throw new Error( 'The var type is undefined. Either pass a variable to ' + 'the constructor, or call fromJson'); diff --git a/core/events/events_var_delete.ts b/core/events/events_var_delete.ts index d3f6c6f4f..be0fec4fe 100644 --- a/core/events/events_var_delete.ts +++ b/core/events/events_var_delete.ts @@ -48,7 +48,7 @@ export class VarDelete extends VarBase { */ override toJson(): VarDeleteJson { const json = super.toJson() as VarDeleteJson; - if (!this.varType) { + if (this.varType === undefined) { throw new Error( 'The var type is undefined. Either pass a variable to ' + 'the constructor, or call fromJson'); diff --git a/tests/mocha/event_var_create_test.js b/tests/mocha/event_var_create_test.js index b90372e8f..dbcd7a4ab 100644 --- a/tests/mocha/event_var_create_test.js +++ b/tests/mocha/event_var_create_test.js @@ -21,7 +21,18 @@ suite('Var Create Event', function() { }); suite('Serialization', function() { - test('events round-trip through JSON', function() { + test('untyped variable events round-trip through JSON', function() { + const varModel = + new Blockly.VariableModel(this.workspace, 'name', '', 'id'); + const origEvent = new Blockly.Events.VarCreate(varModel); + + const json = origEvent.toJson(); + const newEvent = new Blockly.Events.fromJson(json, this.workspace); + + chai.assert.deepEqual(newEvent, origEvent); + }); + + test('typed variable events round-trip through JSON', function() { const varModel = new Blockly.VariableModel(this.workspace, 'name', 'type', 'id'); const origEvent = new Blockly.Events.VarCreate(varModel); diff --git a/tests/mocha/event_var_delete_test.js b/tests/mocha/event_var_delete_test.js index f086873d3..ef532c2d2 100644 --- a/tests/mocha/event_var_delete_test.js +++ b/tests/mocha/event_var_delete_test.js @@ -20,7 +20,18 @@ suite('Var Delete Event', function() { }); suite('Serialization', function() { - test('events round-trip through JSON', function() { + test('untyped variable events round-trip through JSON', function() { + const varModel = + new Blockly.VariableModel(this.workspace, 'name', '', 'id'); + const origEvent = new Blockly.Events.VarDelete(varModel); + + const json = origEvent.toJson(); + const newEvent = new Blockly.Events.fromJson(json, this.workspace); + + chai.assert.deepEqual(newEvent, origEvent); + }); + + test('typed variable events round-trip through JSON', function() { const varModel = new Blockly.VariableModel(this.workspace, 'name', 'type', 'id'); const origEvent = new Blockly.Events.VarDelete(varModel);