-
Notifications
You must be signed in to change notification settings - Fork 51
/
Button.test.js
98 lines (89 loc) · 2.54 KB
/
Button.test.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
jest.disableAutomock()
import React from 'react'
import ReactDOM from 'react-dom'
import ReactDOMServer from 'react-dom/server'
import TestUtils from 'react-dom/test-utils'
import Button from '.'
describe('ButtonTest', () => {
it('Button markup', () => {
const clickFun = function (e) {}
const actual = ReactDOMServer.renderToStaticMarkup(
<Button
title="Come on! Do it! Do it!"
onClick={clickFun}
className="im here">
Come on!
</Button>
)
const expected = ReactDOMServer.renderToStaticMarkup(
<button
className="im here"
disabled={false}
onClick={clickFun}
title="Come on! Do it! Do it!">
Come on!
</button>
)
expect(actual).toEqual(expected)
})
it('Button markup (disabled)', () => {
const clickFun = function (e) {}
const actual = ReactDOMServer.renderToStaticMarkup(
<Button
disabled={true}
title="No such thing, ol' buddy"
onClick={clickFun}
className="one-way-ticket">
Who's our backup?
</Button>
)
const expected = ReactDOMServer.renderToStaticMarkup(
<button
className="one-way-ticket is-disabled"
disabled={true}
onClick={undefined}
title="No such thing, ol' buddy">
Who's our backup?
</button>
)
expect(actual).toEqual(expected)
})
it('Button click event', () => {
let clickEvent = 'nowhere'
const getToTheChopper = (e) => {
clickEvent = e.value
}
const escapeButton = TestUtils.renderIntoDocument(
<Button
onClick={getToTheChopper}>
Run! Get to...
</Button>
)
TestUtils.Simulate.click(
ReactDOM.findDOMNode(escapeButton), {value: 'the chopper'})
expect(clickEvent).toEqual('the chopper',
'Button click event should fire with correct event payload')
})
it('Button does not fire click when disabled', () => {
let clickEvent = 'nowhere'
const getToTheChopper = function (e) {
clickEvent = e.value
}
const escapeButton = TestUtils.renderIntoDocument(
<Button
disabled={true}
onClick={getToTheChopper}>
Run! Get to...
</Button>
)
try {
// simulate click event
TestUtils.Simulate.click(
ReactDOM.findDOMNode(escapeButton), {value: 'the chopper'})
} catch (e) {
// swallow on purpose, valid for code to not bind onClick
}
expect(clickEvent).toEqual('nowhere',
'Button click event should not fire when props.disabled is true')
})
})