Skip to content

Commit 51920e7

Browse files
committed
Add connection to current project database users
1 parent d1e8460 commit 51920e7

File tree

2 files changed

+94
-27
lines changed

2 files changed

+94
-27
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import React from 'react';
33
import {
44
screen,
5-
render,
5+
renderWithActiveConnection,
66
waitFor,
77
userEvent,
88
} from '@mongodb-js/testing-library-compass';
@@ -14,12 +14,19 @@ import { MockDataGeneratorStep } from './types';
1414
import { StepButtonLabelMap } from './constants';
1515
import type { CollectionState } from '../../modules/collection-tab';
1616
import { default as collectionTabReducer } from '../../modules/collection-tab';
17+
import type { ConnectionInfo } from '@mongodb-js/connection-info';
1718

1819
describe('MockDataGeneratorModal', () => {
19-
function renderModal({
20+
async function renderModal({
2021
isOpen = true,
2122
currentStep = MockDataGeneratorStep.SCHEMA_CONFIRMATION,
2223
mockServices = createMockServices(),
24+
connectionInfo,
25+
}: {
26+
isOpen?: boolean;
27+
currentStep?: MockDataGeneratorStep;
28+
mockServices?: any;
29+
connectionInfo?: ConnectionInfo;
2330
} = {}) {
2431
const initialState: CollectionState = {
2532
workspaceTabId: 'test-workspace-tab-id',
@@ -52,10 +59,11 @@ describe('MockDataGeneratorModal', () => {
5259
applyMiddleware(thunk.withExtraArgument(mockServices))
5360
);
5461

55-
return render(
62+
return await renderWithActiveConnection(
5663
<Provider store={store}>
5764
<MockDataGeneratorModal />
58-
</Provider>
65+
</Provider>,
66+
connectionInfo
5967
);
6068
}
6169

@@ -89,20 +97,20 @@ describe('MockDataGeneratorModal', () => {
8997
}
9098

9199
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();
94102

95103
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
96104
});
97105

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 });
100108

101109
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist;
102110
});
103111

104112
it('closes the modal when the close button is clicked', async () => {
105-
renderModal();
113+
await renderModal();
106114

107115
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
108116
userEvent.click(screen.getByLabelText('Close modal'));
@@ -113,7 +121,7 @@ describe('MockDataGeneratorModal', () => {
113121
});
114122

115123
it('closes the modal when the cancel button is clicked', async () => {
116-
renderModal();
124+
await renderModal();
117125

118126
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
119127
userEvent.click(screen.getByText('Cancel'));
@@ -154,7 +162,7 @@ describe('MockDataGeneratorModal', () => {
154162

155163
it('cancels in-flight faker mapping requests when the cancel button is clicked', async () => {
156164
const mockServices = createMockServicesWithSlowAiRequest();
157-
renderModal({ mockServices: mockServices as any });
165+
await renderModal({ mockServices: mockServices as any });
158166

159167
expect(screen.getByTestId('raw-schema-confirmation')).to.exist;
160168
userEvent.click(screen.getByText('Confirm'));
@@ -170,7 +178,7 @@ describe('MockDataGeneratorModal', () => {
170178

171179
it('cancels in-flight faker mapping requests when the back button is clicked after schema confirmation', async () => {
172180
const mockServices = createMockServicesWithSlowAiRequest();
173-
renderModal({ mockServices: mockServices as any });
181+
await renderModal({ mockServices: mockServices as any });
174182

175183
expect(screen.getByTestId('raw-schema-confirmation')).to.exist;
176184
userEvent.click(screen.getByText('Confirm'));
@@ -186,8 +194,8 @@ describe('MockDataGeneratorModal', () => {
186194
});
187195

188196
describe('on the schema confirmation step', () => {
189-
it('disables the Back button', () => {
190-
renderModal();
197+
it('disables the Back button', async () => {
198+
await renderModal();
191199

192200
expect(
193201
screen
@@ -197,7 +205,7 @@ describe('MockDataGeneratorModal', () => {
197205
});
198206

199207
it('renders the faker schema editor when the confirm button is clicked', async () => {
200-
renderModal();
208+
await renderModal();
201209

202210
expect(screen.getByTestId('raw-schema-confirmation')).to.exist;
203211
expect(screen.queryByTestId('faker-schema-editor')).to.not.exist;
@@ -212,7 +220,7 @@ describe('MockDataGeneratorModal', () => {
212220
const mockServices = createMockServices();
213221
mockServices.atlasAiService.getMockDataSchema = () =>
214222
Promise.reject('faker schema generation failed');
215-
renderModal({ mockServices });
223+
await renderModal({ mockServices });
216224

217225
expect(screen.getByTestId('raw-schema-confirmation')).to.exist;
218226
expect(screen.queryByTestId('faker-schema-editor')).to.not.exist;
@@ -229,8 +237,8 @@ describe('MockDataGeneratorModal', () => {
229237
});
230238

231239
describe('on the generate data step', () => {
232-
it('enables the Back button', () => {
233-
renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
240+
it('enables the Back button', async () => {
241+
await renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
234242

235243
expect(
236244
screen
@@ -239,8 +247,8 @@ describe('MockDataGeneratorModal', () => {
239247
).to.not.equal('true');
240248
});
241249

242-
it('renders the main sections: Prerequisites, steps, and Resources', () => {
243-
renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
250+
it('renders the main sections: Prerequisites, steps, and Resources', async () => {
251+
await renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
244252

245253
expect(screen.getByText('Prerequisites')).to.exist;
246254
expect(screen.getByText('1. Create a .js file with the following script'))
@@ -250,7 +258,7 @@ describe('MockDataGeneratorModal', () => {
250258
});
251259

252260
it('closes the modal when the Done button is clicked', async () => {
253-
renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
261+
await renderModal({ currentStep: MockDataGeneratorStep.GENERATE_DATA });
254262

255263
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
256264
userEvent.click(screen.getByText('Done'));
@@ -260,6 +268,57 @@ describe('MockDataGeneratorModal', () => {
260268
);
261269
});
262270

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+
263322
// todo: assert that the generated script is displayed in the code block
264323
});
265324

@@ -270,8 +329,8 @@ describe('MockDataGeneratorModal', () => {
270329

271330
// note: these tests can be removed after every modal step is implemented
272331
steps.forEach((currentStep) => {
273-
it(`renders the button with the correct label when the user is in step "${currentStep}"`, () => {
274-
renderModal({ currentStep });
332+
it(`renders the button with the correct label when the user is in step "${currentStep}"`, async () => {
333+
await renderModal({ currentStep });
275334
expect(screen.getByTestId('next-step-button')).to.have.text(
276335
StepButtonLabelMap[currentStep]
277336
);

packages/compass-collection/src/components/mock-data-generator-modal/script-screen.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
spacing,
1414
useDarkMode,
1515
} from '@mongodb-js/compass-components';
16+
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';
1617

1718
const RUN_SCRIPT_COMMAND = `
1819
mongosh "mongodb+srv://<your-cluster>.mongodb.net/mockDataDB" \\
@@ -60,6 +61,7 @@ const resourceSectionHeader = css({
6061

6162
const ScriptScreen = () => {
6263
const isDarkMode = useDarkMode();
64+
const connectionInfo = useConnectionInfo();
6365

6466
return (
6567
<section className={outerSectionStyles}>
@@ -134,10 +136,16 @@ const ScriptScreen = () => {
134136
Learn About the MongoDB Shell
135137
</Link>
136138
</li>
137-
<li>
138-
{/* TODO: Update URL */}
139-
<Link href="#">Access your Database Users</Link>
140-
</li>
139+
{connectionInfo.atlasMetadata &&
140+
connectionInfo.atlasMetadata.projectId && (
141+
<li>
142+
<Link
143+
href={`/v2/${connectionInfo.atlasMetadata.projectId}#/security/database/users`}
144+
>
145+
Access your Database Users
146+
</Link>
147+
</li>
148+
)}
141149
</ul>
142150
</section>
143151
</section>

0 commit comments

Comments
 (0)