mirror of
https://github.com/google/blockly.git
synced 2026-01-07 09:00:11 +01:00
feat: added tests/typescript to test supported TS examples (#6775)
* feat: added `tests/typescript` to test supported TS examples * fix: update the test name, description, and output * chore: remove unused imports in `test_tasks.js` * fix: wrap README line at 80 characters * fix: implemented `different_user_input.ts` feedback * fix: correct mistaken comments * chore: rename `./eslintrc.json` to `./eslintrc.js` * feat: added linting for tests/typescript * chore: cleanup eslintrc lines over 80 characters * fix: updated `.eslintrc.js` to provide an override for linting itself * fix: updated tests to build to the `build` directory * feat: updated `gulp format` to handle formatting `.eslintrc.js` * fix: updated `.eslintrc.js` to align with both formatter and linter * fix: updated config comment wording * fix: removed quotes for valid identifiers * Revert "fix: removed quotes for valid identifiers" This reverts commit 03eff91aea1468e503bc79a90fb139914d3f39d2.
This commit is contained in:
committed by
GitHub
parent
8386024e44
commit
13fe6eeccf
4
tests/typescript/README.md
Normal file
4
tests/typescript/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# TypeScript Tests
|
||||
|
||||
Tests pass if all files here compile. Add Blockly use case examples we'd like
|
||||
to support, such as subclasses which extend from Blockly classes.
|
||||
82
tests/typescript/src/field/different_user_input.ts
Normal file
82
tests/typescript/src/field/different_user_input.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test: Should allow a subclass of Field to have different setValue and
|
||||
* constructor input type than the type that is stored.
|
||||
*/
|
||||
|
||||
|
||||
import {Field, FieldValidator, fieldRegistry} from 'blockly-test/core';
|
||||
|
||||
interface Cell {
|
||||
cellId: string;
|
||||
}
|
||||
|
||||
interface CellGroup {
|
||||
cells: Cell[];
|
||||
}
|
||||
|
||||
type FieldMitosisValidator = FieldValidator<CellGroup>;
|
||||
|
||||
class FieldMitosis extends Field<CellGroup> {
|
||||
constructor(cell: Cell, validator: FieldMitosisValidator) {
|
||||
super(Field.SKIP_SETUP);
|
||||
|
||||
this.setValue(cell);
|
||||
this.setValidator(validator);
|
||||
}
|
||||
|
||||
// Overwritten Field methods.
|
||||
|
||||
protected doClassValidation_(newCell?: unknown): CellGroup | null {
|
||||
if (!this.isCell(newCell)) return null;
|
||||
|
||||
const cellGroup = this.getValue() ?? {cells: []};
|
||||
cellGroup.cells.push(newCell);
|
||||
return cellGroup;
|
||||
}
|
||||
|
||||
// Example-specific methods.
|
||||
|
||||
private isCell(maybeCell: unknown): maybeCell is Cell {
|
||||
if (!maybeCell) return false;
|
||||
const couldBeCell = maybeCell as { [key: string]: unknown };
|
||||
return 'cellId' in couldBeCell && typeof couldBeCell.cellId === 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* The cell divides, creating two new cells!
|
||||
*/
|
||||
doMitosis(): void {
|
||||
const cellGroup = this.getValue();
|
||||
if (!cellGroup) return;
|
||||
|
||||
const cells = cellGroup.cells.flatMap((cell) => {
|
||||
const leftCell: Cell = {cellId: `${cell.cellId}-left`};
|
||||
const rightCell: Cell = {cellId: `${cell.cellId}-right`};
|
||||
return [leftCell, rightCell];
|
||||
});
|
||||
this.value_ = {cells};
|
||||
}
|
||||
}
|
||||
|
||||
fieldRegistry.register('field_mitosis', FieldMitosis);
|
||||
|
||||
// Example use of the class.
|
||||
|
||||
function cellValidator(cellGroup: CellGroup): CellGroup | undefined {
|
||||
// The cell group is good! Use it as is.
|
||||
if (cellGroup.cells.length > 0) return undefined;
|
||||
|
||||
// Uh oh! No cells.
|
||||
const emergencyCell: Cell = {cellId: 'emergency-cell'};
|
||||
return {cells: [emergencyCell]};
|
||||
}
|
||||
|
||||
const cellField = new FieldMitosis({cellId: 'cell-A'}, cellValidator);
|
||||
cellField.setValue({cellId: 'cell-B'});
|
||||
cellField.doMitosis();
|
||||
22
tests/typescript/tsconfig.json
Normal file
22
tests/typescript/tsconfig.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"include": [
|
||||
"src/**/*",
|
||||
],
|
||||
"compilerOptions": {
|
||||
"allowJs": false,
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"blockly-test/*": ["../../dist/*"],
|
||||
},
|
||||
|
||||
"declaration": false,
|
||||
"declarationMap": false,
|
||||
"sourceMap": false,
|
||||
|
||||
"module": "ES2015",
|
||||
"moduleResolution": "node",
|
||||
"target": "ES2020",
|
||||
"strict": true,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user