Skip to content

Commit fcebf70

Browse files
authored
Merge pull request #34 from NHSDigital/lp-disableDefaultBehaviour-skiplink-fix
If disableDefaultBehaviour is set, dont set the href on the skiplink
2 parents 9f49df5 + 0c24550 commit fcebf70

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/components/skip-link/SkipLink.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SkipLink extends React.Component<SkipLinkProps> {
1414
static defaultProps = {
1515
children: 'Skip to main content',
1616
href: '#maincontent',
17+
tabIndex: 0,
1718
};
1819

1920
constructor(props: SkipLinkProps, ...rest: any[]) {
@@ -84,9 +85,14 @@ class SkipLink extends React.Component<SkipLinkProps> {
8485
};
8586

8687
render() {
87-
const { className, focusTargetRef, disableDefaultBehaviour, ...rest } = this.props;
88+
const { className, focusTargetRef, disableDefaultBehaviour, href, ...rest } = this.props;
8889
return (
89-
<a className={classNames('nhsuk-skip-link', className)} onClick={this.onClick} {...rest} />
90+
<a
91+
className={classNames('nhsuk-skip-link', className)}
92+
onClick={this.onClick}
93+
href={disableDefaultBehaviour ? undefined : href}
94+
{...rest}
95+
/>
9096
);
9197
}
9298
}

src/components/skip-link/__tests__/SkipLink.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { createRef } from 'react';
2-
import { shallow } from 'enzyme';
2+
import { shallow, mount } from 'enzyme';
33
import SkipLink from '..';
44

55
describe('SkipLink', () => {
@@ -17,4 +17,21 @@ describe('SkipLink', () => {
1717
expect(component.prop('aria-label')).toEqual('test');
1818
component.unmount();
1919
});
20+
21+
it('sets the href to #maincontent by default', () => {
22+
const component = mount(<SkipLink />);
23+
expect(component.find('.nhsuk-skip-link').prop('href')).toBe('#maincontent');
24+
});
25+
26+
it('calls onClick callback when clicked', () => {
27+
const onClick = jest.fn();
28+
const component = mount(<SkipLink onClick={onClick} />);
29+
component.find('.nhsuk-skip-link').simulate('click');
30+
expect(onClick).toHaveBeenCalled();
31+
});
32+
33+
it('does not set the href to #maincontent if disableDefaultBehaviour is set', () => {
34+
const component = mount(<SkipLink disableDefaultBehaviour />);
35+
expect(component.find('.nhsuk-skip-link').prop('href')).toBeUndefined();
36+
});
2037
});

src/components/skip-link/__tests__/__snapshots__/SkipLink.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ exports[`SkipLink matches snapshot: SkipLink 1`] = `
55
className="nhsuk-skip-link"
66
href="#maincontent"
77
onClick={[Function]}
8+
tabIndex={0}
89
>
910
Skip to main content
1011
</a>

0 commit comments

Comments
 (0)