mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +01:00
feat: allow the dialog methods to take in extra parameters (#8084)
* feat: allow prompt to take in additional params * feat: allow confirm tot ake in extra args * feat: allow alert dialog to take in extra parameters * chore: add tests for dialog methods
This commit is contained in:
@@ -322,10 +322,10 @@ suite('Context Menu Items', function () {
|
||||
});
|
||||
|
||||
test('Deletes all blocks after confirming', function () {
|
||||
// Mocks the confirmation dialog and calls the callback with 'true' simulating ok.
|
||||
const confirmStub = sinon
|
||||
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
|
||||
.callsArgWith(1, true);
|
||||
// Mocks the confirmation dialog and calls the callback with 'true'
|
||||
// simulating ok.
|
||||
const confirmStub = sinon.stub().callsArgWith(1, true);
|
||||
Blockly.dialog.setConfirm(confirmStub);
|
||||
|
||||
this.workspace.newBlock('text');
|
||||
this.workspace.newBlock('text');
|
||||
@@ -337,9 +337,8 @@ suite('Context Menu Items', function () {
|
||||
|
||||
test('Does not delete blocks if not confirmed', function () {
|
||||
// Mocks the confirmation dialog and calls the callback with 'false' simulating cancel.
|
||||
const confirmStub = sinon
|
||||
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
|
||||
.callsArgWith(1, false);
|
||||
const confirmStub = sinon.stub().callsArgWith(1, false);
|
||||
Blockly.dialog.setConfirm(confirmStub);
|
||||
|
||||
this.workspace.newBlock('text');
|
||||
this.workspace.newBlock('text');
|
||||
@@ -350,10 +349,9 @@ suite('Context Menu Items', function () {
|
||||
});
|
||||
|
||||
test('No dialog for single block', function () {
|
||||
const confirmStub = sinon.stub(
|
||||
Blockly.dialog.TEST_ONLY,
|
||||
'confirmInternal',
|
||||
);
|
||||
const confirmStub = sinon.stub();
|
||||
Blockly.dialog.setConfirm(confirmStub);
|
||||
|
||||
this.workspace.newBlock('text');
|
||||
this.deleteOption.callback(this.scope);
|
||||
this.clock.runAll();
|
||||
|
||||
58
tests/mocha/dialog_test.js
Normal file
58
tests/mocha/dialog_test.js
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2024 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
suite.only('Dialog', function () {
|
||||
teardown(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
suite('Prompt', function () {
|
||||
test('setPrompt can take in a function with additional parameters', function () {
|
||||
const spy = sinon.spy();
|
||||
Blockly.dialog.setPrompt(spy);
|
||||
const callback = () => {};
|
||||
|
||||
Blockly.dialog.prompt(
|
||||
'message',
|
||||
'defaultVal',
|
||||
callback,
|
||||
'extra parameter',
|
||||
);
|
||||
|
||||
chai.assert.isTrue(
|
||||
spy.calledWith('message', 'defaultVal', callback, 'extra parameter'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
suite('Confirm', function () {
|
||||
test('setConfirm can take in a function with additional parameters', function () {
|
||||
const spy = sinon.spy();
|
||||
Blockly.dialog.setConfirm(spy);
|
||||
const callback = () => {};
|
||||
|
||||
Blockly.dialog.confirm('message', callback, 'extra parameter');
|
||||
|
||||
chai.assert.isTrue(
|
||||
spy.calledWith('message', callback, 'extra parameter'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
suite('Alert', function () {
|
||||
test('setAlert can take in a function with additional parameters', function () {
|
||||
const spy = sinon.spy();
|
||||
Blockly.dialog.setAlert(spy);
|
||||
const callback = () => {};
|
||||
|
||||
Blockly.dialog.alert('message', callback, 'extra parameter');
|
||||
|
||||
chai.assert.isTrue(
|
||||
spy.calledWith('message', callback, 'extra parameter'),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -52,6 +52,7 @@
|
||||
import './contextmenu_items_test.js';
|
||||
import './contextmenu_test.js';
|
||||
import './cursor_test.js';
|
||||
import './dialog_test.js';
|
||||
import './dropdowndiv_test.js';
|
||||
import './event_test.js';
|
||||
import './event_block_change_test.js';
|
||||
|
||||
@@ -99,9 +99,8 @@ export function testAWorkspace() {
|
||||
|
||||
test('deleteVariableById(id2) one usage', function () {
|
||||
// Deleting variable one usage should not trigger confirm dialog.
|
||||
const stub = sinon
|
||||
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
|
||||
.callsArgWith(1, true);
|
||||
const stub = sinon.stub().callsArgWith(1, true);
|
||||
Blockly.dialog.setConfirm(stub);
|
||||
this.workspace.deleteVariableById('id2');
|
||||
|
||||
sinon.assert.notCalled(stub);
|
||||
@@ -113,9 +112,8 @@ export function testAWorkspace() {
|
||||
|
||||
test('deleteVariableById(id1) multiple usages confirm', function () {
|
||||
// Deleting variable with multiple usages triggers confirm dialog.
|
||||
const stub = sinon
|
||||
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
|
||||
.callsArgWith(1, true);
|
||||
const stub = sinon.stub().callsArgWith(1, true);
|
||||
Blockly.dialog.setConfirm(stub);
|
||||
this.workspace.deleteVariableById('id1');
|
||||
|
||||
sinon.assert.calledOnce(stub);
|
||||
@@ -127,9 +125,8 @@ export function testAWorkspace() {
|
||||
|
||||
test('deleteVariableById(id1) multiple usages cancel', function () {
|
||||
// Deleting variable with multiple usages triggers confirm dialog.
|
||||
const stub = sinon
|
||||
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
|
||||
.callsArgWith(1, false);
|
||||
const stub = sinon.stub().callsArgWith(1, false);
|
||||
Blockly.dialog.setConfirm(stub);
|
||||
this.workspace.deleteVariableById('id1');
|
||||
|
||||
sinon.assert.calledOnce(stub);
|
||||
|
||||
Reference in New Issue
Block a user