-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add model to store #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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> | ||
| ); | ||
|
|
@@ -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>, | ||
| ); | ||
|
|
@@ -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>, | ||
| ); | ||
|
|
@@ -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>, | ||
| ); | ||
|
|
@@ -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"); | ||
| }); | ||
| }); | ||
lornakelly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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
|
||
| }); | ||
|
|
||
| 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"); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.