@@ -61,6 +61,19 @@ const SEND_MESSAGE_RESPONSE = {
61
61
] ,
62
62
} ;
63
63
64
+ const mockPureFetchResponse = ( props : { headers ?: Headers ; responseJson ?: unknown } = { } ) => {
65
+ const {
66
+ headers = new Headers ( { 'Content-Type' : 'application/json' } ) ,
67
+ responseJson = { } ,
68
+ } = props ;
69
+ return {
70
+ response : {
71
+ headers,
72
+ json : ( ) => Promise . resolve ( responseJson ) ,
73
+ } ,
74
+ } ;
75
+ } ;
76
+
64
77
describe ( 'useChatActions hook' , ( ) => {
65
78
const httpMock = httpServiceMock . createStartContract ( ) ;
66
79
const chatStateDispatchMock = jest . fn ( ) ;
@@ -112,7 +125,11 @@ describe('useChatActions hook', () => {
112
125
} ) ;
113
126
114
127
it ( 'should send message correctly' , async ( ) => {
115
- httpMock . post . mockResolvedValueOnce ( SEND_MESSAGE_RESPONSE ) ;
128
+ httpMock . post . mockImplementationOnce ( async ( ) =>
129
+ mockPureFetchResponse ( {
130
+ responseJson : SEND_MESSAGE_RESPONSE ,
131
+ } )
132
+ ) ;
116
133
jest . spyOn ( chatStateHookExports , 'useChatState' ) . mockReturnValue ( {
117
134
chatState : {
118
135
messages : [ SEND_MESSAGE_RESPONSE . messages [ 0 ] as IMessage ] ,
@@ -134,6 +151,8 @@ describe('useChatActions hook', () => {
134
151
input : INPUT_MESSAGE ,
135
152
} ) ,
136
153
query : dataSourceServiceMock . getDataSourceQuery ( ) ,
154
+ asResponse : true ,
155
+ pureFetch : true ,
137
156
} ) ;
138
157
139
158
// it should send dispatch `receive` action to remove the message without messageId
@@ -209,6 +228,8 @@ describe('useChatActions hook', () => {
209
228
input : { type : 'input' , content : 'message that send as input' , contentType : 'text' } ,
210
229
} ) ,
211
230
query : dataSourceServiceMock . getDataSourceQuery ( ) ,
231
+ pureFetch : true ,
232
+ asResponse : true ,
212
233
} ) ;
213
234
} ) ;
214
235
@@ -276,7 +297,11 @@ describe('useChatActions hook', () => {
276
297
} ) ;
277
298
278
299
it ( 'should regenerate message' , async ( ) => {
279
- httpMock . put . mockResolvedValue ( SEND_MESSAGE_RESPONSE ) ;
300
+ httpMock . put . mockImplementationOnce ( async ( ) =>
301
+ mockPureFetchResponse ( {
302
+ responseJson : SEND_MESSAGE_RESPONSE ,
303
+ } )
304
+ ) ;
280
305
jest
281
306
. spyOn ( chatContextHookExports , 'useChatContext' )
282
307
. mockReturnValue ( { ...chatContextMock , conversationId : 'conversation_id_mock' } ) ;
@@ -300,14 +325,12 @@ describe('useChatActions hook', () => {
300
325
interactionId : 'interaction_id_mock' ,
301
326
} ) ,
302
327
query : dataSourceServiceMock . getDataSourceQuery ( ) ,
328
+ pureFetch : true ,
329
+ asResponse : true ,
303
330
} ) ;
304
- expect ( chatStateDispatchMock ) . toHaveBeenCalledWith (
305
- expect . objectContaining ( { type : 'receive' , payload : { messages : [ ] , interactions : [ ] } } )
306
- ) ;
307
-
308
331
expect ( chatStateDispatchMock ) . toHaveBeenCalledWith (
309
332
expect . objectContaining ( {
310
- type : 'patch ' ,
333
+ type : 'receive ' ,
311
334
payload : {
312
335
messages : SEND_MESSAGE_RESPONSE . messages ,
313
336
interactions : SEND_MESSAGE_RESPONSE . interactions ,
@@ -317,12 +340,18 @@ describe('useChatActions hook', () => {
317
340
} ) ;
318
341
319
342
it ( 'should not handle regenerate response if the regenerate operation has already aborted' , async ( ) => {
320
- const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
321
- signal : { aborted : true } ,
322
- abort : jest . fn ( ) ,
323
- } ) ) ;
343
+ const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => {
344
+ return {
345
+ signal : AbortSignal . abort ( ) ,
346
+ abort : jest . fn ( ) ,
347
+ } ;
348
+ } ) ;
324
349
325
- httpMock . put . mockResolvedValue ( SEND_MESSAGE_RESPONSE ) ;
350
+ httpMock . put . mockImplementationOnce ( async ( ) =>
351
+ mockPureFetchResponse ( {
352
+ responseJson : SEND_MESSAGE_RESPONSE ,
353
+ } )
354
+ ) ;
326
355
jest
327
356
. spyOn ( chatContextHookExports , 'useChatContext' )
328
357
. mockReturnValue ( { ...chatContextMock , conversationId : 'conversation_id_mock' } ) ;
@@ -337,6 +366,8 @@ describe('useChatActions hook', () => {
337
366
interactionId : 'interaction_id_mock' ,
338
367
} ) ,
339
368
query : dataSourceServiceMock . getDataSourceQuery ( ) ,
369
+ pureFetch : true ,
370
+ asResponse : true ,
340
371
} ) ;
341
372
expect ( chatStateDispatchMock ) . not . toHaveBeenCalledWith (
342
373
expect . objectContaining ( { type : 'receive' } )
@@ -359,10 +390,12 @@ describe('useChatActions hook', () => {
359
390
} ) ;
360
391
361
392
it ( 'should not handle regenerate error if the regenerate operation has already aborted' , async ( ) => {
362
- const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
363
- signal : { aborted : true } ,
364
- abort : jest . fn ( ) ,
365
- } ) ) ;
393
+ const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => {
394
+ return {
395
+ signal : AbortSignal . abort ( ) ,
396
+ abort : jest . fn ( ) ,
397
+ } ;
398
+ } ) ;
366
399
httpMock . put . mockImplementationOnce ( ( ) => {
367
400
throw new Error ( ) ;
368
401
} ) ;
@@ -393,7 +426,7 @@ describe('useChatActions hook', () => {
393
426
it ( 'should abort send action after reset chat' , async ( ) => {
394
427
const abortFn = jest . fn ( ) ;
395
428
const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
396
- signal : { aborted : true } ,
429
+ signal : AbortSignal . abort ( ) ,
397
430
abort : abortFn ,
398
431
} ) ) ;
399
432
const { result } = renderHook ( ( ) => useChatActions ( ) ) ;
@@ -407,7 +440,7 @@ describe('useChatActions hook', () => {
407
440
it ( 'should abort load action after reset chat' , async ( ) => {
408
441
const abortFn = jest . fn ( ) ;
409
442
const AbortControllerMock = jest . spyOn ( window , 'AbortController' ) . mockImplementation ( ( ) => ( {
410
- signal : { aborted : true } ,
443
+ signal : AbortSignal . abort ( ) ,
411
444
abort : abortFn ,
412
445
} ) ) ;
413
446
const { result } = renderHook ( ( ) => useChatActions ( ) ) ;
0 commit comments