@@ -195,4 +195,41 @@ describe('XMarkdown', () => {
195195 expect ( wrapper ) . toHaveClass ( 'ant-x-markdown' ) ;
196196 expect ( wrapper . innerHTML ) . toBe ( '<div>This is a paragraph.</div>\n' ) ;
197197 } ) ;
198+
199+ describe ( 'openLinksInNewTab' , ( ) => {
200+ it ( 'should add target="_blank" and rel="noopener noreferrer" to links with title when openLinksInNewTab is true' , ( ) => {
201+ const { container } = render (
202+ < XMarkdown content = '[Google](https://www.google.com "Google Search")' openLinksInNewTab /> ,
203+ ) ;
204+
205+ const link = container . querySelector ( 'a' ) ;
206+ expect ( link ) . toHaveAttribute ( 'href' , 'https://www.google.com' ) ;
207+ expect ( link ) . toHaveAttribute ( 'target' , '_blank' ) ;
208+ expect ( link ) . toHaveAttribute ( 'rel' , 'noopener noreferrer' ) ;
209+ expect ( link ) . toHaveAttribute ( 'title' , 'Google Search' ) ;
210+ expect ( link ) . toHaveTextContent ( 'Google' ) ;
211+ } ) ;
212+
213+ it ( 'should not add target="_blank" when openLinksInNewTab is false' , ( ) => {
214+ const { container } = render (
215+ < XMarkdown content = "[Google](https://www.google.com)" openLinksInNewTab = { false } /> ,
216+ ) ;
217+
218+ const link = container . querySelector ( 'a' ) ;
219+ expect ( link ) . toHaveAttribute ( 'href' , 'https://www.google.com' ) ;
220+ expect ( link ) . not . toHaveAttribute ( 'target' ) ;
221+ expect ( link ) . not . toHaveAttribute ( 'rel' ) ;
222+ expect ( link ) . toHaveTextContent ( 'Google' ) ;
223+ } ) ;
224+
225+ it ( 'should not add target="_blank" when openLinksInNewTab is not provided' , ( ) => {
226+ const { container } = render ( < XMarkdown content = "[Google](https://www.google.com)" /> ) ;
227+
228+ const link = container . querySelector ( 'a' ) ;
229+ expect ( link ) . toHaveAttribute ( 'href' , 'https://www.google.com' ) ;
230+ expect ( link ) . not . toHaveAttribute ( 'target' ) ;
231+ expect ( link ) . not . toHaveAttribute ( 'rel' ) ;
232+ expect ( link ) . toHaveTextContent ( 'Google' ) ;
233+ } ) ;
234+ } ) ;
198235} ) ;
0 commit comments