1
1
import React from 'react' ;
2
2
import { expect } from 'chai' ;
3
3
import {
4
+ createDefaultConnectionInfo ,
4
5
createPluginTestHelpers ,
5
6
screen ,
7
+ userEvent ,
6
8
waitFor ,
9
+ within ,
7
10
} from '@mongodb-js/testing-library-compass' ;
8
11
import DiagramEditor from './diagram-editor' ;
9
12
import type { DataModelingStore } from '../../test/setup-store' ;
@@ -17,6 +20,13 @@ import { DiagramProvider } from '@mongodb-js/diagramming';
17
20
import { DataModelingWorkspaceTab } from '..' ;
18
21
import { openDiagram } from '../store/diagram' ;
19
22
import { 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
+ ] ;
20
30
21
31
const storageItems : MongoDBDataModelDescription [ ] = [
22
32
{
@@ -108,49 +118,87 @@ const mockDiagramming = {
108
118
} ,
109
119
} ;
110
120
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
+ } ) => {
118
139
const mockDataModelStorage = {
119
140
status : 'READY' ,
120
141
error : null ,
121
- items,
142
+ items : storageItems ,
122
143
save : ( ) => {
123
144
return Promise . resolve ( false ) ;
124
145
} ,
125
146
delete : ( ) => {
126
147
return Promise . resolve ( false ) ;
127
148
} ,
128
- loadAll : ( ) => Promise . resolve ( items ) ,
149
+ loadAll : ( ) => Promise . resolve ( storageItems ) ,
129
150
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 ) ;
131
152
} ,
132
153
} ;
133
154
134
- const { renderWithConnections } = createPluginTestHelpers (
155
+ const { renderWithActiveConnection } = createPluginTestHelpers (
135
156
DataModelingWorkspaceTab . provider . withMockServices ( {
136
157
services : {
137
158
dataModelStorage : mockDataModelStorage ,
138
159
} ,
139
160
} ) ,
140
161
{
141
162
namespace : 'foo.bar' ,
142
- } as any
163
+ }
143
164
) ;
144
165
const {
145
166
plugin : { store } ,
146
- } = renderWithConnections (
167
+ } = await renderWithActiveConnection (
147
168
< DrawerAnchor >
148
169
< DiagramProvider fitView >
149
170
< DiagramEditor />
150
171
</ 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
+ }
152
190
) ;
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
+ ) ;
154
202
155
203
return { store } ;
156
204
} ;
@@ -168,8 +216,8 @@ describe('DiagramEditor', function () {
168
216
169
217
context ( 'with existing diagram' , function ( ) {
170
218
beforeEach ( async function ( ) {
171
- const result = renderDiagramEditor ( {
172
- renderedItem : storageItems [ 0 ] ,
219
+ const result = await renderDiagramEditor ( {
220
+ existingDiagram : storageItems [ 0 ] ,
173
221
} ) ;
174
222
store = result . store ;
175
223
@@ -179,6 +227,10 @@ describe('DiagramEditor', function () {
179
227
} ) ;
180
228
} ) ;
181
229
230
+ it ( 'does not show the banner' , function ( ) {
231
+ expect ( screen . queryByText ( 'Worried about your data?' ) ) . not . to . exist ;
232
+ } ) ;
233
+
182
234
it ( 'does not change the position of the nodes' , function ( ) {
183
235
const state = store . getState ( ) ;
184
236
@@ -200,4 +252,40 @@ describe('DiagramEditor', function () {
200
252
) ;
201
253
} ) ;
202
254
} ) ;
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
+ } ) ;
203
291
} ) ;
0 commit comments