@@ -491,10 +491,110 @@ describe('form actions', () => {
491
491
await userEvent . click ( input ) ;
492
492
await userEvent . keyboard ( '{Enter}' ) ;
493
493
494
- // Wait for the second AI response to complete
494
+ expect ( screen . queryByTestId ( 'message-2' ) ) . not . toBeInTheDocument ( ) ;
495
+ } ) ;
496
+ } ) ;
497
+
498
+ describe ( 'form actions (with options)' , ( ) => {
499
+ const TestComponent = ( ) => {
500
+ const { messages, handleSubmit, handleInputChange, isLoading, input } =
501
+ useChat ( ) ;
502
+
503
+ return (
504
+ < div >
505
+ < For each = { messages ( ) } >
506
+ { ( m , idx ) => (
507
+ < div data-testid = { `message-${ idx ( ) } ` } >
508
+ { m . role === 'user' ? 'User: ' : 'AI: ' }
509
+ { m . content }
510
+ </ div >
511
+ ) }
512
+ </ For >
513
+
514
+ < form
515
+ onSubmit = { event => {
516
+ handleSubmit ( event , {
517
+ allowEmptySubmit : true ,
518
+ } ) ;
519
+ } }
520
+ >
521
+ < input
522
+ value = { input ( ) }
523
+ placeholder = "Send message..."
524
+ onInput = { handleInputChange }
525
+ disabled = { isLoading ( ) }
526
+ data-testid = "do-input"
527
+ />
528
+ </ form >
529
+ </ div >
530
+ ) ;
531
+ } ;
532
+
533
+ beforeEach ( ( ) => {
534
+ render ( ( ) => < TestComponent /> ) ;
535
+ } ) ;
536
+
537
+ afterEach ( ( ) => {
538
+ vi . restoreAllMocks ( ) ;
539
+ cleanup ( ) ;
540
+ } ) ;
541
+
542
+ it ( 'allowEmptySubmit' , async ( ) => {
543
+ mockFetchDataStream ( {
544
+ url : 'https://example.com/api/chat' ,
545
+ chunks : [ 'Hello' , ',' , ' world' , '.' ] . map ( token =>
546
+ formatStreamPart ( 'text' , token ) ,
547
+ ) ,
548
+ } ) ;
549
+
550
+ const input = screen . getByTestId ( 'do-input' ) ;
551
+ await userEvent . type ( input , 'hi' ) ;
552
+ await userEvent . keyboard ( '{Enter}' ) ;
553
+ expect ( input ) . toHaveValue ( '' ) ;
554
+
555
+ // Wait for the user message to appear
556
+ await screen . findByTestId ( 'message-0' ) ;
557
+ expect ( screen . getByTestId ( 'message-0' ) ) . toHaveTextContent ( 'User: hi' ) ;
558
+
559
+ // Wait for the AI response to complete
560
+ await screen . findByTestId ( 'message-1' ) ;
561
+ expect ( screen . getByTestId ( 'message-1' ) ) . toHaveTextContent (
562
+ 'AI: Hello, world.' ,
563
+ ) ;
564
+
565
+ mockFetchDataStream ( {
566
+ url : 'https://example.com/api/chat' ,
567
+ chunks : [ 'How' , ' can' , ' I' , ' help' , ' you' , '?' ] . map ( token =>
568
+ formatStreamPart ( 'text' , token ) ,
569
+ ) ,
570
+ } ) ;
571
+
572
+ await userEvent . click ( input ) ;
573
+ await userEvent . keyboard ( '{Enter}' ) ;
574
+
495
575
await screen . findByTestId ( 'message-2' ) ;
496
576
expect ( screen . getByTestId ( 'message-2' ) ) . toHaveTextContent (
497
577
'AI: How can I help you?' ,
498
578
) ;
579
+
580
+ mockFetchDataStream ( {
581
+ url : 'https://example.com/api/chat' ,
582
+ chunks : [ 'The' , ' sky' , ' is' , ' blue.' ] . map ( token =>
583
+ formatStreamPart ( 'text' , token ) ,
584
+ ) ,
585
+ } ) ;
586
+
587
+ await userEvent . type ( input , 'what color is the sky?' ) ;
588
+ await userEvent . keyboard ( '{Enter}' ) ;
589
+
590
+ await screen . findByTestId ( 'message-3' ) ;
591
+ expect ( screen . getByTestId ( 'message-3' ) ) . toHaveTextContent (
592
+ 'User: what color is the sky?' ,
593
+ ) ;
594
+
595
+ await screen . findByTestId ( 'message-4' ) ;
596
+ expect ( screen . getByTestId ( 'message-4' ) ) . toHaveTextContent (
597
+ 'AI: The sky is blue.' ,
598
+ ) ;
499
599
} ) ;
500
600
} ) ;
0 commit comments