@@ -22,10 +22,44 @@ describe('SkipLink', () => {
2222 expect ( container ) . toMatchSnapshot ( 'SkipLink' ) ;
2323 } ) ;
2424
25- it ( 'sets the href to #maincontent by default' , ( ) => {
26- const { container } = render ( < SkipLink /> ) ;
25+ it ( 'sets the href to #maincontent by default and focuses the first heading' , ( ) => {
26+ const { container } = render (
27+ < >
28+ < SkipLink />
29+ < h1 id = "heading" > Heading</ h1 >
30+ </ > ,
31+ ) ;
32+
33+ const headingEl = container . querySelector ( '#heading' ) as HTMLElement ;
34+ const focusSpy = jest . spyOn ( headingEl , 'focus' ) ;
35+
36+ const skipLinkEl = container . querySelector ( '.nhsuk-skip-link' ) ! ;
37+
38+ expect ( skipLinkEl . getAttribute ( 'href' ) ) . toBe ( '#maincontent' ) ;
2739
28- expect ( container . querySelector ( '.nhsuk-skip-link' ) ?. getAttribute ( 'href' ) ) . toBe ( '#maincontent' ) ;
40+ fireEvent . click ( skipLinkEl ) ;
41+
42+ expect ( focusSpy ) . toHaveBeenCalled ( ) ;
43+ } ) ;
44+
45+ it ( 'Does not focus the first heading if disableHeadingFocus is set' , ( ) => {
46+ const { container } = render (
47+ < >
48+ < SkipLink disableHeadingFocus />
49+ < h1 id = "heading" > Heading</ h1 >
50+ </ > ,
51+ ) ;
52+
53+ const headingEl = container . querySelector ( '#heading' ) as HTMLElement ;
54+ const focusSpy = jest . spyOn ( headingEl , 'focus' ) ;
55+
56+ const skipLinkEl = container . querySelector ( '.nhsuk-skip-link' ) ! ;
57+
58+ expect ( skipLinkEl . getAttribute ( 'href' ) ) . toBe ( '#maincontent' ) ;
59+
60+ fireEvent . click ( skipLinkEl ) ;
61+
62+ expect ( focusSpy ) . not . toHaveBeenCalled ( ) ;
2963 } ) ;
3064
3165 it ( 'calls onClick callback when clicked' , ( ) => {
@@ -38,12 +72,6 @@ describe('SkipLink', () => {
3872 expect ( onClick ) . toHaveBeenCalled ( ) ;
3973 } ) ;
4074
41- it ( 'does not set the href when disableDefaultBehaviour is set' , ( ) => {
42- const { container } = render ( < SkipLink disableDefaultBehaviour /> ) ;
43-
44- expect ( container . querySelector ( '.nhsuk-skip-link' ) ?. getAttribute ( 'href' ) ) . toBeFalsy ( ) ;
45- } ) ;
46-
4775 it ( 'Focuses the main content when clicked' , ( ) => {
4876 const { container } = render ( < SkipLinkTestApp /> ) ;
4977
0 commit comments