1
1
import { undefinedValue } from '@/helper/variables' ;
2
2
import '@testing-library/jest-dom' ;
3
3
import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
4
- import { Alova , createAlova } from 'alova' ;
4
+ import { createAlova } from 'alova' ;
5
5
import GlobalFetch from 'alova/GlobalFetch' ;
6
6
import ReactHook from 'alova/react' ;
7
7
import ES from 'eventsource' ;
8
8
import { AddressInfo } from 'net' ;
9
9
import React , { ReactElement } from 'react' ;
10
10
import { IntervalEventName , IntervalMessage , TriggerEventName , server , send as serverSend } from '~/test/sseServer' ;
11
11
import { getAlovaInstance , untilCbCalled } from '~/test/utils' ;
12
- import { FetchRequestInit } from '~/typings/general' ;
13
- import { ReactState , useSSE } from '..' ;
12
+ import { useSSE } from '..' ;
14
13
import { AlovaSSEMessageEvent , SSEHookReadyState } from '../typings/general' ;
15
14
Object . defineProperty ( global , 'EventSource' , { value : ES , writable : false } ) ;
16
15
17
- let alovaInst : Alova < ReactState < any > , unknown , FetchRequestInit , any , any > ;
18
-
19
16
afterEach ( ( ) => {
20
17
server . close ( ) ;
21
18
} ) ;
@@ -28,29 +25,28 @@ type AnyMessageType<T = any> = AlovaSSEMessageEvent<T, any, any, any, any, any,
28
25
const prepareAlova = async ( ) => {
29
26
await server . listen ( ) ;
30
27
const { port } = server . address ( ) as AddressInfo ;
31
- alovaInst = createAlova ( {
28
+ return createAlova ( {
32
29
baseURL : `http://127.0.0.1:${ port } ` ,
33
30
statesHook : ReactHook ,
34
31
requestAdapter : GlobalFetch ( ) ,
35
32
cacheLogger : false
36
- } ) as any ;
33
+ } ) ;
37
34
} ;
38
35
39
36
describe ( 'react => useSSE' , ( ) => {
40
37
// ! 无初始数据,不立即发送请求
41
38
test ( 'should default not request immediately' , async ( ) => {
42
- await prepareAlova ( ) ;
39
+ const alovaInst = await prepareAlova ( ) ;
43
40
const poster = ( data : any ) => alovaInst . Get ( `/${ IntervalEventName } ` , data ) ;
44
41
45
42
let recv = undefinedValue ;
46
43
const mockOpenFn = jest . fn ( ) ;
47
44
const mockOnFn = jest . fn ( ( event : AnyMessageType ) => {
48
45
recv = event . data ;
49
46
} ) ;
50
- // const mockOpenFn = jest.fn();
51
47
52
48
const Page = ( ) => {
53
- const { on, onOpen, data, readyState, send } = useSSE ( poster ) ;
49
+ const { on, onOpen, data, readyState, send, close } = useSSE ( poster ) ;
54
50
on ( IntervalEventName , mockOnFn ) ;
55
51
onOpen ( mockOpenFn ) ;
56
52
@@ -69,6 +65,11 @@ describe('react => useSSE', () => {
69
65
onClick = { send } >
70
66
send request
71
67
</ button >
68
+ < button
69
+ role = "close-btn"
70
+ onClick = { close } >
71
+ close
72
+ </ button >
72
73
</ div >
73
74
) ;
74
75
} ;
@@ -94,11 +95,15 @@ describe('react => useSSE', () => {
94
95
} ,
95
96
{ timeout : 4000 }
96
97
) ;
98
+
99
+ fireEvent . click ( screen . getByRole ( 'close-btn' ) ) ;
100
+ await untilCbCalled ( setTimeout , 200 ) ;
101
+ expect ( screen . getByRole ( 'status' ) ) . toHaveTextContent ( 'closed' ) ;
97
102
} ) ;
98
103
99
104
// ! 有初始数据,不立即发送请求
100
105
test ( 'should get the initial data and NOT send request immediately' , async ( ) => {
101
- await prepareAlova ( ) ;
106
+ const alovaInst = await prepareAlova ( ) ;
102
107
const poster = ( data : any ) => alovaInst . Get ( `/${ TriggerEventName } ` , data ) ;
103
108
const initialData = 'initial-data' ;
104
109
const testDataA = 'test-data-1' ;
@@ -178,7 +183,7 @@ describe('react => useSSE', () => {
178
183
179
184
// ! 有初始数据,立即发送请求
180
185
test ( 'should get the initial data and send request immediately' , async ( ) => {
181
- await prepareAlova ( ) ;
186
+ const alovaInst = await prepareAlova ( ) ;
182
187
const poster = ( data : any ) => alovaInst . Get ( `/${ TriggerEventName } ` , data ) ;
183
188
const initialData = 'initial-data' ;
184
189
const testDataA = 'test-data-1' ;
@@ -230,7 +235,7 @@ describe('react => useSSE', () => {
230
235
231
236
// ! 测试关闭后重新连接
232
237
test ( 'should not trigger handler after close' , async ( ) => {
233
- await prepareAlova ( ) ;
238
+ const alovaInst = await prepareAlova ( ) ;
234
239
const poster = ( data : any ) => alovaInst . Get ( `/${ TriggerEventName } ` , data ) ;
235
240
const testDataA = 'test-data-1' ;
236
241
const testDataB = 'test-data-2' ;
@@ -327,7 +332,7 @@ describe('react => useSSE', () => {
327
332
328
333
// ! 打开失败应该报错,立即发送请求
329
334
test ( 'should throw error then try to connect a not exist url' , async ( ) => {
330
- await prepareAlova ( ) ;
335
+ const alovaInst = await prepareAlova ( ) ;
331
336
const poster = ( data : any ) => alovaInst . Get ( '/not-exist-path' , data ) ;
332
337
333
338
let recv = undefinedValue ;
@@ -359,7 +364,7 @@ describe('react => useSSE', () => {
359
364
360
365
render ( ( < Page /> ) as ReactElement < any , any > ) ;
361
366
362
- await untilCbCalled ( setTimeout , 500 ) ;
367
+ await untilCbCalled ( setTimeout , 1500 ) ;
363
368
await screen . findByText ( / c l o s e d / ) ;
364
369
365
370
expect ( screen . getByRole ( 'data' ) ) . toBeEmptyDOMElement ( ) ;
@@ -371,7 +376,7 @@ describe('react => useSSE', () => {
371
376
372
377
// ! 打开失败应该报错,不立即发送请求
373
378
test ( 'should throw error then try to connect a not exist url (immediate: false)' , async ( ) => {
374
- await prepareAlova ( ) ;
379
+ const alovaInst = await prepareAlova ( ) ;
375
380
const poster = ( data : any ) => alovaInst . Get ( '/not-exist-path' , data ) ;
376
381
377
382
let recv = undefinedValue ;
@@ -399,7 +404,7 @@ describe('react => useSSE', () => {
399
404
< span role = "data" > { data } </ span >
400
405
< button
401
406
role = "send"
402
- onClick = { send } >
407
+ onClick = { ( ) => send ( ) } >
403
408
send request
404
409
</ button >
405
410
</ div >
@@ -441,7 +446,7 @@ describe('react => useSSE', () => {
441
446
const mockResponseErrorFn = jest . fn ( ) ;
442
447
const mockResponseCompleteFn = jest . fn ( ) ;
443
448
444
- alovaInst = getAlovaInstance ( ReactHook , {
449
+ const alovaInst = getAlovaInstance ( ReactHook , {
445
450
baseURL : `http://localhost:${ port } ` ,
446
451
responseExpect : data => {
447
452
mockResponseFn ( ) ;
@@ -600,7 +605,7 @@ describe('react => useSSE', () => {
600
605
const mockResponseErrorFn = jest . fn ( ) ;
601
606
const mockResponseCompleteFn = jest . fn ( ) ;
602
607
603
- alovaInst = getAlovaInstance ( ReactHook , {
608
+ const alovaInst = getAlovaInstance ( ReactHook , {
604
609
baseURL : `http://localhost:${ port } ` ,
605
610
responseExpect : ( ) => {
606
611
mockResponseFn ( ) ;
0 commit comments