Files
blockly/tests/mocha/registry_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

282 lines
7.8 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import {assertWarnings} from './test_helpers/warnings.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Registry', function () {
const TestClass = function () {};
TestClass.prototype.testMethod = function () {
return 'something';
};
setup(function () {
sharedTestSetup.call(this);
});
teardown(function () {
sharedTestTeardown.call(this);
if (Blockly.registry.hasItem('test', 'test_name')) {
Blockly.registry.unregister('test', 'test_name');
}
});
suite('Registration', function () {
test('Simple', function () {
Blockly.registry.register('test', 'test_name', TestClass);
});
test('Empty String Key', function () {
assert.throws(function () {
Blockly.registry.register('test', '', TestClass);
}, 'Invalid name');
});
test('Class as Key', function () {
assert.throws(function () {
Blockly.registry.register('test', TestClass, '');
}, 'Invalid name');
});
test('Overwrite a Key', function () {
Blockly.registry.register('test', 'test_name', TestClass);
assert.throws(function () {
// Registers a different object under the same name
Blockly.registry.register('test', 'test_name', {});
}, 'already registered');
});
test('Register a Duplicate Item', function () {
Blockly.registry.register('test', 'test_name', TestClass);
assert.doesNotThrow(function () {
// Registering the same object under the same name is allowed
Blockly.registry.register('test', 'test_name', TestClass);
}, 'already registered');
});
test('Null Value', function () {
assert.throws(function () {
Blockly.registry.register('test', 'field_custom_test', null);
}, 'Can not register a null value');
});
});
suite('hasItem', function () {
setup(function () {
Blockly.registry.register('test', 'test_name', TestClass);
});
test('Has', function () {
assert.isTrue(Blockly.registry.hasItem('test', 'test_name'));
});
suite('Does not have', function () {
test('Type', function () {
assert.isFalse(Blockly.registry.hasItem('bad_type', 'test_name'));
});
test('Name', function () {
assert.isFalse(Blockly.registry.hasItem('test', 'bad_name'));
});
});
suite('Case', function () {
test('Caseless type', function () {
assert.isTrue(Blockly.registry.hasItem('TEST', 'test_name'));
});
test('Caseless name', function () {
assert.isTrue(Blockly.registry.hasItem('test', 'TEST_NAME'));
});
});
});
suite('getClass', function () {
setup(function () {
Blockly.registry.register('test', 'test_name', TestClass);
});
test('Has', function () {
assert.isNotNull(Blockly.registry.getClass('test', 'test_name'));
});
suite('Does not have', function () {
test('Type', function () {
assertWarnings(() => {
assert.isNull(Blockly.registry.getClass('bad_type', 'test_name'));
}, /Unable to find/);
});
test('Name', function () {
assertWarnings(() => {
assert.isNull(Blockly.registry.getClass('test', 'bad_name'));
}, /Unable to find/);
});
test('Throw if missing', function () {
assert.throws(function () {
Blockly.registry.getClass('test', 'bad_name', true);
});
});
});
suite('Case', function () {
test('Caseless type', function () {
assert.isNotNull(Blockly.registry.getClass('TEST', 'test_name'));
});
test('Caseless name', function () {
assert.isNotNull(Blockly.registry.getClass('test', 'TEST_NAME'));
});
});
});
suite('getObject', function () {
setup(function () {
Blockly.registry.register('test', 'test_name', {});
});
test('Has', function () {
assert.isNotNull(Blockly.registry.getObject('test', 'test_name'));
});
suite('Does not have', function () {
test('Type', function () {
assertWarnings(() => {
assert.isNull(Blockly.registry.getObject('bad_type', 'test_name'));
}, /Unable to find/);
});
test('Name', function () {
assertWarnings(() => {
assert.isNull(Blockly.registry.getObject('test', 'bad_name'));
}, /Unable to find/);
});
test('Throw if missing', function () {
assert.throws(function () {
Blockly.registry.getObject('test', 'bad_name', true);
});
});
});
suite('Case', function () {
test('Caseless type', function () {
assert.isNotNull(Blockly.registry.getObject('TEST', 'test_name'));
});
test('Caseless name', function () {
assert.isNotNull(Blockly.registry.getObject('test', 'TEST_NAME'));
});
});
});
suite('getAllItems', function () {
setup(function () {
Blockly.registry.register('test', 'test_name', {});
Blockly.registry.register('test', 'casedNAME', {});
});
teardown(function () {
Blockly.registry.unregister('test', 'casedname');
});
test('Has', function () {
assert.isNotNull(Blockly.registry.getAllItems('test'));
});
test('Does not have', function () {
assertWarnings(() => {
assert.isNull(Blockly.registry.getAllItems('bad_type'));
}, /Unable to find/);
});
test('Throw if missing', function () {
assert.throws(function () {
Blockly.registry.getAllItems('bad_type', false, true);
});
});
test('Ignore type case', function () {
assert.isNotNull(Blockly.registry.getAllItems('TEST'));
});
test('Respect name case', function () {
assert.deepEqual(Blockly.registry.getAllItems('test', true), {
'test_name': {},
'casedNAME': {},
});
});
test('Respect overwriting name case', function () {
Blockly.registry.register('test', 'CASEDname', {}, true);
assert.deepEqual(Blockly.registry.getAllItems('test', true), {
'test_name': {},
'CASEDname': {},
});
});
});
suite('getClassFromOptions', function () {
setup(function () {
this.defaultClass = function () {};
this.defaultClass.prototype.testMethod = function () {
return 'default';
};
this.options = {
'plugins': {
'test': 'test_name',
},
};
Blockly.registry.register('test', 'test_name', TestClass);
Blockly.registry.register('test', 'default', this.defaultClass);
});
teardown(function () {
Blockly.registry.unregister('test', 'default');
});
test('Simple - Plugin name given', function () {
const testClass = Blockly.registry.getClassFromOptions(
'test',
this.options,
);
assert.instanceOf(new testClass(), TestClass);
});
test('Simple - Plugin class given', function () {
this.options.plugins['test'] = TestClass;
const testClass = Blockly.registry.getClassFromOptions(
'test',
this.options,
);
assert.instanceOf(new testClass(), TestClass);
});
test('No Plugin Name Given', function () {
delete this.options['plugins']['test'];
const testClass = Blockly.registry.getClassFromOptions(
'test',
this.options,
);
assert.instanceOf(new testClass(), this.defaultClass);
});
test('Incorrect Plugin Name', function () {
this.options['plugins']['test'] = 'random';
let testClass;
assertWarnings(() => {
testClass = Blockly.registry.getClassFromOptions('test', this.options);
}, /Unable to find/);
assert.isNull(testClass);
});
});
});