Skip to content

feat(data-modeling): node selection and relationship editing store and actions COMPASS-9476 #7118

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React from 'react';
import { connect } from 'react-redux';
import type { Relationship } from '../services/data-model-storage';
import { Button, H3 } from '@mongodb-js/compass-components';
import {
deleteRelationship,
getCurrentDiagramFromState,
selectCurrentModel,
} from '../store/diagram';
import type { DataModelingState } from '../store/reducer';
import {
createNewRelationship,
startRelationshipEdit,
} from '../store/side-panel';
import RelationshipDrawerContent from './relationship-drawer-content';

type CollectionDrawerContentProps = {
namespace: string;
relationships: Relationship[];
shouldShowRelationshipEditingForm?: boolean;
onCreateNewRelationshipClick: (namespace: string) => void;
onEditRelationshipClick: (relationship: Relationship) => void;
onDeleteRelationshipClick: (rId: string) => void;
};

const CollectionDrawerContent: React.FunctionComponent<
CollectionDrawerContentProps
> = ({
namespace,
relationships,
shouldShowRelationshipEditingForm,
onCreateNewRelationshipClick,
onEditRelationshipClick,
onDeleteRelationshipClick,
}) => {
if (shouldShowRelationshipEditingForm) {
return <RelationshipDrawerContent></RelationshipDrawerContent>;
}

return (
<>
<H3>{namespace}</H3>
<ul>
{relationships.map((r) => {
return (
<li key={r.id} data-relationship-id={r.id}>
{r.relationship[0].fields.join(', ')}&nbsp;-&gt;&nbsp;
{r.relationship[1].fields.join(', ')}
<Button
onClick={() => {
onEditRelationshipClick(r);
}}
>
Edit
</Button>
<Button
onClick={() => {
onDeleteRelationshipClick(r.id);
}}
>
Delete
</Button>
</li>
);
})}
</ul>
<Button
onClick={() => {
onCreateNewRelationshipClick(namespace);
}}
>
Add relationship manually
</Button>
</>
);
};

export default connect(
(state: DataModelingState, ownProps: { namespace: string }) => {
return {
relationships: selectCurrentModel(
getCurrentDiagramFromState(state).edits
).relationships.filter((r) => {
const [local, foreign] = r.relationship;
return (
local.ns === ownProps.namespace || foreign.ns === ownProps.namespace
);
}),
shouldShowRelationshipEditingForm:
state.sidePanel.viewType === 'relationship-editing',
};
},
{
onCreateNewRelationshipClick: createNewRelationship,
onEditRelationshipClick: startRelationshipEdit,
onDeleteRelationshipClick: deleteRelationship,
}
)(CollectionDrawerContent);
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import React from 'react';
import { expect } from 'chai';
import {
createPluginTestHelpers,
screen,
waitFor,
userEvent,
within,
} from '@mongodb-js/testing-library-compass';
import { DataModelingWorkspaceTab } from '../index';
import DiagramEditorSidePanel from './diagram-editor-side-panel';
import {
getCurrentDiagramFromState,
openDiagram,
selectCollection,
selectCurrentModel,
selectRelationship,
} from '../store/diagram';
import dataModel from '../../test/fixtures/data-model-with-relationships.json';
import type { MongoDBDataModelDescription } from '../services/data-model-storage';

async function comboboxSelectItem(
label: string,
value: string,
visibleLabel = value
) {
userEvent.click(screen.getByRole('textbox', { name: label }));
await waitFor(() => {
screen.getByRole('option', { name: visibleLabel });
});
userEvent.click(screen.getByRole('option', { name: visibleLabel }));
await waitFor(() => {
expect(screen.getByRole('textbox', { name: label })).to.have.attribute(
'value',
value
);
});
}

