-
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
Open
handreyrc
wants to merge
4
commits into
serverlessworkflow:main
Choose a base branch
from
handreyrc:add-store
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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((prev) => (prev !== isReadOnly ? isReadOnly : prev)); | ||
| }, []); | ||
|
|
||
| const updateLocale = React.useCallback((locale: string) => { | ||
| setLocale((prev) => (prev !== locale ? locale : prev)); | ||
| }, []); | ||
|
|
||
| // Update states on props changes | ||
| React.useEffect(() => { | ||
| updateIsReadOnly(props.isReadOnly); | ||
| updateLocale(props.locale); | ||
| }, [props, updateIsReadOnly, updateLocale]); | ||
|
|
||
| // Memoize context value to prevent unnecessary re-renders of consumers | ||
| const context = React.useMemo<DiagramEditorContextType>( | ||
| () => ({ | ||
| isReadOnly, | ||
| locale, | ||
| updateIsReadOnly, | ||
| updateLocale, | ||
| }), | ||
| [isReadOnly, locale, updateIsReadOnly, updateLocale], | ||
| ); | ||
|
|
||
| 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, test, expect, afterEach, describe } 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(); | ||
| }); | ||
|
|
||
| test("Consume properties from context", async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for |
||
| 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); | ||
| }); | ||
|
|
||
| test("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- fist render, 2- forced by rerender and 3- caused by state updates | ||
| expect(renderCount).toHaveTextContent(/3/i); | ||
| }); | ||
|
|
||
| test("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- fist 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
props - I think this means is wil run every time as it would be a new reference on every render, better to be explicit for now. I dont think we will have many more props in the future where the dependency array would have too many? If so we can rethink then?
[props.isReadOnly, props.locale, updateIsReadOnly, updateLocale]