Skip to content

Commit 1d3464d

Browse files
committed
fix tests
1 parent 0e16a8f commit 1d3464d

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useState } from 'react';
1+
import React, { useEffect, useMemo, useState } from 'react';
22
import { connect } from 'react-redux';
33
import type { DataModelingState } from '../store/reducer';
44
import {
@@ -27,6 +27,7 @@ import {
2727
Diagram,
2828
type NodeProps,
2929
type EdgeProps,
30+
useDiagram,
3031
} from '@mongodb-js/diagramming';
3132
import type { Edit, StaticModel } from '../services/data-model-storage';
3233
import { UUID } from 'bson';
@@ -133,6 +134,19 @@ const DiagramEditor: React.FunctionComponent<{
133134
}) => {
134135
const isDarkMode = useDarkMode();
135136
const [applyInput, setApplyInput] = useState('{}');
137+
const diagram = useDiagram();
138+
139+
// For tests, we are setting the diagram instance on the window object
140+
// so that we can access it in the tests.
141+
useEffect(() => {
142+
if (
143+
process.env.APP_ENV === 'webdriverio' ||
144+
process.env.NODE_ENV === 'development'
145+
) {
146+
(window as any).diagramInstance = diagram;
147+
}
148+
}, [diagram]);
149+
136150
const isEditValid = useMemo(() => {
137151
try {
138152
JSON.parse(applyInput);

packages/compass-e2e-tests/tests/data-modeling-tab.test.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ import {
1414
createNumbersCollection,
1515
} from '../helpers/insert-data';
1616

17+
type DiagramInstance = {
18+
getNodes: () => Array<{ id: string; data: { title: string } }>;
19+
};
20+
21+
async function getDiagramNodes(browser: CompassBrowser) {
22+
return await browser.execute(() => {
23+
// eslint-disable-next-line no-restricted-globals
24+
if ('diagramInstance' in window) {
25+
// eslint-disable-next-line no-restricted-globals
26+
const diagramInstance = window.diagramInstance as DiagramInstance;
27+
const nodes = diagramInstance.getNodes();
28+
return nodes.map((node) => ({
29+
id: node.id,
30+
title: node.data.title,
31+
}));
32+
}
33+
throw new Error(
34+
'Diagram instance not found in the window object. Ensure the diagram is set for tests.'
35+
);
36+
});
37+
}
38+
1739
describe('Data Modeling tab', function () {
1840
let compass: Compass;
1941
let browser: CompassBrowser;
@@ -80,13 +102,12 @@ describe('Data Modeling tab', function () {
80102
const dataModelEditor = browser.$(Selectors.DataModelEditor);
81103
await dataModelEditor.waitForDisplayed();
82104

83-
// Verify that the diagram is displayed and contains both collections
84-
const text = await browser.getCodemirrorEditorText(
85-
Selectors.DataModelPreview
86-
);
87-
88-
expect(text).to.include('"ns": "test.testCollection1"');
89-
expect(text).to.include('"ns": "test.testCollection2"');
105+
let nodes = await getDiagramNodes(browser);
106+
expect(nodes).to.have.lengthOf(2);
107+
expect(nodes.map((x) => x.title)).to.deep.equal([
108+
'test.testCollection1',
109+
'test.testCollection2',
110+
]);
90111

91112
// Apply change to the model
92113
const newModel = {
@@ -96,40 +117,30 @@ describe('Data Modeling tab', function () {
96117
relationships: [],
97118
},
98119
};
99-
const newPreview = JSON.stringify(newModel.model, null, 2);
100120
await browser.setCodemirrorEditorValue(
101121
Selectors.DataModelApplyEditor,
102122
JSON.stringify(newModel)
103123
);
104124
await browser.clickVisible(Selectors.DataModelEditorApplyButton);
105125

106126
// Verify that the model is updated
107-
const updatedText = await browser.getCodemirrorEditorText(
108-
Selectors.DataModelPreview
109-
);
110-
expect(updatedText).to.equal(newPreview);
127+
nodes = await getDiagramNodes(browser);
128+
expect(nodes).to.have.lengthOf(0);
111129

112130
// Undo the change
113131
await browser.clickVisible(Selectors.DataModelUndoButton);
114-
await browser.waitUntil(async () => {
115-
const textAfterUndo = await browser.getCodemirrorEditorText(
116-
Selectors.DataModelPreview
117-
);
118-
return (
119-
textAfterUndo.includes('"ns": "test.testCollection1"') &&
120-
textAfterUndo.includes('"ns": "test.testCollection2"')
121-
);
122-
});
132+
nodes = await getDiagramNodes(browser);
133+
expect(nodes).to.have.lengthOf(2);
134+
expect(nodes.map((x) => x.title)).to.deep.equal([
135+
'test.testCollection1',
136+
'test.testCollection2',
137+
]);
123138

124139
// Redo the change
125140
await browser.waitForAriaDisabled(Selectors.DataModelRedoButton, false);
126141
await browser.clickVisible(Selectors.DataModelRedoButton);
127-
await browser.waitUntil(async () => {
128-
const redoneText = await browser.getCodemirrorEditorText(
129-
Selectors.DataModelPreview
130-
);
131-
return redoneText === newPreview;
132-
});
142+
nodes = await getDiagramNodes(browser);
143+
expect(nodes).to.have.lengthOf(0);
133144

134145
// Open a new tab
135146
await browser.openNewTab();
@@ -139,10 +150,8 @@ describe('Data Modeling tab', function () {
139150
await browser.$(Selectors.DataModelEditor).waitForDisplayed();
140151

141152
// Verify that the diagram has the latest changes
142-
const savedText = await browser.getCodemirrorEditorText(
143-
Selectors.DataModelPreview
144-
);
145-
expect(savedText).to.equal(newPreview);
153+
nodes = await getDiagramNodes(browser);
154+
expect(nodes).to.have.lengthOf(0);
146155

147156
// Open a new tab
148157
await browser.openNewTab();

0 commit comments

Comments
 (0)