Skip to content

Commit 0df16ea

Browse files
authored
fix(indexes): check for vector search support when showing edit templates COMPASS-8235 (#6196)
1 parent 7d60fbd commit 0df16ea

File tree

5 files changed

+120
-30
lines changed

5 files changed

+120
-30
lines changed

packages/compass-indexes/src/components/search-index-template-dropdown/index.spec.tsx

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,68 @@ import userEvent from '@testing-library/user-event';
99

1010
import React from 'react';
1111

12+
const knnVectorText = 'KNN Vector field mapping';
13+
1214
function templateNamed(name: string) {
1315
return ATLAS_SEARCH_TEMPLATES.find((t) => t.name === name);
1416
}
1517

1618
describe('Search Index Template Dropdown', function () {
17-
let onTemplateSpy: SinonSpy;
18-
19-
beforeEach(function () {
20-
onTemplateSpy = sinon.spy();
21-
22-
render(
23-
<SearchIndexTemplateDropdown
24-
tooltip="Tooltip"
25-
isVectorSearchSupported
26-
onTemplate={onTemplateSpy}
27-
/>
28-
);
29-
});
30-
3119
afterEach(cleanup);
3220

33-
it('notifies upwards with onTemplate when a new template is choosen', async function () {
34-
const dropDown = screen
35-
.getByText('Dynamic field mappings')
36-
.closest('button')!;
21+
describe('when rendered', function () {
22+
let onTemplateSpy: SinonSpy;
23+
24+
beforeEach(function () {
25+
onTemplateSpy = sinon.spy();
26+
27+
render(
28+
<SearchIndexTemplateDropdown
29+
tooltip="Tooltip"
30+
isVectorSearchSupported
31+
onTemplate={onTemplateSpy}
32+
/>
33+
);
34+
});
3735

38-
userEvent.click(dropDown);
36+
it('notifies upwards with onTemplate when a new template is chosen', async function () {
37+
const dropDown = screen
38+
.getByText('Dynamic field mappings')
39+
.closest('button')!;
40+
41+
userEvent.click(dropDown);
42+
43+
const staticFieldMappingOption = await screen.findByText(
44+
'Static field mappings'
45+
);
46+
userEvent.click(staticFieldMappingOption);
47+
48+
expect(onTemplateSpy).to.have.been.calledWith(
49+
templateNamed('Static field mappings')
50+
);
51+
});
52+
53+
it('does not shows the knn vector search template', function () {
54+
userEvent.click(screen.getByRole('button', { name: 'Template' }));
55+
expect(screen.queryByRole('option', { name: knnVectorText })).to.not
56+
.exist;
57+
});
58+
});
3959

40-
const staticFieldMappingOption = await screen.findByText(
41-
'Static field mappings'
42-
);
43-
userEvent.click(staticFieldMappingOption);
60+
describe('when rendered with vector search disabled', function () {
61+
beforeEach(function () {
62+
render(
63+
<SearchIndexTemplateDropdown
64+
tooltip="Tooltip"
65+
isVectorSearchSupported={false}
66+
onTemplate={() => {}}
67+
/>
68+
);
69+
});
4470

45-
expect(onTemplateSpy).to.have.been.calledWith(
46-
templateNamed('Static field mappings')
47-
);
71+
it('shows the knn vector search template', function () {
72+
userEvent.click(screen.getByRole('button', { name: 'Template' }));
73+
expect(screen.getByRole('option', { name: knnVectorText })).to.be.visible;
74+
});
4875
});
4976
});

packages/compass-indexes/src/components/search-index-template-dropdown/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ import {
1414
const containerStyles = css({
1515
display: 'flex',
1616
flexDirection: 'column',
17-
gap: spacing[1],
17+
gap: spacing[100],
1818
});
1919

2020
const dropdownLabelStyles = css({
2121
display: 'flex',
22-
gap: spacing[1],
22+
gap: spacing[100],
2323
alignItems: 'center',
2424
});
2525

2626
type SearchIndexTemplateDropdownProps = {
2727
tooltip: string;
28-
isVectorSearchSupported?: boolean;
28+
isVectorSearchSupported: boolean;
2929
onTemplate: (template: SearchTemplate) => void;
3030
};
3131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type BaseSearchIndexModalProps = {
119119
initialIndexType?: string;
120120
isModalOpen: boolean;
121121
isBusy: boolean;
122-
isVectorSearchSupported?: boolean;
122+
isVectorSearchSupported: boolean;
123123
error: string | undefined;
124124
onSubmit: (index: {
125125
name: string;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react';
2+
import { expect } from 'chai';
3+
import { render, screen, cleanup } from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
6+
import { UpdateSearchIndexModal } from './update-search-index-modal';
7+
8+
const knnVectorText = 'KNN Vector field mapping';
9+
10+
function renderUpdateSearchIndexModal(
11+
props?: Partial<React.ComponentProps<typeof UpdateSearchIndexModal>>
12+
) {
13+
return render(
14+
<UpdateSearchIndexModal
15+
namespace="test.test"
16+
indexName="testIndex"
17+
isVectorSearchSupported
18+
indexDefinition="testDefinition"
19+
isModalOpen={true}
20+
isBusy={false}
21+
onUpdateIndex={() => {}}
22+
onCloseModal={() => {}}
23+
error={'Invalid index definition.'}
24+
{...props}
25+
/>
26+
);
27+
}
28+
29+
describe('Base Search Index Modal', function () {
30+
afterEach(cleanup);
31+
32+
describe('when rendered', function () {
33+
beforeEach(function () {
34+
renderUpdateSearchIndexModal();
35+
});
36+
37+
it('does not show the KNN vector field mapping template', function () {
38+
userEvent.click(screen.getByRole('button', { name: 'Template' }));
39+
expect(screen.queryByRole('option', { name: knnVectorText })).to.not
40+
.exist;
41+
});
42+
});
43+
44+
describe('when rendered and isVectorSearchSupported is false', function () {
45+
beforeEach(function () {
46+
renderUpdateSearchIndexModal({
47+
isVectorSearchSupported: false,
48+
});
49+
});
50+
51+
it('shows the KNN vector field mapping template', function () {
52+
userEvent.click(screen.getByRole('button', { name: 'Template' }));
53+
expect(screen.getByRole('option', { name: knnVectorText })).to.be.visible;
54+
});
55+
});
56+
});

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { connect } from 'react-redux';
44
import type { RootState } from '../../modules';
55
import type { Document } from 'mongodb';
66
import { BaseSearchIndexModal } from './base-search-index-modal';
7+
import { isAtlasVectorSearchSupportedForServerVersion } from '../../utils/vector-search-indexes';
78

89
type UpdateSearchIndexModalProps = {
910
namespace: string;
@@ -12,6 +13,7 @@ type UpdateSearchIndexModalProps = {
1213
indexType?: string;
1314
isModalOpen: boolean;
1415
isBusy: boolean;
16+
isVectorSearchSupported: boolean;
1517
error: string | undefined;
1618
onUpdateIndex: (index: {
1719
name: string;
@@ -30,13 +32,15 @@ export const UpdateSearchIndexModal: React.FunctionComponent<
3032
indexType,
3133
isModalOpen,
3234
isBusy,
35+
isVectorSearchSupported,
3336
error,
3437
onUpdateIndex,
3538
onCloseModal,
3639
}) => {
3740
return (
3841
<BaseSearchIndexModal
3942
namespace={namespace}
43+
isVectorSearchSupported={isVectorSearchSupported}
4044
mode={'update'}
4145
initialIndexName={indexName}
4246
initialIndexType={indexType}
@@ -51,6 +55,7 @@ export const UpdateSearchIndexModal: React.FunctionComponent<
5155
};
5256

5357
const mapState = ({
58+
serverVersion,
5459
namespace,
5560
searchIndexes: {
5661
indexes,
@@ -59,6 +64,8 @@ const mapState = ({
5964
}: RootState) => {
6065
const index = indexes.find((x) => x.name === indexName);
6166
return {
67+
isVectorSearchSupported:
68+
isAtlasVectorSearchSupportedForServerVersion(serverVersion),
6269
namespace,
6370
isModalOpen,
6471
isBusy,

0 commit comments

Comments
 (0)