1
1
import { toBatchAbortSignal } from './signal'
2
2
3
3
describe ( 'toBatchAbortSignal' , ( ) => {
4
+ it ( 'should return undefined if contains undefined or empty array' , ( ) => {
5
+ expect ( toBatchAbortSignal ( [ ] ) ) . toBe ( undefined )
6
+ expect ( toBatchAbortSignal ( [ undefined ] ) ) . toBe ( undefined )
7
+ expect ( toBatchAbortSignal ( [ AbortSignal . timeout ( 100 ) , undefined ] ) ) . toBe ( undefined )
8
+ expect ( toBatchAbortSignal ( [ AbortSignal . timeout ( 100 ) , undefined , AbortSignal . timeout ( 100 ) ] ) ) . toBe ( undefined )
9
+ } )
10
+
4
11
it ( 'should return a non-aborted signal initially if not all inputs are aborted' , ( ) => {
5
12
const controller1 = new AbortController ( )
6
13
const controller2 = new AbortController ( )
7
14
8
- // Case 1: Empty array
9
- expect ( toBatchAbortSignal ( [ ] ) . aborted ) . toBe ( false )
10
-
11
- // Case 2: Only undefined
12
- expect ( toBatchAbortSignal ( [ undefined ] ) . aborted ) . toBe ( false )
13
-
14
- // Case 3: No signals aborted
15
- expect ( toBatchAbortSignal ( [ controller1 . signal , controller2 . signal ] ) . aborted ) . toBe ( false )
15
+ expect ( toBatchAbortSignal ( [ controller1 . signal , controller2 . signal ] ) ?. aborted ) . toBe ( false )
16
16
17
- // Case 4: Some signals aborted (but not all)
18
17
controller1 . abort ( )
19
- expect ( toBatchAbortSignal ( [ controller1 . signal , controller2 . signal ] ) . aborted ) . toBe ( false )
18
+ expect ( toBatchAbortSignal ( [ controller1 . signal , controller2 . signal ] ) ? .aborted ) . toBe ( false )
20
19
} )
21
20
22
21
it ( 'should return an aborted signal initially if all valid inputs are already aborted' , ( ) => {
@@ -25,8 +24,8 @@ describe('toBatchAbortSignal', () => {
25
24
controller1 . abort ( )
26
25
controller2 . abort ( )
27
26
28
- const batchSignal = toBatchAbortSignal ( [ controller1 . signal , undefined , controller2 . signal ] )
29
- expect ( batchSignal . aborted ) . toBe ( true )
27
+ const batchSignal = toBatchAbortSignal ( [ controller1 . signal , controller2 . signal ] )
28
+ expect ( batchSignal ? .aborted ) . toBe ( true )
30
29
} )
31
30
32
31
it ( 'should fire abort event when all signals abort' , ( ) => {
@@ -38,22 +37,21 @@ describe('toBatchAbortSignal', () => {
38
37
39
38
const batchSignal = toBatchAbortSignal ( [
40
39
controllerPreAborted . signal ,
41
- undefined ,
42
40
controllerLater1 . signal ,
43
41
controllerLater2 . signal ,
44
42
] )
45
43
46
- expect ( batchSignal . aborted ) . toBe ( false )
44
+ expect ( batchSignal ? .aborted ) . toBe ( false )
47
45
48
46
const abortSpy = vi . fn ( )
49
- batchSignal . addEventListener ( 'abort' , abortSpy )
47
+ batchSignal ? .addEventListener ( 'abort' , abortSpy )
50
48
51
49
controllerLater1 . abort ( )
52
- expect ( batchSignal . aborted ) . toBe ( false )
50
+ expect ( batchSignal ? .aborted ) . toBe ( false )
53
51
expect ( abortSpy ) . not . toHaveBeenCalled ( )
54
52
55
53
controllerLater2 . abort ( )
56
- expect ( batchSignal . aborted ) . toBe ( true )
54
+ expect ( batchSignal ? .aborted ) . toBe ( true )
57
55
expect ( abortSpy ) . toHaveBeenCalledTimes ( 1 )
58
56
} )
59
57
} )
0 commit comments