@@ -6,70 +6,75 @@ import { useSimulateProgress } from '../src';
6
6
jest . useFakeTimers ( ) ;
7
7
8
8
describe ( 'useSimulateProgress' , ( ) => {
9
- it ( 'should update progress periodically and reach 100% ' , ( ) => {
9
+ test ( 'should not start progress automatically ' , ( ) => {
10
10
const mockCallback = jest . fn ( ) ;
11
11
const { result } = renderHook ( ( ) =>
12
12
useSimulateProgress ( 1000 , mockCallback ) ,
13
13
) ;
14
14
15
- // Initial progress should be 0
16
- expect ( result . current ) . toBe ( 0 ) ;
15
+ // Progress should initially be 0
16
+ expect ( result . current [ 0 ] ) . toBe ( 0 ) ;
17
+
18
+ // Progress should not change before startProgress is called
19
+ act ( ( ) => {
20
+ jest . advanceTimersByTime ( 500 ) ; // Fast-forward 500ms
21
+ } ) ;
22
+ expect ( result . current [ 0 ] ) . toBe ( 0 ) ;
23
+ } ) ;
24
+
25
+ test ( 'should update progress when startProgress is called' , ( ) => {
26
+ const mockCallback = jest . fn ( ) ;
27
+ const { result } = renderHook ( ( ) =>
28
+ useSimulateProgress ( 1000 , mockCallback ) ,
29
+ ) ;
30
+
31
+ // Call startProgress to trigger the progress
32
+ act ( ( ) => {
33
+ result . current [ 1 ] ( ) ; // Call startProgress
34
+ } ) ;
17
35
18
36
// Fast-forward the timer, simulating time passing
19
37
act ( ( ) => {
20
- jest . advanceTimersByTime ( 250 ) ;
38
+ jest . advanceTimersByTime ( 250 ) ; // Fast-forward 250ms
21
39
} ) ;
22
40
23
41
// Progress should update to 25%
24
- expect ( result . current ) . toBe ( 25 ) ;
42
+ expect ( result . current [ 0 ] ) . toBe ( 25 ) ;
25
43
26
44
act ( ( ) => {
27
45
jest . advanceTimersByTime ( 500 ) ; // Fast-forward another 500ms
28
46
} ) ;
29
47
30
48
// Progress should update to 75%
31
- expect ( result . current ) . toBe ( 75 ) ;
49
+ expect ( result . current [ 0 ] ) . toBe ( 75 ) ;
32
50
33
51
act ( ( ) => {
34
52
jest . advanceTimersByTime ( 250 ) ; // Finish the remaining time
35
53
} ) ;
36
54
37
55
// Progress should be 100%, and callback should be called
38
- expect ( result . current ) . toBe ( 100 ) ;
56
+ expect ( result . current [ 0 ] ) . toBe ( 100 ) ;
39
57
expect ( mockCallback ) . toHaveBeenCalledTimes ( 1 ) ;
40
58
} ) ;
41
59
42
- it ( 'should not call callback if progress is less than 100% ' , ( ) => {
60
+ test ( 'should clear interval when unmounted ' , ( ) => {
43
61
const mockCallback = jest . fn ( ) ;
44
- const { result } = renderHook ( ( ) =>
62
+ const { result, unmount } = renderHook ( ( ) =>
45
63
useSimulateProgress ( 1000 , mockCallback ) ,
46
64
) ;
47
65
48
- // Fast-forward the timer to 50%
66
+ // Call startProgress to trigger the progress
49
67
act ( ( ) => {
50
- jest . advanceTimersByTime ( 500 ) ; // Fast-forward 500ms
68
+ result . current [ 1 ] ( ) ; // Call startProgress
51
69
} ) ;
52
70
53
- // Progress should be 50%
54
- expect ( result . current ) . toBe ( 50 ) ;
55
-
56
- // Callback should not be called
57
- expect ( mockCallback ) . not . toHaveBeenCalled ( ) ;
58
- } ) ;
59
-
60
- test ( 'should clear interval when unmounted' , ( ) => {
61
- const mockCallback = jest . fn ( ) ;
62
- const { result, unmount } = renderHook ( ( ) =>
63
- useSimulateProgress ( 1000 , mockCallback ) ,
64
- ) ;
65
-
66
71
// Fast-forward the timer halfway
67
72
act ( ( ) => {
68
73
jest . advanceTimersByTime ( 500 ) ;
69
74
} ) ;
70
75
71
76
// Progress should be 50%
72
- expect ( result . current ) . toBe ( 50 ) ;
77
+ expect ( result . current [ 0 ] ) . toBe ( 50 ) ;
73
78
74
79
// Unmount the component
75
80
unmount ( ) ;
@@ -80,7 +85,7 @@ describe('useSimulateProgress', () => {
80
85
} ) ;
81
86
82
87
// Progress should not update further
83
- expect ( result . current ) . toBe ( 50 ) ;
88
+ expect ( result . current [ 0 ] ) . toBe ( 50 ) ;
84
89
85
90
// Callback should not be called
86
91
expect ( mockCallback ) . not . toHaveBeenCalled ( ) ;
0 commit comments