|
| 1 | +/** |
| 2 | + * Quick inline test to verify TC39 decorators work correctly. |
| 3 | + * Run with: npx ts-node --esm --project tsconfig.test.json packages/dev/core/test/unit/Decorators/decorators.inline-test.ts |
| 4 | + */ |
| 5 | + |
| 6 | +// Test 1: Symbol.metadata polyfill |
| 7 | +(Symbol as any).metadata ??= Symbol.for("Symbol.metadata"); |
| 8 | + |
| 9 | +// Test 2: A simple TC39 class field decorator |
| 10 | +function trackField(displayName: string) { |
| 11 | + return function <This, V>(_value: undefined, context: ClassFieldDecoratorContext<This, V>) { |
| 12 | + const key = `__tracked_${String(context.name)}`; |
| 13 | + if (!Object.hasOwn(context.metadata, key)) { |
| 14 | + (context.metadata as any)[key] = displayName; |
| 15 | + } |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +// Test 3: A TC39 accessor decorator (like expandToProperty) |
| 20 | +function doubleAccessor<This, V extends number>( |
| 21 | + _value: ClassAccessorDecoratorTarget<This, V>, |
| 22 | + _context: ClassAccessorDecoratorContext<This, V> |
| 23 | +): ClassAccessorDecoratorResult<This, V> { |
| 24 | + return { |
| 25 | + get(this: This): V { |
| 26 | + return (_value.get.call(this) * 2) as V; |
| 27 | + }, |
| 28 | + set(this: This, value: V) { |
| 29 | + _value.set.call(this, value); |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +class TestClass { |
| 35 | + @trackField("My Number") |
| 36 | + myNum: number = 42; |
| 37 | + |
| 38 | + @doubleAccessor |
| 39 | + accessor doubled: number = 5; |
| 40 | +} |
| 41 | + |
| 42 | +// Verify metadata |
| 43 | +const metadata = (TestClass as any)[Symbol.metadata]; |
| 44 | +console.assert(metadata !== undefined, "Symbol.metadata should be set on class"); |
| 45 | +console.assert((metadata as any).__tracked_myNum === "My Number", "Field decorator should store metadata"); |
| 46 | + |
| 47 | +// Verify accessor |
| 48 | +const instance = new TestClass(); |
| 49 | +console.assert(instance.doubled === 10, `Accessor should return doubled value, got ${instance.doubled}`); |
| 50 | +instance.doubled = 7; |
| 51 | +console.assert(instance.doubled === 14, `After setting 7, accessor should return 14, got ${instance.doubled}`); |
| 52 | + |
| 53 | +// Test 4: Inheritance |
| 54 | +class ChildClass extends TestClass { |
| 55 | + @trackField("Child Prop") |
| 56 | + childProp: string = "hello"; |
| 57 | +} |
| 58 | + |
| 59 | +const childMeta = (ChildClass as any)[Symbol.metadata]; |
| 60 | +console.assert(childMeta !== undefined, "Child should have metadata"); |
| 61 | +console.assert((childMeta as any).__tracked_childProp === "Child Prop", "Child metadata should have its own properties"); |
| 62 | + |
| 63 | +// Walk the prototype chain |
| 64 | +const parentMeta = Object.getPrototypeOf(childMeta); |
| 65 | +console.assert(parentMeta !== null, "Child metadata prototype should be parent metadata"); |
| 66 | +console.assert((parentMeta as any).__tracked_myNum === "My Number", "Parent metadata should be accessible via prototype chain"); |
| 67 | + |
| 68 | +console.log("All decorator tests passed!"); |
0 commit comments