|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
1 | 2 | import 'reflect-metadata' |
| 3 | +import faker from '@faker-js/faker' |
| 4 | +import {PropertyDecoratorConfig, ClassDecoratorConfig} from './utils/types-helper' |
2 | 5 |
|
3 | | -@Reflect.metadata('type', 'class') |
4 | | -class A { |
5 | | - constructor(public name: string, public age: number) {} |
6 | | - @Reflect.metadata(undefined, undefined) |
7 | | - method(): boolean { |
8 | | - return true |
| 6 | +const METADATA_KEY_MOCK = 'mock' |
| 7 | + |
| 8 | +function FakerDecorator<T extends (...args: any[]) => any>( |
| 9 | + fakerFn: T, |
| 10 | + ...fakerParams: Parameters<T> |
| 11 | +): PropertyDecorator & { |
| 12 | + config(config?: PropertyDecoratorConfig): PropertyDecorator |
| 13 | +} { |
| 14 | + function decorator(target: Object, propertyKey: string | symbol, config: PropertyDecoratorConfig = {}) { |
| 15 | + const metadata: { |
| 16 | + fakerFn: T |
| 17 | + fakerParams: Parameters<T> |
| 18 | + config: PropertyDecoratorConfig |
| 19 | + } = { |
| 20 | + fakerFn, |
| 21 | + fakerParams, |
| 22 | + config |
| 23 | + } |
| 24 | + let classMetadata = Reflect.getOwnMetadata(METADATA_KEY_MOCK, target) |
| 25 | + if (!classMetadata) classMetadata = {} |
| 26 | + if (!classMetadata.property) classMetadata.property = {} |
| 27 | + classMetadata.property[propertyKey] = metadata |
| 28 | + Reflect.defineMetadata(METADATA_KEY_MOCK, classMetadata, target) |
| 29 | + Reflect.defineMetadata(propertyKey, metadata, target, propertyKey) |
9 | 30 | } |
| 31 | + |
| 32 | + decorator.config = (config: PropertyDecoratorConfig) => (target: Object, propertyKey: string | symbol) => |
| 33 | + decorator(target, propertyKey, config) |
| 34 | + |
| 35 | + return decorator |
10 | 36 | } |
11 | 37 |
|
12 | | -const t1 = Reflect.getMetadata('design:paramtypes', A) |
13 | | -const t2 = Reflect.getMetadata('design:returntype', A.prototype, 'method') |
14 | | -const t3 = Reflect.getMetadata('design:type', A.prototype, 'method') |
| 38 | +function Mock(config: ClassDecoratorConfig = {}): ClassDecorator { |
| 39 | + function decorator<TFunction extends Function>(target: TFunction): TFunction | void { |
| 40 | + const metadata: { |
| 41 | + config: PropertyDecoratorConfig |
| 42 | + } = { |
| 43 | + config |
| 44 | + } |
| 45 | + Reflect.defineMetadata(METADATA_KEY_MOCK, metadata, target) |
| 46 | + } |
| 47 | + return decorator |
| 48 | +} |
15 | 49 |
|
16 | | -console.log(...t1, t2, t3) |
| 50 | +interface MockClass<T = any> { |
| 51 | + new (...args: any[]): T |
| 52 | +} |
| 53 | + |
| 54 | +function createMock<T = any>(target: MockClass<T>): T { |
| 55 | + const classMetadata = Reflect.getOwnMetadata(METADATA_KEY_MOCK, target) |
| 56 | + const metadataKeys = Reflect.getMetadataKeys(target) |
| 57 | + |
| 58 | + return '' as any as T |
| 59 | +} |
| 60 | + |
| 61 | +@Mock() |
| 62 | +class User { |
| 63 | + @FakerDecorator(faker.random.number, {min: 0, max: 100}).config({ |
| 64 | + alwaysRandom: true |
| 65 | + }) |
| 66 | + age!: number |
| 67 | +} |
0 commit comments