Files
blockly/tests/mocha/tooltip_test.js
dependabot[bot] bfb5b1dd49 chore(deps): Bump chai from 4.3.10 to 5.1.1 (#8092)
* chore(deps): Bump chai from 4.3.10 to 5.1.1

  Bumps [chai](https://github.com/chaijs/chai) from 4.3.10 to 5.1.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/v4.3.10...v5.1.1)

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

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

* fix(tests): Migrate all usage of chai to ESM (#8216)

* fix(tests): Migrate node tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by that package dropping suppport for CJS in chai v5.0.0.

* fix(tests): Have mocha tests directly import chai

  Previously they relied on obtaining it from the global scope, but it's
  better if imports are explicit.

* fix(tests): Remove broken load of chai as script

  Chai v5.0.0 no longer supports being loaded as a script, so this did
  nothing but emit an syntax error message on the console.

* fix(tests): Migrate browser tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by chai no longer supporting CJS.

* chore(tests): format

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2024-06-17 16:48:21 +01:00

277 lines
7.0 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import {
sharedTestSetup,
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
suite('Tooltip', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
Blockly.defineBlocksWithJsonArray([
{
'type': 'test_block',
'message0': '%1',
'args0': [
{
'type': 'field_input',
'name': 'FIELD',
},
],
},
]);
});
teardown(function () {
delete Blockly.Blocks['test_block'];
sharedTestTeardown.call(this);
});
suite('Custom Tooltip', function () {
setup(function () {
this.renderedWorkspace = Blockly.inject('blocklyDiv', {});
});
teardown(function () {
workspaceTeardown.call(this, this.renderedWorkspace);
});
test('Custom function is called', function () {
// Custom tooltip function is registered and should be called when mouse
// events are fired.
let wasCalled = false;
const customFn = function () {
wasCalled = true;
};
Blockly.Tooltip.setCustomTooltip(customFn);
this.block = this.renderedWorkspace.newBlock('test_block');
this.block.setTooltip('Test Tooltip');
// Fire pointer events directly on the relevant SVG.
this.block.pathObject.svgPath.dispatchEvent(
new PointerEvent('pointerover'),
);
this.block.pathObject.svgPath.dispatchEvent(
new PointerEvent('pointermove'),
);
this.clock.runAll();
assert.isTrue(
wasCalled,
'Expected custom tooltip function to have been called',
);
});
});
suite('set/getTooltip', function () {
const tooltipText = 'testTooltip';
function assertTooltip(obj) {
assert.equal(obj.getTooltip(), tooltipText);
}
function setStringTooltip(obj) {
obj.setTooltip(tooltipText);
}
function setFunctionTooltip(obj) {
obj.setTooltip(() => tooltipText);
}
function setNestedFunctionTooltip(obj) {
function nestFunction(fn, count) {
if (!count) {
return fn;
}
return () => nestFunction(fn, --count);
}
obj.setTooltip(nestFunction(() => tooltipText, 5));
}
function setFunctionReturningObjectTooltip(obj) {
obj.setTooltip(() => {
return {
tooltip: tooltipText,
};
});
}
function setObjectTooltip(obj) {
obj.setTooltip({tooltip: tooltipText});
}
suite('Headless Blocks', function () {
setup(function () {
this.block = this.workspace.newBlock('test_block');
});
test('String', function () {
setStringTooltip(this.block);
assertTooltip(this.block);
});
test('Function', function () {
setFunctionTooltip(this.block);
assertTooltip(this.block);
});
test('Nested Function', function () {
setNestedFunctionTooltip(this.block);
assertTooltip(this.block);
});
test('Function returning object', function () {
setFunctionReturningObjectTooltip(this.block);
assert.throws(
this.block.getTooltip.bind(this.block),
'Tooltip function must return a string.',
);
});
test('Object', function () {
setObjectTooltip(this.block);
assertTooltip(this.block);
});
});
suite('Rendered Blocks', function () {
setup(function () {
this.renderedWorkspace = Blockly.inject('blocklyDiv');
this.block = this.renderedWorkspace.newBlock('test_block');
this.block.initSvg();
this.block.render();
});
teardown(function () {
workspaceTeardown.call(this, this.renderedWorkspace);
});
test('String', function () {
setStringTooltip(this.block);
assertTooltip(this.block);
});
test('Function', function () {
setFunctionTooltip(this.block);
assertTooltip(this.block);
});
test('Nested Function', function () {
setNestedFunctionTooltip(this.block);
assertTooltip(this.block);
});
test('Function returning object', function () {
setFunctionReturningObjectTooltip(this.block);
assert.throws(
this.block.getTooltip.bind(this.block),
'Tooltip function must return a string.',
);
});
test('Object', function () {
setObjectTooltip(this.block);
assertTooltip(this.block);
});
});
suite('Headless Fields', function () {
setup(function () {
this.block = this.workspace.newBlock('test_block');
this.field = this.block.getField('FIELD');
});
test('String', function () {
setStringTooltip(this.field);
assertTooltip(this.field);
});
test('Function', function () {
setFunctionTooltip(this.field);
assertTooltip(this.field);
});
test('Nested Function', function () {
setNestedFunctionTooltip(this.field);
assertTooltip(this.field);
});
test('Function returning object', function () {
setFunctionReturningObjectTooltip(this.field);
assert.throws(
this.field.getTooltip.bind(this.field),
'Tooltip function must return a string.',
);
});
test('Object', function () {
setObjectTooltip(this.field);
assertTooltip(this.field);
});
test('Null', function () {
setStringTooltip(this.block);
this.field.setTooltip(null);
assertTooltip(this.field);
});
});
suite('Rendered Fields', function () {
setup(function () {
this.renderedWorkspace = Blockly.inject('blocklyDiv');
this.block = this.renderedWorkspace.newBlock('test_block');
this.block.initSvg();
this.block.render();
this.field = this.block.getField('FIELD');
});
teardown(function () {
workspaceTeardown.call(this, this.renderedWorkspace);
});
test('String', function () {
setStringTooltip(this.field);
assertTooltip(this.field);
});
test('Function', function () {
setFunctionTooltip(this.field);
assertTooltip(this.field);
});
test('Nested Function', function () {
setNestedFunctionTooltip(this.field);
assertTooltip(this.field);
});
test('Function returning object', function () {
setFunctionReturningObjectTooltip(this.field);
assert.throws(
this.field.getTooltip.bind(this.field),
'Tooltip function must return a string.',
);
});
test('Object', function () {
setObjectTooltip(this.field);
assertTooltip(this.field);
});
test('Null', function () {
setStringTooltip(this.block);
this.field.setTooltip(null);
assertTooltip(this.field);
});
});
});
});