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' ;
25import { expect } from 'chai' ;
36import { BaseSearchIndexModal } from './base-search-index-modal' ;
47import sinon from 'sinon' ;
@@ -7,7 +10,10 @@ import { render, screen, cleanup, waitFor } from '@testing-library/react';
710import userEvent from '@testing-library/user-event' ;
811
912import React from 'react' ;
10- import { getCodemirrorEditorValue } from '@mongodb-js/compass-editor' ;
13+ import {
14+ getCodemirrorEditorValue ,
15+ setCodemirrorEditorValue ,
16+ } from '@mongodb-js/compass-editor' ;
1117
1218function 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+
2242function 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 ( ) {
0 commit comments