1+ import "@testing-library/jest-dom" ;
2+ import { render } from "@testing-library/react" ;
3+ import { SkipLinkContainerProps } from "../../typings/SkipLinkProps" ;
4+ import { SkipLink } from "../SkipLink" ;
5+
6+ describe ( "SkipLink" , ( ) => {
7+ let defaultProps : SkipLinkContainerProps ;
8+ let rootElement : HTMLElement ;
9+
10+ beforeEach ( ( ) => {
11+ // Set up the DOM structure that SkipLink expects
12+ document . body . innerHTML = "" ;
13+ rootElement = document . createElement ( "div" ) ;
14+ rootElement . id = "root" ;
15+ document . body . appendChild ( rootElement ) ;
16+
17+ defaultProps = {
18+ name : "SkipLink1" ,
19+ class : "mx-skiplink" ,
20+ style : { } ,
21+ linkText : "Skip to main content" ,
22+ mainContentId : "main-content"
23+ } ;
24+ } ) ;
25+
26+ afterEach ( ( ) => {
27+ document . body . innerHTML = "" ;
28+ } ) ;
29+
30+ it ( "renders skiplink widget and adds skip link to DOM" , ( ) => {
31+ render ( < SkipLink { ...defaultProps } /> ) ;
32+
33+ // Check that the skip link was added to the root element
34+ const skipLink = rootElement . querySelector ( ".skip-link" ) as HTMLAnchorElement ;
35+ expect ( skipLink ) . toBeInTheDocument ( ) ;
36+ expect ( skipLink . textContent ) . toBe ( "Skip to main content" ) ;
37+ expect ( skipLink . href ) . toBe ( `${ window . location . origin } /#main-content` ) ;
38+ expect ( skipLink . tabIndex ) . toBe ( 0 ) ;
39+
40+ // Snapshot the actual root element that contains the skip link
41+ expect ( rootElement ) . toMatchSnapshot ( ) ;
42+ } ) ;
43+
44+ it ( "renders with custom link text" , ( ) => {
45+ render ( < SkipLink { ...defaultProps } linkText = "Jump to content" /> ) ;
46+
47+ const skipLink = rootElement . querySelector ( ".skip-link" ) as HTMLAnchorElement ;
48+ expect ( skipLink ) . toBeInTheDocument ( ) ;
49+ expect ( skipLink . textContent ) . toBe ( "Jump to content" ) ;
50+
51+ expect ( rootElement ) . toMatchSnapshot ( ) ;
52+ } ) ;
53+
54+ it ( "renders with custom main content id" , ( ) => {
55+ render ( < SkipLink { ...defaultProps } mainContentId = "content-area" /> ) ;
56+
57+ const skipLink = rootElement . querySelector ( ".skip-link" ) as HTMLAnchorElement ;
58+ expect ( skipLink ) . toBeInTheDocument ( ) ;
59+ expect ( skipLink . href ) . toBe ( `${ window . location . origin } /#content-area` ) ;
60+
61+ expect ( rootElement ) . toMatchSnapshot ( ) ;
62+ } ) ;
63+
64+ it ( "renders with empty main content id" , ( ) => {
65+ render ( < SkipLink { ...defaultProps } mainContentId = "" /> ) ;
66+
67+ const skipLink = rootElement . querySelector ( ".skip-link" ) as HTMLAnchorElement ;
68+ expect ( skipLink ) . toBeInTheDocument ( ) ;
69+ expect ( skipLink . href ) . toBe ( `${ window . location . origin } /#` ) ;
70+
71+ expect ( rootElement ) . toMatchSnapshot ( ) ;
72+ } ) ;
73+
74+ it ( "cleans up skip link when component unmounts" , ( ) => {
75+ const { unmount } = render ( < SkipLink { ...defaultProps } /> ) ;
76+
77+ // Verify skip link is present
78+ expect ( rootElement . querySelector ( ".skip-link" ) ) . toBeInTheDocument ( ) ;
79+
80+ // Unmount and verify cleanup
81+ unmount ( ) ;
82+ expect ( rootElement . querySelector ( ".skip-link" ) ) . not . toBeInTheDocument ( ) ;
83+ } ) ;
84+ } ) ;
0 commit comments