Skip to content

fix(test): deflake diagram-editor tests COMPASS-9462 #7030

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

Merged
merged 5 commits into from
Jun 17, 2025
Merged
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
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { connect } from 'react-redux';
import type { DataModelingState } from '../store/reducer';
import {
Expand Down Expand Up @@ -27,6 +27,7 @@ import {
Diagram,
type NodeProps,
type EdgeProps,
useDiagram,
} from '@mongodb-js/diagramming';
import type { Edit, StaticModel } from '../services/data-model-storage';
import { UUID } from 'bson';
Expand Down Expand Up @@ -132,6 +133,20 @@ const DiagramEditor: React.FunctionComponent<{
onApplyClick,
}) => {
const isDarkMode = useDarkMode();
const diagramContainerRef = useRef<HTMLDivElement | null>(null);
const diagram = useDiagram();

const setDiagramContainerRef = useCallback(
(ref: HTMLDivElement | null) => {
if (ref) {
// For debugging purposes, we attach the diagram to the ref.
(ref as any)._diagram = diagram;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could limit this to process.env.NODE_ENV === 'test' || process.env.APP_ENV === 'webdriverio' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i did that in my last PR and Sergey suggested we try this (ref) approach or the one we already have (validate what user actually sees).

The one we have is failing (don't really know why, but my guess is that we are currently doing random positioning and once we have that done this may not be the issue anymore), so i am using ref approach in this PR. We also do this in codemirror to access current instance in e2e.

diagramContainerRef.current = ref;
},
[diagram]
);

const [applyInput, setApplyInput] = useState('{}');

const isEditValid = useMemo(() => {
Expand Down Expand Up @@ -265,6 +280,7 @@ const DiagramEditor: React.FunctionComponent<{
if (step === 'EDITING') {
content = (
<div
ref={setDiagramContainerRef}
className={modelPreviewContainerStyles}
data-testid="diagram-editor-container"
>
Expand Down
2 changes: 0 additions & 2 deletions packages/compass-e2e-tests/helpers/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1451,5 +1451,3 @@ export const DataModelsListItem = (diagramName: string) =>
export const DataModelsListItemActions = (diagramName: string) =>
`${DataModelsListItem(diagramName)} [aria-label="Show actions"]`;
export const DataModelsListItemDeleteButton = `[data-action="delete"]`;
export const DataModelingDiagram = '.react-flow';
export const DataModelingDiagramNode = '.react-flow__node > div';
23 changes: 19 additions & 4 deletions packages/compass-e2e-tests/tests/data-modeling-tab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ import {
createNumbersCollection,
} from '../helpers/insert-data';

type DiagramInstance = {
getNodes: () => Array<{
id: string;
}>;
};

async function getDiagramNodes(browser: CompassBrowser): Promise<string[]> {
await browser.waitForAnimations(Selectors.DataModelingDiagram);
return await browser
.$$(Selectors.DataModelingDiagramNode)
.map((element) => element.getAttribute('title'));
const nodes = await browser.execute(function (selector) {
const node = document.querySelector(selector);
if (!node) {
throw new Error(`Element with selector ${selector} not found`);
}
return (
node as Element & { _diagram: DiagramInstance }
)._diagram.getNodes();
}, Selectors.DataModelEditor);
return nodes.map((x) => x.id);
}

describe('Data Modeling tab', function () {
Expand Down Expand Up @@ -107,13 +119,15 @@ describe('Data Modeling tab', function () {
JSON.stringify(newModel)
);
await browser.clickVisible(Selectors.DataModelEditorApplyButton);
await browser.waitForAnimations(dataModelEditor);

// Verify that the model is updated
nodes = await getDiagramNodes(browser);
expect(nodes).to.have.lengthOf(0);

// Undo the change
await browser.clickVisible(Selectors.DataModelUndoButton);
await browser.waitForAnimations(dataModelEditor);
nodes = await getDiagramNodes(browser);
expect(nodes).to.have.lengthOf(2);
expect(nodes).to.deep.equal([
Expand All @@ -124,6 +138,7 @@ describe('Data Modeling tab', function () {
// Redo the change
await browser.waitForAriaDisabled(Selectors.DataModelRedoButton, false);
await browser.clickVisible(Selectors.DataModelRedoButton);
await browser.waitForAnimations(dataModelEditor);
nodes = await getDiagramNodes(browser);
expect(nodes).to.have.lengthOf(0);

Expand Down
Loading