Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type DiagramEditorRef = {
};

export type DiagramEditorProps = {
content: string;
isReadOnly: boolean;
locale: string;
ref?: React.Ref<DiagramEditorRef>;
Expand Down Expand Up @@ -63,7 +64,7 @@ export const DiagramEditor = (props: DiagramEditorProps) => {

return (
<>
<DiagramEditorContextProvider isReadOnly={props.isReadOnly} locale={locale}>
<DiagramEditorContextProvider content={props.content} isReadOnly={props.isReadOnly} locale={locale}>
<I18nProvider locale={locale} dictionaries={dictionaries}>
<Content />
<Diagram ref={diagramRef} divRef={diagramDivRef} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* limitations under the License.
*/

import type { Specification } from "@serverlessworkflow/sdk";
import * as React from "react";

export type DiagramEditorContextType = {
isReadOnly: boolean;
locale: string;
model: Specification.Workflow | null;
errors: Error[];

updateIsReadOnly: (isReadOnly: boolean) => void;
updateLocale: (locale: string) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import * as React from "react";
import type { Specification } from "@serverlessworkflow/sdk";
import { parseWorkflow } from "../core";
import { DiagramEditorProps } from "../diagram-editor/DiagramEditor";
import { DiagramEditorContext, DiagramEditorContextType } from "./DiagramEditorContext";

Expand All @@ -27,6 +29,8 @@ export const DiagramEditorContextProvider = (
const [isReadOnly, setIsReadOnly] = React.useState<boolean>(props.isReadOnly);
const [locale, setLocale] = React.useState<string>(props.locale);

const { model, errors } = React.useMemo(() => parseWorkflow(props.content), [props.content]);

const updateIsReadOnly = React.useCallback((isReadOnly: boolean) => {
setIsReadOnly(isReadOnly);
}, []);
Expand All @@ -46,10 +50,12 @@ export const DiagramEditorContextProvider = (
() => ({
isReadOnly,
locale,
model,
errors,
updateIsReadOnly,
updateLocale,
}),
[isReadOnly, locale, updateIsReadOnly, updateLocale],
[isReadOnly, locale, model, errors, updateIsReadOnly, updateLocale],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export const Component: Story = {
args: {
isReadOnly: true,
locale: "en",
content: "", // TODO: Replace with a sample workflow YAML once diagram renders from model
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
export const DiagramEditor = ({ ...props }: DiagramEditorProps) => {
return (
<div style={{ height: "100vh" }}>
<Component isReadOnly={props.isReadOnly} locale={props.locale} />
<Component content={props.content} isReadOnly={props.isReadOnly} locale={props.locale} />
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { render, screen } from "@testing-library/react";
import { composeStories } from "@storybook/react-vite";
import * as stories from "../../stories/DiagramEditor.stories";
import { vi, test, expect, afterEach, describe } from "vitest";
import { BASIC_VALID_WORKFLOW_YAML } from "../fixtures/workflows";

// Composes all stories in the file
const { Component } = composeStories(stories);
Expand All @@ -31,7 +32,7 @@ describe("Story - DiagramEditor component", () => {
const locale = "en";
const isReadOnly = true;

render(<Component locale={locale} isReadOnly={isReadOnly} />);
render(<Component content={BASIC_VALID_WORKFLOW_YAML} locale={locale} isReadOnly={isReadOnly} />);

const reactFlowContainer = screen.getByTestId("diagram-container");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { render, screen } from "@testing-library/react";
import { DiagramEditor } from "../../src/diagram-editor";
import { vi, test, expect, afterEach, describe } from "vitest";
import { BASIC_VALID_WORKFLOW_YAML } from "../fixtures/workflows";

describe("DiagramEditor Component", () => {
afterEach(() => {
Expand All @@ -27,7 +28,7 @@ describe("DiagramEditor Component", () => {
const locale = "en";
const isReadOnly = true;

render(<DiagramEditor locale={locale} isReadOnly={isReadOnly} />);
render(<DiagramEditor content={BASIC_VALID_WORKFLOW_YAML} locale={locale} isReadOnly={isReadOnly} />);

const reactFlowContainer = screen.getByTestId("diagram-container");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/

import * as React from "react";
import { render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import { vi, expect, afterEach, describe, it } from "vitest";
import { useDiagramEditorContext } from "../../src/store/DiagramEditorContext";
import { DiagramEditorContextProvider } from "../../src/store/DiagramEditorContextProvider";
import { BASIC_INVALID_WORKFLOW_YAML, BASIC_VALID_WORKFLOW_YAML } from "../fixtures/workflows";

const TestComponent: React.FC = () => {
const { isReadOnly, locale } = useDiagramEditorContext();
const { isReadOnly, locale, model, errors } = useDiagramEditorContext();
const renderCount = React.useRef<number>(0);

// Increments on every render cycle
Expand All @@ -31,6 +32,8 @@ const TestComponent: React.FC = () => {
<div data-testid="test-wrapper">
<p data-testid="test-read-only">{`${isReadOnly}`}</p>
<p data-testid="test-locale">{`${locale}`}</p>
<p data-testid="test-model">{`${model ? model.document?.name : "null"}`}</p>
<p data-testid="test-errors">{`${errors.length}`}</p>
<p data-testid="test-render">{`${renderCount.current}`}</p>
</div>
);
Expand All @@ -43,7 +46,11 @@ describe("DiagramEditorContextProvider Component", () => {

it("Consume properties from context", async () => {
render(
<DiagramEditorContextProvider isReadOnly={true} locale={"en"}>
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);
Expand All @@ -61,13 +68,21 @@ describe("DiagramEditorContextProvider Component", () => {

it("Context provider props changes shall cause internal component to reload", async () => {
const { rerender } = render(
<DiagramEditorContextProvider isReadOnly={true} locale={"en"}>
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

rerender(
<DiagramEditorContextProvider isReadOnly={false} locale={"pt"}>
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={false}
locale={"pt"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);
Expand All @@ -85,13 +100,21 @@ describe("DiagramEditorContextProvider Component", () => {

it("Context provider same props shall not cause internal component to reload", async () => {
const { rerender } = render(
<DiagramEditorContextProvider isReadOnly={true} locale={"en"}>
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

rerender(
<DiagramEditorContextProvider isReadOnly={true} locale={"en"}>
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);
Expand All @@ -106,4 +129,93 @@ describe("DiagramEditorContextProvider Component", () => {
// 2 rendering cycles are expected 1- first render and 2- forced by rerender
expect(renderCount).toHaveTextContent(/2/i);
});

it("Parses valid workflow content into model with no errors", async () => {
render(
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

const modelElement = await screen.findByTestId("test-model");
const errorsElement = screen.getByTestId("test-errors");

await waitFor(() => {
expect(modelElement).toHaveTextContent("valid-workflow-yaml");
expect(errorsElement).toHaveTextContent("0");
});
});

it("Parses invalid workflow content into model with errors", async () => {
render(
<DiagramEditorContextProvider
content={BASIC_INVALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

const modelElement = screen.getByTestId("test-model");
const errorsElement = screen.getByTestId("test-errors");

await waitFor(() => {
// Model is still returned as parsing succeeded but has validation errors
expect(modelElement).toBeInTheDocument();
expect(errorsElement).toHaveTextContent("1");
});
Comment on lines +167 to +171
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asserting an exact error count (\"1\") is brittle: validation can legitimately return multiple errors as rules evolve, causing unrelated test failures. Prefer asserting errors.length > 0 (or checking for a specific error shape/message/code if stable) so the test verifies the intended behavior without over-constraining the exact number.

Copilot uses AI. Check for mistakes.
});

it("Parses empty workflow content into null model with errors", async () => {
render(
<DiagramEditorContextProvider content={""} isReadOnly={true} locale={"en"}>
<TestComponent />
</DiagramEditorContextProvider>,
);

const modelElement = screen.getByTestId("test-model");
const errorsElement = screen.getByTestId("test-errors");

await waitFor(() => {
// Model is null as parsing failed and errors are returned
expect(modelElement).toHaveTextContent("null");
expect(errorsElement).toHaveTextContent("1");
});
});

it("Updates model when content prop changes", async () => {
const { rerender } = render(
<DiagramEditorContextProvider
content={BASIC_VALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

await waitFor(() => {
expect(screen.getByTestId("test-model")).toHaveTextContent("valid-workflow-yaml");
expect(screen.getByTestId("test-errors")).toHaveTextContent("0");
});

rerender(
<DiagramEditorContextProvider
content={BASIC_INVALID_WORKFLOW_YAML}
isReadOnly={true}
locale={"en"}
>
<TestComponent />
</DiagramEditorContextProvider>,
);

await waitFor(() => {
expect(screen.getByTestId("test-errors")).toHaveTextContent("1");
});
});
});
Loading