forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafeAnchorSpec.js
119 lines (93 loc) · 3.04 KB
/
SafeAnchorSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React from 'react';
import { mount, shallow } from 'enzyme';
import SafeAnchor from '../src/SafeAnchor';
describe('SafeAnchor', () => {
it('renders an anchor tag', () => {
mount(<SafeAnchor />)
.getDOMNode()
.tagName.should.equal('A');
});
it('forwards provided href', () => {
shallow(<SafeAnchor href="http://google.com" />)
.find('a')
.prop('href')
.should.equal('http://google.com');
});
it('ensures that an href is provided', () => {
mount(<SafeAnchor />)
.getDOMNode()
.hasAttribute('href').should.be.true;
});
it('forwards onClick handler', () => {
const handleClick = sinon.spy();
shallow(<SafeAnchor onClick={handleClick} />)
.find('a')
.simulate('click', { preventDefault() {} });
handleClick.should.have.been.calledOnce;
});
it('provides onClick handler as onKeyDown handler for "space"', () => {
const handleClick = sinon.spy();
shallow(<SafeAnchor onClick={handleClick} />)
.find('a')
.simulate('keyDown', { key: ' ', preventDefault() {} });
handleClick.should.have.been.calledOnce;
});
it('prevents default when no href is provided', () => {
const handleClick = sinon.spy();
const wrapper = mount(<SafeAnchor onClick={handleClick} />);
wrapper.find('a').simulate('click');
wrapper.setProps({ href: '#' }).find('a').simulate('click');
expect(handleClick).to.have.been.calledTwice;
expect(handleClick.getCall(0).args[0].isDefaultPrevented()).to.be.true;
expect(handleClick.getCall(1).args[0].isDefaultPrevented()).to.be.true;
});
it('does not prevent default when href is provided', () => {
const handleClick = sinon.spy();
mount(<SafeAnchor href="#foo" onClick={handleClick} />)
.find('a')
.simulate('click');
expect(handleClick).to.have.been.calledOnce;
expect(handleClick.getCall(0).args[0].isDefaultPrevented()).to.be.false;
});
it('Should disable link behavior', () => {
let clickSpy = sinon.spy();
mount(
<SafeAnchor disabled href="#foo" onClick={clickSpy}>
Title
</SafeAnchor>,
).simulate('click');
expect(clickSpy).to.have.not.been.called;
});
it('forwards provided role', () => {
shallow(<SafeAnchor role="test" />)
.find('a')
.prop('role')
.should.equal('test');
});
it('forwards provided role with href', () => {
shallow(<SafeAnchor role="test" href="http://google.com" />)
.find('a')
.prop('role')
.should.equal('test');
});
it('set role=button with no provided href', () => {
shallow(<SafeAnchor />)
.find('a')
.prop('role')
.should.equal('button');
shallow(<SafeAnchor href="#" />)
.find('a')
.prop('role')
.should.equal('button');
});
it('sets no role with provided href', () => {
expect(
shallow(<SafeAnchor href="http://google.com" />)
.find('a')
.prop('role'),
).to.not.exist;
});
it('Should have a as default component', () => {
mount(<SafeAnchor />).assertSingle('a');
});
});