describe('DiagramEditorSidePanel', function () {
function renderDrawer() {
const { renderWithConnections } = createPluginTestHelpers(
DataModelingWorkspaceTab.provider.withMockServices({})
);
const result = renderWithConnections(
<DiagramEditorSidePanel></DiagramEditorSidePanel>
);
result.plugin.store.dispatch(
openDiagram(dataModel as MongoDBDataModelDescription)
);
return result;
}

it('should not render if no items are selected', function () {
renderDrawer();
});

it('should render a collection context drawer when collection is clicked', async function () {
const result = renderDrawer();
await result.plugin.store.dispatch(selectCollection('flights.airlines'));
expect(screen.getByText('flights.airlines')).to.be.visible;
});

it('should render a relationship context drawer when relations is clicked', async function () {
const result = renderDrawer();
await result.plugin.store.dispatch(
selectRelationship('204b1fc0-601f-4d62-bba3-38fade71e049')
);
expect(screen.getByText('Edit Relationship')).to.be.visible;
expect(
document.querySelector(
'[data-relationship-id="204b1fc0-601f-4d62-bba3-38fade71e049"]'
)
).to.exist;
});

it('should open and edit relationship starting from collection', async function () {
const result = renderDrawer();
await result.plugin.store.dispatch(selectCollection('flights.countries'));

// Open relationshipt editing form
const relationshipCard = document.querySelector<HTMLElement>(
'[data-relationship-id="204b1fc0-601f-4d62-bba3-38fade71e049"]'
);
userEvent.click(
within(relationshipCard!).getByRole('button', { name: 'Edit' })
);
expect(screen.getByText('Edit Relationship')).to.be.visible;

// Select new values
await comboboxSelectItem('Local collection', 'planes');
await comboboxSelectItem('Local field', 'name');
await comboboxSelectItem('Foreign collection', 'countries');
await comboboxSelectItem('Foreign field', 'iso_code');

userEvent.click(screen.getByRole('button', { name: 'Save' }));

// We should be testing through rendered UI but as it's really hard to make
// diagram rendering in tests property, we are just validating the final
// model here
const modifiedRelationship = selectCurrentModel(
getCurrentDiagramFromState(result.plugin.store.getState()).edits
).relationships.find((r) => {
return r.id === '204b1fc0-601f-4d62-bba3-38fade71e049';
});

expect(modifiedRelationship)
.to.have.property('relationship')
.deep.eq([
{
ns: 'flights.planes',
fields: ['name'],
cardinality: 1,
},
{
ns: 'flights.countries',
fields: ['iso_code'],
cardinality: 1,
},
]);

// After saving when starting from collection, we should end up back in the
// collection drawer that we started from
expect(screen.getByText('flights.countries')).to.be.visible;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ import {
Button,
css,
cx,
Body,
spacing,
palette,
useDarkMode,
} from '@mongodb-js/compass-components';
import CollectionDrawerContent from './collection-drawer-content';
import RelationshipDrawerContent from './relationship-drawer-content';

const containerStyles = css({
width: '400px',
height: '100%',

display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
gap: spacing[400],
borderLeft: `1px solid ${palette.gray.light2}`,
});

Expand All @@ -29,21 +23,35 @@ const darkModeContainerStyles = css({
});

type DiagramEditorSidePanelProps = {
isOpen: boolean;
selectedItems: { type: 'relationship' | 'collection'; id: string } | null;
onClose: () => void;
};

function DiagmramEditorSidePanel({
isOpen,
selectedItems,
onClose,
}: DiagramEditorSidePanelProps) {
const isDarkMode = useDarkMode();
if (!isOpen) {

if (!selectedItems) {
return null;
}

let content;

if (selectedItems.type === 'collection') {
content = (
<CollectionDrawerContent
namespace={selectedItems.id}
></CollectionDrawerContent>
);
} else if (selectedItems.type === 'relationship') {
content = <RelationshipDrawerContent></RelationshipDrawerContent>;
}

return (
<div className={cx(containerStyles, isDarkMode && darkModeContainerStyles)}>
<Body>This feature is under development.</Body>
{content}
<Button onClick={onClose} variant="primary" size="small">
Close Side Panel
</Button>
Expand All @@ -53,9 +61,8 @@ function DiagmramEditorSidePanel({

export default connect(
(state: DataModelingState) => {
const { sidePanel } = state;
return {
isOpen: sidePanel.isOpen,
selectedItems: state.diagram?.selectedItems ?? null,
};
},
{
Expand Down
Loading
Loading