-
Notifications
You must be signed in to change notification settings - Fork 112
(feat) O3-4315: Add support for workspace-launcher rendering type #410
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
rdwaynedehoedt
wants to merge
13
commits into
openmrs:main
Choose a base branch
from
rdwaynedehoedt:main
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 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dbba2f5
Add support for workspace-launcher rendering type
rdwaynedehoedt 37c0064
Update en.json
rdwaynedehoedt e3f7b7e
Merge branch 'main' into main
NethmiRodrigo 086470a
Merge branch 'main' into main
NethmiRodrigo f49ccf4
Merge branch 'openmrs:main' into main
rdwaynedehoedt f54655e
improve workspace-launcher component formatting and test structure
rdwaynedehoedt 8a7d5eb
Merge branch 'main' into main
NethmiRodrigo 4f5d79d
fix: improve workspace-launcher code quality - Update setState to use…
rdwaynedehoedt 6e34072
Merge remote changes with translation updates
rdwaynedehoedt e8fccfd
Merge branch 'main' into main
NethmiRodrigo 0b97e4e
Merge branch 'openmrs:main' into main
rdwaynedehoedt 8bf64b0
Merge branch 'openmrs:main' into main
rdwaynedehoedt 1ae03f3
Merge branch 'openmrs:main' into main
rdwaynedehoedt 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
60 changes: 60 additions & 0 deletions
60
.../question-form/rendering-types/inputs/workspace-launcher/workspace-launcher.component.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,60 @@ | ||
| import React, { useCallback, useMemo } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { TextInput } from '@carbon/react'; | ||
| import { useFormField } from '../../../../form-field-context'; | ||
|
|
||
| const WorkspaceLauncher: React.FC = () => { | ||
| const { t } = useTranslation(); | ||
| const { formField, setFormField } = useFormField(); | ||
|
|
||
| const { buttonLabel, workspaceName } = useMemo( | ||
| () => ({ | ||
| buttonLabel: formField.questionOptions?.buttonLabel ?? '', | ||
| workspaceName: formField.questionOptions?.workspaceName ?? '', | ||
| }), | ||
| [formField.questionOptions?.buttonLabel, formField.questionOptions?.workspaceName], | ||
| ); | ||
|
|
||
| const handleButtonLabelChange = useCallback( | ||
| (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| setFormField((prevFormField) => ({ | ||
| ...prevFormField, | ||
| questionOptions: { ...prevFormField.questionOptions, buttonLabel: event.target.value }, | ||
| })); | ||
| }, | ||
| [setFormField], | ||
| ); | ||
|
|
||
| const handleWorkspaceNameChange = useCallback( | ||
| (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| setFormField((prevFormField) => ({ | ||
| ...prevFormField, | ||
| questionOptions: { ...prevFormField.questionOptions, workspaceName: event.target.value }, | ||
| })); | ||
| }, | ||
| [setFormField], | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <TextInput | ||
| id="buttonLabel" | ||
| labelText={t('buttonLabel', 'Button Label')} | ||
| value={buttonLabel} | ||
| onChange={handleButtonLabelChange} | ||
| placeholder={t('buttonLabelPlaceholder', 'Enter text to display on the button')} | ||
| required | ||
| /> | ||
| <TextInput | ||
| id="workspaceName" | ||
| labelText={t('workspaceName', 'Workspace Name')} | ||
| value={workspaceName} | ||
| onChange={handleWorkspaceNameChange} | ||
| placeholder={t('workspaceNamePlaceholder', 'Enter the name of the workspace to launch')} | ||
| required | ||
| /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default React.memo(WorkspaceLauncher); |
89 changes: 89 additions & 0 deletions
89
...stion/question-form/rendering-types/inputs/workspace-launcher/workspace-launcher.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,89 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import WorkspaceLauncher from './workspace-launcher.component'; | ||
| import { FormFieldProvider } from '../../../../form-field-context'; | ||
| import type { FormField } from '@openmrs/esm-form-engine-lib'; | ||
|
|
||
| const mockSetFormField = jest.fn(); | ||
| const formField: FormField = { | ||
| type: 'obs', | ||
| questionOptions: { | ||
| rendering: 'workspace-launcher' as const, | ||
| buttonLabel: '', | ||
| workspaceName: '', | ||
| }, | ||
| id: '1', | ||
| }; | ||
|
|
||
| jest.mock('../../../../form-field-context', () => ({ | ||
| ...jest.requireActual('../../../../form-field-context'), | ||
| useFormField: () => ({ formField, setFormField: mockSetFormField }), | ||
| })); | ||
|
|
||
| describe('WorkspaceLauncher', () => { | ||
| it('renders workspace launcher inputs', () => { | ||
| renderWorkspaceLauncher(); | ||
| expect(screen.getByLabelText(/Button Label/i)).toBeInTheDocument(); | ||
| expect(screen.getByLabelText(/Workspace Name/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('displays placeholder text for both inputs', () => { | ||
| renderWorkspaceLauncher(); | ||
| expect(screen.getByPlaceholderText(/Enter text to display on the button/i)).toBeInTheDocument(); | ||
| expect(screen.getByPlaceholderText(/Enter the name of the workspace to launch/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('marks both inputs as required', () => { | ||
| renderWorkspaceLauncher(); | ||
| const buttonLabelInput = screen.getByLabelText(/Button Label/i); | ||
| const workspaceNameInput = screen.getByLabelText(/Workspace Name/i); | ||
|
|
||
| expect(buttonLabelInput).toBeRequired(); | ||
| expect(workspaceNameInput).toBeRequired(); | ||
| }); | ||
|
|
||
| it('updates button label when input changes', async () => { | ||
| renderWorkspaceLauncher(); | ||
| const user = userEvent.setup(); | ||
| const buttonLabelInput = screen.getByLabelText(/Button Label/i); | ||
| const newValue = 'Launch Patient Dashboard'; | ||
|
|
||
| await user.clear(buttonLabelInput); | ||
| await user.type(buttonLabelInput, newValue); | ||
|
|
||
| expect(mockSetFormField).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('updates workspace name when input changes', async () => { | ||
| renderWorkspaceLauncher(); | ||
| const user = userEvent.setup(); | ||
| const workspaceNameInput = screen.getByLabelText(/Workspace Name/i); | ||
| const newValue = 'patient-dashboard'; | ||
|
|
||
| await user.clear(workspaceNameInput); | ||
| await user.type(workspaceNameInput, newValue); | ||
|
|
||
| expect(mockSetFormField).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('handles pasting text correctly', async () => { | ||
| renderWorkspaceLauncher(); | ||
| const user = userEvent.setup(); | ||
| const workspaceNameInput = screen.getByLabelText(/Workspace Name/i); | ||
| const textToPaste = 'pasted-workspace-name'; | ||
|
|
||
| await user.clear(workspaceNameInput); | ||
| await user.paste(textToPaste); | ||
|
|
||
| expect(mockSetFormField).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| function renderWorkspaceLauncher() { | ||
| render( | ||
| <FormFieldProvider initialFormField={formField}> | ||
| <WorkspaceLauncher /> | ||
| </FormFieldProvider>, | ||
| ); | ||
| } | ||
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
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
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.
This is good to have, but we can make it a bit better by checking if it got called with the right object. Here's a place where you can use for guidance -
openmrs-esm-form-builder/src/components/interactive-builder/modals/question/question-form/question-types/inputs/obs/obs-type-question.test.tsx
Lines 115 to 129 in d80c228