Skip to content

Commit ed19ff7

Browse files
committed
cleanup and another test
1 parent 60dbf85 commit ed19ff7

File tree

3 files changed

+93
-29
lines changed

3 files changed

+93
-29
lines changed

packages/compass-data-modeling/src/components/diagram-editor.spec.tsx

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ const mockDiagramming = {
2626

2727
import React from 'react';
2828
import { expect } from 'chai';
29-
import {
30-
screen,
31-
userEvent,
32-
waitFor,
33-
} from '@mongodb-js/testing-library-compass';
29+
import { screen, waitFor } from '@mongodb-js/testing-library-compass';
3430
import DiagramEditor from './diagram-editor';
3531
import { renderWithOpenedDiagramStore } from '../../test/setup-store';
3632
import type { DataModelingStore } from '../../test/setup-store';
@@ -43,10 +39,43 @@ import { DiagramProvider } from '@mongodb-js/diagramming';
4339

4440
const storageItems: MongoDBDataModelDescription[] = [
4541
{
46-
id: '1',
42+
id: 'existing-diagram-id',
4743
name: 'One',
4844
createdAt: '2023-10-01T00:00:00.000Z',
4945
updatedAt: '2023-10-03T00:00:00.000Z',
46+
edits: [
47+
{
48+
id: 'edit-id-1',
49+
timestamp: '2023-10-02T00:00:00.000Z',
50+
type: 'SetModel',
51+
model: {
52+
collections: [
53+
{
54+
ns: 'db1.collection1',
55+
indexes: [],
56+
displayPosition: [50, 50],
57+
shardKey: {},
58+
jsonSchema: { bsonType: 'object' },
59+
},
60+
{
61+
ns: 'db1.collection2',
62+
indexes: [],
63+
displayPosition: [150, 150],
64+
shardKey: {},
65+
jsonSchema: { bsonType: 'object' },
66+
},
67+
],
68+
relationships: [],
69+
},
70+
},
71+
],
72+
connectionId: null,
73+
},
74+
{
75+
id: 'new-diagram-id',
76+
name: 'Two',
77+
createdAt: '2023-10-01T00:00:00.000Z',
78+
updatedAt: '2023-10-03T00:00:00.000Z',
5079
edits: [
5180
{
5281
id: 'edit-id-1',
@@ -79,8 +108,10 @@ const storageItems: MongoDBDataModelDescription[] = [
79108

80109
const renderDiagramEditor = async ({
81110
items = storageItems,
111+
renderedItem = items[0],
82112
}: {
83113
items?: MongoDBDataModelDescription[];
114+
renderedItem?: MongoDBDataModelDescription;
84115
} = {}) => {
85116
const mockDataModelStorage = {
86117
status: 'READY',
@@ -108,38 +139,73 @@ const renderDiagramEditor = async ({
108139
dataModelStorage: mockDataModelStorage,
109140
},
110141
},
111-
items[0]
142+
renderedItem
112143
);
113144
return result;
114145
};
115146

116147
describe.only('DiagramEditor', function () {
117148
let store: DataModelingStore;
118149

119-
beforeEach(async function () {
120-
const result = await renderDiagramEditor();
121-
store = result.store;
150+
context('with initial diagram', function () {
151+
beforeEach(async function () {
152+
const result = await renderDiagramEditor({
153+
renderedItem: storageItems[1],
154+
});
155+
store = result.store;
156+
157+
// wait till the editor is loaded
158+
await waitFor(() => {
159+
expect(screen.getByTestId('model-preview')).to.be.visible;
160+
});
161+
});
162+
163+
it('applies the initial layout to unpositioned nodes', async function () {
164+
const state = store.getState();
122165

123-
// wait till the editor is loaded
124-
await waitFor(() => {
125-
expect(screen.getByTestId('model-preview')).to.be.visible;
166+
expect(state.diagram?.edits.current).to.have.lengthOf(1);
167+
expect(state.diagram?.edits.current[0].type).to.equal('SetModel');
168+
const initialEdit = state.diagram?.edits.current[0] as Extract<
169+
Edit,
170+
{ type: 'SetModel' }
171+
>;
172+
expect(initialEdit.model?.collections[0].displayPosition).to.deep.equal([
173+
100, 100,
174+
]);
175+
expect(initialEdit.model?.collections[1].displayPosition).to.deep.equal([
176+
200, 200,
177+
]);
126178
});
127179
});
128180

129-
it('applies the initial layout to unpositioned nodes', async function () {
130-
const state = store.getState();
181+
context('with existing diagram', function () {
182+
beforeEach(async function () {
183+
const result = await renderDiagramEditor({
184+
renderedItem: storageItems[0],
185+
});
186+
store = result.store;
131187

132-
expect(state.diagram?.edits.current).to.have.lengthOf(1);
133-
expect(state.diagram?.edits.current[0].type).to.equal('SetModel');
134-
const initialEdit = state.diagram?.edits.current[0] as Extract<
135-
Edit,
136-
{ type: 'SetModel' }
137-
>;
138-
expect(initialEdit.model?.collections[0].displayPosition).to.deep.equal([
139-
100, 100,
140-
]);
141-
expect(initialEdit.model?.collections[1].displayPosition).to.deep.equal([
142-
200, 200,
143-
]);
188+
// wait till the editor is loaded
189+
await waitFor(() => {
190+
expect(screen.getByTestId('model-preview')).to.be.visible;
191+
});
192+
});
193+
194+
it('does not change the position of the nodes', async function () {
195+
const state = store.getState();
196+
197+
expect(state.diagram?.edits.current).to.have.lengthOf(1);
198+
expect(state.diagram?.edits.current[0].type).to.equal('SetModel');
199+
const initialEdit = state.diagram?.edits.current[0] as Extract<
200+
Edit,
201+
{ type: 'SetModel' }
202+
>;
203+
expect(initialEdit.model?.collections[0].displayPosition).to.deep.equal(
204+
storageItems[0].edits[0].model.collections[0].displayPosition
205+
);
206+
expect(initialEdit.model?.collections[1].displayPosition).to.deep.equal(
207+
storageItems[0].edits[0].model.collections[1].displayPosition
208+
);
209+
});
144210
});
145211
});

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ export function getCurrentModel(
354354
description: MongoDBDataModelDescription
355355
): StaticModel {
356356
// Get the last 'SetModel' edit.
357-
console.log('DESCRIPTION', description);
358357
const reversedSetModelEditIndex = description.edits
359358
.slice()
360359
.reverse()

packages/compass-data-modeling/test/setup-store.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ export const renderWithOpenedDiagramStore = async (
218218
store.dispatch(openDiagram(diagram));
219219
await waitFor(() => {
220220
expect(store.getState().diagram?.edits.current).to.have.lengthOf(1);
221-
console.log('HAS A DIAGRAM', store.getState().diagram?.edits.current[0]);
222221
});
223222
const renderResult = renderWithConnections(
224223
<Provider store={store}>{component}</Provider>,

0 commit comments

Comments
 (0)