@@ -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 ;
@@ -881,57 +881,50 @@ describe('useShikiHighlighter Hook', () => {
881881 } ) ;
882882
883883 describe ( 'Throttling' , ( ) => {
884- beforeEach ( ( ) => {
885- vi . useFakeTimers ( ) ;
886- } ) ;
887-
888- afterEach ( ( ) => {
889- vi . useRealTimers ( ) ;
884+ test ( 'useThrottledDebounce returns initial value' , ( ) => {
885+ const { result } = renderHook ( ( ) =>
886+ useThrottledDebounce ( 'initial' , 500 )
887+ ) ;
888+ expect ( result . current ) . toBe ( 'initial' ) ;
890889 } ) ;
891890
892- test ( 'throttles highlighting function calls based on timing' , ( ) => {
893- // Mock date to have a consistent starting point
894- const originalDateNow = Date . now ;
895- const mockTime = 1000 ;
896- Date . now = vi . fn ( ( ) => mockTime ) ;
897-
898- // Mock the perform highlight function
899- const performHighlight = vi . fn ( ) . mockResolvedValue ( undefined ) ;
891+ test ( 'useThrottledDebounce with no throttle returns value immediately' , async ( ) => {
892+ const { result, rerender } = renderHook (
893+ ( { value } ) => useThrottledDebounce ( value , undefined ) ,
894+ { initialProps : { value : 'initial' } }
895+ ) ;
900896
901- // Setup timeout control like in the hook
902- const timeoutControl = {
903- current : {
904- timeoutId : undefined ,
905- nextAllowedTime : 0 ,
906- } ,
907- } ;
897+ expect ( result . current ) . toBe ( 'initial' ) ;
908898
909- // First call should schedule immediately since nextAllowedTime is in the past
910- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
911- expect ( timeoutControl . current . timeoutId ) . toBeDefined ( ) ;
899+ rerender ( { value : 'updated' } ) ;
912900
913- // Run the timeout
914- vi . runAllTimers ( ) ;
915- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
916- expect ( timeoutControl . current . nextAllowedTime ) . toBe ( 1500 ) ; // 1000 + 500
901+ await waitFor ( ( ) => {
902+ expect ( result . current ) . toBe ( 'updated' ) ;
903+ } ) ;
904+ } ) ;
917905
918- // Reset the mock
919- performHighlight . mockClear ( ) ;
906+ test ( 'delay option throttles code updates' , async ( ) => {
907+ const code1 = 'const a = 1;' ;
908+ const code2 = 'const b = 2;' ;
920909
921- // Call again - should be delayed by the throttle duration
922- throttleHighlighting ( performHighlight , timeoutControl , 500 ) ;
923- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ; // Not called yet
910+ const { getByTestId , rerender } = render (
911+ < TestComponent code = { code1 } language = "javascript" theme = "github-dark" delay = { 100 } />
912+ ) ;
924913
925- // Advance halfway through the delay - should still not be called
926- vi . advanceTimersByTime ( 250 ) ;
927- expect ( performHighlight ) . not . toHaveBeenCalled ( ) ;
914+ // Wait for initial render
915+ await waitFor ( ( ) => {
916+ expect ( getByTestId ( 'highlighted' ) ) . toBeInTheDocument ( ) ;
917+ } ) ;
928918
929- // Advance the full delay
930- vi . advanceTimersByTime ( 250 ) ;
931- expect ( performHighlight ) . toHaveBeenCalledTimes ( 1 ) ;
919+ // Change code - should be throttled
920+ rerender (
921+ < TestComponent code = { code2 } language = "javascript" theme = "github-dark" delay = { 100 } />
922+ ) ;
932923
933- // Restore original Date.now
934- Date . now = originalDateNow ;
924+ // Eventually should show new code
925+ await waitFor ( ( ) => {
926+ expect ( getByTestId ( 'highlighted' ) . textContent ) . toContain ( 'b' ) ;
927+ } ) ;
935928 } ) ;
936929 } ) ;
937930
0 commit comments