@@ -263,4 +263,74 @@ describe('Prompt', () => {
263263
264264 expect ( instance . state ) . to . equal ( 'cancel' ) ;
265265 } ) ;
266+
267+ test ( 'validates initial value on prompt start' , ( ) => {
268+ const instance = new Prompt ( {
269+ input,
270+ output,
271+ render : ( ) => 'foo' ,
272+ initialValue : 'invalid' ,
273+ validate : ( value ) => value === 'valid' ? undefined : 'must be valid' ,
274+ } ) ;
275+ instance . prompt ( ) ;
276+
277+ expect ( instance . state ) . to . equal ( 'error' ) ;
278+ expect ( instance . error ) . to . equal ( 'must be valid' ) ;
279+ } ) ;
280+
281+ test ( 'accepts valid initial value' , ( ) => {
282+ const instance = new Prompt ( {
283+ input,
284+ output,
285+ render : ( ) => 'foo' ,
286+ initialValue : 'valid' ,
287+ validate : ( value ) => value === 'valid' ? undefined : 'must be valid' ,
288+ } ) ;
289+ instance . prompt ( ) ;
290+
291+ expect ( instance . state ) . to . equal ( 'active' ) ;
292+ expect ( instance . error ) . to . equal ( '' ) ;
293+ } ) ;
294+
295+ test ( 'validates initial value with Error object' , ( ) => {
296+ const instance = new Prompt ( {
297+ input,
298+ output,
299+ render : ( ) => 'foo' ,
300+ initialValue : 'invalid' ,
301+ validate : ( value ) => value === 'valid' ? undefined : new Error ( 'must be valid' ) ,
302+ } ) ;
303+ instance . prompt ( ) ;
304+
305+ expect ( instance . state ) . to . equal ( 'error' ) ;
306+ expect ( instance . error ) . to . equal ( 'must be valid' ) ;
307+ } ) ;
308+
309+ test ( 'validates initial value with regex validation' , ( ) => {
310+ const instance = new Prompt ( {
311+ input,
312+ output,
313+ render : ( ) => 'foo' ,
314+ initialValue : 'Invalid Value $$$' ,
315+ validate : ( value ) => / ^ [ A - Z ] + $ / . test ( value ) ? undefined : 'Invalid value' ,
316+ } ) ;
317+ instance . prompt ( ) ;
318+
319+ expect ( instance . state ) . to . equal ( 'error' ) ;
320+ expect ( instance . error ) . to . equal ( 'Invalid value' ) ;
321+ } ) ;
322+
323+ test ( 'accepts valid initial value with regex validation' , ( ) => {
324+ const instance = new Prompt ( {
325+ input,
326+ output,
327+ render : ( ) => 'foo' ,
328+ initialValue : 'VALID' ,
329+ validate : ( value ) => / ^ [ A - Z ] + $ / . test ( value ) ? undefined : 'Invalid value' ,
330+ } ) ;
331+ instance . prompt ( ) ;
332+
333+ expect ( instance . state ) . to . equal ( 'active' ) ;
334+ expect ( instance . error ) . to . equal ( '' ) ;
335+ } ) ;
266336} ) ;
0 commit comments