@@ -14,7 +14,7 @@ import type {
1414 TokensResult ,
1515} from '../src/lib/types' ;
1616import type { ShikiTransformer } from 'shiki' ;
17- import { throttleHighlighting , useStableOptions } from '../src/lib/utils' ;
17+ import { useThrottledDebounce , useStableOptions } from '../src/lib/utils' ;
1818
1919interface TestComponentProps {
2020 code : string ;
@@ -897,57 +897,50 @@ describe('useShikiHighlighter Hook', () => {
897897 } ) ;
898898
899899 describe ( 'Throttling' , ( ) => {
900- beforeEach ( ( ) => {
901- vi . useFakeTimers ( ) ;
902- } ) ;
903-
904- afterEach ( ( ) => {
905- vi . useRealTimers ( ) ;
900+ test ( 'useThrottledDebounce returns initial value' , ( ) => {
901+ const { result } = renderHook ( ( ) =>
902+ useThrottledDebounce ( 'initial' , 500 )
903+ ) ;
904+ expect ( result . current ) . toBe ( 'initial' ) ;
906905 } ) ;
907906
908- test ( 'throttles highlighting function calls based on timing' , ( ) => {
909- // Mock date to have a consistent starting point
910- const originalDateNow = Date . now ;
911- const mockTime = 1000 ;
912- Date . now = vi . fn ( ( ) => mockTime ) ;
913-
914- // Mock the perform highlight function
915- const performHighlight = vi . fn ( ) . mockResolvedValue ( undefined ) ;
907+ test ( 'useThrottledDebounce with no throttle returns value immediately' , async ( ) => {
908+ const { result, rerender } = renderHook (
909+ ( { value } ) => useThrottledDebounce ( value , undefined ) ,
910+ { initialProps : { value : 'initial' } }
911+ ) ;
916912
917- // Setup timeout control like in the hook
918- const timeoutControl = {
919- current : {
920- timeoutId : undefined ,
921- nextAllowedTime : 0 ,
922- } ,
923- } ;
913+ expect ( result . current ) . toBe ( 'initial' ) ;
924914
925- // First call should schedule immediately since nextAllowedTime is in the past
926- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
927- expect ( timeoutControl . current . timeoutId ) . toBeDefined ( ) ;
915+ rerender ( { value : 'updated' } ) ;
928916
929- // Run the timeout
930- vi . runAllTimers ( ) ;
931- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
932- expect ( timeoutControl . current . nextAllowedTime ) . toBe ( 1500 ) ; // 1000 + 500
917+ await waitFor ( ( ) => {
918+ expect ( result . current ) . toBe ( 'updated' ) ;
919+ } ) ;
920+ } ) ;
933921
934- // Reset the mock
935- performHighlight . mockClear ( ) ;
922+ test ( 'delay option throttles code updates' , async ( ) => {
923+ const code1 = 'const a = 1;' ;
924+ const code2 = 'const b = 2;' ;
936925
937- // Call again - should be delayed by the throttle duration
938- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
939- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ; // Not called yet
926+ const { getByTestId , rerender } = render (
927+ < TestComponent code = { code1 } language = "javascript" theme = "github-dark" delay = { 100 } />
928+ ) ;
940929
941- // Advance halfway through the delay - should still not be called
942- vi . advanceTimersByTime ( 250 ) ;
943- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ;
930+ // Wait for initial render
931+ await waitFor ( ( ) => {
932+ expect ( getByTestId ( 'highlighted' ) ) . toBeInTheDocument ( ) ;
933+ } ) ;
944934
945- // Advance the full delay
946- vi . advanceTimersByTime ( 250 ) ;
947- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
935+ // Change code - should be throttled
936+ rerender (
937+ < TestComponent code = { code2 } language = "javascript" theme = "github-dark" delay = { 100 } />
938+ ) ;
948939
949- // Restore original Date.now
950- Date . now = originalDateNow ;
940+ // Eventually should show new code
941+ await waitFor ( ( ) => {
942+ expect ( getByTestId ( 'highlighted' ) . textContent ) . toContain ( 'b' ) ;
943+ } ) ;
951944 } ) ;
952945 } ) ;
953946
0 commit comments