fix: variable events not deserializing properly (#6832)

This commit is contained in:
Beka Westberg
2023-02-27 10:43:19 -08:00
committed by GitHub
parent a8faa69d9a
commit c9e2af2a27
4 changed files with 26 additions and 4 deletions

View File

@@ -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');

View File

@@ -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');

View File

@@ -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);

View File

@@ -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);