Skip to content

Commit

Permalink
test: write tests for complex workflows business functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Devessier committed Aug 23, 2024
1 parent a5354c8 commit b1b24c1
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { WorkflowStep, WorkflowTrigger } from '@/workflow/types/Workflow';
import { generateWorkflowDiagram } from '@/workflow/utils/generateWorkflowDiagram';
import { addCreateStepNodes } from '../addCreateStepNodes';

describe('addCreateStepNodes', () => {
it("adds a create step node to the end of a single-branch flow and doesn't change the shape of other nodes", () => {
const trigger: WorkflowTrigger = {
type: 'DATABASE_EVENT',
settings: {
eventName: 'company.created',
},
};
const steps: WorkflowStep[] = [
{
id: 'step1',
name: 'Step 1',
type: 'CODE_ACTION',
valid: true,
settings: {
errorHandlingOptions: {
retryOnFailure: { value: true },
continueOnFailure: { value: false },
},
serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997',
},
},
{
id: 'step2',
name: 'Step 2',
type: 'CODE_ACTION',
valid: true,
settings: {
errorHandlingOptions: {
retryOnFailure: { value: true },
continueOnFailure: { value: false },
},
serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997',
},
},
];

const diagramInitial = generateWorkflowDiagram({ trigger, steps });

expect(diagramInitial.nodes).toHaveLength(3);
expect(diagramInitial.edges).toHaveLength(2);

const diagramWithCreateStepNodes = addCreateStepNodes(diagramInitial);

expect(diagramWithCreateStepNodes.nodes).toHaveLength(4);
expect(diagramWithCreateStepNodes.edges).toHaveLength(3);

expect(diagramWithCreateStepNodes.nodes[0].type).toBe(undefined);
expect(diagramWithCreateStepNodes.nodes[0].data.nodeType).toBe('trigger');

expect(diagramWithCreateStepNodes.nodes[1].type).toBe(undefined);
expect(diagramWithCreateStepNodes.nodes[1].data.nodeType).toBe('action');

expect(diagramWithCreateStepNodes.nodes[2].type).toBe(undefined);
expect(diagramWithCreateStepNodes.nodes[2].data.nodeType).toBe('action');

expect(diagramWithCreateStepNodes.nodes[3].type).toBe('create-step');
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WorkflowStep, WorkflowTrigger } from '@/workflow/types/Workflow';
import { generateWorklowDiagram } from '../generateWorkflowDiagram';
import { generateWorkflowDiagram } from '../generateWorkflowDiagram';

describe('generateWorklowDiagram', () => {
describe('generateWorkflowDiagram', () => {
it('should generate a single trigger node when no step is provided', () => {
const trigger: WorkflowTrigger = {
type: 'DATABASE_EVENT',
Expand All @@ -11,7 +11,7 @@ describe('generateWorklowDiagram', () => {
};
const steps: WorkflowStep[] = [];

const result = generateWorklowDiagram({ trigger, steps });
const result = generateWorkflowDiagram({ trigger, steps });

expect(result.nodes).toHaveLength(1);
expect(result.edges).toHaveLength(0);
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('generateWorklowDiagram', () => {
},
];

const result = generateWorklowDiagram({ trigger, steps });
const result = generateWorkflowDiagram({ trigger, steps });

expect(result.nodes).toHaveLength(steps.length + 1); // All steps + trigger
expect(result.edges).toHaveLength(steps.length - 1 + 1); // Edges are one less than nodes + the edge from the trigger to the first node
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('generateWorklowDiagram', () => {
},
];

const result = generateWorklowDiagram({ trigger, steps });
const result = generateWorkflowDiagram({ trigger, steps });

expect(result.edges[0].source).toEqual(result.nodes[0].id);
expect(result.edges[0].target).toEqual(result.nodes[1].id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Workflow } from '@/workflow/types/Workflow';
import { getWorkflowLastDiagramVersion } from '../getWorkflowLastDiagramVersion';

describe('getWorkflowLastDiagramVersion', () => {
it('returns an empty diagram if the provided workflow is undefined', () => {
const result = getWorkflowLastDiagramVersion(undefined);

expect(result).toEqual({ nodes: [], edges: [] });
});

it('returns an empty diagram if the provided workflow has no versions', () => {
const result = getWorkflowLastDiagramVersion({
__typename: 'Workflow',
id: 'aa',
name: 'aa',
publishedVersionId: '',
versions: [],
});

expect(result).toEqual({ nodes: [], edges: [] });
});

it('returns the diagram for the last version', () => {
const workflow: Workflow = {
__typename: 'Workflow',
id: 'aa',
name: 'aa',
publishedVersionId: '',
versions: [
{
__typename: 'WorkflowVersion',
createdAt: '',
id: '1',
name: '',
steps: [],
trigger: {
settings: { eventName: 'company.created' },
type: 'DATABASE_EVENT',
},
updatedAt: '',
workflowId: '',
},
{
__typename: 'WorkflowVersion',
createdAt: '',
id: '1',
name: '',
steps: [
{
id: 'step-1',
name: '',
settings: {
errorHandlingOptions: {
retryOnFailure: { value: true },
continueOnFailure: { value: false },
},
serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997',
},
type: 'CODE_ACTION',
valid: true,
},
],
trigger: {
settings: { eventName: 'company.created' },
type: 'DATABASE_EVENT',
},
updatedAt: '',
workflowId: '',
},
],
};

const result = getWorkflowLastDiagramVersion(workflow);

// Corresponds to the trigger + 1 step
expect(result.nodes).toHaveLength(2);
expect(result.edges).toHaveLength(1);
});


});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { MarkerType } from '@xyflow/react';
import { v4 } from 'uuid';

export const generateWorklowDiagram = ({
export const generateWorkflowDiagram = ({
trigger,
steps,
}: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Workflow } from '@/workflow/types/Workflow';
import { WorkflowDiagram } from '@/workflow/types/WorkflowDiagram';
import { generateWorklowDiagram } from '@/workflow/utils/generateWorkflowDiagram';
import { generateWorkflowDiagram } from '@/workflow/utils/generateWorkflowDiagram';
import { isDefined } from 'twenty-ui';

const EMPTY_FLOW_DATA: WorkflowDiagram = {
const EMPTY_DIAGRAM: WorkflowDiagram = {
nodes: [],
edges: [],
};
Expand All @@ -12,15 +12,15 @@ export const getWorkflowLastDiagramVersion = (
workflow: Workflow | undefined,
): WorkflowDiagram => {
if (!isDefined(workflow)) {
return EMPTY_FLOW_DATA;
return EMPTY_DIAGRAM;
}

const lastVersion = workflow.versions.at(-1);
if (!isDefined(lastVersion) || !isDefined(lastVersion.trigger)) {
return EMPTY_FLOW_DATA;
return EMPTY_DIAGRAM;
}

return generateWorklowDiagram({
return generateWorkflowDiagram({
trigger: lastVersion.trigger,
steps: lastVersion.steps,
});
Expand Down

0 comments on commit b1b24c1

Please sign in to comment.