|
| 1 | +import { Name } from './name.decorator'; |
| 2 | +import { MetadataDescriptor } from '../descriptors/metadata.descriptor'; |
| 3 | + |
| 4 | +describe('Name', () => { |
| 5 | + |
| 6 | + it('should set name on class', () => { |
| 7 | + @Name('Product') |
| 8 | + class Product { |
| 9 | + name!: string; |
| 10 | + } |
| 11 | + |
| 12 | + const descriptor = MetadataDescriptor.for(Product); |
| 13 | + expect(descriptor.name).toEqual('Product'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should set name on property', () => { |
| 17 | + class Product { |
| 18 | + @Name('skuCode') |
| 19 | + sku!: string; |
| 20 | + } |
| 21 | + |
| 22 | + const descriptor = MetadataDescriptor.for(Product, 'sku'); |
| 23 | + expect(descriptor.name).toEqual('skuCode'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should set name on method', () => { |
| 27 | + class ProductService { |
| 28 | + @Name('createProduct') |
| 29 | + create(): void { |
| 30 | + // ... |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + const descriptor = MetadataDescriptor.for(ProductService, 'create'); |
| 35 | + expect(descriptor.name).toEqual('createProduct'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should set name on parameter', () => { |
| 39 | + class ProductService { |
| 40 | + create(@Name('product') product: any): void { |
| 41 | + // ... |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const descriptor = MetadataDescriptor.for(ProductService, 'create', 0); |
| 46 | + expect(descriptor.name).toEqual('product'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should override automatically set name', () => { |
| 50 | + @Name('CustomProduct') |
| 51 | + class Product { |
| 52 | + @Name('customSku') |
| 53 | + sku!: string; |
| 54 | + } |
| 55 | + |
| 56 | + const classDescriptor = MetadataDescriptor.for(Product); |
| 57 | + const propertyDescriptor = MetadataDescriptor.for(Product, 'sku'); |
| 58 | + |
| 59 | + expect(classDescriptor.name).toEqual('CustomProduct'); |
| 60 | + expect(propertyDescriptor.name).toEqual('customSku'); |
| 61 | + }); |
| 62 | + |
| 63 | +}); |
0 commit comments