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' ;
2
5
import { expect } from 'chai' ;
3
6
import { BaseSearchIndexModal } from './base-search-index-modal' ;
4
7
import sinon from 'sinon' ;
@@ -7,7 +10,10 @@ import { render, screen, cleanup, waitFor } from '@testing-library/react';
7
10
import userEvent from '@testing-library/user-event' ;
8
11
9
12
import React from 'react' ;
10
- import { getCodemirrorEditorValue } from '@mongodb-js/compass-editor' ;
13
+ import {
14
+ getCodemirrorEditorValue ,
15
+ setCodemirrorEditorValue ,
16
+ } from '@mongodb-js/compass-editor' ;
11
17
12
18
function normalizedTemplateNamed ( name : string ) {
13
19
const snippet =
@@ -19,6 +25,20 @@ function normalizedTemplateNamed(name: string) {
19
25
return snippet . replace ( / \$ { \d + : ( [ ^ } ] + ) } / gm, '$1' ) ;
20
26
}
21
27
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
+
22
42
function renderBaseSearchIndexModal (
23
43
props ?: Partial < React . ComponentProps < typeof BaseSearchIndexModal > >
24
44
) {
@@ -113,7 +133,7 @@ describe('Base Search Index Modal', function () {
113
133
} ) ;
114
134
} ) ;
115
135
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 ( ) {
117
137
userEvent . click (
118
138
screen . getByTestId ( 'search-index-type-vectorSearch-button' ) ,
119
139
undefined ,
@@ -122,15 +142,73 @@ describe('Base Search Index Modal', function () {
122
142
skipPointerEventsCheck : true ,
123
143
}
124
144
) ;
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
+ ) ;
125
157
userEvent . click (
126
158
screen . getByRole ( 'button' , { name : 'Create Search Index' } )
127
159
) ;
128
160
expect ( onSubmitSpy ) . to . have . been . calledOnceWithExactly ( {
129
161
name : 'default' ,
130
- definition : { } ,
162
+ definition : VALID_ATLAS_SEARCH_INDEX_DEFINITION ,
131
163
type : 'vectorSearch' ,
132
164
} ) ;
133
165
} ) ;
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
+ } ) ;
134
212
} ) ;
135
213
136
214
describe ( 'templates' , function ( ) {
0 commit comments