forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-button.js
More file actions
165 lines (124 loc) · 4.39 KB
/
test-button.js
File metadata and controls
165 lines (124 loc) · 4.39 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* MUI test react appbar library
* @module test/react-tests/test-appbar
*/
import assert from 'assert';
import React from 'react';
import ReactUtils from 'react-addons-test-utils';
import ReactDOM from 'react-dom';
import Button from '../../src/react/button';
import { getShallowRendererOutput } from '../lib/react-helpers';
describe('react/button', function() {
it('renders properly', function() {
let result = getShallowRendererOutput(<Button>test</Button>);
assert.equal(result.type, 'button');
assert.equal(/\bmui-btn\b/.test(result.props.className), true);
assert.equal(result.props.children[0], 'test');
});
it('renders properly with addtional classNames', function () {
let result = getShallowRendererOutput(
<Button className="additional">
test
</Button>
);
assert.equal(/\badditional\b/.test(result.props.className), true)
})
it('renders properly with additional styles', function() {
let result = getShallowRendererOutput(
<Button style={{additonal: 'style'}}>
test
</Button>
);
assert.equal(result.props.style.additonal, 'style');
});
it('supports colors', function() {
let result = getShallowRendererOutput(
<Button color="primary">test</Button>
);
assert.equal(/mui-btn--primary/.test(result.props.className), true);
});
it('supports variants', function() {
let result = getShallowRendererOutput(
<Button variant="raised">test</Button>
);
assert.equal(/mui-btn--raised/.test(result.props.className), true);
});
it('supports sizes', function() {
let result = getShallowRendererOutput(<Button size="large">test</Button>);
assert.equal(/mui-btn--large/.test(result.props.className), true);
});
it('supports type attribute', function() {
// check default
let result = getShallowRendererOutput(<Button>test</Button>);
assert.equal(result.props.type, null);
// check 'button' type
result = getShallowRendererOutput(
<Button type="button">
test
</Button>
);
assert.equal(result.props.type, 'button');
});
it('supports click, mouse and touch events', function() {
let executedEvents = [],
triggeredEvents = [];
// define callback
const fn = function(ev) {executedEvents.push(ev.type);};
// define props
const props = {
onClick: fn,
onMouseDown: fn,
onMouseUp: fn,
onMouseLeave: fn,
onTouchStart: fn,
onTouchEnd: fn
};
const node = ReactUtils.renderIntoDocument(
<Button { ...props }>test</Button>
);
// trigger events
let k, eventName, eventType;
for (k in props) {
// remove 'on' and lowercase first letter
eventName = k.charAt(2).toLowerCase() + k.substr(3, k.length);
eventType = eventName.toLowerCase();
// trigger event
ReactUtils.Simulate[eventName](node.refs.buttonEl, {type: eventType});
triggeredEvents.push(eventType);
}
// check that events were executed
assert.deepEqual(triggeredEvents, executedEvents);
});
it('renders ripples on click', function() {
let node = ReactUtils.renderIntoDocument(<Button>test</Button>);
let buttonEl = node.refs.buttonEl;
// check state before ripple
assert.equal(Object.keys(node.state.ripples).length, 0);
assert.equal(buttonEl.children.length, 0);
// trigger ripple
ReactUtils.Simulate.mouseDown(buttonEl);
// check state after ripple
assert.equal(Object.keys(node.state.ripples).length, 1);
assert.equal(buttonEl.children.length, 1);
assert.equal(buttonEl.children[0].className, 'mui-ripple-effect');
// add another ripple
ReactUtils.Simulate.mouseDown(node.refs.buttonEl);
assert.equal(Object.keys(node.state.ripples).length, 2);
});
it('removes ripples after mouseup and animation duration', function(done) {
this.timeout(700);
let node = ReactUtils.renderIntoDocument(<Button>test</Button>);
ReactUtils.Simulate.mouseDown(node.refs.buttonEl);
ReactUtils.Simulate.mouseUp(node.refs.buttonEl);
assert.equal(Object.keys(node.state.ripples).length, 1);
setTimeout(function() {
// check that ripple is still there
assert.equal(Object.keys(node.state.ripples).length, 1);
}, 500);
setTimeout(function() {
// check that ripple has been removed
assert.equal(Object.keys(node.state.ripples).length, 0);
done();
}, 601);
});
});