-
Notifications
You must be signed in to change notification settings - Fork 7
Add ContextProvider, Context and Hook #16 #57
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1bfe6d4
Add ContextProvider, Context and Hook
handreyrc 81ef8b5
Fix copilot complaints
handreyrc 5892d8e
Rename Context Component
handreyrc 217af1b
Fix copilot complaints
handreyrc c1c413e
Review fixes
handreyrc 47f3a4f
Fix copilot complaints
handreyrc 6fe610c
Fix copilot complaints
handreyrc 8916b2e
Review fixes
handreyrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/serverless-workflow-diagram-editor/src/store/DiagramEditorContext.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as React from "react"; | ||
|
|
||
| export type DiagramEditorContextType = { | ||
| isReadOnly: boolean; | ||
| locale: string; | ||
|
|
||
| updateIsReadOnly: (isReadOnly: boolean) => void; | ||
| updateLocale: (locale: string) => void; | ||
| }; | ||
|
|
||
| export const DiagramEditorContext = React.createContext<DiagramEditorContextType | undefined>( | ||
| undefined, | ||
| ); | ||
|
|
||
| export const useDiagramEditorContext = () => { | ||
| const context = React.useContext(DiagramEditorContext); | ||
|
|
||
| if (context === undefined) { | ||
| throw new Error("useDiagramEditorContext must be used within a DiagramEditorContextProvider"); | ||
| } | ||
|
|
||
| return context; | ||
| }; |
58 changes: 58 additions & 0 deletions
58
packages/serverless-workflow-diagram-editor/src/store/DiagramEditorContextProvider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as React from "react"; | ||
| import { DiagramEditorProps } from "../diagram-editor/DiagramEditor"; | ||
| import { DiagramEditorContext, DiagramEditorContextType } from "./DiagramEditorContext"; | ||
|
|
||
| export type ContextProviderProps = Omit<DiagramEditorProps, "ref">; | ||
|
|
||
| export const DiagramEditorContextProvider = ( | ||
| props: React.PropsWithChildren<ContextProviderProps>, | ||
| ) => { | ||
| // Initialize states with props values | ||
| const [isReadOnly, setIsReadOnly] = React.useState<boolean>(props.isReadOnly); | ||
| const [locale, setLocale] = React.useState<string>(props.locale); | ||
|
|
||
| const updateIsReadOnly = React.useCallback((isReadOnly: boolean) => { | ||
| setIsReadOnly(isReadOnly); | ||
| }, []); | ||
|
|
||
| const updateLocale = React.useCallback((locale: string) => { | ||
| setLocale(locale); | ||
| }, []); | ||
|
|
||
| // Update states on props changes | ||
| React.useEffect(() => { | ||
| updateIsReadOnly(props.isReadOnly); | ||
| updateLocale(props.locale); | ||
| }, [props.isReadOnly, props.locale, updateIsReadOnly, updateLocale]); | ||
handreyrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Memoize context value to prevent unnecessary re-renders of consumers | ||
| const context = React.useMemo<DiagramEditorContextType>( | ||
| () => ({ | ||
| isReadOnly, | ||
| locale, | ||
| updateIsReadOnly, | ||
| updateLocale, | ||
| }), | ||
| [isReadOnly, locale, updateIsReadOnly, updateLocale], | ||
| ); | ||
handreyrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <DiagramEditorContext.Provider value={context}>{props.children}</DiagramEditorContext.Provider> | ||
| ); | ||
| }; | ||
109 changes: 109 additions & 0 deletions
109
...ages/serverless-workflow-diagram-editor/tests/store/DiagramEditorContextProvider.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as React from "react"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import { vi, expect, afterEach, describe, it } from "vitest"; | ||
| import { useDiagramEditorContext } from "../../src/store/DiagramEditorContext"; | ||
| import { DiagramEditorContextProvider } from "../../src/store/DiagramEditorContextProvider"; | ||
|
|
||
| const TestComponent: React.FC = () => { | ||
| const { isReadOnly, locale } = useDiagramEditorContext(); | ||
| const renderCount = React.useRef<number>(0); | ||
|
|
||
| // Increments on every render cycle | ||
| renderCount.current++; | ||
|
|
||
| return ( | ||
| <div data-testid="test-wrapper"> | ||
| <p data-testid="test-read-only">{`${isReadOnly}`}</p> | ||
| <p data-testid="test-locale">{`${locale}`}</p> | ||
| <p data-testid="test-render">{`${renderCount.current}`}</p> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| describe("DiagramEditorContextProvider Component", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("Consume properties from context", async () => { | ||
| render( | ||
| <DiagramEditorContextProvider isReadOnly={true} locale={"en"}> | ||
| <TestComponent /> | ||
| </DiagramEditorContextProvider>, | ||
| ); | ||
|
|
||
| const readOnlyElement = screen.getByTestId("test-read-only"); | ||
| const readOnlyLocale = screen.getByTestId("test-locale"); | ||
| const renderCount = screen.getByTestId("test-render"); | ||
|
|
||
| expect(readOnlyElement).toHaveTextContent(/true/i); | ||
| expect(readOnlyLocale).toHaveTextContent(/en/i); | ||
|
|
||
| // Only one rendering cycle is expected | ||
| expect(renderCount).toHaveTextContent(/1/i); | ||
| }); | ||
|
|
||
| it("Context provider props changes shall cause internal component to reload", async () => { | ||
| const { rerender } = render( | ||
| <DiagramEditorContextProvider isReadOnly={true} locale={"en"}> | ||
| <TestComponent /> | ||
| </DiagramEditorContextProvider>, | ||
| ); | ||
|
|
||
| rerender( | ||
| <DiagramEditorContextProvider isReadOnly={false} locale={"pt"}> | ||
| <TestComponent /> | ||
| </DiagramEditorContextProvider>, | ||
| ); | ||
|
|
||
| const readOnlyElementChanged = screen.getByTestId("test-read-only"); | ||
| const readOnlyLocaleChanged = screen.getByTestId("test-locale"); | ||
| const renderCount = screen.getByTestId("test-render"); | ||
|
|
||
| expect(readOnlyElementChanged).toHaveTextContent(/false/i); | ||
| expect(readOnlyLocaleChanged).toHaveTextContent(/pt/i); | ||
|
|
||
| // 3 rendering cycles are expected 1- first render, 2- forced by rerender and 3- caused by state updates | ||
| expect(renderCount).toHaveTextContent(/3/i); | ||
| }); | ||
|
|
||
| it("Context provider same props shall not cause internal component to reload", async () => { | ||
| const { rerender } = render( | ||
| <DiagramEditorContextProvider isReadOnly={true} locale={"en"}> | ||
| <TestComponent /> | ||
| </DiagramEditorContextProvider>, | ||
| ); | ||
|
|
||
| rerender( | ||
| <DiagramEditorContextProvider isReadOnly={true} locale={"en"}> | ||
| <TestComponent /> | ||
| </DiagramEditorContextProvider>, | ||
| ); | ||
|
|
||
| const readOnlyElementChanged = screen.getByTestId("test-read-only"); | ||
| const readOnlyLocaleChanged = screen.getByTestId("test-locale"); | ||
| const renderCount = screen.getByTestId("test-render"); | ||
|
|
||
| expect(readOnlyElementChanged).toHaveTextContent(/true/i); | ||
| expect(readOnlyLocaleChanged).toHaveTextContent(/en/i); | ||
|
|
||
| // 2 rendering cycles are expected 1- first render and 2- forced by rerender | ||
| expect(renderCount).toHaveTextContent(/2/i); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.