@@ -146,26 +146,15 @@ describe('TextField', () => {
146
146
147
147
describe ( 'validation message' , ( ) => {
148
148
it ( 'should not render validationMessage if enableErrors prop not supplied' , async ( ) => {
149
- const component = (
150
- < TestCase
151
- value = { '' }
152
- validationMessage = { 'mock message' }
153
- validateOnStart
154
- /> ) ;
149
+ const component = < TestCase value = { '' } validationMessage = { 'mock message' } validateOnStart /> ;
155
150
156
151
const textFieldDriver = new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
157
152
158
153
expect ( await textFieldDriver . isValidationMsgExists ( ) ) . toBe ( false ) ;
159
154
} ) ;
160
155
161
156
it ( 'should render validationMessage on start if input required and validateOnStart passed' , async ( ) => {
162
- const component = (
163
- < TestCase
164
- value = { '' }
165
- validationMessage = { 'mock message' }
166
- enableErrors
167
- validateOnStart
168
- /> ) ;
157
+ const component = < TestCase value = { '' } validationMessage = { 'mock message' } enableErrors validateOnStart /> ;
169
158
const textFieldDriver = new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
170
159
171
160
expect ( await textFieldDriver . isValidationMsgExists ( ) ) . toBe ( true ) ;
@@ -174,13 +163,8 @@ describe('TextField', () => {
174
163
175
164
it ( 'should render validationMessage when input is requires after changing the input to empty string' , async ( ) => {
176
165
const component = (
177
- < TestCase
178
- value = { '' }
179
- validate = { 'required' }
180
- validationMessage = { 'mock message' }
181
- enableErrors
182
- validateOnChange
183
- /> ) ;
166
+ < TestCase value = { '' } validate = { 'required' } validationMessage = { 'mock message' } enableErrors validateOnChange />
167
+ ) ;
184
168
const textFieldDriver = new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
185
169
186
170
expect ( await textFieldDriver . isValidationMsgExists ( ) ) . toBe ( false ) ;
@@ -242,13 +226,7 @@ describe('TextField', () => {
242
226
243
227
describe ( 'validateOnBlur' , ( ) => {
244
228
it ( 'validate is called with undefined when defaultValue is not given' , async ( ) => {
245
- const component = (
246
- < TestCase
247
- validateOnBlur
248
- validationMessage = { 'Not valid' }
249
- validate = { [ validate ] }
250
- />
251
- ) ;
229
+ const component = < TestCase validateOnBlur validationMessage = { 'Not valid' } validate = { [ validate ] } /> ;
252
230
const textFieldDriver = new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
253
231
textFieldDriver . focus ( ) ;
254
232
textFieldDriver . blur ( ) ;
@@ -259,12 +237,7 @@ describe('TextField', () => {
259
237
it ( 'validate is called with defaultValue when defaultValue is given' , async ( ) => {
260
238
const defaultValue = '1' ;
261
239
const component = (
262
- < TestCase
263
- validateOnBlur
264
- validationMessage = { 'Not valid' }
265
- validate = { [ validate ] }
266
- defaultValue = { defaultValue }
267
- />
240
+ < TestCase validateOnBlur validationMessage = { 'Not valid' } validate = { [ validate ] } defaultValue = { defaultValue } />
268
241
) ;
269
242
const textFieldDriver = new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
270
243
textFieldDriver . focus ( ) ;
@@ -273,4 +246,95 @@ describe('TextField', () => {
273
246
await waitFor ( ( ) => expect ( validate ) . toHaveBeenCalledWith ( defaultValue ) ) ;
274
247
} ) ;
275
248
} ) ;
249
+ describe ( 'Mandatory Indication' , ( ) => {
250
+ const getTestCaseDriver = ( props : TextFieldProps ) => {
251
+ const component = < TestCase { ...props } /> ;
252
+ return new TextFieldDriver ( { component, testID : TEXT_FIELD_TEST_ID } ) ;
253
+ } ;
254
+ const starReg = / .* \* $ / ;
255
+
256
+ //Sanity
257
+ it ( 'Should show mandatory indication on the label' , async ( ) => {
258
+ const textFieldDriver = getTestCaseDriver ( { label : 'Label' , validate : 'required' , showMandatoryIndication : true } ) ;
259
+ const labelContent = await textFieldDriver . getLabelContent ( ) ;
260
+ expect ( labelContent ) . toMatch ( starReg ) ;
261
+ } ) ;
262
+ it ( 'Should show mandatory indication on the label' , async ( ) => {
263
+ const textFieldDriver = getTestCaseDriver ( {
264
+ label : 'Label' ,
265
+ validate : [ 'required' ] ,
266
+ showMandatoryIndication : true
267
+ } ) ;
268
+ const labelContent = await textFieldDriver . getLabelContent ( ) ;
269
+ expect ( labelContent ) . toMatch ( starReg ) ;
270
+ } ) ;
271
+ it ( 'Should not show mandatory indication on label' , async ( ) => {
272
+ const textFieldDriver = getTestCaseDriver ( { label : 'label' , showMandatoryIndication : true } ) ;
273
+ const labelText = await textFieldDriver . getLabelContent ( ) ;
274
+ expect ( labelText ) . not . toMatch ( starReg ) ;
275
+ } ) ;
276
+ it ( 'Should not show mandatory indication on label' , async ( ) => {
277
+ const textFieldDriver = getTestCaseDriver ( { label : 'label' , validate : 'required' } ) ;
278
+ const labelText = await textFieldDriver . getLabelContent ( ) ;
279
+ expect ( labelText ) . not . toMatch ( starReg ) ;
280
+ } ) ;
281
+ it ( 'Should have mandatory on the placeholder' , async ( ) => {
282
+ const textFieldDriver = getTestCaseDriver ( {
283
+ placeholder : 'placeholder' ,
284
+ showMandatoryIndication : true ,
285
+ validate : 'required'
286
+ } ) ;
287
+ const placeholderText = await textFieldDriver . getPlaceholderContent ( ) ;
288
+ expect ( placeholderText ) . toMatch ( starReg ) ;
289
+ } ) ;
290
+ it ( 'Should not have any mandatory - 1' , async ( ) => {
291
+ const textFieldDriver = getTestCaseDriver ( {
292
+ placeholder : 'placeholder' ,
293
+ showMandatoryIndication : true ,
294
+ // validate: 'required',
295
+ label : 'label'
296
+ } ) ;
297
+ const placeholderText = await textFieldDriver . getPlaceholderContent ( ) ;
298
+ const labelText = await textFieldDriver . getLabelContent ( ) ;
299
+ expect ( placeholderText ) . not . toMatch ( starReg ) ;
300
+ expect ( labelText ) . not . toMatch ( starReg ) ;
301
+ } ) ;
302
+ it ( 'Should not have any mandatory - 2' , async ( ) => {
303
+ const textFieldDriver = getTestCaseDriver ( {
304
+ placeholder : 'placeholder' ,
305
+ // showMandatoryIndication: true,
306
+ validate : 'required' ,
307
+ label : 'label'
308
+ } ) ;
309
+ const placeholderText = await textFieldDriver . getPlaceholderContent ( ) ;
310
+ const labelText = await textFieldDriver . getLabelContent ( ) ;
311
+ expect ( placeholderText ) . not . toMatch ( starReg ) ;
312
+ expect ( labelText ) . not . toMatch ( starReg ) ;
313
+ } ) ;
314
+ it ( 'Should have mandatory on the floating placeholder' , async ( ) => {
315
+ const textFieldDriver = getTestCaseDriver ( {
316
+ placeholder : 'placeholder' ,
317
+ floatingPlaceholder : true ,
318
+ floatOnFocus : true ,
319
+ showMandatoryIndication : true ,
320
+ validate : 'required'
321
+ } ) ;
322
+ const placeholderText = await textFieldDriver . getPlaceholderContent ( ) ;
323
+ expect ( placeholderText ) . toMatch ( starReg ) ;
324
+ } ) ;
325
+
326
+ // Special cases
327
+ it ( 'Should have mandatory on the label and not on the placeholder' , async ( ) => {
328
+ const textFieldDriver = getTestCaseDriver ( {
329
+ placeholder : 'placeholder' ,
330
+ showMandatoryIndication : true ,
331
+ validate : 'required' ,
332
+ label : 'label'
333
+ } ) ;
334
+ const labelText = await textFieldDriver . getLabelContent ( ) ;
335
+ const placeholderText = await textFieldDriver . getPlaceholderContent ( ) ;
336
+ expect ( labelText ) . toMatch ( starReg ) ;
337
+ expect ( placeholderText ) . not . toMatch ( starReg ) ;
338
+ } ) ;
339
+ } ) ;
276
340
} ) ;
0 commit comments