@@ -2,7 +2,7 @@ import { expect } from 'chai';
2
2
import React from 'react' ;
3
3
import {
4
4
screen ,
5
- render ,
5
+ renderWithActiveConnection ,
6
6
waitFor ,
7
7
userEvent ,
8
8
} from '@mongodb-js/testing-library-compass' ;
@@ -14,12 +14,19 @@ import { MockDataGeneratorStep } from './types';
14
14
import { StepButtonLabelMap } from './constants' ;
15
15
import type { CollectionState } from '../../modules/collection-tab' ;
16
16
import { default as collectionTabReducer } from '../../modules/collection-tab' ;
17
+ import type { ConnectionInfo } from '@mongodb-js/connection-info' ;
17
18
18
19
describe ( 'MockDataGeneratorModal' , ( ) => {
19
- function renderModal ( {
20
+ async function renderModal ( {
20
21
isOpen = true ,
21
22
currentStep = MockDataGeneratorStep . SCHEMA_CONFIRMATION ,
22
23
mockServices = createMockServices ( ) ,
24
+ connectionInfo,
25
+ } : {
26
+ isOpen ?: boolean ;
27
+ currentStep ?: MockDataGeneratorStep ;
28
+ mockServices ?: any ;
29
+ connectionInfo ?: ConnectionInfo ;
23
30
} = { } ) {
24
31
const initialState : CollectionState = {
25
32
workspaceTabId : 'test-workspace-tab-id' ,
@@ -52,10 +59,11 @@ describe('MockDataGeneratorModal', () => {
52
59
applyMiddleware ( thunk . withExtraArgument ( mockServices ) )
53
60
) ;
54
61
55
- return render (
62
+ return await renderWithActiveConnection (
56
63
< Provider store = { store } >
57
64
< MockDataGeneratorModal />
58
- </ Provider >
65
+ </ Provider > ,
66
+ connectionInfo
59
67
) ;
60
68
}
61
69
@@ -89,20 +97,20 @@ describe('MockDataGeneratorModal', () => {
89
97
}
90
98
91
99
describe ( 'generally' , ( ) => {
92
- it ( 'renders the modal when isOpen is true' , ( ) => {
93
- renderModal ( ) ;
100
+ it ( 'renders the modal when isOpen is true' , async ( ) => {
101
+ await renderModal ( ) ;
94
102
95
103
expect ( screen . getByTestId ( 'generate-mock-data-modal' ) ) . to . exist ;
96
104
} ) ;
97
105
98
- it ( 'does not render the modal when isOpen is false' , ( ) => {
99
- renderModal ( { isOpen : false } ) ;
106
+ it ( 'does not render the modal when isOpen is false' , async ( ) => {
107
+ await renderModal ( { isOpen : false } ) ;
100
108
101
109
expect ( screen . queryByTestId ( 'generate-mock-data-modal' ) ) . to . not . exist ;
102
110
} ) ;
103
111
104
112
it ( 'closes the modal when the close button is clicked' , async ( ) => {
105
- renderModal ( ) ;
113
+ await renderModal ( ) ;
106
114
107
115
expect ( screen . getByTestId ( 'generate-mock-data-modal' ) ) . to . exist ;
108
116
userEvent . click ( screen . getByLabelText ( 'Close modal' ) ) ;
@@ -113,7 +121,7 @@ describe('MockDataGeneratorModal', () => {
113
121
} ) ;
114
122
115
123
it ( 'closes the modal when the cancel button is clicked' , async ( ) => {
116
- renderModal ( ) ;
124
+ await renderModal ( ) ;
117
125
118
126
expect ( screen . getByTestId ( 'generate-mock-data-modal' ) ) . to . exist ;
119
127
userEvent . click ( screen . getByText ( 'Cancel' ) ) ;
@@ -154,7 +162,7 @@ describe('MockDataGeneratorModal', () => {
154
162
155
163
it ( 'cancels in-flight faker mapping requests when the cancel button is clicked' , async ( ) => {
156
164
const mockServices = createMockServicesWithSlowAiRequest ( ) ;
157
- renderModal ( { mockServices : mockServices as any } ) ;
165
+ await renderModal ( { mockServices : mockServices as any } ) ;
158
166
159
167
expect ( screen . getByTestId ( 'raw-schema-confirmation' ) ) . to . exist ;
160
168
userEvent . click ( screen . getByText ( 'Confirm' ) ) ;
@@ -170,7 +178,7 @@ describe('MockDataGeneratorModal', () => {
170
178
171
179
it ( 'cancels in-flight faker mapping requests when the back button is clicked after schema confirmation' , async ( ) => {
172
180
const mockServices = createMockServicesWithSlowAiRequest ( ) ;
173
- renderModal ( { mockServices : mockServices as any } ) ;
181
+ await renderModal ( { mockServices : mockServices as any } ) ;
174
182
175
183
expect ( screen . getByTestId ( 'raw-schema-confirmation' ) ) . to . exist ;
176
184
userEvent . click ( screen . getByText ( 'Confirm' ) ) ;
@@ -186,8 +194,8 @@ describe('MockDataGeneratorModal', () => {
186
194
} ) ;
187
195
188
196
describe ( 'on the schema confirmation step' , ( ) => {
189
- it ( 'disables the Back button' , ( ) => {
190
- renderModal ( ) ;
197
+ it ( 'disables the Back button' , async ( ) => {
198
+ await renderModal ( ) ;
191
199
192
200
expect (
193
201
screen
@@ -197,7 +205,7 @@ describe('MockDataGeneratorModal', () => {
197
205
} ) ;
198
206
199
207
it ( 'renders the faker schema editor when the confirm button is clicked' , async ( ) => {
200
- renderModal ( ) ;
208
+ await renderModal ( ) ;
201
209
202
210
expect ( screen . getByTestId ( 'raw-schema-confirmation' ) ) . to . exist ;
203
211
expect ( screen . queryByTestId ( 'faker-schema-editor' ) ) . to . not . exist ;
@@ -212,7 +220,7 @@ describe('MockDataGeneratorModal', () => {
212
220
const mockServices = createMockServices ( ) ;
213
221
mockServices . atlasAiService . getMockDataSchema = ( ) =>
214
222
Promise . reject ( 'faker schema generation failed' ) ;
215
- renderModal ( { mockServices } ) ;
223
+ await renderModal ( { mockServices } ) ;
216
224
217
225
expect ( screen . getByTestId ( 'raw-schema-confirmation' ) ) . to . exist ;
218
226
expect ( screen . queryByTestId ( 'faker-schema-editor' ) ) . to . not . exist ;
@@ -228,15 +236,101 @@ describe('MockDataGeneratorModal', () => {
228
236
// todo: assert that closing then re-opening the modal after an LLM err removes the err message
229
237
} ) ;
230
238
239
+ describe ( 'on the generate data step' , ( ) => {
240
+ it ( 'enables the Back button' , async ( ) => {
241
+ await renderModal ( { currentStep : MockDataGeneratorStep . GENERATE_DATA } ) ;
242
+
243
+ expect (
244
+ screen
245
+ . getByRole ( 'button' , { name : 'Back' } )
246
+ . getAttribute ( 'aria-disabled' )
247
+ ) . to . not . equal ( 'true' ) ;
248
+ } ) ;
249
+
250
+ it ( 'renders the main sections: Prerequisites, steps, and Resources' , async ( ) => {
251
+ await renderModal ( { currentStep : MockDataGeneratorStep . GENERATE_DATA } ) ;
252
+
253
+ expect ( screen . getByText ( 'Prerequisites' ) ) . to . exist ;
254
+ expect ( screen . getByText ( '1. Create a .js file with the following script' ) )
255
+ . to . exist ;
256
+ expect ( screen . getByText ( '2. Run the script with' ) ) . to . exist ;
257
+ expect ( screen . getByText ( 'Resources' ) ) . to . exist ;
258
+ } ) ;
259
+
260
+ it ( 'closes the modal when the Done button is clicked' , async ( ) => {
261
+ await renderModal ( { currentStep : MockDataGeneratorStep . GENERATE_DATA } ) ;
262
+
263
+ expect ( screen . getByTestId ( 'generate-mock-data-modal' ) ) . to . exist ;
264
+ userEvent . click ( screen . getByText ( 'Done' ) ) ;
265
+ await waitFor (
266
+ ( ) =>
267
+ expect ( screen . queryByTestId ( 'generate-mock-data-modal' ) ) . to . not . exist
268
+ ) ;
269
+ } ) ;
270
+
271
+ it ( 'renders the Database Users link with correct URL when projectId is available' , async ( ) => {
272
+ const atlasConnectionInfo : ConnectionInfo = {
273
+ id : 'test-atlas-connection' ,
274
+ connectionOptions : { connectionString : 'mongodb://localhost:27017' } ,
275
+ atlasMetadata : {
276
+ orgId : 'test-org' ,
277
+ projectId : 'test-project-123' ,
278
+ clusterName : 'test-cluster' ,
279
+ clusterUniqueId : 'test-cluster-unique-id' ,
280
+ clusterType : 'REPLICASET' as const ,
281
+ clusterState : 'IDLE' as const ,
282
+ metricsId : 'test-metrics-id' ,
283
+ metricsType : 'replicaSet' as const ,
284
+ regionalBaseUrl : null ,
285
+ instanceSize : 'M10' ,
286
+ supports : {
287
+ globalWrites : false ,
288
+ rollingIndexes : true ,
289
+ } ,
290
+ } ,
291
+ } ;
292
+
293
+ await renderModal ( {
294
+ currentStep : MockDataGeneratorStep . GENERATE_DATA ,
295
+ connectionInfo : atlasConnectionInfo ,
296
+ } ) ;
297
+
298
+ const databaseUsersLink = screen . getByRole ( 'link' , {
299
+ name : 'Access your Database Users' ,
300
+ } ) ;
301
+ expect ( databaseUsersLink . getAttribute ( 'href' ) ) . to . equal (
302
+ '/v2/test-project-123#/security/database/users'
303
+ ) ;
304
+ } ) ;
305
+
306
+ it ( 'does not render the Database Users link when projectId is not available' , async ( ) => {
307
+ const nonAtlasConnectionInfo : ConnectionInfo = {
308
+ id : 'test-local-connection' ,
309
+ connectionOptions : { connectionString : 'mongodb://localhost:27017' } ,
310
+ // No atlasMetadata means no projectId
311
+ } ;
312
+
313
+ await renderModal ( {
314
+ currentStep : MockDataGeneratorStep . GENERATE_DATA ,
315
+ connectionInfo : nonAtlasConnectionInfo ,
316
+ } ) ;
317
+
318
+ expect ( screen . queryByRole ( 'link' , { name : 'Access your Database Users' } ) )
319
+ . to . not . exist ;
320
+ } ) ;
321
+
322
+ // todo: assert that the generated script is displayed in the code block (CLOUDP-333860)
323
+ } ) ;
324
+
231
325
describe ( 'when rendering the modal in a specific step' , ( ) => {
232
326
const steps = Object . keys (
233
327
StepButtonLabelMap
234
328
) as unknown as MockDataGeneratorStep [ ] ;
235
329
236
330
// note: these tests can be removed after every modal step is implemented
237
331
steps . forEach ( ( currentStep ) => {
238
- it ( `renders the button with the correct label when the user is in step "${ currentStep } "` , ( ) => {
239
- renderModal ( { currentStep } ) ;
332
+ it ( `renders the button with the correct label when the user is in step "${ currentStep } "` , async ( ) => {
333
+ await renderModal ( { currentStep } ) ;
240
334
expect ( screen . getByTestId ( 'next-step-button' ) ) . to . have . text (
241
335
StepButtonLabelMap [ currentStep ]
242
336
) ;
0 commit comments