@@ -671,6 +671,54 @@ describe('Anchor Render', () => {
671671 expect ( onChange ) . toHaveBeenLastCalledWith ( `#${ hash2 } ` ) ;
672672 } ) ;
673673
674+ it ( 'should trigger onChange when a raw link matches the mapped active link' , ( ) => {
675+ const hash1 = getHashUrl ( ) ;
676+ const hash2 = getHashUrl ( ) ;
677+ const onChange = jest . fn ( ) ;
678+ const { container } = render (
679+ < Anchor
680+ onChange = { onChange }
681+ getCurrentAnchor = { ( link ) => ( link === `#${ hash1 } ` ? `#${ hash2 } ` : link ) }
682+ items = { [
683+ { key : hash1 , href : `#${ hash1 } ` , title : hash1 } ,
684+ { key : hash2 , href : `#${ hash2 } ` , title : hash2 } ,
685+ ] }
686+ /> ,
687+ ) ;
688+
689+ fireEvent . click ( container . querySelector ( `a[href="#${ hash1 } "]` ) ! ) ;
690+ expect ( container . querySelector ( `.ant-anchor-link-title-active` ) ?. textContent ) . toBe ( hash2 ) ;
691+ expect ( onChange ) . toHaveBeenLastCalledWith ( `#${ hash1 } ` ) ;
692+ onChange . mockClear ( ) ;
693+ fireEvent . click ( container . querySelector ( `a[href="#${ hash2 } "]` ) ! ) ;
694+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
695+ expect ( onChange ) . toHaveBeenCalledWith ( `#${ hash2 } ` ) ;
696+ } ) ;
697+
698+ it ( 'should not trigger onChange repeatedly when scrolling with getCurrentAnchor' , ( ) => {
699+ const hash1 = getHashUrl ( ) ;
700+ const hash2 = getHashUrl ( ) ;
701+ const onChange = jest . fn ( ) ;
702+ const getCurrentAnchor = jest . fn ( ( ) => `#${ hash2 } ` ) ;
703+ render (
704+ < Anchor
705+ onChange = { onChange }
706+ getCurrentAnchor = { getCurrentAnchor }
707+ items = { [
708+ { key : hash1 , href : `#${ hash1 } ` , title : hash1 } ,
709+ { key : hash2 , href : `#${ hash2 } ` , title : hash2 } ,
710+ ] }
711+ /> ,
712+ ) ;
713+
714+ onChange . mockClear ( ) ;
715+ getCurrentAnchor . mockClear ( ) ;
716+ fireEvent . scroll ( window ) ;
717+ fireEvent . scroll ( window ) ;
718+ expect ( getCurrentAnchor ) . toHaveBeenCalledTimes ( 2 ) ;
719+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
720+ } ) ;
721+
674722 // https://github.com/ant-design/ant-design/issues/34784
675723 it ( 'getCurrentAnchor have default link as argument' , ( ) => {
676724 const hash1 = getHashUrl ( ) ;
@@ -711,6 +759,40 @@ describe('Anchor Render', () => {
711759 expect ( container . querySelector ( `.ant-anchor-link-title-active` ) ?. textContent ) . toBe ( hash2 ) ;
712760 } ) ;
713761
762+ it ( 'should apply getCurrentAnchor once when it changes' , ( ) => {
763+ const hash1 = getHashUrl ( ) ;
764+ const hash2 = getHashUrl ( ) ;
765+ const hash3 = getHashUrl ( ) ;
766+ interface DemoProps {
767+ mappedLink : string ;
768+ }
769+
770+ const Demo : React . FC < DemoProps > = ( { mappedLink } ) => (
771+ < Anchor
772+ getCurrentAnchor = { ( link ) => {
773+ if ( link === `#${ hash1 } ` ) {
774+ return `#${ hash2 } ` ;
775+ }
776+ if ( link === `#${ hash2 } ` ) {
777+ return `#${ mappedLink } ` ;
778+ }
779+ return link ;
780+ } }
781+ items = { [
782+ { key : hash1 , href : `#${ hash1 } ` , title : hash1 } ,
783+ { key : hash2 , href : `#${ hash2 } ` , title : hash2 } ,
784+ { key : hash3 , href : `#${ hash3 } ` , title : hash3 } ,
785+ ] }
786+ />
787+ ) ;
788+ const { container, rerender } = render ( < Demo mappedLink = { hash2 } /> ) ;
789+ fireEvent . click ( container . querySelector ( `a[href="#${ hash1 } "]` ) ! ) ;
790+ expect ( container . querySelector ( `.ant-anchor-link-title-active` ) ?. textContent ) . toBe ( hash2 ) ;
791+
792+ rerender ( < Demo mappedLink = { hash3 } /> ) ;
793+ expect ( container . querySelector ( `.ant-anchor-link-title-active` ) ?. textContent ) . toBe ( hash2 ) ;
794+ } ) ;
795+
714796 it ( 'should render correctly when href is null' , ( ) => {
715797 expect ( ( ) => {
716798 render (
0 commit comments