Skip to content

Commit

Permalink
renames & 2nd phase todos (#8)
Browse files Browse the repository at this point in the history
* refactor: renames and add second phase todos

* fix: add 'unknown' overlap to property dto

Co-authored-by: Idanpt <idan.ptichi@naturalint.com>
Co-authored-by: Omer Morad <omer.moradd@gmail.com>
  • Loading branch information
3 people committed Jan 25, 2021
1 parent 125c70a commit cd02670
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/class-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,7 +15,7 @@ import FakerStatic = Faker.FakerStatic;
export class ClassProcessor<T> implements IClassProcessor<T> {
private static readonly VALUE_INSPECTORS: Class<ValueHandler<FixtureOptions>>[] = [
EnumValueHandler,
MultiClassValueHandler,
ArrayValueHandler,
SingleClassValueHandler,
CallbackValueHandler,
ObjectLiteralValueHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MultiClass>;
let handler: ArrayValueHandler<MultiClass>;

const dto: PropertyDto<MultiClass> = {
type: 'object',
Expand All @@ -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 })", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import { PrimitiveHandlerAbstract } from './primitive-handler-abstract';

import FakerStatic = Faker.FakerStatic;

export class MultiClassValueHandler<P extends MultiClass>
extends PrimitiveHandlerAbstract<P>
implements ValueHandler<P> {
// TODO: refactor (2nd phase). All other fixture options should be wrapped with 'multiple' functionality
export class ArrayValueHandler<P extends MultiClass> extends PrimitiveHandlerAbstract<P> implements ValueHandler<P> {
public constructor(protected readonly faker: FakerStatic, protected readonly classProcessor: ClassProcessor<Class>) {
super(faker);
}

public static isTypeValue(propertyDto: PropertyDto<any>): boolean {
public static hasTypeKey(propertyDto: PropertyDto<MultiClass>): boolean {
const { value } = propertyDto;

return Object.prototype.hasOwnProperty.call(value, 'type');
}

public shouldHandle(propertyDto: PropertyDto<P>): boolean {
return propertyDto.type === 'object' && MultiClassValueHandler.isTypeValue(propertyDto);
return propertyDto.type === 'object' && ArrayValueHandler.hasTypeKey(propertyDto);
}

public produceValue<T>(propertyDto: PropertyDto<P>): any {
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/object-literal-value-handler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,7 +8,7 @@ export class ObjectLiteralValueHandler<P extends ObjectLiteral> implements Value
public shouldHandle(propertyDto: PropertyDto<P>): boolean {
return (
propertyDto.type === 'object' &&
!MultiClassValueHandler.isTypeValue((propertyDto as unknown) as PropertyDto<MultiClass>) &&
!ArrayValueHandler.hasTypeKey((propertyDto as unknown) as PropertyDto<MultiClass>) &&
!EnumValueHandler.isEnumValue((propertyDto as unknown) as PropertyDto<EnumObject>)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/primitive-value-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +21,7 @@ export class PrimitiveValueHandler<P extends ExactValue>
const { value } = propertyDto;

if (typeof value !== 'undefined') {
if (MultiClassValueHandler.isTypeValue((propertyDto as unknown) as PropertyDto<MultiClass>)) {
if (ArrayValueHandler.hasTypeKey((propertyDto as unknown) as PropertyDto<MultiClass>)) {
throw new Error(
'Type mismatch. Properties decorated with @Fixture({ type: ClassType }) must be typed as array (e.g. prop: string[])'
);
Expand Down
1 change: 1 addition & 0 deletions src/types/property-dto.interface.ts
Original file line number Diff line number Diff line change
@@ -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<T extends FixtureOptions> {
value: T;
name: string;
Expand Down

0 comments on commit cd02670

Please sign in to comment.