@@ -3,17 +3,17 @@ import { renderHook, act } from '@testing-library/react';
33import { useVisualization } from './useVisualization' ;
44import { ELEMENT_STATES , VISUALIZATION_MODES } from '../constants' ;
55
6- // Mock the delay function
7- vi . mock ( '../utils/arrayHelpers' , ( ) => ( {
8- delay : vi . fn ( ( ) => Promise . resolve ( ) ) ,
9- } ) ) ;
10-
116describe ( 'useVisualization Hook' , ( ) => {
127 beforeEach ( ( ) => {
138 vi . useFakeTimers ( ) ;
149 } ) ;
1510
1611 afterEach ( ( ) => {
12+ //! Critical: run all pending timers before cleanup
13+ act ( ( ) => {
14+ vi . runOnlyPendingTimers ( ) ;
15+ } ) ;
16+ vi . clearAllTimers ( ) ;
1717 vi . useRealTimers ( ) ;
1818 vi . clearAllMocks ( ) ;
1919 } ) ;
@@ -111,7 +111,6 @@ describe('useVisualization Hook', () => {
111111 result . current . loadSteps ( mockSteps ) ;
112112 } ) ;
113113
114- // Play should act like stepForward in manual mode
115114 act ( ( ) => {
116115 result . current . play ( ) ;
117116 } ) ;
@@ -131,16 +130,14 @@ describe('useVisualization Hook', () => {
131130 result . current . loadSteps ( mockSteps ) ;
132131 } ) ;
133132
134- // Go to last step
135133 act ( ( ) => {
136- result . current . play ( ) ; // Step 1
137- result . current . play ( ) ; // Step 2 (final)
134+ result . current . play ( ) ;
135+ result . current . play ( ) ;
138136 } ) ;
139137
140138 expect ( result . current . currentStep ) . toBe ( 2 ) ;
141139 expect ( result . current . isComplete ) . toBe ( true ) ;
142140
143- // Try to play again - should not advance
144141 act ( ( ) => {
145142 result . current . play ( ) ;
146143 } ) ;
@@ -188,8 +185,8 @@ describe('useVisualization Hook', () => {
188185 } ) ;
189186
190187 describe ( 'Autoplay Mode' , ( ) => {
191- it ( 'should start autoplay correctly' , async ( ) => {
192- const { result } = renderHook ( ( ) =>
188+ it ( 'should start autoplay correctly' , ( ) => {
189+ const { result, unmount } = renderHook ( ( ) =>
193190 useVisualization ( [ 3 , 1 , 2 ] , 100 , VISUALIZATION_MODES . AUTOPLAY )
194191 ) ;
195192
@@ -204,7 +201,6 @@ describe('useVisualization Hook', () => {
204201 expect ( result . current . isPlaying ) . toBe ( true ) ;
205202 expect ( result . current . isAutoplayActive ) . toBe ( true ) ;
206203
207- // Fast-forward timers to simulate autoplay progression
208204 act ( ( ) => {
209205 vi . advanceTimersByTime ( 100 ) ;
210206 } ) ;
@@ -218,11 +214,16 @@ describe('useVisualization Hook', () => {
218214 expect ( result . current . currentStep ) . toBe ( 2 ) ;
219215 expect ( result . current . isComplete ) . toBe ( true ) ;
220216 expect ( result . current . isPlaying ) . toBe ( false ) ;
221- expect ( result . current . isAutoplayActive ) . toBe ( false ) ;
217+
218+ // Cleanup
219+ act ( ( ) => {
220+ result . current . pause ( ) ;
221+ } ) ;
222+ unmount ( ) ;
222223 } ) ;
223224
224225 it ( 'should pause autoplay correctly' , ( ) => {
225- const { result } = renderHook ( ( ) =>
226+ const { result, unmount } = renderHook ( ( ) =>
226227 useVisualization ( [ 3 , 1 , 2 ] , 100 , VISUALIZATION_MODES . AUTOPLAY )
227228 ) ;
228229
@@ -239,10 +240,12 @@ describe('useVisualization Hook', () => {
239240
240241 expect ( result . current . isPlaying ) . toBe ( false ) ;
241242 expect ( result . current . isAutoplayActive ) . toBe ( false ) ;
243+
244+ unmount ( ) ;
242245 } ) ;
243246
244247 it ( 'should pause and resume autoplay' , ( ) => {
245- const { result } = renderHook ( ( ) =>
248+ const { result, unmount } = renderHook ( ( ) =>
246249 useVisualization ( [ 3 , 1 , 2 ] , 100 , VISUALIZATION_MODES . AUTOPLAY )
247250 ) ;
248251
@@ -259,12 +262,17 @@ describe('useVisualization Hook', () => {
259262
260263 expect ( result . current . isPlaying ) . toBe ( false ) ;
261264
262- // Resume
263265 act ( ( ) => {
264266 result . current . play ( ) ;
265267 } ) ;
266268
267269 expect ( result . current . isPlaying ) . toBe ( true ) ;
270+
271+ // Cleanup
272+ act ( ( ) => {
273+ result . current . pause ( ) ;
274+ } ) ;
275+ unmount ( ) ;
268276 } ) ;
269277 } ) ;
270278
@@ -314,7 +322,7 @@ describe('useVisualization Hook', () => {
314322 } ,
315323 ] ;
316324
317- const { result } = renderHook ( ( ) =>
325+ const { result, unmount } = renderHook ( ( ) =>
318326 useVisualization ( [ 42 ] , 500 , VISUALIZATION_MODES . AUTOPLAY )
319327 ) ;
320328
@@ -325,12 +333,13 @@ describe('useVisualization Hook', () => {
325333 expect ( result . current . array ) . toEqual ( [ 42 ] ) ;
326334 expect ( result . current . totalSteps ) . toBe ( 1 ) ;
327335
328- // In autoplay mode with single step, should complete immediately
329336 act ( ( ) => {
330337 result . current . play ( ) ;
331338 } ) ;
332339
333340 expect ( result . current . isComplete ) . toBe ( true ) ;
341+
342+ unmount ( ) ;
334343 } ) ;
335344
336345 it ( 'should handle play when already complete' , ( ) => {
@@ -340,14 +349,12 @@ describe('useVisualization Hook', () => {
340349
341350 act ( ( ) => {
342351 result . current . loadSteps ( mockSteps ) ;
343- // Go to completion
344352 result . current . stepForward ( ) ;
345353 result . current . stepForward ( ) ;
346354 } ) ;
347355
348356 expect ( result . current . isComplete ) . toBe ( true ) ;
349357
350- // Try to play when complete - should not start
351358 act ( ( ) => {
352359 result . current . play ( ) ;
353360 } ) ;
@@ -366,7 +373,6 @@ describe('useVisualization Hook', () => {
366373
367374 expect ( result . current . currentStep ) . toBe ( 0 ) ;
368375
369- // Try to step backward from beginning - should not change
370376 act ( ( ) => {
371377 result . current . stepBackward ( ) ;
372378 } ) ;
0 commit comments