diff --git a/src/__tests__/unit/deco.test.tsx b/src/__tests__/unit/deco.test.tsx new file mode 100644 index 00000000..6b87fea7 --- /dev/null +++ b/src/__tests__/unit/deco.test.tsx @@ -0,0 +1,49 @@ +import { describe, it, expect } from "vitest"; + +function logAround(_target: any, key: string, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value; + + descriptor.value = function (...args: any[]) { + console.log(`Before calling ${key}`); + const result = originalMethod.apply(this, args); + console.log(`After calling ${key}`); + return result; + }; + + return descriptor; +} + +function addOne(_target: any, _key: string, descriptor: PropertyDescriptor) { + const originalMethod = descriptor.value; + + descriptor.value = function (...args: any[]) { + const result = originalMethod.apply(this, args); + return result + 1; + }; + + return descriptor; +} + +export class TestClass { + @logAround + hello() { + console.log("Hello, world!"); + } + + @addOne + add(a: number, b: number) { + return a + b; + } +} + +describe("deco.test.tsx", () => { + // it("should pass", () => { + // const testClass = new TestClass(); + // testClass.hello(); + // }); + it("should add one", () => { + const testClass = new TestClass(); + const result = testClass.add(2, 3); + expect(result).toBe(6); + }); +});