Best JavaScript code snippet using wpt
event_test.js
Source:event_test.js  
...45  }46  suite('Constructors', function() {47    test('Abstract', function() {48      var event = new Blockly.Events.Abstract();49      assertEventEquals(event, undefined, undefined, undefined, {50        'recordUndo': true,51        'group': ''52      });53    });54    test('UI event without block', function() {55      var event = new Blockly.Events.UiBase(this.workspace.id);56      assertEventEquals(event, undefined, this.workspace.id, undefined, {57        'recordUndo': false,58        'group': '',59      }, true);60    });61    test('Click without block', function() {62      var event = new Blockly.Events.Click(null, this.workspace.id, 'workspace');63      assertEventEquals(event, Blockly.Events.CLICK, this.workspace.id, null, {64        'targetType': 'workspace',65        'recordUndo': false,66        'group': ''67      }, true);68    });69    test('Old UI event without block', function() {70      var TEST_GROUP_ID = 'testGroup';71      Blockly.Events.setGroup(TEST_GROUP_ID);72      var event = new Blockly.Events.Ui(null, 'foo', 'bar', 'baz');73      assertEventEquals(event, Blockly.Events.UI, '', null, {74        'element': 'foo',75        'oldValue': 'bar',76        'newValue': 'baz',77        'recordUndo': false,78        'group': TEST_GROUP_ID79      }, true);80    });81    suite('With simple blocks', function() {82      setup(function() {83        this.TEST_BLOCK_ID = 'test_block_id';84        this.TEST_PARENT_ID = 'parent';85        // genUid is expected to be called either once or twice in this suite.86        this.genUidStub = createGenUidStubWithReturns(87            [this.TEST_BLOCK_ID, this.TEST_PARENT_ID]);88        this.block = createSimpleTestBlock(this.workspace);89      });90      test('Block base', function() {91        var event = new Blockly.Events.BlockBase(this.block);92        sinon.assert.calledOnce(this.genUidStub);93        assertEventEquals(event, undefined,94            this.workspace.id, this.TEST_BLOCK_ID,95            {96              'varId': undefined,97              'recordUndo': true,98              'group': '',99            });100      });101      test('Create', function() {102        var event = new Blockly.Events.Create(this.block);103        sinon.assert.calledOnce(this.genUidStub);104        assertEventEquals(event, Blockly.Events.CREATE,105            this.workspace.id, this.TEST_BLOCK_ID,106            {107              'recordUndo': true,108              'group': '',109            });110      });111      test('Block create', function() {112        var event = new Blockly.Events.BlockCreate(this.block);113        sinon.assert.calledOnce(this.genUidStub);114        assertEventEquals(event, Blockly.Events.CREATE,115            this.workspace.id, this.TEST_BLOCK_ID,116            {117              'recordUndo': true,118              'group': '',119            });120      });121      test('Delete', function() {122        var event = new Blockly.Events.Delete(this.block);123        sinon.assert.calledOnce(this.genUidStub);124        assertEventEquals(event, Blockly.Events.DELETE,125            this.workspace.id, this.TEST_BLOCK_ID,126            {127              'recordUndo': true,128              'group': '',129            });130      });131      test('Block delete', function() {132        var event = new Blockly.Events.BlockDelete(this.block);133        sinon.assert.calledOnce(this.genUidStub);134        assertEventEquals(event, Blockly.Events.DELETE,135            this.workspace.id, this.TEST_BLOCK_ID,136            {137              'recordUndo': true,138              'group': '',139            });140      });141      test('Old UI event with block', function() {142        var TEST_GROUP_ID = 'testGroup';143        Blockly.Events.setGroup(TEST_GROUP_ID);144        var event = new Blockly.Events.Ui(this.block, 'foo', 'bar', 'baz');145        sinon.assert.calledOnce(this.genUidStub);146        assertEventEquals(event, Blockly.Events.UI, this.workspace.id,147            this.TEST_BLOCK_ID,148            {149              'element': 'foo',150              'oldValue': 'bar',151              'newValue': 'baz',152              'recordUndo': false,153              'group': TEST_GROUP_ID154            }, true);155      });156      test('Click with block', function() {157        var TEST_GROUP_ID = 'testGroup';158        Blockly.Events.setGroup(TEST_GROUP_ID);159        var event = new Blockly.Events.Click(this.block, null, 'block');160        assertEventEquals(event, Blockly.Events.CLICK, this.workspace.id,161            this.TEST_BLOCK_ID, {162              'targetType': 'block',163              'recordUndo': false,164              'group': TEST_GROUP_ID165            }, true);166      });167      suite('Move', function() {168        test('Move by coordinate', function() {169          var coordinate = new Blockly.utils.Coordinate(3, 4);170          this.block.xy_ = coordinate;171          var event = new Blockly.Events.Move(this.block);172          sinon.assert.calledOnce(this.genUidStub);173          assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,174              this.TEST_BLOCK_ID, {175                'oldParentId': undefined,176                'oldInputName': undefined,177                'oldCoordinate': coordinate,178                'recordUndo': true,179                'group': ''180              });181        });182        test('Block move by coordinate', function() {183          var coordinate = new Blockly.utils.Coordinate(3, 4);184          this.block.xy_ = coordinate;185          var event = new Blockly.Events.BlockMove(this.block);186          sinon.assert.calledOnce(this.genUidStub);187          assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,188              this.TEST_BLOCK_ID, {189                'oldParentId': undefined,190                'oldInputName': undefined,191                'oldCoordinate': coordinate,192                'recordUndo': true,193                'group': ''194              });195        });196        suite('Move by parent', function() {197          setup(function() {198            this.parentBlock = createSimpleTestBlock(this.workspace);199            this.block.parentBlock_ = this.parentBlock;200            this.block.xy_ = new Blockly.utils.Coordinate(3, 4);201          });202          teardown(function() {203            // This needs to be cleared, otherwise workspace.dispose will fail.204            this.block.parentBlock_ = null;205          });206          test('Move by parent', function() {207            var event = new Blockly.Events.Move(this.block);208            sinon.assert.calledTwice(this.genUidStub);209            assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,210                this.TEST_BLOCK_ID, {211                  'oldParentId': this.TEST_PARENT_ID,212                  'oldInputName': undefined,213                  'oldCoordinate': undefined,214                  'recordUndo': true,215                  'group': ''216                });217          });218          test('Block move by parent', function() {219            var event = new Blockly.Events.BlockMove(this.block);220            sinon.assert.calledTwice(this.genUidStub);221            assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,222                this.TEST_BLOCK_ID,223                {224                  'oldParentId': this.TEST_PARENT_ID,225                  'oldInputName': undefined,226                  'oldCoordinate': undefined,227                  'recordUndo': true,228                  'group': ''229                });230          });231        });232      });233    });234    suite('With shadow blocks', function() {235      setup(function() {236        this.TEST_BLOCK_ID = 'test_block_id';237        this.TEST_PARENT_ID = 'parent';238        // genUid is expected to be called either once or twice in this suite.239        this.genUidStub = createGenUidStubWithReturns(240            [this.TEST_BLOCK_ID, this.TEST_PARENT_ID]);241        this.block = createSimpleTestBlock(this.workspace);242        this.block.setShadow(true);243      });244      test('Block base', function() {245        var event = new Blockly.Events.BlockBase(this.block);246        sinon.assert.calledOnce(this.genUidStub);247        assertEventEquals(event, undefined,248            this.workspace.id, this.TEST_BLOCK_ID,249            {250              'varId': undefined,251              'recordUndo': true,252              'group': '',253            });254      });255      test('Change', function() {256        var event = new Blockly.Events.Change(257            this.block, 'field', 'FIELD_NAME', 'old', 'new');258        sinon.assert.calledOnce(this.genUidStub);259        assertEventEquals(event, Blockly.Events.CHANGE,260            this.workspace.id, this.TEST_BLOCK_ID,261            {262              'varId': undefined,263              'element': 'field',264              'name': 'FIELD_NAME',265              'oldValue': 'old',266              'newValue': 'new',267              'recordUndo': true,268              'group': '',269            });270      });271      test('Block change', function() {272        var event = new Blockly.Events.BlockChange(273            this.block, 'field', 'FIELD_NAME', 'old', 'new');274        sinon.assert.calledOnce(this.genUidStub);275        assertEventEquals(event, Blockly.Events.CHANGE,276            this.workspace.id, this.TEST_BLOCK_ID,277            {278              'varId': undefined,279              'element': 'field',280              'name': 'FIELD_NAME',281              'oldValue': 'old',282              'newValue': 'new',283              'recordUndo': true,284              'group': '',285            });286      });287      test('Create', function() {288        var event = new Blockly.Events.Create(this.block);289        sinon.assert.calledOnce(this.genUidStub);290        assertEventEquals(event, Blockly.Events.CREATE,291            this.workspace.id, this.TEST_BLOCK_ID,292            {293              'recordUndo': false,294              'group': '',295            });296      });297      test('Block create', function() {298        var event = new Blockly.Events.BlockCreate(this.block);299        sinon.assert.calledOnce(this.genUidStub);300        assertEventEquals(event, Blockly.Events.CREATE,301            this.workspace.id, this.TEST_BLOCK_ID,302            {303              'recordUndo': false,304              'group': '',305            });306      });307      test('Delete', function() {308        var event = new Blockly.Events.Delete(this.block);309        sinon.assert.calledOnce(this.genUidStub);310        assertEventEquals(event, Blockly.Events.DELETE,311            this.workspace.id, this.TEST_BLOCK_ID,312            {313              'recordUndo': false,314              'group': '',315            });316      });317      test('Block delete', function() {318        var event = new Blockly.Events.BlockDelete(this.block);319        sinon.assert.calledOnce(this.genUidStub);320        assertEventEquals(event, Blockly.Events.DELETE,321            this.workspace.id, this.TEST_BLOCK_ID,322            {323              'recordUndo': false,324              'group': '',325            });326      });327      suite('Move', function() {328        setup(function() {329          this.parentBlock = createSimpleTestBlock(this.workspace);330          this.block.parentBlock_ = this.parentBlock;331          this.block.xy_ = new Blockly.utils.Coordinate(3, 4);332        });333        teardown(function() {334          // This needs to be cleared, otherwise workspace.dispose will fail.335          this.block.parentBlock_ = null;336        });337        test('Move', function() {338          var event = new Blockly.Events.Move(this.block);339          sinon.assert.calledTwice(this.genUidStub);340          assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,341              this.TEST_BLOCK_ID, {342                'oldParentId': this.TEST_PARENT_ID,343                'oldInputName': undefined,344                'oldCoordinate': undefined,345                'recordUndo': false,346                'group': ''347              });348        });349        test('Block move', function() {350          var event = new Blockly.Events.BlockMove(this.block);351          sinon.assert.calledTwice(this.genUidStub);352          assertEventEquals(event, Blockly.Events.MOVE, this.workspace.id,353              this.TEST_BLOCK_ID,354              {355                'oldParentId': this.TEST_PARENT_ID,356                'oldInputName': undefined,357                'oldCoordinate': undefined,358                'recordUndo': false,359                'group': ''360              });361        });362      });363    });364    suite('With variable getter blocks', function() {365      setup(function() {366        this.genUidStub = createGenUidStubWithReturns(367            [this.TEST_BLOCK_ID, 'test_var_id', 'test_group_id']);368        // Disabling events when creating a block with variable can cause issues369        // at workspace dispose.370        this.block = new Blockly.Block(371            this.workspace, 'field_variable_test_block');372      });373      test('Change', function() {374        var event = new Blockly.Events.Change(375            this.block, 'field', 'VAR', 'id1', 'id2');376        assertEventEquals(event, Blockly.Events.CHANGE, this.workspace.id,377            this.TEST_BLOCK_ID,378            {379              'element': 'field',380              'name': 'VAR',381              'oldValue': 'id1',382              'newValue': 'id2',383              'recordUndo': true,384              'group': ''385            });386      });387      test('Block change', function() {388        var event = new Blockly.Events.BlockChange(389            this.block, 'field', 'VAR', 'id1', 'id2');390        assertEventEquals(event, Blockly.Events.CHANGE, this.workspace.id,391            this.TEST_BLOCK_ID,392            {393              'element': 'field',394              'name': 'VAR',395              'oldValue': 'id1',396              'newValue': 'id2',397              'recordUndo': true,398              'group': ''399            });400      });401    });402  });403  suite('Serialization', function() {404    var safeStringify = (json) => {405      let cache = [];406      return JSON.stringify(json, (key, value) => {407        if (typeof value == 'object' && value != null) {408          if (cache.includes(value)) {409            // Discard duplicate reference.410            return undefined;411          }412          cache.push(value);413          return value;414        }415        return value;416      });417    };418    var variableEventTestCases = [419      {title: 'Var create', class: Blockly.Events.VarCreate,420        getArgs: (thisObj) => [thisObj.variable],421        getExpectedJson: () => ({type: 'var_create', varId: 'id1',422          varType: 'type1', varName: 'name1'})},423      {title: 'Var delete', class: Blockly.Events.VarDelete,424        getArgs: (thisObj) => [thisObj.variable],425        getExpectedJson: () => ({type: 'var_delete', varId: 'id1',426          varType: 'type1', varName: 'name1'})},427      {title: 'Var rename', class: Blockly.Events.VarRename,428        getArgs: (thisObj) => [thisObj.variable, 'name2'],429        getExpectedJson: () => ({type: 'var_rename', varId: 'id1',430          oldName: 'name1', newName: 'name2'})},431    ];432    var uiEventTestCases = [433      {title: 'Bubble open', class: Blockly.Events.BubbleOpen,434        getArgs: (thisObj) => [thisObj.block, true, 'mutator'],435        getExpectedJson: (thisObj) => ({type: 'bubble_open', isOpen: true,436          bubbleType: 'mutator', blockId: thisObj.block.id})},437      {title: 'Block click', class: Blockly.Events.Click,438        getArgs: (thisObj) => [thisObj.block, null, 'block'],439        getExpectedJson: (thisObj) => ({type: 'click', targetType: 'block',440          blockId: thisObj.block.id})},441      {title: 'Workspace click', class: Blockly.Events.Click,442        getArgs: (thisObj) => [null, thisObj.workspace.id, 'workspace'],443        getExpectedJson: (thisObj) => ({type: 'click',444          targetType: 'workspace'})},445      {title: 'Drag start', class: Blockly.Events.BlockDrag,446        getArgs: (thisObj) => [thisObj.block, true, [thisObj.block]],447        getExpectedJson: (thisObj) => ({type: 'drag',448          isStart: true, blockId: thisObj.block.id, blocks: [thisObj.block]})},449      {title: 'Drag end', class: Blockly.Events.BlockDrag,450        getArgs: (thisObj) => [thisObj.block, false, [thisObj.block]],451        getExpectedJson: (thisObj) => ({type: 'drag',452          isStart: false, blockId: thisObj.block.id, blocks: [thisObj.block]})},453      {title: 'null to Block Marker move', class: Blockly.Events.MarkerMove,454        getArgs: (thisObj) => [thisObj.block, true, null,455          new Blockly.ASTNode(Blockly.ASTNode.types.BLOCK, thisObj.block)],456        getExpectedJson: (thisObj) => ({type: 'marker_move',457          isCursor: true, blockId: thisObj.block.id, oldNode: null,458          newNode: new Blockly.ASTNode(Blockly.ASTNode.types.BLOCK,459              thisObj.block)})},460      {title: 'null to Workspace Marker move', class: Blockly.Events.MarkerMove,461        getArgs: (thisObj) => [null, true, null,462          Blockly.ASTNode.createWorkspaceNode(thisObj.workspace,463              new Blockly.utils.Coordinate(0, 0))],464        getExpectedJson: (thisObj) => ({type: 'marker_move',465          isCursor: true, blockId: null, oldNode: null,466          newNode: Blockly.ASTNode.createWorkspaceNode(thisObj.workspace,467              new Blockly.utils.Coordinate(0, 0))})},468      {title: 'Workspace to Block Marker move',469        class: Blockly.Events.MarkerMove,470        getArgs: (thisObj) => [thisObj.block, true,471          Blockly.ASTNode.createWorkspaceNode(thisObj.workspace,472              new Blockly.utils.Coordinate(0, 0)),473          new Blockly.ASTNode(Blockly.ASTNode.types.BLOCK, thisObj.block)],474        getExpectedJson: (thisObj) => ({type: 'marker_move',475          isCursor: true, blockId: thisObj.block.id,476          oldNode: Blockly.ASTNode.createWorkspaceNode(thisObj.workspace,477              new Blockly.utils.Coordinate(0, 0)),478          newNode: new Blockly.ASTNode(Blockly.ASTNode.types.BLOCK,479              thisObj.block)})},480      {title: 'Block to Workspace Marker move',481        class: Blockly.Events.MarkerMove,482        getArgs: (thisObj) => [null, true,483          new Blockly.ASTNode(Blockly.ASTNode.types.BLOCK, thisObj.block),484          Blockly.ASTNode.createWorkspaceNode(thisObj.workspace,485              new Blockly.utils.Coordinate(0, 0))]},486      {title: 'Selected', class: Blockly.Events.Selected,487        getArgs: (thisObj) => [null, thisObj.block.id, thisObj.workspace.id],488        getExpectedJson: (thisObj) => ({type: 'selected', oldElementId: null,489          newElementId: thisObj.block.id})},490      {title: 'Selected (deselect)', class: Blockly.Events.Selected,491        getArgs: (thisObj) => [thisObj.block.id, null, thisObj.workspace.id],492        getExpectedJson: (thisObj) => ({type: 'selected',493          oldElementId: thisObj.block.id, newElementId: null})},494      {title: 'Theme Change', class: Blockly.Events.ThemeChange,495        getArgs: (thisObj) => ['classic', thisObj.workspace.id],496        getExpectedJson: () => ({type: 'theme_change', themeName: 'classic'})},497      {title: 'Toolbox item select',498        class: Blockly.Events.ToolboxItemSelect,499        getArgs: (thisObj) => ['Math', 'Loops', thisObj.workspace.id],500        getExpectedJson: () => ({type: 'toolbox_item_select', oldItem: 'Math',501          newItem: 'Loops'})},502      {title: 'Toolbox item select (no previous)',503        class: Blockly.Events.ToolboxItemSelect,504        getArgs: (thisObj) => [null, 'Loops', thisObj.workspace.id],505        getExpectedJson: () => ({type: 'toolbox_item_select', oldItem: null,506          newItem: 'Loops'})},507      {title: 'Toolbox item select (deselect)',508        class: Blockly.Events.ToolboxItemSelect,509        getArgs: (thisObj) => ['Math', null, thisObj.workspace.id],510        getExpectedJson: () => ({type: 'toolbox_item_select', oldItem: 'Math',511          newItem: null})},512      {title: 'Trashcan open', class: Blockly.Events.TrashcanOpen,513        getArgs: (thisObj) => [true, thisObj.workspace.id],514        getExpectedJson: () => ({type: 'trashcan_open', isOpen: true})},515      {title: 'Viewport change', class: Blockly.Events.ViewportChange,516        getArgs: (thisObj) => [2.666, 1.333, 1.2, thisObj.workspace.id, 1],517        getExpectedJson: () => ({type: 'viewport_change', viewTop: 2.666,518          viewLeft: 1.333, scale: 1.2, oldScale: 1})},519      {title: 'Viewport change (0,0)', class: Blockly.Events.ViewportChange,520        getArgs: (thisObj) => [0, 0, 1.2, thisObj.workspace.id, 1],521        getExpectedJson: () => ({type: 'viewport_change', viewTop: 0,522          viewLeft: 0, scale: 1.2, oldScale: 1})},523    ];524    var blockEventTestCases = [525      {title: 'Block change', class: Blockly.Events.BlockChange,526        getArgs: (thisObj) => [thisObj.block, 'collapsed', null, false, true],527        getExpectedJson: (thisObj) => ({type: 'change',528          blockId: thisObj.block.id, element: 'collapsed', oldValue: false,529          newValue: true})},530      {title: 'Block create', class: Blockly.Events.BlockCreate,531        getArgs: (thisObj) => [thisObj.block],532        getExpectedJson: (thisObj) => ({type: 'create',533          blockId: thisObj.block.id,534          xml: '<block xmlns="https://developers.google.com/blockly/xml"' +535              ' type="simple_test_block" id="testBlockId1"></block>',536          ids: [thisObj.block.id]})},537      {title: 'Block create (shadow)', class: Blockly.Events.BlockCreate,538        getArgs: (thisObj) => [thisObj.shadowBlock],539        getExpectedJson: (thisObj) => ({type: 'create',540          blockId: thisObj.shadowBlock.id,541          xml: '<shadow xmlns="https://developers.google.com/blockly/xml"' +542              ' type="simple_test_block" id="testBlockId2"></shadow>',543          ids: [thisObj.shadowBlock.id], recordUndo: false})},544      {title: 'Block delete', class: Blockly.Events.BlockDelete,545        getArgs: (thisObj) => [thisObj.block],546        getExpectedJson: (thisObj) => ({type: 'delete',547          blockId: thisObj.block.id,548          oldXml: '<block xmlns="https://developers.google.com/blockly/xml"' +549              ' type="simple_test_block" id="testBlockId1"></block>',550          ids: [thisObj.block.id]})},551      {title: 'Block delete (shadow)', class: Blockly.Events.BlockDelete,552        getArgs: (thisObj) => [thisObj.shadowBlock],553        getExpectedJson: (thisObj) => ({type: 'delete',554          blockId: thisObj.shadowBlock.id,555          oldXml: '<shadow xmlns="https://developers.google.com/blockly/xml"' +556              ' type="simple_test_block" id="testBlockId2"></shadow>',557          ids: [thisObj.shadowBlock.id], recordUndo: false})},558      // TODO(#4577) Test serialization of move event coordinate properties.559      {title: 'Block move', class: Blockly.Events.BlockMove,560        getArgs: (thisObj) => [thisObj.block],561        getExpectedJson: (thisObj) => ({type: 'move',562          blockId: thisObj.block.id})},563      {title: 'Block move (shadow)', class: Blockly.Events.BlockMove,564        getArgs: (thisObj) => [thisObj.shadowBlock],565        getExpectedJson: (thisObj) => ({type: 'move',566          blockId: thisObj.shadowBlock.id, recordUndo: false})},567    ];568    var workspaceEventTestCases = [569      {title: 'Finished Loading', class: Blockly.Events.FinishedLoading,570        getArgs: (thisObj) => [thisObj.workspace],571        getExpectedJson: (thisObj) => ({type: 'finished_loading',572          workspaceId: thisObj.workspace.id})},573    ];574    var workspaceCommentEventTestCases = [575      {title: 'Comment change', class: Blockly.Events.CommentChange,576        getArgs: (thisObj) => [thisObj.comment, 'bar', 'foo'],577        getExpectedJson: (thisObj) => ({type: 'comment_change',578          commentId: thisObj.comment.id, oldContents: 'bar',579          newContents: 'foo'})},580      {title: 'Comment create', class: Blockly.Events.CommentCreate,581        getArgs: (thisObj) => [thisObj.comment],582        getExpectedJson: (thisObj) => ({type: 'comment_create',583          commentId: thisObj.comment.id,584          xml: Blockly.Xml.domToText(thisObj.comment.toXmlWithXY())})},585      {title: 'Comment delete', class: Blockly.Events.CommentDelete,586        getArgs: (thisObj) => [thisObj.comment],587        getExpectedJson: (thisObj) => ({type: 'comment_delete',588          commentId: thisObj.comment.id})},589      // TODO(#4577) Test serialization of move event coordinate properties.590      {title: 'Comment move', class: Blockly.Events.CommentMove,591        getArgs: (thisObj) => [thisObj.comment],592        getExpectedJson: (thisObj) => ({type: 'comment_move',593          commentId: thisObj.comment.id, oldCoordinate: '0,0'})},594    ];595    var testSuites = [596      {title: 'Variable events', testCases: variableEventTestCases,597        setup: (thisObj) => {598          thisObj.variable =599              thisObj.workspace.createVariable('name1', 'type1', 'id1');600        }},601      {title: 'UI events', testCases: uiEventTestCases,602        setup: (thisObj) => {603          thisObj.block = createSimpleTestBlock(thisObj.workspace);604        }},605      {title: 'Block events', testCases: blockEventTestCases,606        setup: (thisObj) => {607          createGenUidStubWithReturns(['testBlockId1', 'testBlockId2']);608          thisObj.block = createSimpleTestBlock(thisObj.workspace);609          thisObj.shadowBlock = createSimpleTestBlock(thisObj.workspace);610          thisObj.shadowBlock.setShadow(true);611        }},612      {title: 'Workspace events',613        testCases: workspaceEventTestCases,614        setup: (_) => {}},615      {title: 'WorkspaceComment events',616        testCases: workspaceCommentEventTestCases,617        setup: (thisObj) => {618          thisObj.comment = new Blockly.WorkspaceComment(619              thisObj.workspace, 'comment text', 0, 0, 'comment id');620        }},621    ];622    testSuites.forEach((testSuite) => {623      suite(testSuite.title, function() {624        setup(function() {625          testSuite.setup(this);626        });627        suite('fromJson', function() {628          testSuite.testCases.forEach((testCase) => {629            test(testCase.title, function() {630              var event = new testCase.class(...testCase.getArgs(this));631              var event2 = new testCase.class();632              var json = event.toJson();633              event2.fromJson(json);634              chai.assert.equal(635                  safeStringify(event2.toJson()), safeStringify(json));636            });637          });638        });639        suite('toJson', function() {640          testSuite.testCases.forEach((testCase) => {641            if (testCase.getExpectedJson) {642              test(testCase.title, function() {643                var event = new testCase.class(...testCase.getArgs(this));644                var json = event.toJson();645                var expectedJson = testCase.getExpectedJson(this);646                chai.assert.equal(647                    safeStringify(json), safeStringify(expectedJson));648              });649            }650          });651        });652      });653    });654  });655  suite('Variable events', function() {656    setup(function() {657      this.variable = this.workspace.createVariable('name1', 'type1', 'id1');658    });659    /**660     * Check if a variable with the given values exists.661     * @param {Blockly.Workspace|Blockly.VariableMap} container The workspace  or662     *     variableMap the checked variable belongs to.663     * @param {!string} name The expected name of the variable.664     * @param {!string} type The expected type of the variable.665     * @param {!string} id The expected id of the variable.666     */667    function checkVariableValues(container, name, type, id) {668      var variable = container.getVariableById(id);669      chai.assert.isDefined(variable);670      chai.assert.equal(name, variable.name);671      chai.assert.equal(type, variable.type);672      chai.assert.equal(id, variable.getId());673    }674    suite('Constructors', function() {675      test('Var base', function() {676        var event = new Blockly.Events.VarBase(this.variable);677        assertEventEquals(event, undefined, this.workspace.id, undefined, {678          'varId': 'id1',679          'recordUndo': true,680          'group': ''681        });682      });683      test('Var create', function() {684        var event = new Blockly.Events.VarCreate(this.variable);685        assertEventEquals(event, Blockly.Events.VAR_CREATE, this.workspace.id,686            undefined,687            {688              'varId': 'id1',689              'varType': 'type1',690              'varName': 'name1',691              'recordUndo': true,692              'group': ''693            });694      });695      test('Var delete', function() {696        var event = new Blockly.Events.VarDelete(this.variable);697        assertEventEquals(event, Blockly.Events.VAR_DELETE, this.workspace.id,698            undefined,699            {700              'varId': 'id1',701              'varType': 'type1',702              'varName': 'name1',703              'recordUndo': true,704              'group': ''705            });706      });707      test('Var rename', function() {708        var event = new Blockly.Events.VarRename(this.variable, 'name2');709        assertEventEquals(event, Blockly.Events.VAR_RENAME, this.workspace.id,710            undefined,711            {712              'varId': 'id1',713              'oldName': 'name1',714              'newName': 'name2',715              'recordUndo': true,716              'group': ''717            });718      });719    });720    suite('Run Forward', function() {721      test('Var create', function() {722        var json = {type: "var_create", varId: "id2", varType: "type2",723          varName: "name2"};...Using AI Code Generation
1var assertEventEquals = wpt.assertEventEquals;2var testFunc = function() {3  var test = new Test("test");4  test.log("test");5  test.done();6}7var testFunc2 = function() {8  var test = new Test("test2");9  test.log("test2");10  test.done();11}12var testFunc3 = function() {13  var test = new Test("test3");14  test.log("test3");15  test.done();16}17var testFunc4 = function() {18  var test = new Test("test4");19  test.log("test4");20  test.done();21}22var testFunc5 = function() {23  var test = new Test("test5");24  test.log("test5");25  test.done();26}27var testFunc6 = function() {28  var test = new Test("test6");29  test.log("test6");30  test.done();31}32var testFunc7 = function() {33  var test = new Test("test7");34  test.log("test7");35  test.done();36}37var testFunc8 = function() {38  var test = new Test("test8");39  test.log("test8");40  test.done();41}42var testFunc9 = function() {43  var test = new Test("test9");44  test.log("test9");45  test.done();46}47var testFunc10 = function() {48  var test = new Test("test10");49  test.log("test10");50  test.done();51}52var testFunc11 = function() {53  var test = new Test("test11");54  test.log("test11");55  test.done();56}57var testFunc12 = function() {58  var test = new Test("test12");59  test.log("test12");60  test.done();61}62var testFunc13 = function() {63  var test = new Test("test13");64  test.log("test13");65  test.done();66}67var testFunc14 = function() {68  var test = new Test("test14");69  test.log("test14");70  test.done();71}72var testFunc15 = function() {73  var test = new Test("test15");74  test.log("test15");75  test.done();76}Using AI Code Generation
1var test = async_test();2test.step(function() {3  var event = new Event('test');4  event.initEvent('test', true, true);5  event.data = 'test';6  event.origin = 'test';7  event.source = 'test';8  event.lastEventId = 'test';9  event.ports = 'test';10  event.bubbles = true;11  event.cancelable = true;12  event.cancelBubble = true;13  event.defaultPrevented = true;14  event.eventPhase = 1;15  event.returnValue = true;16  event.srcElement = 'test';17  event.target = 'test';18  event.timeStamp = 1;19  event.type = 'test';20  event.isTrusted = true;21  event.currentTarget = 'test';Using AI Code Generation
1var wptTest = require('wpt-test');2var assertEventEquals = wptTest.assertEventEquals;3var test = wptTest.test;4var testWithIframe = wptTest.testWithIframe;5testWithIframe('test1', 'test1.html', function() {6  assertEventEquals('foo', 'foo');7});8test('test2', function() {9  assertEventEquals('foo', 'foo');10});11var assertEventEquals = wptTest.assertEventEquals;12var test = wptTest.test;13var testWithIframe = wptTest.testWithIframe;14testWithIframe('test1', 'test1.html', function() {15  assertEventEquals('foo', 'foo');16});17test('test2', function() {18  assertEventEquals('foo', 'foo');19});Using AI Code Generation
1var assert = require('./assert.js');2var assertEventEquals = assert.assertEventEquals;3var assertEventNotEquals = assert.assertEventNotEquals;4var assertEventFired = assert.assertEventFired;5var assertEventNotFired = assert.assertEventNotFired;6var assertEventFiredTimes = assert.assertEventFiredTimes;7var assertEventNotFiredTimes = assert.assertEventNotFiredTimes;8var assert = require('./assert.js');9var assertEventEquals = assert.assertEventEquals;10var assertEventNotEquals = assert.assertEventNotEquals;11var assertEventFired = assert.assertEventFired;12var assertEventNotFired = assert.assertEventNotFired;13var assertEventFiredTimes = assert.assertEventFiredTimes;14var assertEventNotFiredTimes = assert.assertEventNotFiredTimes;15var assert = require('./assert.js');16var assertEventEquals = assert.assertEventEquals;17var assertEventNotEquals = assert.assertEventNotEquals;18var assertEventFired = assert.assertEventFired;19var assertEventNotFired = assert.assertEventNotFired;20var assertEventFiredTimes = assert.assertEventFiredTimes;21var assertEventNotFiredTimes = assert.assertEventNotFiredTimes;22var assert = require('./assert.js');23var assertEventEquals = assert.assertEventEquals;24var assertEventNotEquals = assert.assertEventNotEquals;25var assertEventFired = assert.assertEventFired;26var assertEventNotFired = assert.assertEventNotFired;27var assertEventFiredTimes = assert.assertEventFiredTimes;28var assertEventNotFiredTimes = assert.assertEventNotFiredTimes;29var assert = require('./assert.js');30var assertEventEquals = assert.assertEventEquals;31var assertEventNotEquals = assert.assertEventNotEquals;32var assertEventFired = assert.assertEventFired;33var assertEventNotFired = assert.assertEventNotFired;34var assertEventFiredTimes = assert.assertEventFiredTimes;35var assertEventNotFiredTimes = assert.assertEventNotFiredTimes;36var assert = require('./assert.js');37var assertEventEquals = assert.assertEventEquals;38var assertEventNotEquals = assert.assertEventNotEquals;39var assertEventFired = assert.assertEventFired;Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
