Files
blockly/tests/browser/test/delete_blocks_test.mjs
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

212 lines
6.5 KiB
JavaScript

/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as chai from 'chai';
import {
testSetup,
testFileLocations,
getAllBlocks,
getBlockElementById,
clickBlock,
contextMenuSelect,
PAUSE_TIME,
} from './test_setup.mjs';
import {Key} from 'webdriverio';
const firstBlockId = 'root_block';
const startBlocks = {
blocks: {
languageVersion: 0,
blocks: [
{
type: 'text_print',
id: firstBlockId,
x: 63,
y: 88,
inputs: {
TEXT: {
shadow: {
type: 'text',
id: 'text_shadow',
fields: {
TEXT: '1',
},
},
},
},
next: {
block: {
type: 'text_print',
id: 'second_block',
inputs: {
TEXT: {
shadow: {
type: 'text',
id: 'second_text_shadow',
fields: {
TEXT: '2',
},
},
block: {
type: 'text_trim',
id: 'trim_block',
fields: {
MODE: 'BOTH',
},
inputs: {
TEXT: {
shadow: {
type: 'text',
id: 'text_to_trim_shadow',
fields: {
TEXT: 'abc',
},
},
block: {
type: 'text',
id: 'text_to_trim_real',
fields: {
TEXT: 'hello',
},
},
},
},
},
},
},
next: {
block: {
type: 'text_print',
id: 'third_block',
inputs: {
TEXT: {
shadow: {
type: 'text',
id: 'third_text_shadow',
fields: {
TEXT: '3',
},
},
},
},
},
},
},
},
},
],
},
};
suite('Delete blocks', function (done) {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);
// Setup Selenium for all of the tests
suiteSetup(async function () {
this.browser = await testSetup(testFileLocations.PLAYGROUND);
});
// Clear the workspace and load the start blocks before each test
setup(async function () {
await this.browser.execute(() => {
// Clear the workspace manually so we can ensure it's clear before moving on to the next test.
Blockly.getMainWorkspace().clear();
});
// Wait for the workspace to be cleared of blocks (no blocks found on main workspace)
await this.browser
.$(
'.blocklySvg .blocklyWorkspace > .blocklyBlockCanvas > .blocklyDraggable',
)
.waitForExist({timeout: 2000, reverse: true});
// Load the start blocks
await this.browser.execute((blocks) => {
Blockly.serialization.workspaces.load(blocks, Blockly.getMainWorkspace());
}, startBlocks);
// Wait for there to be a block on the main workspace before continuing
(await getBlockElementById(this.browser, firstBlockId)).waitForExist({
timeout: 2000,
});
this.firstBlock = await getBlockElementById(this.browser, firstBlockId);
});
test('Delete block using backspace key', async function () {
const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key.
await clickBlock(this.browser, this.firstBlock, {button: 1});
await this.browser.keys([Key.Backspace]);
const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal(
before - 2,
after,
'Expected there to be two fewer blocks after deletion of block and shadow',
);
});
test('Delete block using delete key', async function () {
const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using delete key.
await clickBlock(this.browser, this.firstBlock, {button: 1});
await this.browser.keys([Key.Delete]);
const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal(
before - 2,
after,
'Expected there to be two fewer blocks after deletion of block and shadow',
);
});
test('Delete block using context menu', async function () {
const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using context menu.
await contextMenuSelect(this.browser, this.firstBlock, 'Delete 2 Blocks');
const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal(
before - 2,
after,
'Expected there to be two fewer blocks after deletion of block and shadow',
);
});
test('Undo block deletion', async function () {
const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key.
await clickBlock(this.browser, this.firstBlock, {button: 1});
await this.browser.keys([Key.Backspace]);
await this.browser.pause(PAUSE_TIME);
// Undo
await this.browser.keys([Key.Ctrl, 'z']);
await this.browser.pause(PAUSE_TIME);
const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal(
before,
after,
'Expected there to be the original number of blocks after undoing a delete',
);
});
test('Redo block deletion', async function () {
const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key.
await clickBlock(this.browser, this.firstBlock, {button: 1});
await this.browser.keys([Key.Backspace]);
await this.browser.pause(PAUSE_TIME);
// Undo
await this.browser.keys([Key.Ctrl, 'z']);
await this.browser.pause(PAUSE_TIME);
// Redo
await this.browser.keys([Key.Ctrl, Key.Shift, 'z']);
await this.browser.pause(PAUSE_TIME);
const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal(
before - 2,
after,
'Expected there to be fewer blocks after undoing and redoing a delete',
);
});
});