Skip to content

Commit

Permalink
revert: "feat: allow the dialog methods to take in extra parameters (#…
Browse files Browse the repository at this point in the history
…8084)" (#8085)

This reverts commit 278006b.
  • Loading branch information
BeksOmega committed May 10, 2024
1 parent 278006b commit c704d5a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 104 deletions.
85 changes: 55 additions & 30 deletions core/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,50 @@

// Former goog.module ID: Blockly.dialog

let alertImplementation = function (
message: string,
opt_callback?: () => void,
) {
window.alert(message);
if (opt_callback) {
opt_callback();
}
};

let confirmImplementation = function (
message: string,
callback: (result: boolean) => void,
) {
callback(window.confirm(message));
};

let promptImplementation = function (
message: string,
defaultValue: string,
callback: (result: string | null) => void,
) {
callback(window.prompt(message, defaultValue));
};

/**
* Wrapper to window.alert() that app developers may override via setAlert to
* provide alternatives to the modal browser window.
*
* @param message The message to display to the user.
* @param opt_callback The callback when the alert is dismissed.
*/
export let alert = (message: string, opt_callback?: () => void) => {
window.alert(message);
if (opt_callback) {
opt_callback();
}
};
export function alert(message: string, opt_callback?: () => void) {
alertImplementation(message, opt_callback);
}

/**
* Sets the function to be run when Blockly.dialog.alert() is called.
*
* @param alertFunction The function to be run.
* @see Blockly.dialog.alert
*/
export function setAlert(
alertFunction: (message: string, callback?: () => void) => void,
) {
alert = alertFunction;
export function setAlert(alertFunction: (p1: string, p2?: () => void) => void) {
alertImplementation = alertFunction;
}

/**
Expand All @@ -39,12 +59,16 @@ export function setAlert(
* @param message The message to display to the user.
* @param callback The callback for handling user response.
*/
export let confirm = (
message: string,
callback: (confirmed: boolean) => void,
) => {
callback(window.confirm(message));
};
export function confirm(message: string, callback: (p1: boolean) => void) {
TEST_ONLY.confirmInternal(message, callback);
}

/**
* Private version of confirm for stubbing in tests.
*/
function confirmInternal(message: string, callback: (p1: boolean) => void) {
confirmImplementation(message, callback);
}

/**
* Sets the function to be run when Blockly.dialog.confirm() is called.
Expand All @@ -53,12 +77,9 @@ export let confirm = (
* @see Blockly.dialog.confirm
*/
export function setConfirm(
confirmFunction: (
message: string,
callback: (confirmed: boolean) => void,
) => void,
confirmFunction: (p1: string, p2: (p1: boolean) => void) => void,
) {
confirm = confirmFunction;
confirmImplementation = confirmFunction;
}

/**
Expand All @@ -71,13 +92,13 @@ export function setConfirm(
* @param defaultValue The value to initialize the prompt with.
* @param callback The callback for handling user response.
*/
export let prompt = (
export function prompt(
message: string,
defaultValue: string,
callback: (userInput: string | null) => void,
) => {
callback(window.prompt(message, defaultValue));
};
callback: (p1: string | null) => void,
) {
promptImplementation(message, defaultValue, callback);
}

/**
* Sets the function to be run when Blockly.dialog.prompt() is called.
Expand All @@ -87,10 +108,14 @@ export let prompt = (
*/
export function setPrompt(
promptFunction: (
message: string,
defaultValue: string,
callback: (userInput: string | null) => void,
p1: string,
p2: string,
p3: (p1: string | null) => void,
) => void,
) {
prompt = promptFunction;
promptImplementation = promptFunction;
}

export const TEST_ONLY = {
confirmInternal,
};
20 changes: 11 additions & 9 deletions tests/mocha/contextmenu_items_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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().callsArgWith(1, true);
Blockly.dialog.setConfirm(confirmStub);
// Mocks the confirmation dialog and calls the callback with 'true' simulating ok.
const confirmStub = sinon
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
.callsArgWith(1, true);

this.workspace.newBlock('text');
this.workspace.newBlock('text');
Expand All @@ -337,8 +337,9 @@ 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().callsArgWith(1, false);
Blockly.dialog.setConfirm(confirmStub);
const confirmStub = sinon
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
.callsArgWith(1, false);

this.workspace.newBlock('text');
this.workspace.newBlock('text');
Expand All @@ -349,9 +350,10 @@ suite('Context Menu Items', function () {
});

test('No dialog for single block', function () {
const confirmStub = sinon.stub();
Blockly.dialog.setConfirm(confirmStub);

const confirmStub = sinon.stub(
Blockly.dialog.TEST_ONLY,
'confirmInternal',
);
this.workspace.newBlock('text');
this.deleteOption.callback(this.scope);
this.clock.runAll();
Expand Down
58 changes: 0 additions & 58 deletions tests/mocha/dialog_test.js

This file was deleted.

1 change: 0 additions & 1 deletion tests/mocha/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
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';
Expand Down
15 changes: 9 additions & 6 deletions tests/mocha/test_helpers/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ export function testAWorkspace() {

test('deleteVariableById(id2) one usage', function () {
// Deleting variable one usage should not trigger confirm dialog.
const stub = sinon.stub().callsArgWith(1, true);
Blockly.dialog.setConfirm(stub);
const stub = sinon
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
.callsArgWith(1, true);
this.workspace.deleteVariableById('id2');

sinon.assert.notCalled(stub);
Expand All @@ -112,8 +113,9 @@ export function testAWorkspace() {

test('deleteVariableById(id1) multiple usages confirm', function () {
// Deleting variable with multiple usages triggers confirm dialog.
const stub = sinon.stub().callsArgWith(1, true);
Blockly.dialog.setConfirm(stub);
const stub = sinon
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
.callsArgWith(1, true);
this.workspace.deleteVariableById('id1');

sinon.assert.calledOnce(stub);
Expand All @@ -125,8 +127,9 @@ export function testAWorkspace() {

test('deleteVariableById(id1) multiple usages cancel', function () {
// Deleting variable with multiple usages triggers confirm dialog.
const stub = sinon.stub().callsArgWith(1, false);
Blockly.dialog.setConfirm(stub);
const stub = sinon
.stub(Blockly.dialog.TEST_ONLY, 'confirmInternal')
.callsArgWith(1, false);
this.workspace.deleteVariableById('id1');

sinon.assert.calledOnce(stub);
Expand Down

0 comments on commit c704d5a

Please sign in to comment.