@@ -88,6 +88,88 @@ describe('Form rendering', () => {
88
88
cy . getDropdownByLabelText ( 'Urgency' ) . should ( 'have.text' , "Very low" ) ;
89
89
cy . getDropdownByLabelText ( 'Request type' ) . should ( 'have.text' , "Request" ) ;
90
90
} ) ;
91
+
92
+ it ( 'Mandatory questions must be filled' , ( ) => {
93
+ // Set up a form with two sections, each with a mandatory question
94
+ cy . createWithAPI ( 'Glpi\\Form\\Form' , {
95
+ 'name' : 'Test mandatory questions' ,
96
+ } ) . as ( 'form_id' ) ;
97
+ cy . get ( '@form_id' ) . then ( ( form_id ) => {
98
+ // Add mandatory question to default section
99
+ cy . addQuestionToDefaultSectionWithAPI (
100
+ form_id ,
101
+ 'First question' ,
102
+ 'Glpi\\Form\\QuestionType\\QuestionTypeShortText' ,
103
+ 0 ,
104
+ null ,
105
+ null ,
106
+ null ,
107
+ true // Mandatory
108
+ ) ;
109
+
110
+ // Add second section
111
+ cy . createWithAPI ( 'Glpi\\Form\\Section' , {
112
+ 'name' : 'Second section' ,
113
+ 'rank' : 1 ,
114
+ 'forms_forms_id' : form_id ,
115
+ } ) . as ( 'second_section_id' ) ;
116
+
117
+ cy . get ( '@second_section_id' ) . then ( ( second_section_id ) => {
118
+ // Add mandatory question to second section
119
+ cy . createWithAPI ( 'Glpi\\Form\\Question' , {
120
+ 'name' : 'Second question' ,
121
+ 'type' : 'Glpi\\Form\\QuestionType\\QuestionTypeShortText' ,
122
+ 'vertical_rank' : 1 ,
123
+ 'forms_sections_id' : second_section_id ,
124
+ 'is_mandatory' : true ,
125
+ } ) . as ( 'second_section_id' ) ;
126
+ } ) ;
127
+
128
+ // Preview form
129
+ cy . login ( ) ;
130
+ cy . visit ( `/Form/Render/${ form_id } ` ) ;
131
+
132
+ // Try to submit first section, should fail since we didn't answer
133
+ // the mandatory question
134
+ cy . findByRole ( 'button' , { name : 'Continue' } ) . click ( ) ;
135
+ // Wait to be sure that the current state will not be used a false
136
+ // positive to pass the following assertions in case the current
137
+ // section was not yet updated by the JS logic.
138
+ // This would not be needed if we could detect and wait for the "Please
139
+ // fill this input" popup from the browser but it doesn't seem
140
+ // supported by cypress.
141
+ // eslint-disable-next-line cypress/no-unnecessary-waiting
142
+ cy . wait ( 600 ) ;
143
+ cy . findByRole ( 'heading' , { name : 'First section' } ) . should ( 'be.visible' ) ;
144
+ cy . findByRole ( 'heading' , { name : 'Second section' } ) . should ( 'not.exist' ) ;
145
+
146
+ // Submit again with a value
147
+ cy . findByRole ( 'textbox' , { name : 'First question' } ) . type ( "test" ) ;
148
+ cy . findByRole ( 'button' , { name : 'Continue' } ) . click ( ) ;
149
+ cy . findByRole ( 'heading' , { name : 'Second section' } ) . should ( 'be.visible' ) ;
150
+ cy . findByRole ( 'heading' , { name : 'First section' } ) . should ( 'not.exist' ) ;
151
+
152
+ // Try to submit the final section, should fail since we didn't answer
153
+ // the mandatory question
154
+ cy . findByRole ( 'button' , { name : 'Submit' } ) . click ( ) ;
155
+ // Wait to be sure that the current state will not be used a false
156
+ // positive to pass the following assertions in case the current
157
+ // section was not yet updated by the JS logic.
158
+ // This would not be needed if we could detect and wait for the "Please
159
+ // fill this input" popup from the browser but it doesn't seem
160
+ // supported by cypress.
161
+ // eslint-disable-next-line cypress/no-unnecessary-waiting
162
+ cy . wait ( 600 ) ;
163
+ cy . findByRole ( 'heading' , { name : 'Second section' } ) . should ( 'be.visible' ) ;
164
+ cy . findByText ( 'Form submitted' ) . should ( 'not.be.visible' ) ;
165
+
166
+ // Submit again with a value
167
+ cy . findByRole ( 'textbox' , { name : 'Second question' } ) . type ( "test" ) ;
168
+ cy . findByRole ( 'button' , { name : 'Submit' } ) . click ( ) ;
169
+ cy . findByRole ( 'heading' , { name : 'Second section' } ) . should ( 'not.exist' ) ;
170
+ cy . findByText ( 'Form submitted' ) . should ( 'be.visible' ) ;
171
+ } ) ;
172
+ } ) ;
91
173
} ) ;
92
174
93
175
function addQuestionAndGetUuuid ( name , type = null , subtype = null ) {
0 commit comments