@@ -2,6 +2,7 @@ import { formatStreamPart } from '@ai-sdk/ui-utils';
2
2
import {
3
3
mockFetchDataStream ,
4
4
mockFetchDataStreamWithGenerator ,
5
+ mockFetchError ,
5
6
} from '@ai-sdk/ui-utils/test' ;
6
7
import '@testing-library/jest-dom/vitest' ;
7
8
import { cleanup , findByText , render , screen } from '@testing-library/react' ;
@@ -10,13 +11,14 @@ import { useAssistant } from './use-assistant';
10
11
11
12
describe ( 'stream data stream' , ( ) => {
12
13
const TestComponent = ( ) => {
13
- const { status, messages, append } = useAssistant ( {
14
+ const { status, messages, error , append } = useAssistant ( {
14
15
api : '/api/assistant' ,
15
16
} ) ;
16
17
17
18
return (
18
19
< div >
19
20
< div data-testid = "status" > { status } </ div >
21
+ { error && < div data-testid = "error" > { error . toString ( ) } </ div > }
20
22
{ messages . map ( ( m , idx ) => (
21
23
< div data-testid = { `message-${ idx } ` } key = { idx } >
22
24
{ m . role === 'user' ? 'User: ' : 'AI: ' }
@@ -83,6 +85,17 @@ describe('stream data stream', () => {
83
85
) ;
84
86
} ) ;
85
87
88
+ it ( 'should show error response' , async ( ) => {
89
+ mockFetchError ( { statusCode : 500 , errorMessage : 'Internal Error' } ) ;
90
+
91
+ await userEvent . click ( screen . getByTestId ( 'do-append' ) ) ;
92
+
93
+ await screen . findByTestId ( 'error' ) ;
94
+ expect ( screen . getByTestId ( 'error' ) ) . toHaveTextContent (
95
+ 'Error: Internal Error' ,
96
+ ) ;
97
+ } ) ;
98
+
86
99
describe ( 'loading state' , ( ) => {
87
100
it ( 'should show loading state' , async ( ) => {
88
101
let finishGeneration : ( ( value ?: unknown ) => void ) | undefined ;
0 commit comments