Files
blockly/tests/mocha/blocks/lists_test.js
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

197 lines
5.2 KiB
JavaScript

/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../../node_modules/chai/chai.js';
import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from '../test_helpers/setup_teardown.js';
import {ConnectionType} from '../../../build/src/core/connection_type.js';
import {defineStatementBlock} from '../test_helpers/block_definitions.js';
suite('Lists', function () {
setup(function () {
sharedTestSetup.call(this);
defineStatementBlock();
this.workspace = new Blockly.Workspace();
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('ListsGetIndex', function () {
/**
* Test cases for serialization tests.
* @type {Array<SerializationTestCase>}
*/
const testCases = [
{
title: 'JSON not requiring mutations',
json: {
type: 'lists_getIndex',
id: '1',
fields: {MODE: 'GET', WHERE: 'FIRST'},
},
assertBlockStructure: (block) => {
assert.equal(block.type, 'lists_getIndex');
assert.exists(block.outputConnection);
},
},
{
title: 'JSON requiring mutations',
json: {
type: 'lists_getIndex',
id: '1',
extraState: {isStatement: true},
fields: {MODE: 'REMOVE', WHERE: 'FROM_START'},
},
assertBlockStructure: (block) => {
assert.equal(block.type, 'lists_getIndex');
assert.isNotTrue(block.outputConnection);
assert.isTrue(
block.getInput('AT').type === ConnectionType.INPUT_VALUE,
);
},
},
{
title:
'JSON requiring mutations and extra state for previous connection',
json: {
type: 'statement_block',
id: '1',
next: {
block: {
type: 'lists_getIndex',
id: '2',
extraState: {isStatement: true},
fields: {MODE: 'REMOVE', WHERE: 'FROM_START'},
},
},
},
assertBlockStructure: (block) => {},
},
{
title: 'JSON requiring mutations with XML extra state',
json: {
type: 'statement_block',
id: '1',
next: {
block: {
type: 'lists_getIndex',
id: '2',
extraState: '<mutation statement="true" at="true"></mutation>',
fields: {MODE: 'REMOVE', WHERE: 'FROM_START'},
},
},
},
expectedJson: {
type: 'statement_block',
id: '1',
next: {
block: {
type: 'lists_getIndex',
id: '2',
extraState: {isStatement: true},
fields: {MODE: 'REMOVE', WHERE: 'FROM_START'},
},
},
},
assertBlockStructure: (block) => {},
},
];
runSerializationTestSuite(testCases);
});
/**
* Test cases for serialization where JSON hooks should have null
* implementation to avoid serializing xml mutations in json.
* @param {!Object} serializedJson basic serialized json
* @param {!string} xmlMutation xml mutation that should be ignored/not reserialized in round trip
* @return {Array<SerializationTestCase>} test cases
*/
function makeTestCasesForBlockNotNeedingExtraState_(
serializedJson,
xmlMutation,
) {
return [
{
title: 'JSON not requiring mutations',
json: serializedJson,
assertBlockStructure: (block) => {
assert.equal(block.type, serializedJson.type);
},
},
{
title: 'JSON with XML extra state',
json: {
...serializedJson,
'extraState': xmlMutation,
},
expectedJson: serializedJson,
assertBlockStructure: (block) => {},
},
];
}
suite('ListsSetIndex', function () {
/**
* Test cases for serialization tests.
* @type {Array<SerializationTestCase>}
*/
const testCases = makeTestCasesForBlockNotNeedingExtraState_(
{
'type': 'lists_setIndex',
'id': '1',
'fields': {
'MODE': 'SET',
'WHERE': 'FROM_START',
},
},
'<mutation at="true"></mutation>',
);
runSerializationTestSuite(testCases);
});
suite('ListsGetSubList', function () {
/**
* Test cases for serialization tests.
* @type {Array<SerializationTestCase>}
*/
const testCases = makeTestCasesForBlockNotNeedingExtraState_(
{
'type': 'lists_getSublist',
'id': '1',
'fields': {
'WHERE1': 'FROM_START',
'WHERE2': 'FROM_START',
},
},
'<mutation at1="true" at2="true"></mutation>',
);
runSerializationTestSuite(testCases);
});
suite('ListsSplit', function () {
/**
* Test cases for serialization tests.
* @type {Array<SerializationTestCase>}
*/
const testCases = makeTestCasesForBlockNotNeedingExtraState_(
{
'type': 'lists_split',
'id': '1',
'fields': {
'MODE': 'SPLIT',
},
},
'<mutation mode="SPLIT"></mutation>',
);
runSerializationTestSuite(testCases);
});
});