From cd02670db442eb8a29704c698aca79e651be8e7c Mon Sep 17 00:00:00 2001 From: idanpt Date: Mon, 25 Jan 2021 14:58:55 +0200 Subject: [PATCH] renames & 2nd phase todos (#8) * refactor: renames and add second phase todos * fix: add 'unknown' overlap to property dto Co-authored-by: Idanpt Co-authored-by: Omer Morad --- src/class-processor.ts | 4 ++-- ...lue-handler.test.ts => array-value-handler.test.ts} | 10 +++++----- ...i-class-value-handler.ts => array-value-handler.ts} | 9 ++++----- src/handlers/object-literal-value-handler.ts | 4 ++-- src/handlers/primitive-value-handler.ts | 4 ++-- src/types/property-dto.interface.ts | 1 + 6 files changed, 16 insertions(+), 16 deletions(-) rename src/handlers/{multi-class-value-handler.test.ts => array-value-handler.test.ts} (92%) rename src/handlers/{multi-class-value-handler.ts => array-value-handler.ts} (78%) diff --git a/src/class-processor.ts b/src/class-processor.ts index 9eb2f94..70464ad 100644 --- a/src/class-processor.ts +++ b/src/class-processor.ts @@ -2,7 +2,7 @@ import { ClassReflector } from './class-reflector'; import { CallbackValueHandler } from './handlers/callback-value-handler'; import { ObjectLiteralValueHandler } from './handlers/object-literal-value-handler'; import { EnumValueHandler } from './handlers/enum-value-handler'; -import { MultiClassValueHandler } from './handlers/multi-class-value-handler'; +import { ArrayValueHandler } from './handlers/array-value-handler'; import { SingleClassValueHandler } from './handlers/single-class-value-handler'; import { PrimitiveValueHandler } from './handlers/primitive-value-handler'; import { ClassLiteral, Class, FixtureOptions } from './types/fixture-options.type'; @@ -15,7 +15,7 @@ import FakerStatic = Faker.FakerStatic; export class ClassProcessor implements IClassProcessor { private static readonly VALUE_INSPECTORS: Class>[] = [ EnumValueHandler, - MultiClassValueHandler, + ArrayValueHandler, SingleClassValueHandler, CallbackValueHandler, ObjectLiteralValueHandler, diff --git a/src/handlers/multi-class-value-handler.test.ts b/src/handlers/array-value-handler.test.ts similarity index 92% rename from src/handlers/multi-class-value-handler.test.ts rename to src/handlers/array-value-handler.test.ts index 8fda3e1..3fe5411 100644 --- a/src/handlers/multi-class-value-handler.test.ts +++ b/src/handlers/array-value-handler.test.ts @@ -1,15 +1,15 @@ import { PropertyDto } from '../types/property-dto.interface'; -import { MultiClassValueHandler } from '../handlers/multi-class-value-handler'; +import { ArrayValueHandler } from './array-value-handler'; import { ClassProcessor } from '../class-processor'; import { MultiClass } from '../types/fixture-options.type'; import FakerStatic = Faker.FakerStatic; -describe('MultiClassValueHandler Unit', () => { +describe('ArrayValueHandler Unit', () => { const DTO_CLASS_VALUE = class TestClass {}; const DEFAULT_COUNT_FOR_DTO = 3; - let handler: MultiClassValueHandler; + let handler: ArrayValueHandler; const dto: PropertyDto = { type: 'object', @@ -34,9 +34,9 @@ describe('MultiClassValueHandler Unit', () => { }, } as unknown) as FakerStatic; - describe('given a MultiClassValueHandler', () => { + describe('given a ArrayValueHandler', () => { beforeAll(() => { - handler = new MultiClassValueHandler(fakerMock, classProcessorMock); + handler = new ArrayValueHandler(fakerMock, classProcessorMock); }); describe("when calling 'shouldHandle' with type 'object' and value of multi class ({ type: ClassType })", () => { diff --git a/src/handlers/multi-class-value-handler.ts b/src/handlers/array-value-handler.ts similarity index 78% rename from src/handlers/multi-class-value-handler.ts rename to src/handlers/array-value-handler.ts index 3f1e42f..7bea16b 100644 --- a/src/handlers/multi-class-value-handler.ts +++ b/src/handlers/array-value-handler.ts @@ -7,21 +7,20 @@ import { PrimitiveHandlerAbstract } from './primitive-handler-abstract'; import FakerStatic = Faker.FakerStatic; -export class MultiClassValueHandler

- extends PrimitiveHandlerAbstract

- implements ValueHandler

{ +// TODO: refactor (2nd phase). All other fixture options should be wrapped with 'multiple' functionality +export class ArrayValueHandler

extends PrimitiveHandlerAbstract

implements ValueHandler

{ public constructor(protected readonly faker: FakerStatic, protected readonly classProcessor: ClassProcessor) { super(faker); } - public static isTypeValue(propertyDto: PropertyDto): boolean { + public static hasTypeKey(propertyDto: PropertyDto): boolean { const { value } = propertyDto; return Object.prototype.hasOwnProperty.call(value, 'type'); } public shouldHandle(propertyDto: PropertyDto

): boolean { - return propertyDto.type === 'object' && MultiClassValueHandler.isTypeValue(propertyDto); + return propertyDto.type === 'object' && ArrayValueHandler.hasTypeKey(propertyDto); } public produceValue(propertyDto: PropertyDto

): any { diff --git a/src/handlers/object-literal-value-handler.ts b/src/handlers/object-literal-value-handler.ts index c653960..ede99b7 100644 --- a/src/handlers/object-literal-value-handler.ts +++ b/src/handlers/object-literal-value-handler.ts @@ -1,4 +1,4 @@ -import { MultiClassValueHandler } from './multi-class-value-handler'; +import { ArrayValueHandler } from './array-value-handler'; import { EnumValueHandler } from './enum-value-handler'; import { ValueHandler } from '../types/value-handler.interface'; import { PropertyDto } from '../types/property-dto.interface'; @@ -8,7 +8,7 @@ export class ObjectLiteralValueHandler

implements Value public shouldHandle(propertyDto: PropertyDto

): boolean { return ( propertyDto.type === 'object' && - !MultiClassValueHandler.isTypeValue((propertyDto as unknown) as PropertyDto) && + !ArrayValueHandler.hasTypeKey((propertyDto as unknown) as PropertyDto) && !EnumValueHandler.isEnumValue((propertyDto as unknown) as PropertyDto) ); } diff --git a/src/handlers/primitive-value-handler.ts b/src/handlers/primitive-value-handler.ts index e7a90a9..3f461e6 100644 --- a/src/handlers/primitive-value-handler.ts +++ b/src/handlers/primitive-value-handler.ts @@ -2,7 +2,7 @@ import { PrimitiveHandlerAbstract } from './primitive-handler-abstract'; import { ValueHandler } from '../types/value-handler.interface'; import { PropertyDto } from '../types/property-dto.interface'; import { ExactValue, MultiClass } from '../types/fixture-options.type'; -import { MultiClassValueHandler } from '../handlers/multi-class-value-handler'; +import { ArrayValueHandler } from './array-value-handler'; import FakerStatic = Faker.FakerStatic; @@ -21,7 +21,7 @@ export class PrimitiveValueHandler

const { value } = propertyDto; if (typeof value !== 'undefined') { - if (MultiClassValueHandler.isTypeValue((propertyDto as unknown) as PropertyDto)) { + if (ArrayValueHandler.hasTypeKey((propertyDto as unknown) as PropertyDto)) { throw new Error( 'Type mismatch. Properties decorated with @Fixture({ type: ClassType }) must be typed as array (e.g. prop: string[])' ); diff --git a/src/types/property-dto.interface.ts b/src/types/property-dto.interface.ts index b5d5e06..6d4bb58 100644 --- a/src/types/property-dto.interface.ts +++ b/src/types/property-dto.interface.ts @@ -1,6 +1,7 @@ import { FixtureOptions } from './fixture-options.type'; // TODO: Make generic +// TODO: refactor (2nd phase). change to class, which will contain all relevant methods which are now speared as static functions in different classes export interface PropertyDto { value: T; name: string;