From e7d681d075b3b49f0feb09c587027abf5f305f59 Mon Sep 17 00:00:00 2001 From: Karan Palan Date: Thu, 5 Sep 2024 03:13:24 +0530 Subject: [PATCH] feat(masonry): Update brickFactory and its tests according to the updated classes --- .../src/brick/design0/brickFactory.spec.ts | 94 +++++++++++++++++-- .../masonry/src/brick/design0/brickFactory.ts | 61 ++++++++++-- 2 files changed, 142 insertions(+), 13 deletions(-) diff --git a/modules/masonry/src/brick/design0/brickFactory.spec.ts b/modules/masonry/src/brick/design0/brickFactory.spec.ts index e6fa6ae2..9bfd40a7 100644 --- a/modules/masonry/src/brick/design0/brickFactory.spec.ts +++ b/modules/masonry/src/brick/design0/brickFactory.spec.ts @@ -3,6 +3,9 @@ import { createBrickData, createBrickExpression, createBrickStatement, + addBrickToWarehouse, + getBrickFromWarehouse, + deleteBrickFromWarehouse, } from './brickFactory'; import BrickBlock from './BrickBlock'; import BrickData from './BrickData'; @@ -15,9 +18,17 @@ describe('Factory Functions', () => { name: 'Block', label: 'Label', glyph: 'Glyph', - args: {}, + args: { + arg1: { + label: 'Arg1', + dataType: 'string', + meta: { argId: '1', argLabel: 'Arg1', argTypeIncoming: 'string' }, + }, + }, colorBg: 'red', colorFg: 'blue', + colorBgHighlight: 'pink', + colorFgHighlight: 'cyan', outline: 'black', scale: 1, connectAbove: true, @@ -26,8 +37,9 @@ describe('Factory Functions', () => { }); expect(block).toBeInstanceOf(BrickBlock); - expect(block.id).toBeDefined(); + expect(block.uuid).toBeDefined(); expect(block.SVGpath).toBeDefined(); + expect(block.args).toHaveProperty('arg1'); }); test('createBrickData creates a BrickData instance', () => { @@ -41,13 +53,16 @@ describe('Factory Functions', () => { input: 'string', colorBg: 'green', colorFg: 'white', + colorBgHighlight: 'lightgreen', + colorFgHighlight: 'lightgrey', outline: 'black', scale: 1, }); expect(data).toBeInstanceOf(BrickData); - expect(data.id).toBeDefined(); + expect(data.uuid).toBeDefined(); expect(data.SVGpath).toBeDefined(); + expect(data.value).toBe('Test'); }); test('createBrickExpression creates a BrickExpression instance', () => { @@ -56,16 +71,21 @@ describe('Factory Functions', () => { label: 'Label', glyph: 'Glyph', dataType: 'number', - args: {}, + args: { + arg1: { label: 'Arg1', dataType: 'number', meta: {} }, + }, colorBg: 'blue', colorFg: 'yellow', + colorBgHighlight: 'lightblue', + colorFgHighlight: 'lightyellow', outline: 'black', scale: 1, }); expect(expression).toBeInstanceOf(BrickExpression); - expect(expression.id).toBeDefined(); + expect(expression.uuid).toBeDefined(); expect(expression.SVGpath).toBeDefined(); + expect(expression.args).toHaveProperty('arg1'); }); test('createBrickStatement creates a BrickStatement instance', () => { @@ -73,9 +93,13 @@ describe('Factory Functions', () => { name: 'Statement', label: 'Label', glyph: 'Glyph', - args: {}, + args: { + arg1: { label: 'Arg1', dataType: 'boolean', meta: {} }, + }, colorBg: 'orange', colorFg: 'purple', + colorBgHighlight: 'lightorange', + colorFgHighlight: 'lightpurple', outline: 'black', scale: 1, connectAbove: true, @@ -83,7 +107,63 @@ describe('Factory Functions', () => { }); expect(statement).toBeInstanceOf(BrickStatement); - expect(statement.id).toBeDefined(); + expect(statement.uuid).toBeDefined(); expect(statement.SVGpath).toBeDefined(); + expect(statement.args).toHaveProperty('arg1'); + }); +}); + +describe('Warehouse Module', () => { + test('addBrickToWarehouse adds a Brick instance to the warehouse', () => { + const brick = createBrickBlock({ + name: 'Block', + label: 'Label', + glyph: 'Glyph', + args: { + arg1: { + label: 'Arg1', + dataType: 'string', + meta: { argId: '1', argLabel: 'Arg1', argTypeIncoming: 'string' }, + }, + }, + colorBg: 'red', + colorFg: 'blue', + colorBgHighlight: 'pink', + colorFgHighlight: 'cyan', + outline: 'black', + scale: 1, + connectAbove: true, + connectBelow: false, + nestLengthY: 20, + }); + + addBrickToWarehouse(brick); + const storedBrick = getBrickFromWarehouse(brick.uuid); + expect(storedBrick).toBe(brick); + }); + + test('deleteBrickFromWarehouse removes a Brick instance from the warehouse', () => { + const brick = createBrickStatement({ + name: 'Statement', + label: 'Label', + glyph: 'Glyph', + args: { + arg1: { label: 'Arg1', dataType: 'boolean', meta: {} }, + }, + colorBg: 'orange', + colorFg: 'purple', + colorBgHighlight: 'lightorange', + colorFgHighlight: 'lightpurple', + outline: 'black', + scale: 1, + connectAbove: true, + connectBelow: false, + }); + + addBrickToWarehouse(brick); + const isDeleted = deleteBrickFromWarehouse(brick.uuid); + const storedBrick = getBrickFromWarehouse(brick.uuid); + expect(isDeleted).toBe(true); + expect(storedBrick).toBeUndefined(); }); }); diff --git a/modules/masonry/src/brick/design0/brickFactory.ts b/modules/masonry/src/brick/design0/brickFactory.ts index 814db3a7..73a22cff 100644 --- a/modules/masonry/src/brick/design0/brickFactory.ts +++ b/modules/masonry/src/brick/design0/brickFactory.ts @@ -4,7 +4,40 @@ import BrickStatement from './BrickStatement'; import BrickExpression from './BrickExpression'; import BrickData from './BrickData'; import BrickBlock from './BrickBlock'; -import { addBrickToWarehouse } from './brickWarehouse'; + +// Warehouse to manage brick instances +const brickWarehouse: Map = + new Map(); + +/** + * Adds a brick instance to the warehouse. + * @param brick - The brick instance to add. + */ +function addBrickToWarehouse( + brick: BrickStatement | BrickExpression | BrickData | BrickBlock, +): void { + brickWarehouse.set(brick.uuid, brick); +} + +/** + * Retrieves a brick instance from the warehouse by its ID. + * @param id - The ID of the brick to retrieve. + * @returns The brick instance if found, otherwise undefined. + */ +function getBrickFromWarehouse( + id: string, +): BrickStatement | BrickExpression | BrickData | BrickBlock | undefined { + return brickWarehouse.get(id); +} + +/** + * Deletes a brick instance from the warehouse by its ID. + * @param id - The ID of the brick to delete. + * @returns True if the brick was deleted, false if it was not found. + */ +function deleteBrickFromWarehouse(id: string): boolean { + return brickWarehouse.delete(id); +} /** * Factory function to create a new BrickBlock instance. @@ -20,19 +53,26 @@ export function createBrickBlock(params: { { label: string; dataType: TBrickArgDataType; - meta: unknown; + meta: { + argId: string; + argLabel: string; + argTypeIncoming: string; + }; } >; colorBg: TBrickColor; colorFg: TBrickColor; + colorBgHighlight: TBrickColor; + colorFgHighlight: TBrickColor; outline: TBrickColor; scale: number; connectAbove: boolean; connectBelow: boolean; nestLengthY: number; + folded?: boolean; }): BrickBlock { const brick = new BrickBlock({ - id: uuidv4(), + uuid: uuidv4(), ...params, }); addBrickToWarehouse(brick); @@ -54,11 +94,13 @@ export function createBrickData(params: { input?: 'boolean' | 'number' | 'string' | 'options'; colorBg: TBrickColor; colorFg: TBrickColor; + colorBgHighlight: TBrickColor; + colorFgHighlight: TBrickColor; outline: TBrickColor; scale: number; }): BrickData { const brick = new BrickData({ - id: uuidv4(), + uuid: uuidv4(), ...params, }); addBrickToWarehouse(brick); @@ -85,11 +127,13 @@ export function createBrickExpression(params: { >; colorBg: TBrickColor; colorFg: TBrickColor; + colorBgHighlight: TBrickColor; + colorFgHighlight: TBrickColor; outline: TBrickColor; scale: number; }): BrickExpression { const brick = new BrickExpression({ - id: uuidv4(), + uuid: uuidv4(), ...params, }); addBrickToWarehouse(brick); @@ -115,15 +159,20 @@ export function createBrickStatement(params: { >; colorBg: TBrickColor; colorFg: TBrickColor; + colorBgHighlight: TBrickColor; + colorFgHighlight: TBrickColor; outline: TBrickColor; scale: number; connectAbove: boolean; connectBelow: boolean; }): BrickStatement { const brick = new BrickStatement({ - id: uuidv4(), + uuid: uuidv4(), ...params, }); addBrickToWarehouse(brick); return brick; } + +// Exporting warehouse functions for external use +export { addBrickToWarehouse, getBrickFromWarehouse, deleteBrickFromWarehouse };