forked from KaotoIO/kaoto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Temp]: Rework how to add new Entities to the canvas
fix: KaotoIO#1030
- Loading branch information
Showing
28 changed files
with
1,162 additions
and
294 deletions.
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
43 changes: 43 additions & 0 deletions
43
...omponents/Visualization/ContextToolbar/DSLSelector/ChangeDSLModal/ChangeDSLModal.test.tsx
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 { fireEvent, render } from '@testing-library/react'; | ||
import { ChangeDSLModal } from './ChangeDSLModal'; | ||
|
||
describe('ChangeDSLModal', () => { | ||
it('should be hidden when isOpen is false', () => { | ||
const wrapper = render(<ChangeDSLModal isOpen={false} onConfirm={jest.fn()} onCancel={jest.fn()} />); | ||
|
||
expect(wrapper.queryByTestId('confirmation-modal')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should be visible when isOpen is true', () => { | ||
const wrapper = render(<ChangeDSLModal isOpen={true} onConfirm={jest.fn()} onCancel={jest.fn()} />); | ||
|
||
expect(wrapper.queryByTestId('confirmation-modal')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onConfirm when confirm button is clicked', () => { | ||
const onConfirm = jest.fn(); | ||
const wrapper = render(<ChangeDSLModal isOpen={true} onConfirm={onConfirm} onCancel={jest.fn()} />); | ||
|
||
fireEvent.click(wrapper.getByTestId('confirmation-modal-confirm')); | ||
|
||
expect(onConfirm).toBeCalled(); | ||
}); | ||
|
||
it('should call onCancel when cancel button is clicked', () => { | ||
const onCancel = jest.fn(); | ||
const wrapper = render(<ChangeDSLModal isOpen={true} onConfirm={jest.fn()} onCancel={onCancel} />); | ||
|
||
fireEvent.click(wrapper.getByTestId('confirmation-modal-cancel')); | ||
|
||
expect(onCancel).toBeCalled(); | ||
}); | ||
|
||
it('should call onCancel when close button is clicked', () => { | ||
const onCancel = jest.fn(); | ||
const wrapper = render(<ChangeDSLModal isOpen={true} onConfirm={jest.fn()} onCancel={onCancel} />); | ||
|
||
fireEvent.click(wrapper.getByLabelText('Close')); | ||
|
||
expect(onCancel).toBeCalled(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
...src/components/Visualization/ContextToolbar/DSLSelector/ChangeDSLModal/ChangeDSLModal.tsx
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,34 @@ | ||
import { Button, Modal, ModalVariant } from '@patternfly/react-core'; | ||
import { FunctionComponent } from 'react'; | ||
|
||
interface ChangeDSLModalProps { | ||
isOpen: boolean; | ||
onConfirm: () => void; | ||
onCancel: () => void; | ||
} | ||
|
||
export const ChangeDSLModal: FunctionComponent<ChangeDSLModalProps> = (props) => { | ||
return ( | ||
<Modal | ||
variant={ModalVariant.small} | ||
title="Warning" | ||
data-testid="confirmation-modal" | ||
titleIconVariant="warning" | ||
onClose={props.onCancel} | ||
actions={[ | ||
<Button key="confirm" variant="primary" data-testid="confirmation-modal-confirm" onClick={props.onConfirm}> | ||
Confirm | ||
</Button>, | ||
<Button key="cancel" variant="link" data-testid="confirmation-modal-cancel" onClick={props.onCancel}> | ||
Cancel | ||
</Button>, | ||
]} | ||
isOpen={props.isOpen} | ||
> | ||
<p> | ||
This will remove any existing integration and you will lose your current work. Are you sure you would like to | ||
proceed? | ||
</p> | ||
</Modal> | ||
); | ||
}; |
81 changes: 81 additions & 0 deletions
81
packages/ui/src/components/Visualization/ContextToolbar/DSLSelector/DSLSelector.test.tsx
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,81 @@ | ||
import { act, fireEvent, render } from '@testing-library/react'; | ||
import { EntitiesContextResult } from '../../../../hooks'; | ||
import { KaotoSchemaDefinition } from '../../../../models'; | ||
import { SourceSchemaType, sourceSchemaConfig } from '../../../../models/camel'; | ||
import { EntitiesContext } from '../../../../providers/entities.provider'; | ||
import { SourceCodeApiContext } from '../../../../providers/source-code.provider'; | ||
import { DSLSelector } from './DSLSelector'; | ||
|
||
describe('DSLSelector.tsx', () => { | ||
const config = sourceSchemaConfig; | ||
config.config[SourceSchemaType.Integration].schema = { | ||
schema: { name: 'Integration', description: 'desc' } as KaotoSchemaDefinition['schema'], | ||
} as KaotoSchemaDefinition; | ||
config.config[SourceSchemaType.Pipe].schema = { | ||
schema: { name: 'Pipe', description: 'desc' } as KaotoSchemaDefinition['schema'], | ||
} as KaotoSchemaDefinition; | ||
config.config[SourceSchemaType.Kamelet].schema = { | ||
schema: { name: 'Kamelet', description: 'desc' } as KaotoSchemaDefinition['schema'], | ||
} as KaotoSchemaDefinition; | ||
config.config[SourceSchemaType.KameletBinding].schema = { | ||
name: 'kameletBinding', | ||
schema: { description: 'desc' }, | ||
} as KaotoSchemaDefinition; | ||
config.config[SourceSchemaType.Route].schema = { | ||
schema: { name: 'route', description: 'desc' } as KaotoSchemaDefinition['schema'], | ||
} as KaotoSchemaDefinition; | ||
|
||
const renderWithContext = () => { | ||
return render( | ||
<SourceCodeApiContext.Provider | ||
value={{ | ||
setCodeAndNotify: jest.fn(), | ||
}} | ||
> | ||
<EntitiesContext.Provider | ||
value={ | ||
{ | ||
currentSchemaType: SourceSchemaType.Integration, | ||
} as unknown as EntitiesContextResult | ||
} | ||
> | ||
<DSLSelector /> | ||
</EntitiesContext.Provider> | ||
</SourceCodeApiContext.Provider>, | ||
); | ||
}; | ||
|
||
it('should render all of the types', async () => { | ||
const wrapper = renderWithContext(); | ||
const trigger = await wrapper.findByTestId('dsl-list-dropdown'); | ||
|
||
/** Open Select */ | ||
act(() => { | ||
fireEvent.click(trigger); | ||
}); | ||
|
||
for (const name of ['Pipe', 'Camel Route']) { | ||
const element = await wrapper.findByText(name); | ||
expect(element).toBeInTheDocument(); | ||
} | ||
}); | ||
|
||
it('should warn the user when adding a different type of flow', async () => { | ||
const wrapper = renderWithContext(); | ||
const trigger = await wrapper.findByTestId('dsl-list-dropdown'); | ||
|
||
/** Open Select */ | ||
act(() => { | ||
fireEvent.click(trigger); | ||
}); | ||
|
||
/** Select an option */ | ||
act(() => { | ||
const element = wrapper.getByText('Pipe'); | ||
fireEvent.click(element); | ||
}); | ||
|
||
const modal = await wrapper.findByTestId('confirmation-modal'); | ||
expect(modal).toBeInTheDocument(); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/ui/src/components/Visualization/ContextToolbar/DSLSelector/DSLSelector.tsx
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,39 @@ | ||
import { FunctionComponent, PropsWithChildren, useCallback, useContext, useState } from 'react'; | ||
import { SourceSchemaType } from '../../../../models/camel'; | ||
import { FlowTemplateService } from '../../../../models/visualization/flows/support/flow-templates-service'; | ||
import { SourceCodeApiContext } from '../../../../providers'; | ||
import { ChangeDSLModal } from './ChangeDSLModal/ChangeDSLModal'; | ||
import { DSLSelectorToggle } from './DSLSelectorToggle/DSLSelectorToggle'; | ||
|
||
export const DSLSelector: FunctionComponent<PropsWithChildren> = () => { | ||
const sourceCodeContextApi = useContext(SourceCodeApiContext); | ||
const [isConfirmationModalOpen, setIsConfirmationModalOpen] = useState(false); | ||
const [proposedFlowType, setProposedFlowType] = useState<SourceSchemaType>(); | ||
|
||
const checkBeforeAddNewFlow = useCallback((flowType: SourceSchemaType) => { | ||
/** | ||
* If it is not the same DSL, this operation might result in | ||
* removing the existing flows, so then we warn the user first | ||
*/ | ||
setProposedFlowType(flowType); | ||
setIsConfirmationModalOpen(true); | ||
}, []); | ||
|
||
const onConfirm = useCallback(() => { | ||
if (proposedFlowType) { | ||
sourceCodeContextApi.setCodeAndNotify(FlowTemplateService.getFlowYamlTemplate(proposedFlowType)); | ||
setIsConfirmationModalOpen(false); | ||
} | ||
}, [proposedFlowType, sourceCodeContextApi]); | ||
|
||
const onCancel = useCallback(() => { | ||
setIsConfirmationModalOpen(false); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<DSLSelectorToggle onSelect={checkBeforeAddNewFlow} /> | ||
<ChangeDSLModal isOpen={isConfirmationModalOpen} onConfirm={onConfirm} onCancel={onCancel} /> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.