Skip to content

Commit c14faac

Browse files
committed
better mock
1 parent b019387 commit c14faac

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
const mockDiagramming = {
2-
// Keep original exports by spreading them (if needed)
3-
// eslint-disable-next-line @typescript-eslint/no-require-imports
4-
...require('@mongodb-js/diagramming'),
5-
6-
// Override Diagram import because it's causing esm/cjs interop issues
7-
Diagram: (props: any) => (
8-
<div data-testid="mock-diagram">
9-
{Object.entries(props).map(([key, value]) => (
10-
<div key={key} data-testid={`diagram-prop-${key}`}>
11-
{JSON.stringify(value)}
12-
</div>
13-
))}
14-
</div>
15-
),
16-
applyLayout: (nodes: any) => {
17-
return {
18-
nodes: nodes.map((node: any, index: number) => ({
19-
...node,
20-
position: { x: (index + 1) * 100, y: (index + 1) * 100 },
21-
})),
22-
};
23-
},
24-
};
25-
(require.cache[require.resolve('@mongodb-js/diagramming')] as any).exports =
26-
mockDiagramming;
27-
281
import React from 'react';
292
import { expect } from 'chai';
303
import {
@@ -38,6 +11,8 @@ import type {
3811
Edit,
3912
MongoDBDataModelDescription,
4013
} from '../services/data-model-storage';
14+
import diagramming from '@mongodb-js/diagramming';
15+
import sinon from 'sinon';
4116
import { DiagramProvider } from '@mongodb-js/diagramming';
4217
import { DataModelingWorkspaceTab } from '..';
4318
import { openDiagram } from '../store/diagram';
@@ -111,6 +86,27 @@ const storageItems: MongoDBDataModelDescription[] = [
11186
},
11287
];
11388

89+
const mockDiagramming = {
90+
// Override Diagram import because it's causing esm/cjs interop issues
91+
Diagram: (props: any) => (
92+
<div data-testid="mock-diagram">
93+
{Object.entries(props).map(([key, value]) => (
94+
<div key={key} data-testid={`diagram-prop-${key}`}>
95+
{JSON.stringify(value)}
96+
</div>
97+
))}
98+
</div>
99+
),
100+
applyLayout: (nodes: any) => {
101+
return {
102+
nodes: nodes.map((node: any, index: number) => ({
103+
...node,
104+
position: { x: (index + 1) * 100, y: (index + 1) * 100 },
105+
})),
106+
};
107+
},
108+
};
109+
114110
const renderDiagramEditor = ({
115111
items = storageItems,
116112
renderedItem = items[0],
@@ -159,6 +155,14 @@ const renderDiagramEditor = ({
159155
describe('DiagramEditor', function () {
160156
let store: DataModelingStore;
161157

158+
before(function () {
159+
// We need to tub the Diagram import because it has problems with ESM/CJS interop
160+
sinon.stub(diagramming, 'Diagram').callsFake(mockDiagramming.Diagram);
161+
sinon
162+
.stub(diagramming, 'applyLayout')
163+
.callsFake(mockDiagramming.applyLayout as any);
164+
});
165+
162166
context('with initial diagram', function () {
163167
beforeEach(async function () {
164168
const result = renderDiagramEditor({

0 commit comments

Comments
 (0)