This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
feat(metadata): Metadata configuration UI #1951
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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 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 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 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,35 @@ | ||
import { AddPropertyButtons } from './AddPropertyButtons'; | ||
import { screen } from '@testing-library/dom'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
describe('AddPropertyButtons.tsx', () => { | ||
test('Add string property button', () => { | ||
const events: boolean[] = []; | ||
render( | ||
<AddPropertyButtons | ||
path={['foo', 'bar']} | ||
createPlaceholder={(isObject) => events.push(isObject)} | ||
/>, | ||
); | ||
const element = screen.getByTestId('properties-add-string-property-foo-bar-btn'); | ||
expect(events.length).toBe(0); | ||
fireEvent.click(element); | ||
expect(events.length).toBe(1); | ||
expect(events[0]).toBeFalsy(); | ||
}); | ||
|
||
test('Add object property button', () => { | ||
const events: boolean[] = []; | ||
render( | ||
<AddPropertyButtons | ||
path={['foo', 'bar']} | ||
createPlaceholder={(isObject) => events.push(isObject)} | ||
/>, | ||
); | ||
const element = screen.getByTestId('properties-add-object-property-foo-bar-btn'); | ||
expect(events.length).toBe(0); | ||
fireEvent.click(element); | ||
expect(events.length).toBe(1); | ||
expect(events[0]).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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,52 @@ | ||
import { Button, Split, SplitItem, Tooltip } from '@patternfly/react-core'; | ||
import { FolderPlusIcon, PlusCircleIcon } from '@patternfly/react-icons'; | ||
|
||
type AddPropertyPopoverProps = { | ||
showLabel?: boolean; | ||
path: string[]; | ||
disabled?: boolean; | ||
createPlaceholder: (isObject: boolean) => void; | ||
}; | ||
|
||
/** | ||
* A set of "add string property" and "add object property" buttons which triggers creating a placeholder. | ||
* @param props | ||
* @constructor | ||
*/ | ||
export function AddPropertyButtons({ | ||
showLabel = false, | ||
path, | ||
disabled = false, | ||
createPlaceholder, | ||
}: AddPropertyPopoverProps) { | ||
return ( | ||
<Split> | ||
<SplitItem> | ||
<Tooltip content="Add string property"> | ||
<Button | ||
data-testid={`properties-add-string-property-${path.join('-')}-btn`} | ||
variant={'link'} | ||
icon={<PlusCircleIcon />} | ||
isDisabled={disabled} | ||
onClick={() => createPlaceholder(false)} | ||
> | ||
{showLabel && 'Add string property'} | ||
</Button> | ||
</Tooltip> | ||
</SplitItem> | ||
<SplitItem> | ||
<Tooltip content="Add object property"> | ||
<Button | ||
data-testid={`properties-add-object-property-${path.join('-')}-btn`} | ||
variant={'link'} | ||
icon={<FolderPlusIcon />} | ||
isDisabled={disabled} | ||
onClick={() => createPlaceholder(true)} | ||
> | ||
{showLabel && 'Add object property'} | ||
</Button> | ||
</Tooltip> | ||
</SplitItem> | ||
</Split> | ||
); | ||
} |
This file contains 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,25 @@ | ||
import { FieldLabelIcon } from '../FieldLabelIcon'; | ||
import PropertiesField from './PropertiesField'; | ||
import JSONSchemaBridge from 'uniforms-bridge-json-schema'; | ||
|
||
/** | ||
* Add {@link PropertiesField} custom field for adding generic properties editor. | ||
*/ | ||
export class MetadataEditorBridge extends JSONSchemaBridge { | ||
getField(name: string): Record<string, any> { | ||
const field = super.getField(name); | ||
const { defaultValue, description, ...props } = field; | ||
const revisedField: Record<string, any> = { | ||
labelIcon: description | ||
? FieldLabelIcon({ defaultValue, description, disabled: false }) | ||
: undefined, | ||
...props, | ||
}; | ||
if (revisedField.type === 'object' && !revisedField.properties) { | ||
revisedField.uniforms = { | ||
component: PropertiesField, | ||
}; | ||
} | ||
return revisedField; | ||
} | ||
} |
This file contains 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,19 @@ | ||
.metadataEditorModal { | ||
height: 90vh; | ||
width: 90vw; | ||
} | ||
|
||
.metadataEditorModalListView { | ||
width: 50vw; | ||
} | ||
|
||
.metadataEditorModalDetailsView { | ||
width: 50vw; | ||
} | ||
|
||
.propertyRow { | ||
--pf-c-table--m-compact--cell--PaddingTop: 0px; | ||
--pf-c-table--m-compact--cell--PaddingBottom: 0px; | ||
--pf-c-table--m-compact--cell--PaddingRight: 0px; | ||
--pf-c-table--m-compact--cell--PaddingLeft: 0px; | ||
} |
This file contains 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,43 @@ | ||
import { MetadataEditorModal } from './MetadataEditorModal'; | ||
import { mockSchema } from './TestUtil'; | ||
import { useArgs } from '@storybook/client-api'; | ||
import { StoryFn, Meta } from '@storybook/react'; | ||
|
||
export default { | ||
title: 'Metadata/MetadataEditorModal', | ||
component: MetadataEditorModal, | ||
excludeStories: ['schemaMock'], | ||
decorators: [ | ||
(Story) => ( | ||
<div style={{ margin: '3em' }}> | ||
<Story /> | ||
</div> | ||
), | ||
], | ||
argTypes: { handleCloseModal: { action: 'clicked' } }, | ||
} as Meta<typeof MetadataEditorModal>; | ||
|
||
const Template: StoryFn<typeof MetadataEditorModal> = (args) => { | ||
const [{ isModalOpen }, updateArgs] = useArgs(); | ||
const handleClose = () => updateArgs({ isModalOpen: !isModalOpen }); | ||
return ( | ||
<> | ||
<button onClick={() => updateArgs({ isModalOpen: !isModalOpen })}> | ||
Open Metadata Editor Modal | ||
</button> | ||
<MetadataEditorModal {...args} handleCloseModal={handleClose} /> | ||
</> | ||
); | ||
}; | ||
|
||
export const BeansArray = Template.bind({}); | ||
BeansArray.args = { | ||
name: 'beans', | ||
schema: mockSchema.beans, | ||
}; | ||
|
||
export const SingleObject = Template.bind({}); | ||
SingleObject.args = { | ||
name: 'singleObject', | ||
schema: mockSchema.single, | ||
}; |
This file contains 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,105 @@ | ||
import { MetadataEditorModal } from './MetadataEditorModal'; | ||
import { mockModel, mockSchema } from './TestUtil'; | ||
import { useFlowsStore } from '@kaoto/store'; | ||
import { screen } from '@testing-library/dom'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
describe('MetadataEditorModal.tsx', () => { | ||
test('component renders if open', () => { | ||
useFlowsStore.getState().setMetadata('beans', []); | ||
render( | ||
<MetadataEditorModal | ||
handleCloseModal={jest.fn()} | ||
isModalOpen={true} | ||
name="beans" | ||
schema={mockSchema.beans} | ||
/>, | ||
); | ||
const element = screen.queryByTestId('metadata-beans-modal'); | ||
expect(element).toBeInTheDocument(); | ||
}); | ||
|
||
test('component does not render if closed', () => { | ||
useFlowsStore.getState().setMetadata('beans', []); | ||
render( | ||
<MetadataEditorModal | ||
handleCloseModal={jest.fn()} | ||
isModalOpen={false} | ||
name="beans" | ||
schema={mockSchema.beans} | ||
/>, | ||
); | ||
const element = screen.queryByTestId('metadata-beans-modal'); | ||
expect(element).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('Details disabled if empty', async () => { | ||
useFlowsStore.getState().setMetadata('beans', []); | ||
render( | ||
<MetadataEditorModal | ||
handleCloseModal={jest.fn()} | ||
isModalOpen={true} | ||
name="beans" | ||
schema={mockSchema.beans} | ||
/>, | ||
); | ||
const inputs = screen | ||
.getAllByTestId('text-field') | ||
.filter((input) => input.getAttribute('name') === 'name'); | ||
expect(inputs.length).toBe(1); | ||
expect(inputs[0]).toBeDisabled(); | ||
const addStringPropBtn = screen.getByTestId('properties-add-string-property--btn'); | ||
expect(addStringPropBtn).toBeDisabled(); | ||
const addObjectPropBtn = screen.getByTestId('properties-add-object-property--btn'); | ||
expect(addObjectPropBtn).toBeDisabled(); | ||
}); | ||
|
||
test('Details enabled if select', async () => { | ||
useFlowsStore.getState().setMetadata('beans', mockModel.beans); | ||
render( | ||
<MetadataEditorModal | ||
handleCloseModal={jest.fn()} | ||
isModalOpen={true} | ||
name="beans" | ||
schema={mockSchema.beans} | ||
/>, | ||
); | ||
const row = screen.getByTestId('metadata-row-0'); | ||
fireEvent.click(row); | ||
const inputs = screen | ||
.getAllByTestId('text-field') | ||
.filter((input) => input.getAttribute('name') === 'name'); | ||
expect(inputs.length).toBe(1); | ||
expect(inputs[0]).toBeEnabled(); | ||
const addStringPropBtn = screen.getByTestId('properties-add-string-property--btn'); | ||
expect(addStringPropBtn).toBeEnabled(); | ||
const addObjectPropBtn = screen.getByTestId('properties-add-object-property--btn'); | ||
expect(addObjectPropBtn).toBeEnabled(); | ||
const propObj2AddStringPropBtn = screen.getByTestId( | ||
'properties-add-string-property-propObj1-btn', | ||
); | ||
fireEvent.click(propObj2AddStringPropBtn); | ||
const input = screen.getByTestId('properties-propObj1-placeholder-name-input'); | ||
fireEvent.input(input, { target: { value: 'propObj1Child' } }); | ||
fireEvent.blur(input); | ||
}); | ||
|
||
test('render properties empty state', async () => { | ||
useFlowsStore.getState().setMetadata('beans', mockModel.beansNoProp); | ||
render( | ||
<MetadataEditorModal | ||
handleCloseModal={jest.fn()} | ||
isModalOpen={true} | ||
name="beans" | ||
schema={mockSchema.beans} | ||
/>, | ||
); | ||
const row = screen.getByTestId('metadata-row-0'); | ||
fireEvent.click(row); | ||
let addStringPropBtns = screen.getAllByTestId('properties-add-string-property--btn'); | ||
expect(addStringPropBtns.length).toBe(2); | ||
fireEvent.click(addStringPropBtns[1]); | ||
addStringPropBtns = screen.getAllByTestId('properties-add-string-property--btn'); | ||
expect(addStringPropBtns.length).toBe(1); | ||
}); | ||
}); |
Oops, something went wrong.
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.
The content of this file is only exporting one single function, in which case, wouldn't be enough to do something like below?
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.
I'm curious if it's preferred in typescript? Generally the wildcard is not preferred in Java coding standards I know
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.
In this case, it's ok as we're exposing tokens and not necessarily mean that they will be incorporated in the final bundle, is mostly convenience I would say.