forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeAnchorSpec.js
96 lines (73 loc) · 3.33 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
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ReactDOM from 'react-dom';
import SafeAnchor from '../src/SafeAnchor';
describe('SafeAnchor', () => {
it('renders an anchor tag', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor />);
const node = ReactDOM.findDOMNode(instance);
node.tagName.should.equal('A');
});
it('forwards provided href', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor href='http://google.com' />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
anchor.getAttribute('href').should.equal('http://google.com');
});
it('ensures that an href is provided', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
anchor.getAttribute('href').should.equal('');
});
it('forwards onClick handler', (done) => {
const handleClick = () => {
done();
};
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor onClick={handleClick} />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
ReactTestUtils.Simulate.click(anchor);
});
it('prevents default when no href is provided', (done) => {
const handleClick = (event) => {
expect(event.isDefaultPrevented()).to.not.be.ok;
setTimeout(() => {
event.isDefaultPrevented().should.be.true;
done();
}, 100);
};
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor onClick={handleClick} />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
ReactTestUtils.Simulate.click(anchor);
});
it('does not prevent default when href is provided', (done) => {
const handleClick = (event) => {
expect(event.isDefaultPrevented()).to.not.be.ok;
setTimeout(() => {
expect(event.isDefaultPrevented()).to.not.be.ok;
done();
});
};
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor href='#' onClick={handleClick} />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
ReactTestUtils.Simulate.click(anchor);
});
it('forwards provided role', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor role='test' />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
anchor.getAttribute('role').should.equal('test');
});
it('forwards provided role with href', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor role='test' href='http://google.com' />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
anchor.getAttribute('role').should.equal('test');
});
it('set role=button with no provided href', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
anchor.getAttribute('role').should.equal('button');
});
it('sets no role with provided href', () => {
const instance = ReactTestUtils.renderIntoDocument(<SafeAnchor href='http://google.com' />);
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'A');
expect(anchor.getAttribute('role')).to.not.exist;
});
});