Files
blockly/tests/mocha/dialog_test.js
Aaron Dodson c6e58c4f92 feat: Add support for displaying toast-style notifications. (#8896)
* feat: Allow resetting alert/prompt/confirm to defaults.

* chore: Add unit tests for Blockly.dialog.

* fix: Removed TEST_ONLY hack from Blockly.dialog.

* feat: Add a default toast notification implementation.

* feat: Add support for toasts to Blockly.dialog.

* chore: Add tests for default toast implementation.

* chore: Fix docstring.

* refactor: Use default arguments for dialog functions.

* refactor: Add 'close' to the list of messages.

* chore: Add new message in several other places.

* chore: clarify docstrings.

* feat: Make toast assertiveness configurable.
2025-04-21 15:32:45 -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/chai.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();
});
});