1- /* eslint-disable compat/compat */
21import { scheduler } from '../scheduler'
32
43describe ( 'Scheduler' , ( ) => {
@@ -15,38 +14,52 @@ describe('Scheduler', () => {
1514 it . each ( [
1615 { items : [ 1 , 2 , 3 , 4 , 5 ] , expected : [ 2 , 4 , 6 , 8 , 10 ] } ,
1716 { items : [ 10 ] , expected : [ 20 ] } ,
18- ] ) ( 'returns results in order for $items' , async ( { items, expected } ) => {
19- const promise = scheduler . processEach ( items , ( x ) => x * 2 )
17+ ] ) ( 'returns results in order for $items' , ( { items, expected } ) => {
18+ let completedResults : number [ ] | undefined
19+ scheduler . processEach ( items , ( x ) => x * 2 , {
20+ onComplete : ( results ) => {
21+ completedResults = results
22+ } ,
23+ } )
2024 jest . runAllTimers ( )
21- expect ( await promise ) . toEqual ( expected )
25+ expect ( completedResults ) . toEqual ( expected )
2226 } )
2327
24- it ( 'provides index to callback' , async ( ) => {
25- const promise = scheduler . processEach ( [ 'a' , 'b' , 'c' ] , ( item , index ) => `${ index } :${ item } ` )
28+ it ( 'provides index to callback' , ( ) => {
29+ let completedResults : string [ ] | undefined
30+ scheduler . processEach ( [ 'a' , 'b' , 'c' ] , ( item , index ) => `${ index } :${ item } ` , {
31+ onComplete : ( results ) => {
32+ completedResults = results
33+ } ,
34+ } )
2635 jest . runAllTimers ( )
27- expect ( await promise ) . toEqual ( [ '0:a' , '1:b' , '2:c' ] )
36+ expect ( completedResults ) . toEqual ( [ '0:a' , '1:b' , '2:c' ] )
2837 } )
2938
30- it ( 'handles empty array' , async ( ) => {
31- const results = await scheduler . processEach ( [ ] , ( x ) => x )
32- expect ( results ) . toEqual ( [ ] )
39+ it ( 'handles empty array' , ( ) => {
40+ let completedResults : unknown [ ] | undefined
41+ scheduler . processEach ( [ ] , ( x ) => x , {
42+ onComplete : ( results ) => {
43+ completedResults = results
44+ } ,
45+ } )
46+ expect ( completedResults ) . toEqual ( [ ] )
3347 } )
3448
35- it ( 'continues processing after task error' , async ( ) => {
49+ it ( 'continues processing after task error' , ( ) => {
3650 const results : number [ ] = [ ]
37- const promise = scheduler . processEach ( [ 1 , 2 , 3 ] , ( x ) => {
51+ scheduler . processEach ( [ 1 , 2 , 3 ] , ( x ) => {
3852 if ( x === 2 ) throw new Error ( 'fail' )
3953 results . push ( x )
4054 return x
4155 } )
4256 jest . runAllTimers ( )
43- await promise
4457 expect ( results ) . toEqual ( [ 1 , 3 ] )
4558 } )
4659 } )
4760
4861 describe ( 'priority' , ( ) => {
49- it ( 'processes high priority before normal priority' , async ( ) => {
62+ it ( 'processes high priority before normal priority' , ( ) => {
5063 const order : string [ ] = [ ]
5164
5265 scheduler . processEach ( [ 'n1' , 'n2' ] , ( x ) => {
@@ -64,15 +77,13 @@ describe('Scheduler', () => {
6477 )
6578
6679 jest . runAllTimers ( )
67- // eslint-disable-next-line compat/compat
68- await Promise . resolve ( )
6980
7081 expect ( order ) . toEqual ( [ 'h1' , 'h2' , 'n1' , 'n2' ] )
7182 } )
7283 } )
7384
7485 describe ( 'yielding' , ( ) => {
75- it ( 'yields to browser after time budget exceeded' , async ( ) => {
86+ it ( 'yields to browser after time budget exceeded' , ( ) => {
7687 let mockTime = 0
7788 jest . spyOn ( performance , 'now' ) . mockImplementation ( ( ) => mockTime )
7889 scheduler . _reset ( 30 )
@@ -86,16 +97,10 @@ describe('Scheduler', () => {
8697 return x
8798 } )
8899
89- // First tick - should process ~30 items then yield
90100 jest . advanceTimersByTime ( 0 )
91- // eslint-disable-next-line compat/compat
92- await Promise . resolve ( )
93101 expect ( results . length ) . toBeLessThan ( 100 )
94102
95- // Complete all
96103 jest . runAllTimers ( )
97- // eslint-disable-next-line compat/compat
98- await Promise . resolve ( )
99104 expect ( results ) . toHaveLength ( 100 )
100105 } )
101106 } )
0 commit comments