11import React from 'react' ;
22import { expect } from 'chai' ;
33import {
4+ createDefaultConnectionInfo ,
45 createPluginTestHelpers ,
56 screen ,
7+ userEvent ,
68 waitFor ,
9+ within ,
710} from '@mongodb-js/testing-library-compass' ;
811import DiagramEditor from './diagram-editor' ;
912import type { DataModelingStore } from '../../test/setup-store' ;
@@ -17,6 +20,13 @@ import { DiagramProvider } from '@mongodb-js/diagramming';
1720import { DataModelingWorkspaceTab } from '..' ;
1821import { openDiagram } from '../store/diagram' ;
1922import { DrawerAnchor } from '@mongodb-js/compass-components' ;
23+ import { type AnalysisOptions , startAnalysis } from '../store/analysis-process' ;
24+ import type { DataService } from '@mongodb-js/compass-connections/provider' ;
25+
26+ const mockConnections = [
27+ { ...createDefaultConnectionInfo ( ) , id : 'connection1' } ,
28+ { ...createDefaultConnectionInfo ( ) , id : 'connection2' } ,
29+ ] ;
2030
2131const storageItems : MongoDBDataModelDescription [ ] = [
2232 {
@@ -108,49 +118,87 @@ const mockDiagramming = {
108118 } ,
109119} ;
110120
111- const renderDiagramEditor = ( {
112- items = storageItems ,
113- renderedItem = items [ 0 ] ,
114- } : {
115- items ?: MongoDBDataModelDescription [ ] ;
116- renderedItem ?: MongoDBDataModelDescription ;
117- } = { } ) => {
121+ const renderDiagramEditor = async ( {
122+ existingDiagram,
123+ newDiagram,
124+ } :
125+ | {
126+ existingDiagram : MongoDBDataModelDescription ;
127+ newDiagram ?: never ;
128+ }
129+ | {
130+ newDiagram : {
131+ name : string ;
132+ database : string ;
133+ connectionId : string ;
134+ collections : string [ ] ;
135+ analysisOptions : AnalysisOptions ;
136+ } ;
137+ existingDiagram ?: never ;
138+ } ) => {
118139 const mockDataModelStorage = {
119140 status : 'READY' ,
120141 error : null ,
121- items,
142+ items : storageItems ,
122143 save : ( ) => {
123144 return Promise . resolve ( false ) ;
124145 } ,
125146 delete : ( ) => {
126147 return Promise . resolve ( false ) ;
127148 } ,
128- loadAll : ( ) => Promise . resolve ( items ) ,
149+ loadAll : ( ) => Promise . resolve ( storageItems ) ,
129150 load : ( id : string ) => {
130- return Promise . resolve ( items . find ( ( x ) => x . id === id ) ?? null ) ;
151+ return Promise . resolve ( storageItems . find ( ( x ) => x . id === id ) ?? null ) ;
131152 } ,
132153 } ;
133154
134- const { renderWithConnections } = createPluginTestHelpers (
155+ const { renderWithActiveConnection } = createPluginTestHelpers (
135156 DataModelingWorkspaceTab . provider . withMockServices ( {
136157 services : {
137158 dataModelStorage : mockDataModelStorage ,
138159 } ,
139160 } ) ,
140161 {
141162 namespace : 'foo.bar' ,
142- } as any
163+ }
143164 ) ;
144165 const {
145166 plugin : { store } ,
146- } = renderWithConnections (
167+ } = await renderWithActiveConnection (
147168 < DrawerAnchor >
148169 < DiagramProvider fitView >
149170 < DiagramEditor />
150171 </ DiagramProvider >
151- </ DrawerAnchor >
172+ </ DrawerAnchor > ,
173+ mockConnections [ 0 ] ,
174+ {
175+ connections : mockConnections ,
176+ connectFn : ( ) => {
177+ return {
178+ sample : ( ) =>
179+ Promise . resolve ( [
180+ {
181+ _id : 'doc1' ,
182+ } ,
183+ {
184+ _id : 'doc2' ,
185+ } ,
186+ ] ) ,
187+ } as unknown as DataService ;
188+ } ,
189+ }
152190 ) ;
153- store . dispatch ( openDiagram ( renderedItem ) ) ;
191+ if ( existingDiagram ) store . dispatch ( openDiagram ( existingDiagram ) ) ;
192+ if ( newDiagram )
193+ store . dispatch (
194+ startAnalysis (
195+ newDiagram . name ,
196+ newDiagram . connectionId ,
197+ newDiagram . database ,
198+ newDiagram . collections ,
199+ newDiagram . analysisOptions
200+ )
201+ ) ;
154202
155203 return { store } ;
156204} ;
@@ -168,8 +216,8 @@ describe('DiagramEditor', function () {
168216
169217 context ( 'with existing diagram' , function ( ) {
170218 beforeEach ( async function ( ) {
171- const result = renderDiagramEditor ( {
172- renderedItem : storageItems [ 0 ] ,
219+ const result = await renderDiagramEditor ( {
220+ existingDiagram : storageItems [ 0 ] ,
173221 } ) ;
174222 store = result . store ;
175223
@@ -179,6 +227,10 @@ describe('DiagramEditor', function () {
179227 } ) ;
180228 } ) ;
181229
230+ it ( 'does not show the banner' , function ( ) {
231+ expect ( screen . queryByText ( 'Worried about your data?' ) ) . not . to . exist ;
232+ } ) ;
233+
182234 it ( 'does not change the position of the nodes' , function ( ) {
183235 const state = store . getState ( ) ;
184236
@@ -200,4 +252,40 @@ describe('DiagramEditor', function () {
200252 ) ;
201253 } ) ;
202254 } ) ;
255+
256+ context ( 'with a new diagram' , function ( ) {
257+ beforeEach ( async function ( ) {
258+ const result = await renderDiagramEditor ( {
259+ newDiagram : {
260+ name : 'New Diagram' ,
261+ database : 'test' ,
262+ connectionId : 'connection1' ,
263+ collections : [ 'collection1' , 'collection2' ] ,
264+ analysisOptions : {
265+ automaticallyInferRelations : false ,
266+ } ,
267+ } ,
268+ } ) ;
269+ store = result . store ;
270+
271+ // wait till the editor is loaded
272+ await waitFor ( ( ) => {
273+ expect ( screen . getByTestId ( 'model-preview' ) ) . to . be . visible ;
274+ } ) ;
275+ } ) ;
276+
277+ it ( 'shows the banner' , function ( ) {
278+ expect ( screen . getByText ( 'Worried about your data?' ) ) . to . be . visible ;
279+ } ) ;
280+
281+ it ( 'banner can be closed' , function ( ) {
282+ const closeBtn = within ( screen . getByTestId ( 'data-info-banner' ) ) . getByRole (
283+ 'button' ,
284+ { name : 'Close Message' }
285+ ) ;
286+ expect ( closeBtn ) . to . be . visible ;
287+ userEvent . click ( closeBtn ) ;
288+ expect ( screen . queryByText ( 'Worried about your data?' ) ) . not . to . exist ;
289+ } ) ;
290+ } ) ;
203291} ) ;
0 commit comments