@@ -13,7 +13,7 @@ import type {
1313 TokensResult ,
1414} from '../src/lib/types' ;
1515import type { ShikiTransformer } from 'shiki' ;
16- import { throttleHighlighting , useStableOptions } from '../src/lib/utils' ;
16+ import { useThrottledDebounce , useStableOptions } from '../src/lib/utils' ;
1717
1818interface TestComponentProps {
1919 code : string ;
@@ -878,57 +878,50 @@ describe('useShikiHighlighter Hook', () => {
878878 } ) ;
879879
880880 describe ( 'Throttling' , ( ) => {
881- beforeEach ( ( ) => {
882- vi . useFakeTimers ( ) ;
883- } ) ;
884-
885- afterEach ( ( ) => {
886- vi . useRealTimers ( ) ;
881+ test ( 'useThrottledDebounce returns initial value' , ( ) => {
882+ const { result } = renderHook ( ( ) =>
883+ useThrottledDebounce ( 'initial' , 500 )
884+ ) ;
885+ expect ( result . current ) . toBe ( 'initial' ) ;
887886 } ) ;
888887
889- test ( 'throttles highlighting function calls based on timing' , ( ) => {
890- // Mock date to have a consistent starting point
891- const originalDateNow = Date . now ;
892- const mockTime = 1000 ;
893- Date . now = vi . fn ( ( ) => mockTime ) ;
894-
895- // Mock the perform highlight function
896- const performHighlight = vi . fn ( ) . mockResolvedValue ( undefined ) ;
888+ test ( 'useThrottledDebounce with no throttle returns value immediately' , async ( ) => {
889+ const { result, rerender } = renderHook (
890+ ( { value } ) => useThrottledDebounce ( value , undefined ) ,
891+ { initialProps : { value : 'initial' } }
892+ ) ;
897893
898- // Setup timeout control like in the hook
899- const timeoutControl = {
900- current : {
901- timeoutId : undefined ,
902- nextAllowedTime : 0 ,
903- } ,
904- } ;
894+ expect ( result . current ) . toBe ( 'initial' ) ;
905895
906- // First call should schedule immediately since nextAllowedTime is in the past
907- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
908- expect ( timeoutControl . current . timeoutId ) . toBeDefined ( ) ;
896+ rerender ( { value : 'updated' } ) ;
909897
910- // Run the timeout
911- vi . runAllTimers ( ) ;
912- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
913- expect ( timeoutControl . current . nextAllowedTime ) . toBe ( 1500 ) ; // 1000 + 500
898+ await waitFor ( ( ) => {
899+ expect ( result . current ) . toBe ( 'updated' ) ;
900+ } ) ;
901+ } ) ;
914902
915- // Reset the mock
916- performHighlight . mockClear ( ) ;
903+ test ( 'delay option throttles code updates' , async ( ) => {
904+ const code1 = 'const a = 1;' ;
905+ const code2 = 'const b = 2;' ;
917906
918- // Call again - should be delayed by the throttle duration
919- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
920- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ; // Not called yet
907+ const { getByTestId , rerender } = render (
908+ < TestComponent code = { code1 } language = "javascript" theme = "github-dark" delay = { 100 } />
909+ ) ;
921910
922- // Advance halfway through the delay - should still not be called
923- vi . advanceTimersByTime ( 250 ) ;
924- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ;
911+ // Wait for initial render
912+ await waitFor ( ( ) => {
913+ expect ( getByTestId ( 'highlighted' ) ) . toBeInTheDocument ( ) ;
914+ } ) ;
925915
926- // Advance the full delay
927- vi . advanceTimersByTime ( 250 ) ;
928- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
916+ // Change code - should be throttled
917+ rerender (
918+ < TestComponent code = { code2 } language = "javascript" theme = "github-dark" delay = { 100 } />
919+ ) ;
929920
930- // Restore original Date.now
931- Date . now = originalDateNow ;
921+ // Eventually should show new code
922+ await waitFor ( ( ) => {
923+ expect ( getByTestId ( 'highlighted' ) . textContent ) . toContain ( 'b' ) ;
924+ } ) ;
932925 } ) ;
933926 } ) ;
934927
0 commit comments