|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { describe, it } from 'node:test' |
| 4 | +import ts from 'typescript' |
| 5 | + |
| 6 | +const sourceFile = ts.createSourceFile( |
| 7 | + 'QuillEditor.ts', |
| 8 | + readFileSync(new URL('./QuillEditor.ts', import.meta.url), 'utf8'), |
| 9 | + ts.ScriptTarget.Latest, |
| 10 | + true, |
| 11 | + ts.ScriptKind.TS, |
| 12 | +) |
| 13 | + |
| 14 | +const getPropertyName = (node: ts.PropertyName): string | undefined => { |
| 15 | + if (ts.isIdentifier(node) || ts.isStringLiteral(node)) return node.text |
| 16 | + return undefined |
| 17 | +} |
| 18 | + |
| 19 | +const findToolbarProp = (): ts.ObjectLiteralExpression => { |
| 20 | + let toolbarProp: ts.ObjectLiteralExpression | undefined |
| 21 | + |
| 22 | + const visit = (node: ts.Node) => { |
| 23 | + if ( |
| 24 | + ts.isPropertyAssignment(node) && |
| 25 | + getPropertyName(node.name) === 'toolbar' && |
| 26 | + ts.isObjectLiteralExpression(node.initializer) |
| 27 | + ) { |
| 28 | + toolbarProp = node.initializer |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + ts.forEachChild(node, visit) |
| 33 | + } |
| 34 | + |
| 35 | + visit(sourceFile) |
| 36 | + |
| 37 | + assert.ok(toolbarProp, 'toolbar prop should be defined') |
| 38 | + return toolbarProp |
| 39 | +} |
| 40 | + |
| 41 | +describe('QuillEditor toolbar prop', () => { |
| 42 | + it('accepts false to disable the toolbar', () => { |
| 43 | + const typeProperty = findToolbarProp().properties.find( |
| 44 | + (property): property is ts.PropertyAssignment => |
| 45 | + ts.isPropertyAssignment(property) && |
| 46 | + getPropertyName(property.name) === 'type', |
| 47 | + ) |
| 48 | + |
| 49 | + assert.ok(typeProperty, 'toolbar prop should define runtime types') |
| 50 | + assert.match( |
| 51 | + typeProperty.initializer.getText(sourceFile), |
| 52 | + /\bBoolean\b/, |
| 53 | + 'toolbar prop runtime types should include Boolean', |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it('keeps the absent toolbar prop distinct from false', () => { |
| 58 | + const defaultProperty = findToolbarProp().properties.find( |
| 59 | + (property): property is ts.PropertyAssignment => |
| 60 | + ts.isPropertyAssignment(property) && |
| 61 | + getPropertyName(property.name) === 'default', |
| 62 | + ) |
| 63 | + |
| 64 | + assert.ok(defaultProperty, 'toolbar prop should define an explicit default') |
| 65 | + assert.equal( |
| 66 | + defaultProperty.initializer.getText(sourceFile), |
| 67 | + 'undefined', |
| 68 | + 'toolbar prop should not cast an absent prop to false', |
| 69 | + ) |
| 70 | + }) |
| 71 | +}) |
0 commit comments