forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeAnchorSpec.js
More file actions
127 lines (106 loc) · 3.09 KB
/
SafeAnchorSpec.js
File metadata and controls
127 lines (106 loc) · 3.09 KB
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
120
121
122
123
124
125
126
127
import React from 'react';
import tsp from 'teaspoon';
import SafeAnchor from '../src/SafeAnchor';
describe('SafeAnchor', () => {
it('renders an anchor tag', () => {
tsp(<SafeAnchor />)
.render()
.dom()
.tagName
.should.equal('A');
});
it('forwards provided href', () => {
tsp(<SafeAnchor href="http://google.com" />)
.shallowRender()
.find('a')
.props('href')
.should.equal('http://google.com');
});
it('ensures that an href is provided', () => {
tsp(<SafeAnchor />)
.render()
.dom()
.hasAttribute('href')
.should.be.true;
});
it('forwards onClick handler', () => {
const handleClick = sinon.spy();
tsp(<SafeAnchor onClick={handleClick} />)
.shallowRender()
.find('a')
.trigger('click', { preventDefault() {} });
handleClick.should.have.been.calledOnce;
});
it('prevents default when no href is provided', () => {
const handleClick = sinon.spy();
tsp(<SafeAnchor onClick={handleClick} />)
.render()
.find('a')
.trigger('click')
.end()
.props({ href: '#' })
.find('a')
.trigger('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();
tsp(<SafeAnchor href="#foo" onClick={handleClick} />)
.render()
.find('a')
.trigger('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();
let spy = sinon.spy(SafeAnchor.prototype, 'handleClick');
tsp(
<SafeAnchor disabled href="#foo" onClick={clickSpy}>
Title
</SafeAnchor>
)
.render()
.trigger('click');
expect(spy).to.have.been.calledOnce;
expect(clickSpy).to.have.not.been.called;
expect(spy.getCall(0).args[0].isDefaultPrevented()).to.equal(true);
expect(spy.getCall(0).args[0].isPropagationStopped()).to.equal(true);
});
it('forwards provided role', () => {
tsp(<SafeAnchor role="test" />)
.shallowRender()
.find('a')
.props('role')
.should.equal('test');
});
it('forwards provided role with href', () => {
tsp(<SafeAnchor role="test" href="http://google.com" />)
.shallowRender()
.find('a')
.props('role')
.should.equal('test');
});
it('set role=button with no provided href', () => {
tsp(<SafeAnchor />)
.shallowRender()
.find('a')
.props('role')
.should.equal('button');
tsp(<SafeAnchor href="#" />)
.shallowRender()
.find('a')
.props('role')
.should.equal('button');
});
it('sets no role with provided href', () => {
expect(
tsp(<SafeAnchor href="http://google.com" />)
.shallowRender()
.find('a')
.props('role')
).to.not.exist;
});
});