Skip to content

Commit

Permalink
✅ 装饰器测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefean committed Jan 25, 2025
1 parent 1f09dec commit 814080c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/__tests__/unit/deco.test.tsx
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 814080c

Please sign in to comment.