@@ -2,11 +2,22 @@ import React from 'react';
22import { mount } from 'enzyme' ;
33import { expect } from 'chai' ;
44import sinon from 'sinon' ;
5- import { Select , TextInput } from '@mongodb-js/compass-components' ;
5+ import { Select } from '@mongodb-js/compass-components' ;
66
77import CollectionFields from '.' ;
88import TimeSeriesFields from './time-series-fields' ;
99
10+ const additionalPreferenceSelector =
11+ 'button[data-testid="additional-collection-preferences"]' ;
12+ const timeSeriesCollectionSelector =
13+ 'input[type="checkbox"][data-testid="time-series-fields-checkbox"]' ;
14+ const cappedCollectionSelector =
15+ 'input[type="checkbox"][data-testid="capped-collection-fields-checkbox"]' ;
16+ const customCollationSelector =
17+ 'input[type="checkbox"][data-testid="use-custom-collation-fields-checkbox"]' ;
18+ const clusteredCollectionSelector =
19+ 'input[type="checkbox"][data-testid="clustered-collection-fields-checkbox"]' ;
20+
1021describe ( 'CollectionFields [Component]' , function ( ) {
1122 context ( 'when withDatabase prop is true' , function ( ) {
1223 let component ;
@@ -26,7 +37,9 @@ describe('CollectionFields [Component]', function () {
2637 } ) ;
2738
2839 it ( 'renders a database name input field' , function ( ) {
29- expect ( component . find ( TextInput ) . length ) . to . equal ( 2 ) ;
40+ expect (
41+ component . find ( 'input[type="text"][data-testid="database-name"]' )
42+ ) . to . be . present ( ) ;
3043 expect ( component . text ( ) . includes ( 'Database Name' ) ) . to . equal ( true ) ;
3144 } ) ;
3245 } ) ;
@@ -45,11 +58,77 @@ describe('CollectionFields [Component]', function () {
4558 } ) ;
4659
4760 it ( 'does not render a database name input field' , function ( ) {
48- expect ( component . find ( TextInput ) . length ) . to . equal ( 1 ) ;
61+ expect (
62+ component . find ( 'input[type="text"][data-testid="database-name"]' )
63+ ) . to . not . be . present ( ) ;
4964 expect ( component . text ( ) . includes ( 'Database Name' ) ) . to . equal ( false ) ;
5065 } ) ;
5166 } ) ;
5267
68+ context ( 'with server version >= 5.3' , function ( ) {
69+ let component ;
70+ let onChangeSpy ;
71+
72+ beforeEach ( function ( ) {
73+ onChangeSpy = sinon . spy ( ) ;
74+ component = mount (
75+ < CollectionFields
76+ onChange = { onChangeSpy }
77+ withDatabase
78+ serverVersion = "5.3"
79+ />
80+ ) ;
81+ component . find ( additionalPreferenceSelector ) . simulate ( 'click' ) ;
82+ } ) ;
83+
84+ afterEach ( function ( ) {
85+ component = null ;
86+ onChangeSpy = null ;
87+ } ) ;
88+
89+ describe ( 'when the clustered collection checkbox is clicked' , function ( ) {
90+ beforeEach ( function ( ) {
91+ component
92+ . find ( clusteredCollectionSelector )
93+ . at ( 0 )
94+ . simulate ( 'change' , { target : { checked : true } } ) ;
95+ component . update ( ) ;
96+ } ) ;
97+
98+ it ( 'calls the onchange with clustered collection on' , function ( ) {
99+ expect ( onChangeSpy . callCount ) . to . equal ( 1 ) ;
100+ expect ( onChangeSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
101+ database : '' ,
102+ collection : '' ,
103+ options : {
104+ clusteredIndex : {
105+ key : { _id : 1 } ,
106+ unique : true ,
107+ } ,
108+ } ,
109+ } ) ;
110+ } ) ;
111+
112+ context ( 'when clicked twice' , function ( ) {
113+ beforeEach ( function ( ) {
114+ component
115+ . find ( clusteredCollectionSelector )
116+ . at ( 0 )
117+ . simulate ( 'change' , { target : { checked : false } } ) ;
118+ component . update ( ) ;
119+ } ) ;
120+ it ( 'calls the onchange with clustered collection off' , function ( ) {
121+ expect ( onChangeSpy . callCount ) . to . equal ( 2 ) ;
122+ expect ( onChangeSpy . secondCall . args [ 0 ] ) . to . deep . equal ( {
123+ database : '' ,
124+ collection : '' ,
125+ options : { } ,
126+ } ) ;
127+ } ) ;
128+ } ) ;
129+ } ) ;
130+ } ) ;
131+
53132 context ( 'with server version >= 5.0' , function ( ) {
54133 let component ;
55134 let onChangeSpy ;
@@ -63,9 +142,6 @@ describe('CollectionFields [Component]', function () {
63142 serverVersion = "5.0"
64143 />
65144 ) ;
66- component
67- . find ( 'button[data-testid="advanced-collection-options"]' )
68- . simulate ( 'click' ) ;
69145 } ) ;
70146
71147 afterEach ( function ( ) {
@@ -81,8 +157,8 @@ describe('CollectionFields [Component]', function () {
81157 describe ( 'when the time series checkbox is clicked' , function ( ) {
82158 beforeEach ( function ( ) {
83159 component
84- . find ( 'input[type="checkbox"]' )
85- . at ( 2 )
160+ . find ( timeSeriesCollectionSelector )
161+ . at ( 0 )
86162 . simulate ( 'change' , { target : { checked : true } } ) ;
87163 component . update ( ) ;
88164 } ) ;
@@ -111,9 +187,6 @@ describe('CollectionFields [Component]', function () {
111187 serverVersion = "4.3.0"
112188 />
113189 ) ;
114- component
115- . find ( 'button[data-testid="advanced-collection-options"]' )
116- . simulate ( 'click' ) ;
117190 } ) ;
118191
119192 afterEach ( function ( ) {
@@ -137,9 +210,7 @@ describe('CollectionFields [Component]', function () {
137210 component = mount (
138211 < CollectionFields onChange = { onChangeSpy } serverVersion = "4.3.0" />
139212 ) ;
140- component
141- . find ( 'button[data-testid="advanced-collection-options"]' )
142- . simulate ( 'click' ) ;
213+ component . find ( additionalPreferenceSelector ) . simulate ( 'click' ) ;
143214 } ) ;
144215
145216 afterEach ( function ( ) {
@@ -150,8 +221,8 @@ describe('CollectionFields [Component]', function () {
150221 describe ( 'when the collation checkbox is clicked' , function ( ) {
151222 beforeEach ( function ( ) {
152223 component
153- . find ( 'input[type="checkbox"]' )
154- . at ( 1 )
224+ . find ( customCollationSelector )
225+ . at ( 0 )
155226 . simulate ( 'change' , { target : { checked : true } } ) ;
156227 component . update ( ) ;
157228 } ) ;
@@ -171,8 +242,8 @@ describe('CollectionFields [Component]', function () {
171242 describe ( 'when the collation checkbox is clicked and a locale chosen' , function ( ) {
172243 beforeEach ( function ( ) {
173244 component
174- . find ( 'input[type="checkbox"]' )
175- . at ( 1 )
245+ . find ( customCollationSelector )
246+ . at ( 0 )
176247 . simulate ( 'change' , { target : { checked : true } } ) ;
177248 component . update ( ) ;
178249 component . find ( Select ) . at ( 0 ) . props ( ) . onChange ( 'af' ) ;
@@ -196,7 +267,7 @@ describe('CollectionFields [Component]', function () {
196267 describe ( 'when the capped collection checkbox is clicked' , function ( ) {
197268 beforeEach ( function ( ) {
198269 component
199- . find ( 'input[type="checkbox"]' )
270+ . find ( cappedCollectionSelector )
200271 . at ( 0 )
201272 . simulate ( 'change' , { target : { checked : true } } ) ;
202273 component . update ( ) ;
@@ -217,12 +288,12 @@ describe('CollectionFields [Component]', function () {
217288 describe ( 'when the capped collection checkbox is clicked twice' , function ( ) {
218289 beforeEach ( function ( ) {
219290 component
220- . find ( 'input[type="checkbox"]' )
291+ . find ( cappedCollectionSelector )
221292 . at ( 0 )
222293 . simulate ( 'change' , { target : { checked : true } } ) ;
223294 component . update ( ) ;
224295 component
225- . find ( 'input[type="checkbox"]' )
296+ . find ( cappedCollectionSelector )
226297 . at ( 0 )
227298 . simulate ( 'change' , { target : { checked : false } } ) ;
228299 component . update ( ) ;
0 commit comments