Files
blockly/tests/mocha/dialog_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

169 lines
5.2 KiB
JavaScript

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/index.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Dialog utilities', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = Blockly.inject('blocklyDiv', {});
});
teardown(function () {
sharedTestTeardown.call(this);
Blockly.dialog.setAlert();
Blockly.dialog.setPrompt();
Blockly.dialog.setConfirm();
Blockly.dialog.setToast();
});
test('use the browser alert by default', function () {
const alert = sinon.stub(window, 'alert');
Blockly.dialog.alert('test');
assert.isTrue(alert.calledWith('test'));
alert.restore();
});
test('support setting a custom alert handler', function () {
const alert = sinon.spy();
Blockly.dialog.setAlert(alert);
const callback = () => {};
const message = 'test';
Blockly.dialog.alert(message, callback);
assert.isTrue(alert.calledWith('test', callback));
});
test('do not call the browser alert if a custom alert handler is set', function () {
const browserAlert = sinon.stub(window, 'alert');
const alert = sinon.spy();
Blockly.dialog.setAlert(alert);
Blockly.dialog.alert(test);
assert.isFalse(browserAlert.called);
browserAlert.restore();
});
test('use the browser confirm by default', function () {
const confirm = sinon.stub(window, 'confirm');
const callback = () => {};
const message = 'test';
Blockly.dialog.confirm(message, callback);
assert.isTrue(confirm.calledWith(message));
confirm.restore();
});
test('support setting a custom confirm handler', function () {
const confirm = sinon.spy();
Blockly.dialog.setConfirm(confirm);
const callback = () => {};
const message = 'test';
Blockly.dialog.confirm(message, callback);
assert.isTrue(confirm.calledWith('test', callback));
});
test('do not call the browser confirm if a custom confirm handler is set', function () {
const browserConfirm = sinon.stub(window, 'confirm');
const confirm = sinon.spy();
Blockly.dialog.setConfirm(confirm);
const callback = () => {};
const message = 'test';
Blockly.dialog.confirm(message, callback);
assert.isFalse(browserConfirm.called);
browserConfirm.restore();
});
test('invokes the provided callback with the confirmation response', function () {
const confirm = sinon.stub(window, 'confirm').returns(true);
const callback = sinon.spy();
const message = 'test';
Blockly.dialog.confirm(message, callback);
assert.isTrue(callback.calledWith(true));
confirm.restore();
});
test('use the browser prompt by default', function () {
const prompt = sinon.stub(window, 'prompt');
const callback = () => {};
const message = 'test';
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
assert.isTrue(prompt.calledWith(message, defaultValue));
prompt.restore();
});
test('support setting a custom prompt handler', function () {
const prompt = sinon.spy();
Blockly.dialog.setPrompt(prompt);
const callback = () => {};
const message = 'test';
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
assert.isTrue(prompt.calledWith('test', defaultValue, callback));
});
test('do not call the browser prompt if a custom prompt handler is set', function () {
const browserPrompt = sinon.stub(window, 'prompt');
const prompt = sinon.spy();
Blockly.dialog.setPrompt(prompt);
const callback = () => {};
const message = 'test';
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
assert.isFalse(browserPrompt.called);
browserPrompt.restore();
});
test('invokes the provided callback with the prompt response', function () {
const prompt = sinon.stub(window, 'prompt').returns('something');
const callback = sinon.spy();
const message = 'test';
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
assert.isTrue(callback.calledWith('something'));
prompt.restore();
});
test('use the built-in toast by default', function () {
const message = 'test toast';
Blockly.dialog.toast(this.workspace, {message});
const toast = this.workspace
.getInjectionDiv()
.querySelector('.blocklyToast');
assert.isNotNull(toast);
assert.equal(toast.textContent, message);
});
test('support setting a custom toast handler', function () {
const toast = sinon.spy();
Blockly.dialog.setToast(toast);
const message = 'test toast';
const options = {message};
Blockly.dialog.toast(this.workspace, options);
assert.isTrue(toast.calledWith(this.workspace, options));
});
test('do not use the built-in toast if a custom toast handler is set', function () {
const builtInToast = sinon.stub(Blockly.Toast, 'show');
const toast = sinon.spy();
Blockly.dialog.setToast(toast);
const message = 'test toast';
Blockly.dialog.toast(this.workspace, {message});
assert.isFalse(builtInToast.called);
builtInToast.restore();
});
});