Files
blockly/tests/mocha/contextmenu_test.js
dependabot[bot] 8873e5fe7a chore(deps): bump chai from 5.2.1 to 6.0.1 (#9330)
* chore(deps): bump chai from 5.2.1 to 6.0.1

Bumps [chai](https://github.com/chaijs/chai) from 5.2.1 to 6.0.1.
- [Release notes](https://github.com/chaijs/chai/releases)
- [Changelog](https://github.com/chaijs/chai/blob/main/History.md)
- [Commits](https://github.com/chaijs/chai/compare/v5.2.1...v6.0.1)

---
updated-dependencies:
- dependency-name: chai
  dependency-version: 6.0.1
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: Fix Chai import path.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Aaron Dodson <adodson@google.com>
2025-08-26 09:08:01 -07:00

74 lines
2.4 KiB
JavaScript

/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {callbackFactory} from '../../build/src/core/contextmenu.js';
import * as xmlUtils from '../../build/src/core/utils/xml.js';
import {assert} from '../../node_modules/chai/index.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Context Menu', function () {
setup(function () {
sharedTestSetup.call(this);
// Creates a WorkspaceSVG
const toolbox = document.getElementById('toolbox-categories');
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('Callback Factory', function () {
setup(function () {
this.forLoopBlock = this.workspace.newBlock('controls_for');
});
test('callback with xml state creates block', function () {
const variable = this.forLoopBlock.getField('VAR').getVariable();
const xmlField = document.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('id', variable.getId());
xmlField.setAttribute('variabletype', variable.getType());
xmlField.textContent = variable.getName();
const xmlBlock = xmlUtils.createElement('block');
xmlBlock.setAttribute('type', 'variables_get');
xmlBlock.appendChild(xmlField);
const callback = callbackFactory(this.forLoopBlock, xmlBlock);
const getVarBlock = callback();
assert.equal(getVarBlock.type, 'variables_get');
assert.equal(getVarBlock.workspace, this.forLoopBlock.workspace);
assert.equal(
getVarBlock.getField('VAR').getVariable().getId(),
this.forLoopBlock.getField('VAR').getVariable().getId(),
);
});
test('callback with json state creates block', function () {
const jsonState = {
type: 'variables_get',
fields: {VAR: this.forLoopBlock.getField('VAR').saveState(true)},
};
const callback = callbackFactory(this.forLoopBlock, jsonState);
const getVarBlock = callback();
assert.equal(getVarBlock.type, 'variables_get');
assert.equal(getVarBlock.workspace, this.forLoopBlock.workspace);
assert.equal(
getVarBlock.getField('VAR').getVariable().getId(),
this.forLoopBlock.getField('VAR').getVariable().getId(),
);
});
});
});