Skip to content

Commit

Permalink
fix: 🐛 when creating shape from object, all fields are mandator
Browse files Browse the repository at this point in the history
ShapeObject generic interface were forcing ShapeOptions to have all the
fields filled. This commit fixes this behaviour by applying Partial type
over ShapeOptions in generic ShapeObject
  • Loading branch information
ghaiklor committed Apr 26, 2020
1 parent 47f08ae commit ba02210
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
13 changes: 6 additions & 7 deletions packages/kittik-shape-basic/spec/Shape.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Canvas } from 'terminal-canvas';
import { Shape } from '../src/Shape';
import { ShapeObject } from '../src/ShapeObject';

describe('basic shape', () => {
it('should properly get/set text', () => {
Expand Down Expand Up @@ -122,7 +121,7 @@ describe('basic shape', () => {
const obj = shape.toObject();

expect(obj).toStrictEqual({
type: 'Shape',
type: 'Basic',
options: {
background: 'none',
foreground: 'none',
Expand Down Expand Up @@ -150,7 +149,7 @@ describe('basic shape', () => {

const obj = shape.toObject();
expect(obj).toStrictEqual({
type: 'Shape',
type: 'Basic',
options: {
background: 'red',
foreground: 'black',
Expand All @@ -170,7 +169,7 @@ describe('basic shape', () => {
const json = shape.toJSON();

// eslint-disable-next-line max-len
expect(json).toStrictEqual('{"type":"Shape","options":{"background":"none","foreground":"none","height":"25%","text":"","width":"50%","x":"left","y":"top"}}');
expect(json).toStrictEqual('{"type":"Basic","options":{"background":"none","foreground":"none","height":"25%","text":"","width":"50%","x":"left","y":"top"}}');
});

it('should properly serialize shape to JSON with custom options', () => {
Expand All @@ -187,7 +186,7 @@ describe('basic shape', () => {
const json = shape.toJSON();

// eslint-disable-next-line max-len
expect(json).toStrictEqual('{"type":"Shape","options":{"background":"none","foreground":"none","height":"50","text":"test","width":"30","x":"0","y":"0"}}');
expect(json).toStrictEqual('{"type":"Basic","options":{"background":"none","foreground":"none","height":"50","text":"test","width":"30","x":"0","y":"0"}}');
});

it('should properly create Shape instance from static create()', () => {
Expand All @@ -200,7 +199,7 @@ describe('basic shape', () => {
it('should properly throw error if trying to create Shape not from its representation', () => {
expect.hasAssertions();

const obj = { type: 'Rectangle' };
const obj = { type: 'Rectangle', options: {} };
expect(() => Shape.fromObject(obj)).toThrow(
'You specified configuration for "Rectangle" but provided it to "Shape". ' +
'Did you mean to set "type" in configuration to "Shape"?'
Expand All @@ -210,7 +209,7 @@ describe('basic shape', () => {
it('should properly create Shape instance from Object representation', () => {
expect.hasAssertions();

const obj: ShapeObject = {
const obj = {
type: 'Shape',
options: {
background: 'red',
Expand Down
8 changes: 4 additions & 4 deletions packages/kittik-shape-basic/src/Shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export class Shape implements ShapeOptions, ShapeRenderable {
return (new this(options)) as S;
}

public static fromObject <T, O extends ShapeOptions, S extends Shape>(obj: ShapeObject<T, O>): S
public static fromObject <T, O extends ShapeOptions>(obj: ShapeObject<T, O>): Shape
public static fromObject <T>(obj: ShapeObject<T, ShapeOptions>): Shape
public static fromObject (obj: ShapeObject<'Basic', ShapeOptions>): Shape {
public static fromObject <T, O extends Partial<ShapeOptions>, S extends Shape>(obj: ShapeObject<T, O>): S
public static fromObject <T, O extends Partial<ShapeOptions>>(obj: ShapeObject<T, O>): Shape
public static fromObject <T>(obj: ShapeObject<T, Partial<ShapeOptions>>): Shape
public static fromObject (obj: ShapeObject<'Basic', Partial<ShapeOptions>>): Shape {
if (obj.type !== this.name) {
throw new Error(
`You specified configuration for "${obj.type}" but provided it to "${this.name}". ` +
Expand Down
2 changes: 1 addition & 1 deletion packages/kittik-shape-basic/src/ShapeObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ShapeOptions } from './ShapeOptions';

export interface ShapeObject<T, O extends ShapeOptions> {
export interface ShapeObject<T, O extends Partial<ShapeOptions>> {
type: T
options: O
}

0 comments on commit ba02210

Please sign in to comment.