Skip to content

Commit 11a752e

Browse files
author
Luke Pearson
committed
If disableDefaultBehaviour is set, dont set the href on the skiplink
1 parent 9f49df5 commit 11a752e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/components/skip-link/SkipLink.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ class SkipLink extends React.Component<SkipLinkProps> {
8484
};
8585

8686
render() {
87-
const { className, focusTargetRef, disableDefaultBehaviour, ...rest } = this.props;
87+
const { className, focusTargetRef, disableDefaultBehaviour, href, ...rest } = this.props;
8888
return (
89-
<a className={classNames('nhsuk-skip-link', className)} onClick={this.onClick} {...rest} />
89+
<a
90+
className={classNames('nhsuk-skip-link', className)}
91+
onClick={this.onClick}
92+
href={disableDefaultBehaviour ? undefined : href}
93+
{...rest}
94+
/>
9095
);
9196
}
9297
}

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

Lines changed: 19 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,22 @@ 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 onClick = jest.fn();
35+
const component = mount(<SkipLink disableDefaultBehaviour onClick={onClick} />);
36+
expect(component.find('.nhsuk-skip-link').prop('href')).toBeUndefined();
37+
});
2038
});

0 commit comments

Comments
 (0)