Fixed issues that caused the factory creation to fail for SimpleProduct types

This commit is contained in:
Christopher Allford 2020-11-09 12:02:01 -08:00
parent d205d6551e
commit 11c7e3bff6
6 changed files with 50 additions and 33 deletions

View File

@ -82,7 +82,7 @@ export class ModelTransformer< T extends Model > {
*/
public fromModel( model: Partial< T > ): any {
// Convert the model class to raw properties so that the transformations can be simple.
const raw = JSON.parse( JSON.stringify( model ) );
const raw = Object.assign( {}, model );
return this.transformations.reduce(
( properties: any, transformer: ModelTransformation ) => {

View File

@ -66,16 +66,6 @@ describe( 'PropertyTypeTransformation', () => {
expect( transformed.date ).toStrictEqual( '2020-11-06T03:11:41.000Z' );
} );
it( 'should convert empty dates', () => {
let transformed = transformation.toModel( { date: '' } );
expect( transformed.date ).toStrictEqual( null );
transformed = transformation.fromModel( { date: null } );
expect( transformed.date ).toStrictEqual( '' );
} );
it( 'should use conversion callbacks', () => {
let transformed = transformation.toModel( { callback: 'Test' } );
@ -95,4 +85,24 @@ describe( 'PropertyTypeTransformation', () => {
expect( transformed.integer ).toStrictEqual( [ '100', '200', '300' ] );
} );
it( 'should do nothing without property', () => {
let transformed = transformation.toModel( { name: 'Test' } );
expect( transformed.name ).toStrictEqual( 'Test' );
transformed = transformation.fromModel( { name: 'Test' } );
expect( transformed.name ).toStrictEqual( 'Test' );
} );
it( 'should preserve null', () => {
let transformed = transformation.toModel( { integer: null } );
expect( transformed.integer ).toStrictEqual( null );
transformed = transformation.fromModel( { integer: null } );
expect( transformed.integer ).toStrictEqual( null );
} );
} );

View File

@ -48,6 +48,10 @@ export class KeyChangeTransformation< T extends Model > implements ModelTransfor
for ( const key in this.changes ) {
const value = this.changes[ key ];
if ( ! properties.hasOwnProperty( key ) ) {
continue;
}
properties[ value ] = properties[ key ];
delete properties[ key ];
}
@ -65,6 +69,10 @@ export class KeyChangeTransformation< T extends Model > implements ModelTransfor
for ( const key in this.changes ) {
const value = this.changes[ key ];
if ( ! properties.hasOwnProperty( value ) ) {
continue;
}
properties[ key ] = properties[ value ];
delete properties[ value ];
}

View File

@ -12,6 +12,7 @@ export enum PropertyType {
Boolean,
Date,
}
type PropertyTypeTypes = null | string | number | boolean | Date;
/**
* A callback that can be used to transform property types.
@ -115,9 +116,13 @@ export class PropertyTypeTransformation implements ModelTransformation {
* @return {*} The converted type.
* @private
*/
private convertTo( value: string | string[], type: PropertyType ): any {
private convertTo( value: any, type: PropertyType ): PropertyTypeTypes | PropertyTypeTypes[] {
if ( Array.isArray( value ) ) {
return value.map( ( v: string ) => this.convertTo( v, type ) );
return value.map( ( v: string ) => this.convertTo( v, type ) as PropertyTypeTypes );
}
if ( null === value ) {
return null;
}
switch ( type ) {
@ -126,10 +131,6 @@ export class PropertyTypeTransformation implements ModelTransformation {
case PropertyType.Float: return parseFloat( value );
case PropertyType.Boolean: return Boolean( value );
case PropertyType.Date:
if ( ! value ) {
return null;
}
return new Date( value );
}
}
@ -142,9 +143,13 @@ export class PropertyTypeTransformation implements ModelTransformation {
* @return {*} The converted type.
* @private
*/
private convertFrom( value: any | any[], type: PropertyType ): string | string[] {
private convertFrom( value: PropertyTypeTypes | PropertyTypeTypes[], type: PropertyType ): any {
if ( Array.isArray( value ) ) {
return value.map( ( v: string ) => this.convertFrom( v, type ) as string );
return value.map( ( v ) => this.convertFrom( v, type ) );
}
if ( null === value ) {
return null;
}
switch ( type ) {
@ -155,10 +160,6 @@ export class PropertyTypeTransformation implements ModelTransformation {
return String( value );
case PropertyType.Date: {
if ( ! value ) {
return '';
}
return ( value as Date ).toISOString();
}
}

View File

@ -193,7 +193,7 @@ export function restUpdate< T extends ModelRepositoryParams >(
return async ( id, params ) => {
const response = await httpClient.patch(
buildURL( id ),
transformer.fromModel( params as Partial< ModelClass< T > > ),
transformer.fromModel( params as any ),
);
return Promise.resolve( transformer.toModel( modelClass, response.data ) );
@ -218,7 +218,7 @@ export function restUpdateChild< T extends ModelRepositoryParams >(
return async ( parent, id, params ) => {
const response = await httpClient.patch(
buildURL( parent, id ),
transformer.fromModel( params as Partial< ModelClass< T > > ),
transformer.fromModel( params as any ),
);
return Promise.resolve( transformer.toModel( modelClass, response.data ) );
@ -236,9 +236,8 @@ export function restDelete< T extends ModelRepositoryParams >(
buildURL: HasParent< T, never, BuildURLFn >,
httpClient: HTTPClient,
): DeleteFn {
return async ( id ) => {
await httpClient.delete( buildURL( id ) );
return Promise.resolve( true );
return ( id ) => {
return httpClient.delete( buildURL( id ) ).then( () => true );
};
}
@ -253,8 +252,7 @@ export function restDeleteChild< T extends ModelRepositoryParams >(
buildURL: HasParent< T, BuildURLWithParentFn< T >, never >,
httpClient: HTTPClient,
): DeleteChildFn< T > {
return async ( parent, id ) => {
await httpClient.delete( buildURL( parent, id ) );
return Promise.resolve( true );
return ( parent, id ) => {
return httpClient.delete( buildURL( parent, id ) ).then( () => true );
};
}

View File

@ -13,10 +13,10 @@ export function simpleProductFactory( httpClient ) {
return new AsyncFactory(
( { params } ) => {
return new SimpleProduct( {
return {
name: params.name ?? faker.commerce.productName(),
regularPrice: params.regularPrice ?? faker.commerce.price(),
} );
};
},
( params ) => repository.create( params ),
);