1+ const mockDiagramming = {
2+ // Keep original exports by spreading them (if needed)
3+ ...require ( '@mongodb-js/diagramming' ) ,
4+
5+ // Override Diagram import because it's causing esm/cjs interop issues
6+ Diagram : ( props ) => (
7+ < div data-testid = "mock-diagram" >
8+ { Object . entries ( props ) . map ( ( [ key , value ] ) => (
9+ < div key = { key } data-testid = { `diagram-prop-${ key } ` } >
10+ { JSON . stringify ( value ) }
11+ </ div >
12+ ) ) }
13+ </ div >
14+ ) ,
15+ applyLayout : async ( nodes ) => {
16+ return {
17+ nodes : nodes . map ( ( node , index ) => ( {
18+ ...node ,
19+ position : { x : ( index + 1 ) * 100 , y : ( index + 1 ) * 100 } ,
20+ } ) ) ,
21+ } ;
22+ } ,
23+ } ;
24+ ( require . cache [ require . resolve ( '@mongodb-js/diagramming' ) ] as any ) . exports =
25+ mockDiagramming ;
26+
127import React from 'react' ;
228import { expect } from 'chai' ;
329import {
@@ -9,7 +35,10 @@ import DiagramEditor from './diagram-editor';
935import { renderWithOpenedDiagramStore } from '../../test/setup-store' ;
1036import type { DataModelingStore } from '../../test/setup-store' ;
1137import { DataModelStorageServiceProvider } from '../provider' ;
12- import type { MongoDBDataModelDescription } from '../services/data-model-storage' ;
38+ import type {
39+ Edit ,
40+ MongoDBDataModelDescription ,
41+ } from '../services/data-model-storage' ;
1342import { DiagramProvider } from '@mongodb-js/diagramming' ;
1443
1544const storageItems : MongoDBDataModelDescription [ ] = [
@@ -28,7 +57,14 @@ const storageItems: MongoDBDataModelDescription[] = [
2857 {
2958 ns : 'db1.collection1' ,
3059 indexes : [ ] ,
31- displayPosition : [ 1 , 1 ] ,
60+ displayPosition : [ - 1 , - 1 ] ,
61+ shardKey : { } ,
62+ jsonSchema : { bsonType : 'object' } ,
63+ } ,
64+ {
65+ ns : 'db1.collection2' ,
66+ indexes : [ ] ,
67+ displayPosition : [ - 1 , - 1 ] ,
3268 shardKey : { } ,
3369 jsonSchema : { bsonType : 'object' } ,
3470 } ,
@@ -61,7 +97,6 @@ const renderDiagramEditor = async ({
6197 return Promise . resolve ( items . find ( ( x ) => x . id === id ) ?? null ) ;
6298 } ,
6399 } ;
64- console . log ( DiagramProvider ) ;
65100 const result = await renderWithOpenedDiagramStore (
66101 < DataModelStorageServiceProvider storage = { mockDataModelStorage } >
67102 < DiagramProvider fitView >
@@ -86,12 +121,25 @@ describe.only('DiagramEditor', function () {
86121 store = result . store ;
87122
88123 // wait till the editor is loaded
89- // await waitFor(() => {
90- // expect(screen.getByTestId('model-preview')).to.be.visible;
91- // });
124+ await waitFor ( ( ) => {
125+ expect ( screen . getByTestId ( 'model-preview' ) ) . to . be . visible ;
126+ } ) ;
92127 } ) ;
93128
94- it ( 'shows the list of diagrams' , async function ( ) {
95- // screen.debug()
129+ it ( 'applies the initial layout to unpositioned nodes' , async function ( ) {
130+ const state = store . getState ( ) ;
131+
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+ ] ) ;
96144 } ) ;
97145} ) ;
0 commit comments