@@ -26,7 +26,14 @@ jest.mock('../services/conversations_service', () => {
26
26
jest . mock ( '../services/conversation_load_service' , ( ) => {
27
27
return {
28
28
ConversationLoadService : jest . fn ( ) . mockImplementation ( ( ) => {
29
- return { load : jest . fn ( ) . mockReturnValue ( { messages : [ ] , interactions : [ ] } ) } ;
29
+ const conversationLoadMock = {
30
+ abortController : new AbortController ( ) ,
31
+ load : jest . fn ( ) . mockImplementation ( async ( ) => {
32
+ conversationLoadMock . abortController = new AbortController ( ) ;
33
+ return { messages : [ ] , interactions : [ ] } ;
34
+ } ) ,
35
+ } ;
36
+ return conversationLoadMock ;
30
37
} ) ,
31
38
} ;
32
39
} ) ;
@@ -126,7 +133,7 @@ describe('useChatActions hook', () => {
126
133
messages : [ SEND_MESSAGE_RESPONSE . messages [ 0 ] ] ,
127
134
input : INPUT_MESSAGE ,
128
135
} ) ,
129
- query : await dataSourceServiceMock . getDataSourceQuery ( ) ,
136
+ query : dataSourceServiceMock . getDataSourceQuery ( ) ,
130
137
} ) ;
131
138
132
139
// it should send dispatch `receive` action to remove the message without messageId
@@ -201,7 +208,7 @@ describe('useChatActions hook', () => {
201
208
messages : [ ] ,
202
209
input : { type : 'input' , content : 'message that send as input' , contentType : 'text' } ,
203
210
} ) ,
204
- query : await dataSourceServiceMock . getDataSourceQuery ( ) ,
211
+ query : dataSourceServiceMock . getDataSourceQuery ( ) ,
205
212
} ) ;
206
213
} ) ;
207
214
@@ -264,7 +271,7 @@ describe('useChatActions hook', () => {
264
271
expect ( chatStateDispatchMock ) . toHaveBeenCalledWith ( { type : 'abort' } ) ;
265
272
expect ( httpMock . post ) . toHaveBeenCalledWith ( ASSISTANT_API . ABORT_AGENT_EXECUTION , {
266
273
body : JSON . stringify ( { conversationId : 'conversation_id_to_abort' } ) ,
267
- query : await dataSourceServiceMock . getDataSourceQuery ( ) ,
274
+ query : dataSourceServiceMock . getDataSourceQuery ( ) ,
268
275
} ) ;
269
276
} ) ;
270
277
@@ -292,7 +299,7 @@ describe('useChatActions hook', () => {
292
299
conversationId : 'conversation_id_mock' ,
293
300
interactionId : 'interaction_id_mock' ,
294
301
} ) ,
295
- query : await dataSourceServiceMock . getDataSourceQuery ( ) ,
302
+ query : dataSourceServiceMock . getDataSourceQuery ( ) ,
296
303
} ) ;
297
304
expect ( chatStateDispatchMock ) . toHaveBeenCalledWith (
298
305
expect . objectContaining ( { type : 'receive' , payload : { messages : [ ] , interactions : [ ] } } )
@@ -312,6 +319,7 @@ describe('useChatActions hook', () => {
312
319
it ( 'should not handle regenerate response if the regenerate operation has already aborted' , async ( ) => {
313
320
const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
314
321
signal : { aborted : true } ,
322
+ abort : jest . fn ( ) ,
315
323
} ) ) ;
316
324
317
325
httpMock . put . mockResolvedValue ( SEND_MESSAGE_RESPONSE ) ;
@@ -328,7 +336,7 @@ describe('useChatActions hook', () => {
328
336
conversationId : 'conversation_id_mock' ,
329
337
interactionId : 'interaction_id_mock' ,
330
338
} ) ,
331
- query : await dataSourceServiceMock . getDataSourceQuery ( ) ,
339
+ query : dataSourceServiceMock . getDataSourceQuery ( ) ,
332
340
} ) ;
333
341
expect ( chatStateDispatchMock ) . not . toHaveBeenCalledWith (
334
342
expect . objectContaining ( { type : 'receive' } )
@@ -353,6 +361,7 @@ describe('useChatActions hook', () => {
353
361
it ( 'should not handle regenerate error if the regenerate operation has already aborted' , async ( ) => {
354
362
const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
355
363
signal : { aborted : true } ,
364
+ abort : jest . fn ( ) ,
356
365
} ) ) ;
357
366
httpMock . put . mockImplementationOnce ( ( ) => {
358
367
throw new Error ( ) ;
@@ -369,4 +378,43 @@ describe('useChatActions hook', () => {
369
378
) ;
370
379
AbortControllerMock . mockRestore ( ) ;
371
380
} ) ;
381
+
382
+ it ( 'should clear chat title, conversation id, flyoutComponent and call reset action' , async ( ) => {
383
+ const { result } = renderHook ( ( ) => useChatActions ( ) ) ;
384
+ result . current . resetChat ( ) ;
385
+
386
+ expect ( chatContextMock . setConversationId ) . toHaveBeenLastCalledWith ( undefined ) ;
387
+ expect ( chatContextMock . setTitle ) . toHaveBeenLastCalledWith ( undefined ) ;
388
+ expect ( chatContextMock . setFlyoutComponent ) . toHaveBeenLastCalledWith ( null ) ;
389
+
390
+ expect ( chatStateDispatchMock ) . toHaveBeenLastCalledWith ( { type : 'reset' } ) ;
391
+ } ) ;
392
+
393
+ it ( 'should abort send action after reset chat' , async ( ) => {
394
+ const abortFn = jest . fn ( ) ;
395
+ const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
396
+ signal : { aborted : true } ,
397
+ abort : abortFn ,
398
+ } ) ) ;
399
+ const { result } = renderHook ( ( ) => useChatActions ( ) ) ;
400
+ await result . current . send ( INPUT_MESSAGE ) ;
401
+ result . current . resetChat ( ) ;
402
+
403
+ expect ( abortFn ) . toHaveBeenCalled ( ) ;
404
+ AbortControllerMock . mockRestore ( ) ;
405
+ } ) ;
406
+
407
+ it ( 'should abort load action after reset chat' , async ( ) => {
408
+ const abortFn = jest . fn ( ) ;
409
+ const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
410
+ signal : { aborted : true } ,
411
+ abort : abortFn ,
412
+ } ) ) ;
413
+ const { result } = renderHook ( ( ) => useChatActions ( ) ) ;
414
+ await result . current . loadChat ( 'conversation_id_mock' ) ;
415
+ result . current . resetChat ( ) ;
416
+
417
+ expect ( abortFn ) . toHaveBeenCalled ( ) ;
418
+ AbortControllerMock . mockRestore ( ) ;
419
+ } ) ;
372
420
} ) ;
0 commit comments