Skip to content

Commit

Permalink
chore(viz): Remove getBaseEntity() method form IVizNode
Browse files Browse the repository at this point in the history
  • Loading branch information
lordrip committed Aug 16, 2024
1 parent 1284cf1 commit 3f7b6a1
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/ui-tests/stories/canvas/Aggregate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const selectedNode: CanvasNode = {
parentNode: undefined,
previousNode: undefined,
label: 'test',
getId: () => 'aggregate-6839',
getOmitFormFields: () => [],
getComponentSchema: () => {
return {
title: 'aggregate',
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-tests/stories/canvas/CanvasSideBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const selectedNode: CanvasNode = {
parentNode: undefined,
previousNode: undefined,
label: 'test',
getId: () => 'log-sink-6839',
getOmitFormFields: () => [],
getComponentSchema: () => {
return {
title: 'My Node',
Expand Down Expand Up @@ -73,6 +75,8 @@ const unknownSelectedNode: CanvasNode = {
label: 'test',
icon: NodeIconResolver.getIcon(''),
} as IVisualizationNodeData,
getId: () => 'test',
getOmitFormFields: () => [],
getComponentSchema: () => {
return {
title: 'My Node',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CamelCatalogService,
CamelRouteVisualEntity,
CatalogKind,
createVisualizationNode,
ICamelComponentDefinition,
ICamelProcessorDefinition,
IKameletDefinition,
Expand All @@ -26,6 +27,7 @@ import { CanvasForm } from './CanvasForm';
import { CanvasNode } from './canvas.models';
import { CanvasService } from './canvas.service';
import { FormTabsModes } from './canvasformtabs.modes';
import { ROOT_PATH } from '../../../utils';

describe('CanvasForm', () => {
let camelRouteVisualEntity: CamelRouteVisualEntity;
Expand Down Expand Up @@ -73,17 +75,23 @@ describe('CanvasForm', () => {
});

it('should render nothing if no schema is available', () => {
const vizNode = createVisualizationNode('route', {
path: ROOT_PATH,
entity: new CamelRouteVisualEntity(camelRouteJson),
isGroup: true,
processorName: 'route',
});

const selectedNode: CanvasNode = {
id: '1',
type: 'node',
data: {
vizNode: {
getComponentSchema: () => undefined,
getBaseEntity: () => new CamelRouteVisualEntity(camelRouteJson),
} as unknown as IVisualizationNode,
vizNode,
},
};

jest.spyOn(vizNode, 'getComponentSchema').mockReturnValue(undefined);

const { container } = render(
<EntitiesContext.Provider value={null}>
<VisibleFlowsProvider>
Expand All @@ -102,17 +110,23 @@ describe('CanvasForm', () => {
definition: null,
};

const vizNode = createVisualizationNode('route', {
path: ROOT_PATH,
entity: new CamelRouteVisualEntity(camelRouteJson),
isGroup: true,
processorName: 'route',
});

const selectedNode: CanvasNode = {
id: '1',
type: 'node',
data: {
vizNode: {
getComponentSchema: () => visualComponentSchema,
getBaseEntity: () => new CamelRouteVisualEntity(camelRouteJson),
} as unknown as IVisualizationNode,
vizNode,
},
};

jest.spyOn(vizNode, 'getComponentSchema').mockReturnValue(visualComponentSchema);

const { container } = render(
<EntitiesContext.Provider value={null}>
<VisibleFlowsProvider>
Expand Down Expand Up @@ -140,6 +154,8 @@ describe('CanvasForm', () => {
vizNode: {
getComponentSchema: () => visualComponentSchema,
getBaseEntity: () => new CamelRouteVisualEntity(camelRouteJson),
getId: () => 'route-8888',
getOmitFields: () => [],
} as unknown as IVisualizationNode,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export const CanvasForm: FunctionComponent<CanvasFormProps> = (props) => {

/** Store the flow's initial Id */
useEffect(() => {
flowIdRef.current = props.selectedNode.data?.vizNode?.getBaseEntity()?.getId();
flowIdRef.current = props.selectedNode.data?.vizNode?.getId();
}, []);

const onClose = useCallback(() => {
props.onClose?.();
const newId = props.selectedNode.data?.vizNode?.getBaseEntity()?.getId();
const newId = props.selectedNode.data?.vizNode?.getId();
if (typeof flowIdRef.current === 'string' && typeof newId === 'string' && flowIdRef.current !== newId) {
visualFlowsApi.renameFlow(flowIdRef.current, newId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const CanvasFormTabs: FunctionComponent<CanvasFormTabsProps> = (props) =>
const { selectedTab } = useContext(CanvasFormTabsContext);
const divRef = useRef<HTMLDivElement>(null);
const formRef = useRef<CustomAutoFormRef>(null);
const omitFields = useRef(props.selectedNode.data?.vizNode?.getBaseEntity()?.getOmitFormFields() || []);
const omitFields = useRef(props.selectedNode.data?.vizNode?.getOmitFormFields() || []);

const visualComponentSchema = useMemo(() => {
const answer = props.selectedNode.data?.vizNode?.getComponentSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ItemDeleteGroupProps extends PropsWithChildren<IDataTestID> {
export const ItemDeleteGroup: FunctionComponent<ItemDeleteGroupProps> = (props) => {
const entitiesContext = useContext(EntitiesContext);
const deleteModalContext = useContext(DeleteModalContext);
const flowId = props.vizNode?.getBaseEntity()?.getId();
const flowId = props.vizNode?.getId();

const onRemoveGroup = useCallback(async () => {
/** Open delete confirm modal, get the confirmation */
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/src/models/visualization/base-visual-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export interface IVisualizationNode<T extends IVisualizationNodeData = IVisualiz
id: string;
data: T;

/** This property is only set on the root node */
getBaseEntity(): BaseVisualCamelEntity | undefined;
getId(): string | undefined;

/** This method returns the label to be used by the canvas nodes */
getNodeLabel(labelType?: NodeLabelType): string;
Expand All @@ -87,6 +86,9 @@ export interface IVisualizationNode<T extends IVisualizationNodeData = IVisualiz

getComponentSchema(): VisualComponentSchema | undefined;

/** Returnt fields that should be omitted when configuring this entity */
getOmitFormFields(): string[];

updateModel(value: unknown): void;

getParentNode(): IVisualizationNode | undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EIPMap = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class StepEip {}
18 changes: 15 additions & 3 deletions packages/ui/src/models/visualization/visualization-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ describe('VisualizationNode', () => {
expect(node.id).toEqual('test-1234');
});

it('should return the base visual entity', () => {
const visualEntity = {} as BaseVisualCamelEntity;
it('should return the base entity ID', () => {
const visualEntity = new CamelRouteVisualEntity(camelRouteJson);
node = createVisualizationNode('test', { entity: visualEntity });

expect(node.getBaseEntity()).toEqual(visualEntity);
expect(node.getId()).toEqual('route-8888');
});

it('should return the component schema from the underlying BaseVisualCamelEntity', () => {
Expand All @@ -34,6 +34,18 @@ describe('VisualizationNode', () => {
expect(getComponentSchemaSpy).toHaveBeenCalledWith(node.data.path);
});

it('should delegate getOmitFormFields() to the underlying BaseVisualCamelEntity', () => {
const getOmitFormFieldsSpy = jest.fn();
const visualEntity = {
getOmitFormFields: getOmitFormFieldsSpy,
} as unknown as BaseVisualCamelEntity;

node = createVisualizationNode('test', { path: 'test-path', entity: visualEntity });
node.getOmitFormFields();

expect(getOmitFormFieldsSpy).toHaveBeenCalled();
});

describe('getNodeLabel', () => {
it('should return the label from the underlying BaseVisualCamelEntity', () => {
const getNodeLabelSpy = jest.fn().mockReturnValue('test-label');
Expand Down
18 changes: 15 additions & 3 deletions packages/ui/src/models/visualization/visualization-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class VisualizationNode<T extends IVisualizationNodeData = IVisualizationNodeDat
public data: T,
) {}

getBaseEntity(): BaseVisualCamelEntity | undefined {
return this.getRootNode().data.entity;
getId(): string | undefined {
return this.getBaseEntity()?.getId();
}

getNodeLabel(labelType?: NodeLabelType): string {
Expand All @@ -65,6 +65,10 @@ class VisualizationNode<T extends IVisualizationNodeData = IVisualizationNodeDat
return this.getBaseEntity()?.getComponentSchema(this.data.path);
}

getOmitFormFields(): string[] {
return this.getBaseEntity()?.getOmitFormFields() ?? [];
}

updateModel(value: unknown): void {
this.getBaseEntity()?.updateModel(this.data.path, value);
}
Expand Down Expand Up @@ -130,7 +134,15 @@ class VisualizationNode<T extends IVisualizationNodeData = IVisualizationNodeDat
return this.getBaseEntity()?.getNodeValidationText(this.data.path);
}

private getRootNode(): IVisualizationNode {
/**
* Get the underlying entity for the entire flow
* This property is only set on the root node
*/
protected getBaseEntity(): BaseVisualCamelEntity | undefined {
return this.getRootNode().data.entity;
}

protected getRootNode(): IVisualizationNode {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let rootNode: IVisualizationNode | undefined = this;

Expand Down

0 comments on commit 3f7b6a1

Please sign in to comment.