Skip to content

Commit

Permalink
feat(masonry): Update brickFactory and its tests according to the upd…
Browse files Browse the repository at this point in the history
…ated classes
  • Loading branch information
Karan-Palan committed Sep 4, 2024
1 parent f52f067 commit e7d681d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 13 deletions.
94 changes: 87 additions & 7 deletions modules/masonry/src/brick/design0/brickFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
createBrickData,
createBrickExpression,
createBrickStatement,
addBrickToWarehouse,
getBrickFromWarehouse,
deleteBrickFromWarehouse,
} from './brickFactory';
import BrickBlock from './BrickBlock';
import BrickData from './BrickData';
Expand All @@ -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,
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -56,34 +71,99 @@ 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', () => {
const statement = createBrickStatement({
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,
connectBelow: false,
});

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();
});
});
61 changes: 55 additions & 6 deletions modules/masonry/src/brick/design0/brickFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, BrickStatement | BrickExpression | BrickData | BrickBlock> =
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.
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 };

0 comments on commit e7d681d

Please sign in to comment.