@@ -3,35 +3,44 @@ import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
33import { expect } from 'chai' ;
44import sinon from 'sinon' ;
55import { ValidationEditor } from './validation-editor' ;
6+ import { PreferencesProvider } from 'compass-preferences-model/provider' ;
7+ import { createSandboxFromDefaultPreferences } from 'compass-preferences-model' ;
68
7- function renderValidationEditor (
9+ async function renderValidationEditor (
810 props : Partial < React . ComponentProps < typeof ValidationEditor > >
911) {
1012 const validation = {
11- validator : '' ,
13+ validator : '{} ' ,
1214 validationAction : 'warn' ,
1315 validationLevel : 'moderate' ,
1416 isChanged : false ,
1517 syntaxError : null ,
1618 error : null ,
1719 } as const ;
1820
21+ const preferences = await createSandboxFromDefaultPreferences ( ) ;
22+ await preferences . savePreferences ( { enableExportSchema : true } ) ;
23+
1924 return render (
20- < ValidationEditor
21- namespace = "test.test"
22- validatorChanged = { ( ) => { } }
23- validationActionChanged = { ( ) => { } }
24- validationLevelChanged = { ( ) => { } }
25- cancelValidation = { ( ) => { } }
26- saveValidation = { ( ) => { } }
27- clearSampleDocuments = { ( ) => { } }
28- serverVersion = "8.0.5"
29- onClickEnableEditRules = { ( ) => { } }
30- validation = { validation }
31- isEditable
32- isEditingEnabled
33- { ...props }
34- />
25+ < PreferencesProvider value = { preferences } >
26+ < ValidationEditor
27+ namespace = "test.test"
28+ validatorChanged = { ( ) => { } }
29+ validationActionChanged = { ( ) => { } }
30+ validationLevelChanged = { ( ) => { } }
31+ cancelValidation = { ( ) => { } }
32+ saveValidation = { ( ) => { } }
33+ clearSampleDocuments = { ( ) => { } }
34+ serverVersion = "8.0.5"
35+ onClickEnableEditRules = { ( ) => { } }
36+ validation = { validation }
37+ generateValidationRules = { ( ) => { } }
38+ isRulesGenerationInProgress = { false }
39+ isEditable
40+ isEditingEnabled
41+ { ...props }
42+ />
43+ </ PreferencesProvider >
3544 ) ;
3645}
3746
@@ -40,9 +49,9 @@ describe('ValidationEditor [Component]', function () {
4049 'when it is an editable mode but editing is not yet enabled' ,
4150 function ( ) {
4251 let onClickEnableEditRulesSpy : sinon . SinonSpy ;
43- beforeEach ( function ( ) {
52+ beforeEach ( async function ( ) {
4453 onClickEnableEditRulesSpy = sinon . spy ( ) ;
45- renderValidationEditor ( {
54+ await renderValidationEditor ( {
4655 onClickEnableEditRules : onClickEnableEditRulesSpy ,
4756 isEditingEnabled : false ,
4857 isEditable : true ,
@@ -62,8 +71,8 @@ describe('ValidationEditor [Component]', function () {
6271 ) ;
6372
6473 context ( 'when it is an editable mode and editing is enabled' , function ( ) {
65- beforeEach ( function ( ) {
66- renderValidationEditor ( {
74+ beforeEach ( async function ( ) {
75+ await renderValidationEditor ( {
6776 isEditable : true ,
6877 isEditingEnabled : true ,
6978 } ) ;
@@ -78,8 +87,8 @@ describe('ValidationEditor [Component]', function () {
7887 } ) ;
7988
8089 context ( 'when it is a not editable mode' , function ( ) {
81- beforeEach ( function ( ) {
82- renderValidationEditor ( {
90+ beforeEach ( async function ( ) {
91+ await renderValidationEditor ( {
8392 isEditable : false ,
8493 } ) ;
8594 } ) ;
@@ -91,4 +100,83 @@ describe('ValidationEditor [Component]', function () {
91100 ) . to . not . exist ;
92101 } ) ;
93102 } ) ;
103+
104+ context ( 'when the validator is empty' , function ( ) {
105+ let onGenerateRulesSpy : sinon . SinonSpy ;
106+ beforeEach ( async function ( ) {
107+ onGenerateRulesSpy = sinon . spy ( ) ;
108+ await renderValidationEditor ( {
109+ generateValidationRules : onGenerateRulesSpy ,
110+ isEditable : true ,
111+ validation : {
112+ validator : '' ,
113+ validationAction : 'error' ,
114+ validationLevel : 'moderate' ,
115+ isChanged : false ,
116+ syntaxError : null ,
117+ error : null ,
118+ } ,
119+ } ) ;
120+ } ) ;
121+
122+ it ( 'allows to generate rules' , function ( ) {
123+ const generateBtn = screen . getByRole ( 'button' , {
124+ name : 'Generate rules' ,
125+ } ) ;
126+ expect ( generateBtn ) . to . be . visible ;
127+ userEvent . click ( generateBtn ) ;
128+ expect ( onGenerateRulesSpy ) . to . have . been . calledOnce ;
129+ } ) ;
130+ } ) ;
131+
132+ context ( 'when the validator is empty object' , function ( ) {
133+ let onGenerateRulesSpy : sinon . SinonSpy ;
134+ beforeEach ( async function ( ) {
135+ onGenerateRulesSpy = sinon . spy ( ) ;
136+ await renderValidationEditor ( {
137+ generateValidationRules : onGenerateRulesSpy ,
138+ isEditable : true ,
139+ validation : {
140+ validator : '{}' ,
141+ validationAction : 'error' ,
142+ validationLevel : 'moderate' ,
143+ isChanged : false ,
144+ syntaxError : null ,
145+ error : null ,
146+ } ,
147+ } ) ;
148+ } ) ;
149+
150+ it ( 'allows to generate rules' , function ( ) {
151+ const generateBtn = screen . getByRole ( 'button' , {
152+ name : 'Generate rules' ,
153+ } ) ;
154+ expect ( generateBtn ) . to . be . visible ;
155+ userEvent . click ( generateBtn ) ;
156+ expect ( onGenerateRulesSpy ) . to . have . been . calledOnce ;
157+ } ) ;
158+ } ) ;
159+
160+ context ( 'when rules generation is in progress' , function ( ) {
161+ beforeEach ( async function ( ) {
162+ await renderValidationEditor ( {
163+ isEditable : true ,
164+ isRulesGenerationInProgress : true ,
165+ validation : {
166+ validator : '{}' ,
167+ validationAction : 'error' ,
168+ validationLevel : 'moderate' ,
169+ isChanged : false ,
170+ syntaxError : null ,
171+ error : null ,
172+ } ,
173+ } ) ;
174+ } ) ;
175+
176+ it ( 'allows to generate rules' , function ( ) {
177+ const generateBtn = screen . getByTestId ( 'generate-rules-button' ) ;
178+ expect ( generateBtn ) . to . be . visible ;
179+ expect ( generateBtn ) . to . have . attribute ( 'aria-disabled' , 'true' ) ;
180+ } ) ;
181+ } ) ;
94182} ) ;
0 commit comments