Skip to content

Commit 32f7985

Browse files
authored
fix(compass-indexes): reset atlas search index on reopen and type change (#5489)
1 parent 9472f51 commit 32f7985

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

packages/compass-indexes/src/components/search-indexes-modals/base-search-index-modal.spec.tsx

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { ATLAS_SEARCH_TEMPLATES } from '@mongodb-js/mongodb-constants';
1+
import {
2+
ATLAS_SEARCH_TEMPLATES,
3+
ATLAS_VECTOR_SEARCH_TEMPLATE,
4+
} from '@mongodb-js/mongodb-constants';
25
import { expect } from 'chai';
36
import { BaseSearchIndexModal } from './base-search-index-modal';
47
import sinon from 'sinon';
@@ -7,7 +10,10 @@ import { render, screen, cleanup, waitFor } from '@testing-library/react';
710
import userEvent from '@testing-library/user-event';
811

912
import React from 'react';
10-
import { getCodemirrorEditorValue } from '@mongodb-js/compass-editor';
13+
import {
14+
getCodemirrorEditorValue,
15+
setCodemirrorEditorValue,
16+
} from '@mongodb-js/compass-editor';
1117

1218
function normalizedTemplateNamed(name: string) {
1319
const snippet =
@@ -19,6 +25,20 @@ function normalizedTemplateNamed(name: string) {
1925
return snippet.replace(/\${\d+:([^}]+)}/gm, '$1');
2026
}
2127

28+
const VALID_ATLAS_SEARCH_INDEX_DEFINITION = {
29+
fields: [
30+
{
31+
type: 'vector',
32+
path: 'pineapple',
33+
numDimensions: 1000,
34+
similarity: 'cosine',
35+
},
36+
],
37+
};
38+
const VALID_ATLAS_SEARCH_INDEX_DEFINITION_STRING = JSON.stringify(
39+
VALID_ATLAS_SEARCH_INDEX_DEFINITION
40+
);
41+
2242
function renderBaseSearchIndexModal(
2343
props?: Partial<React.ComponentProps<typeof BaseSearchIndexModal>>
2444
) {
@@ -113,7 +133,7 @@ describe('Base Search Index Modal', function () {
113133
});
114134
});
115135

116-
it('submits the modal with the correct type on create search index', function () {
136+
it('submits the modal with the correct type on create search index', async function () {
117137
userEvent.click(
118138
screen.getByTestId('search-index-type-vectorSearch-button'),
119139
undefined,
@@ -122,15 +142,73 @@ describe('Base Search Index Modal', function () {
122142
skipPointerEventsCheck: true,
123143
}
124144
);
145+
await waitFor(() => {
146+
const indexDef = getCodemirrorEditorValue(
147+
'definition-of-search-index'
148+
);
149+
expect(indexDef).to.equal(ATLAS_VECTOR_SEARCH_TEMPLATE.snippet);
150+
});
151+
152+
// Set the value to something where the create index button is enabled.
153+
await setCodemirrorEditorValue(
154+
'definition-of-search-index',
155+
VALID_ATLAS_SEARCH_INDEX_DEFINITION_STRING
156+
);
125157
userEvent.click(
126158
screen.getByRole('button', { name: 'Create Search Index' })
127159
);
128160
expect(onSubmitSpy).to.have.been.calledOnceWithExactly({
129161
name: 'default',
130-
definition: {},
162+
definition: VALID_ATLAS_SEARCH_INDEX_DEFINITION,
131163
type: 'vectorSearch',
132164
});
133165
});
166+
167+
it('resets the template on type switch', async function () {
168+
userEvent.click(
169+
screen.getByTestId('search-index-type-vectorSearch-button'),
170+
undefined,
171+
{
172+
// leafygreen adds pointer-events: none on actually clickable elements
173+
skipPointerEventsCheck: true,
174+
}
175+
);
176+
177+
await waitFor(() => {
178+
const indexDef = getCodemirrorEditorValue(
179+
'definition-of-search-index'
180+
);
181+
expect(indexDef).to.equal(ATLAS_VECTOR_SEARCH_TEMPLATE.snippet);
182+
});
183+
184+
userEvent.click(
185+
screen.getByTestId('search-index-type-search-button'),
186+
undefined,
187+
{
188+
// leafygreen adds pointer-events: none on actually clickable elements
189+
skipPointerEventsCheck: true,
190+
}
191+
);
192+
193+
await waitFor(() => {
194+
const indexDef = getCodemirrorEditorValue(
195+
'definition-of-search-index'
196+
);
197+
expect(indexDef).to.equal(
198+
normalizedTemplateNamed('Dynamic field mappings')
199+
);
200+
});
201+
// Ensure the state is updated when the ace snippet changes.
202+
await new Promise((resolve) => setTimeout(resolve, 10));
203+
userEvent.click(
204+
screen.getByRole('button', { name: 'Create Search Index' })
205+
);
206+
expect(onSubmitSpy).to.have.been.calledOnceWithExactly({
207+
name: 'default',
208+
definition: { mappings: { dynamic: true } },
209+
type: 'search',
210+
});
211+
});
134212
});
135213

136214
describe('templates', function () {

packages/compass-indexes/src/components/search-indexes-modals/base-search-index-modal.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type { Document } from 'mongodb';
3737
import { useTrackOnChange } from '@mongodb-js/compass-logging/provider';
3838
import { SearchIndexTemplateDropdown } from '../search-index-template-dropdown';
3939
import {
40+
ATLAS_SEARCH_TEMPLATES,
4041
ATLAS_VECTOR_SEARCH_TEMPLATE,
4142
type SearchTemplate,
4243
} from '@mongodb-js/mongodb-constants';
@@ -201,6 +202,7 @@ export const BaseSearchIndexModal: React.FunctionComponent<
201202

202203
useEffect(() => {
203204
if (isModalOpen) {
205+
setSearchIndexType('search');
204206
setIndexName(initialIndexName);
205207
setIndexDefinition(initialIndexDefinition);
206208
setParsingError(undefined);
@@ -261,12 +263,14 @@ export const BaseSearchIndexModal: React.FunctionComponent<
261263

262264
// Set the template.
263265
if (value === 'vectorSearch') {
266+
setIndexDefinition(ATLAS_VECTOR_SEARCH_TEMPLATE.snippet);
264267
onChangeTemplate(ATLAS_VECTOR_SEARCH_TEMPLATE);
265268
} else {
266-
onSearchIndexDefinitionChanged(DEFAULT_INDEX_DEFINITION);
269+
setIndexDefinition(ATLAS_SEARCH_TEMPLATES[0].snippet);
270+
onChangeTemplate(ATLAS_SEARCH_TEMPLATES[0]);
267271
}
268272
},
269-
[setSearchIndexType, onChangeTemplate, onSearchIndexDefinitionChanged]
273+
[setSearchIndexType, onChangeTemplate, setIndexDefinition]
270274
);
271275

272276
const fields = useAutocompleteFields(namespace);
@@ -421,7 +425,7 @@ export const BaseSearchIndexModal: React.FunctionComponent<
421425
data-testid="search-index-submit-button"
422426
variant="primary"
423427
onClick={onSubmitIndex}
424-
disabled={isBusy}
428+
disabled={isBusy || !!parsingError}
425429
>
426430
{mode === 'create' ? 'Create Search Index' : 'Save'}
427431
</Button>

0 commit comments

Comments
 (0)