Skip to content

Commit 9778bb1

Browse files
committed
fixed tests and automatically generate suggestions when applicable
1 parent 247d46f commit 9778bb1

File tree

5 files changed

+45
-68
lines changed

5 files changed

+45
-68
lines changed

packages/compass-indexes/src/components/create-index-form/index-flow-section.spec.tsx

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import React from 'react';
22
import { render, screen } from '@mongodb-js/testing-library-compass';
3-
import IndexFlowSection, {
4-
generateCoveredQueries,
5-
generateCoveredQueriesArr,
6-
generateOptimalQueries,
7-
} from './index-flow-section';
3+
import IndexFlowSection from './index-flow-section';
84
import { expect } from 'chai';
95
import { ActionTypes, type Field } from '../../modules/create-index';
106
import { Provider } from 'react-redux';
@@ -87,19 +83,16 @@ describe('IndexFlowSection', () => {
8783
{ name: 'field4', type: '1 (asc)' },
8884
];
8985

90-
const coveredQueriesArr = generateCoveredQueriesArr(fields);
91-
const coveredQueries = generateCoveredQueries(coveredQueriesArr, () => {});
92-
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
93-
9486
beforeEach(() => {
9587
renderComponent({ fields });
9688

9789
screen.getByTestId('index-flow-section-covered-queries-button').click();
90+
store.dispatch({
91+
type: ActionTypes.FieldsChanged,
92+
fields,
93+
});
9894
store.dispatch({
9995
type: ActionTypes.CoveredQueriesFetched,
100-
coveredQueries,
101-
optimalQueries,
102-
showCoveredQueries: true,
10396
});
10497
});
10598

@@ -146,19 +139,17 @@ describe('IndexFlowSection', () => {
146139
{ name: 'field3', type: '1 (asc)' },
147140
];
148141

149-
const coveredQueriesArr = generateCoveredQueriesArr(fields);
150-
const coveredQueries = generateCoveredQueries(coveredQueriesArr, () => {});
151-
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
152-
153142
beforeEach(() => {
154143
renderComponent({ fields });
155144

156145
screen.getByTestId('index-flow-section-covered-queries-button').click();
146+
147+
store.dispatch({
148+
type: ActionTypes.FieldsChanged,
149+
fields,
150+
});
157151
store.dispatch({
158152
type: ActionTypes.CoveredQueriesFetched,
159-
coveredQueries,
160-
optimalQueries,
161-
showCoveredQueries: true,
162153
});
163154
});
164155

@@ -203,19 +194,16 @@ describe('IndexFlowSection', () => {
203194
{ name: 'field2', type: '1 (asc)' },
204195
];
205196

206-
const coveredQueriesArr = generateCoveredQueriesArr(fields);
207-
const coveredQueries = generateCoveredQueries(coveredQueriesArr, () => {});
208-
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
209-
210197
beforeEach(() => {
211198
renderComponent({ fields });
212199

213200
screen.getByTestId('index-flow-section-covered-queries-button').click();
201+
store.dispatch({
202+
type: ActionTypes.FieldsChanged,
203+
fields,
204+
});
214205
store.dispatch({
215206
type: ActionTypes.CoveredQueriesFetched,
216-
coveredQueries,
217-
optimalQueries,
218-
showCoveredQueries: true,
219207
});
220208
});
221209

@@ -243,19 +231,16 @@ describe('IndexFlowSection', () => {
243231
describe('when 1 index field is filled in and user clicks on covered queries button', () => {
244232
const fields: Field[] = [{ name: 'field1', type: '1 (asc)' }];
245233

246-
const coveredQueriesArr = generateCoveredQueriesArr(fields);
247-
const coveredQueries = generateCoveredQueries(coveredQueriesArr, () => {});
248-
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
249-
250234
beforeEach(() => {
251235
renderComponent({ fields });
252236

253237
screen.getByTestId('index-flow-section-covered-queries-button').click();
238+
store.dispatch({
239+
type: ActionTypes.FieldsChanged,
240+
fields,
241+
});
254242
store.dispatch({
255243
type: ActionTypes.CoveredQueriesFetched,
256-
coveredQueries,
257-
optimalQueries,
258-
showCoveredQueries: true,
259244
});
260245
});
261246

packages/compass-indexes/src/components/create-index-form/query-flow-section.spec.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ describe('QueryFlowSection', () => {
1818
serverVersion="5.0.0"
1919
dbName={dbName}
2020
collectionName={collectionName}
21-
initialQuery={null}
22-
inputQuery={''}
23-
setInputQuery={() => {}}
2421
/>
2522
</Provider>
2623
);

packages/compass-indexes/src/components/create-index-form/query-flow-section.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ const QueryFlowSection = ({
115115
dbName: string;
116116
collectionName: string;
117117
onSuggestedIndexButtonClick: ({
118-
dbName,
119-
collectionName,
120118
query,
121119
}: SuggestedIndexFetchedProps) => Promise<void>;
122120
indexSuggestions: Record<string, number> | null;
@@ -152,11 +150,9 @@ const QueryFlowSection = ({
152150
const sanitizedInputQuery = query.trim();
153151

154152
void onSuggestedIndexButtonClick({
155-
dbName,
156-
collectionName,
157153
query: sanitizedInputQuery,
158154
});
159-
}, [query, onSuggestedIndexButtonClick, dbName, collectionName]);
155+
}, [query, onSuggestedIndexButtonClick]);
160156

161157
const handleSuggestedIndexButtonClick = () => {
162158
generateSuggestedIndexes();
@@ -189,16 +185,6 @@ const QueryFlowSection = ({
189185
}
190186
}, [hasQueryChanges, query]);
191187

192-
// useEffect(() => {
193-
// // If there is an initial query from the insights nudge, we generate suggested indexes
194-
// if (initialQuery !== null) {
195-
// generateSuggestedIndexes();
196-
// }
197-
// // we do not want to update this when the initialQuery changes
198-
// // this should just be done when the component first renders
199-
// // eslint-disable-next-line
200-
// }, []);
201-
202188
return (
203189
<>
204190
{initialQuery && (

packages/compass-indexes/src/components/create-index-modal/create-index-modal.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ import {
3030
import { useConnectionInfoRef } from '@mongodb-js/compass-connections/provider';
3131
import { usePreference } from 'compass-preferences-model/provider';
3232
import CreateIndexModalHeader from './create-index-modal-header';
33-
import type { Document } from 'mongodb';
3433

3534
type CreateIndexModalProps = React.ComponentProps<typeof CreateIndexForm> & {
3635
isVisible: boolean;
3736
namespace: string;
3837
error: string | null;
3938
currentTab: Tab;
40-
query: Document | null;
4139
onErrorBannerCloseClick: () => void;
4240
onCreateIndexClick: () => void;
4341
onCancelCreateIndexClick: () => void;
@@ -52,7 +50,6 @@ function CreateIndexModal({
5250
onErrorBannerCloseClick,
5351
onCreateIndexClick,
5452
onCancelCreateIndexClick,
55-
query,
5653
...props
5754
}: CreateIndexModalProps) {
5855
const connectionInfoRef = useConnectionInfoRef();
@@ -117,7 +114,6 @@ function CreateIndexModal({
117114
namespace={namespace}
118115
showIndexesGuidanceVariant={showIndexesGuidanceVariant}
119116
currentTab={currentTab}
120-
query={query}
121117
/>
122118
</ModalBody>
123119

@@ -134,15 +130,14 @@ function CreateIndexModal({
134130
}
135131

136132
const mapState = ({ namespace, serverVersion, createIndex }: RootState) => {
137-
const { fields, error, isVisible, currentTab, query } = createIndex;
133+
const { fields, error, isVisible, currentTab } = createIndex;
138134
return {
139135
fields,
140136
error,
141137
isVisible,
142138
namespace,
143139
serverVersion,
144140
currentTab,
145-
query,
146141
};
147142
};
148143

packages/compass-indexes/src/modules/create-index.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { IndexesThunkAction, RootState } from '.';
1010
import { createRegularIndex } from './regular-indexes';
1111
import * as mql from 'mongodb-mql-engines';
1212
import _parseShellBSON, { ParseMode } from '@mongodb-js/shell-bson-parser';
13+
import toNS from 'mongodb-ns';
1314

1415
export enum ActionTypes {
1516
FieldAdded = 'compass-indexes/create-index/fields/field-added',
@@ -363,10 +364,27 @@ function getInitialState(): State {
363364

364365
//-------
365366

366-
export const createIndexOpened = (initialQuery?: Document) => ({
367-
type: ActionTypes.CreateIndexOpened,
368-
initialQuery,
369-
});
367+
export const createIndexOpened = (
368+
initialQuery?: Document
369+
): IndexesThunkAction<
370+
Promise<void>,
371+
SuggestedIndexFetchedAction | CreateIndexOpenedAction
372+
> => {
373+
return async (dispatch) => {
374+
dispatch({
375+
type: ActionTypes.CreateIndexOpened,
376+
initialQuery,
377+
});
378+
379+
if (initialQuery) {
380+
await dispatch(
381+
fetchIndexSuggestions({
382+
query: JSON.stringify(initialQuery, null, 2) || '',
383+
})
384+
);
385+
}
386+
};
387+
};
370388

371389
export const createIndexClosed = () => ({
372390
type: ActionTypes.CreateIndexClosed,
@@ -398,8 +416,6 @@ export type SuggestedIndexFetchedAction = {
398416
};
399417

400418
export type SuggestedIndexFetchedProps = {
401-
dbName: string;
402-
collectionName: string;
403419
query: string;
404420
};
405421

@@ -419,8 +435,6 @@ export type QueryUpdatedAction = {
419435
};
420436

421437
export const fetchIndexSuggestions = ({
422-
dbName,
423-
collectionName,
424438
query,
425439
}: SuggestedIndexFetchedProps): IndexesThunkAction<
426440
Promise<void>,
@@ -430,12 +444,11 @@ export const fetchIndexSuggestions = ({
430444
dispatch({
431445
type: ActionTypes.SuggestedIndexesRequested,
432446
});
433-
const namespace = `${dbName}.${collectionName}`;
434447

448+
const namespace = getState().namespace;
435449
// Get sample documents from state if it's already there, otherwise fetch it
436450
let sampleDocuments: Array<Document> | null =
437451
getState().createIndex.sampleDocs || null;
438-
439452
// If it's null, that means it has not been fetched before
440453
if (sampleDocuments === null) {
441454
try {
@@ -462,8 +475,9 @@ export const fetchIndexSuggestions = ({
462475

463476
// Analyze namespace and fetch suggestions
464477
try {
478+
const { database, collection } = toNS(getState().namespace);
465479
const analyzedNamespace = mql.analyzeNamespace(
466-
{ database: dbName, collection: collectionName },
480+
{ database, collection },
467481
sampleDocuments
468482
);
469483

0 commit comments

Comments
 (0)